Skip to content

Commit 8c56d0f

Browse files
authored
chore(tool/cmd/migrate): delete unused Rust migration code (#3774)
Remove unused functions in the migration tool. Now that we have migrated Rust to librarian, delete the unused migration code. `readCargoConfig` is not removed because it requires additional logic to parse the library name, which will be added in follow-up PRs. For #3580
1 parent 44d1303 commit 8c56d0f

13 files changed

Lines changed: 11 additions & 658 deletions

File tree

tool/cmd/migrate/main.go

Lines changed: 11 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"flag"
2222
"fmt"
2323
"log"
24-
"maps"
2524
"os"
2625
"path/filepath"
2726
"reflect"
@@ -61,11 +60,6 @@ var (
6160
fetchSource = fetchGoogleapis
6261
)
6362

64-
var excludedVeneerLibraries = map[string]struct{}{
65-
"echo-server": {},
66-
"gcp-sdk": {},
67-
}
68-
6963
func main() {
7064
ctx := context.Background()
7165
if err := run(ctx, os.Args[1:]); err != nil {
@@ -115,19 +109,8 @@ func runSidekickMigration(ctx context.Context, repoPath string) error {
115109
if err != nil {
116110
return fmt.Errorf("failed to read sidekick.toml files: %w", err)
117111
}
118-
cargoFiles, err := findCargos(filepath.Join(repoPath, "src"))
119-
if err != nil {
120-
return fmt.Errorf("failed to find Cargo.toml files: %w", err)
121-
}
122-
veneers, err := buildVeneer(cargoFiles, repoPath)
123-
if err != nil {
124-
return fmt.Errorf("failed to build veneers: %w", err)
125-
}
126-
allLibraries := make(map[string]*config.Library, len(libraries)+len(veneers))
127-
maps.Copy(allLibraries, libraries)
128-
maps.Copy(allLibraries, veneers)
129112

130-
cfg := buildConfig(allLibraries, defaults)
113+
cfg := buildConfig(libraries, defaults)
131114

132115
if err := librarian.RunTidyOnConfig(ctx, cfg); err != nil {
133116
return errTidyFailed
@@ -345,7 +328,7 @@ func buildGAPIC(files []string, repoPath string) (map[string]*config.Library, er
345328
}
346329

347330
if extraModules, ok := sidekick.Codec["extra-modules"]; ok {
348-
for _, module := range strToSlice(extraModules, false) {
331+
for _, module := range strToSlice(extraModules) {
349332
if module == "" {
350333
continue
351334
}
@@ -411,7 +394,7 @@ func buildGAPIC(files []string, repoPath string) (map[string]*config.Library, er
411394
rustCrate := &config.RustCrate{
412395
RustDefault: config.RustDefault{
413396
PackageDependencies: packageDeps,
414-
DisabledRustdocWarnings: strToSlice(disabledRustdocWarnings, false),
397+
DisabledRustdocWarnings: strToSlice(disabledRustdocWarnings),
415398
GenerateSetterSamples: generateSetterSamples,
416399
GenerateRpcSamples: generateRpcSamples,
417400
},
@@ -420,12 +403,12 @@ func buildGAPIC(files []string, repoPath string) (map[string]*config.Library, er
420403
TemplateOverride: templateOverride,
421404
PackageNameOverride: packageNameOverride,
422405
RootName: rootName,
423-
Roots: strToSlice(roots, false),
424-
DefaultFeatures: strToSlice(defaultFeatures, false),
425-
IncludeList: strToSlice(includeList, false),
426-
IncludedIds: strToSlice(includeIds, false),
427-
SkippedIds: strToSlice(skippedIds, false),
428-
DisabledClippyWarnings: strToSlice(disabledClippyWarnings, false),
406+
Roots: strToSlice(roots),
407+
DefaultFeatures: strToSlice(defaultFeatures),
408+
IncludeList: strToSlice(includeList),
409+
IncludedIds: strToSlice(includeIds),
410+
SkippedIds: strToSlice(skippedIds),
411+
DisabledClippyWarnings: strToSlice(disabledClippyWarnings),
429412
HasVeneer: strToBool(hasVeneer),
430413
RoutingRequired: strToBool(routingRequired),
431414
IncludeGrpcOnlyMethods: strToBool(includeGrpcOnlyMethods),
@@ -499,156 +482,6 @@ func findCargos(path string) ([]string, error) {
499482
return files, err
500483
}
501484

502-
func buildVeneer(files []string, repoPath string) (map[string]*config.Library, error) {
503-
veneers := make(map[string]*config.Library)
504-
for _, file := range files {
505-
cargo, err := readCargoConfig(filepath.Dir(file))
506-
if err != nil {
507-
return nil, err
508-
}
509-
510-
if _, ok := excludedVeneerLibraries[cargo.Package.Name]; ok {
511-
continue
512-
}
513-
514-
dir := filepath.Dir(file)
515-
rustModules, err := buildModules(dir, repoPath)
516-
if err != nil {
517-
return nil, fmt.Errorf("failed to build modules in %q: %w", dir, err)
518-
}
519-
relativePath, err := filepath.Rel(repoPath, dir)
520-
if err != nil {
521-
return nil, fmt.Errorf("failed to calculate relative path: %w", err)
522-
}
523-
name := cargo.Package.Name
524-
veneer := &config.Library{
525-
Name: name,
526-
Veneer: true,
527-
Output: relativePath,
528-
CopyrightYear: "2025",
529-
}
530-
if cargo.Package.Version != "" && cargo.Package.Version != "0.0.0" {
531-
veneer.Version = cargo.Package.Version
532-
}
533-
veneers[name] = veneer
534-
if len(rustModules) > 0 {
535-
veneers[name].Rust = &config.RustCrate{
536-
Modules: rustModules,
537-
}
538-
}
539-
if !cargo.Package.Publish {
540-
veneers[name].SkipPublish = true
541-
}
542-
}
543-
return veneers, nil
544-
}
545-
546-
func buildModules(rootDir string, repoPath string) ([]*config.RustModule, error) {
547-
var modules []*config.RustModule
548-
err := filepath.WalkDir(rootDir, func(path string, d os.DirEntry, err error) error {
549-
if err != nil {
550-
return err
551-
}
552-
553-
if d.IsDir() || d.Name() != sidekickFile {
554-
return nil
555-
}
556-
557-
if strings.Contains(path, "tests") {
558-
// Only use a .sidekick.toml in tests directory to represent a rust module if the following directory
559-
// exists:
560-
//
561-
// |-src
562-
// | |-generated
563-
// | |-.sidekick.toml
564-
// |-Cargo.toml
565-
// Only one pair of Cargo.toml and .sidekick.toml (within tests directory) is not comply this structure in
566-
// google-cloud-rust, which is wkt/Cargo.toml and src/wkt/tests/common/src/generated/.sidekick.toml.
567-
srcDir := strings.TrimSuffix(path, fmt.Sprintf("/src/generated/%s", sidekickFile))
568-
if rootDir != srcDir {
569-
return nil
570-
}
571-
}
572-
573-
sidekick, err := readTOML[sidekickconfig.Config](path)
574-
if err != nil {
575-
return err
576-
}
577-
578-
includedIds := sidekick.Source["included-ids"]
579-
includeList := sidekick.Source["include-list"]
580-
skippedIds := sidekick.Source["skipped-ids"]
581-
moduleRoots := make(map[string]string)
582-
roots, ok := sidekick.Source["roots"]
583-
if ok {
584-
for _, root := range strings.Split(roots, ",") {
585-
root = fmt.Sprintf("%s-root", root)
586-
modPath, ok := sidekick.Source[root]
587-
if ok {
588-
moduleRoots[root] = modPath
589-
}
590-
}
591-
}
592-
593-
hasVeneer := sidekick.Codec["has-veneer"]
594-
includeGrpcOnlyMethods := sidekick.Codec["include-grpc-only-methods"]
595-
routingRequired := sidekick.Codec["routing-required"]
596-
extendGrpcTransport := sidekick.Codec["extend-grpc-transport"]
597-
modulePath := sidekick.Codec["module-path"]
598-
nameOverrides := sidekick.Codec["name-overrides"]
599-
postProcessProtos := sidekick.Codec["post-process-protos"]
600-
templateOverride := sidekick.Codec["template-override"]
601-
generateSetterSamples := sidekick.Codec["generate-setter-samples"]
602-
603-
// Parse documentation overrides
604-
var documentationOverrides []config.RustDocumentationOverride
605-
for _, do := range sidekick.CommentOverrides {
606-
documentationOverrides = append(documentationOverrides, config.RustDocumentationOverride{
607-
ID: do.ID,
608-
Match: do.Match,
609-
Replace: do.Replace,
610-
})
611-
}
612-
relativePath, err := filepath.Rel(repoPath, filepath.Dir(path))
613-
if err != nil {
614-
return fmt.Errorf("failed to calculate relative path: %w", err)
615-
}
616-
module := &config.RustModule{
617-
DocumentationOverrides: documentationOverrides,
618-
GenerateSetterSamples: generateSetterSamples,
619-
HasVeneer: strToBool(hasVeneer),
620-
IncludedIds: strToSlice(includedIds, false),
621-
IncludeGrpcOnlyMethods: strToBool(includeGrpcOnlyMethods),
622-
IncludeList: includeList,
623-
ModulePath: modulePath,
624-
NameOverrides: nameOverrides,
625-
Output: relativePath,
626-
PostProcessProtos: postProcessProtos,
627-
RoutingRequired: strToBool(routingRequired),
628-
ExtendGrpcTransport: strToBool(extendGrpcTransport),
629-
ServiceConfig: sidekick.General.ServiceConfig,
630-
SkippedIds: strToSlice(skippedIds, false),
631-
Source: sidekick.General.SpecificationSource,
632-
Template: strings.TrimPrefix(templateOverride, "templates/"),
633-
}
634-
635-
if len(moduleRoots) > 0 {
636-
module.ModuleRoots = moduleRoots
637-
}
638-
639-
disabledRustdocWarnings, ok := sidekick.Codec["disabled-rustdoc-warnings"]
640-
if ok {
641-
module.DisabledRustdocWarnings = strToSlice(disabledRustdocWarnings, true)
642-
}
643-
644-
modules = append(modules, module)
645-
646-
return nil
647-
})
648-
649-
return modules, err
650-
}
651-
652485
// buildConfig builds the complete config from libraries.
653486
func buildConfig(libraries map[string]*config.Library, defaults *config.Config) *config.Config {
654487
cfg := defaults
@@ -726,40 +559,18 @@ func strToBool(s string) bool {
726559
}
727560

728561
// strToSlice converts a comma-separated string into a slice of strings.
729-
//
730-
// The wantEmpty parameter controls the behavior when the input string is empty:
731-
// - If true: Returns an empty initialized slice (make([]string, 0)).
732-
// - If false: Returns nil.
733-
func strToSlice(s string, wantEmpty bool) []string {
562+
// Returns nil if the string is empty.
563+
func strToSlice(s string) []string {
734564
if s == "" {
735-
if wantEmpty {
736-
return make([]string, 0)
737-
}
738-
739565
return nil
740566
}
741-
742567
return strings.Split(s, ",")
743568
}
744569

745570
func isEmptyRustCrate(r *config.RustCrate) bool {
746571
return reflect.DeepEqual(r, &config.RustCrate{})
747572
}
748573

749-
func readTOML[T any](file string) (*T, error) {
750-
data, err := os.ReadFile(file)
751-
if err != nil {
752-
return nil, fmt.Errorf("failed to read %s: %w", file, err)
753-
}
754-
755-
var tomlData T
756-
if err := toml.Unmarshal(data, &tomlData); err != nil {
757-
return nil, fmt.Errorf("failed to unmarshal %s: %w", file, err)
758-
}
759-
760-
return &tomlData, nil
761-
}
762-
763574
func readCargoConfig(dir string) (*rust.Cargo, error) {
764575
cargoData, err := os.ReadFile(filepath.Join(dir, cargoFile))
765576
if err != nil {

0 commit comments

Comments
 (0)