diff --git a/eng/tools/generator/cmd/v2/common/fileProcessor.go b/eng/tools/generator/cmd/v2/common/fileProcessor.go index 50fb05f00bf2..30e625af72e3 100644 --- a/eng/tools/generator/cmd/v2/common/fileProcessor.go +++ b/eng/tools/generator/cmd/v2/common/fileProcessor.go @@ -5,6 +5,7 @@ package common import ( "fmt" + "io/fs" "io/ioutil" "log" "os" @@ -495,3 +496,73 @@ func AddTagSet(path, tag string) error { return os.WriteFile(path, []byte(strings.Join(lines, "\n")), 0644) } + +func isGenerateFake(path string) bool { + b, _ := os.ReadFile(filepath.Join(path, "autorest.md")) + if strings.Contains(string(b), "generate-fakes: true") { + return true + } + + return false +} + +func replaceModuleImport(path, rpName, namespaceName, previousVersion, currentVersion, subPath string, suffixes ...string) error { + previous, err := semver.NewVersion(previousVersion) + if err != nil { + return err + } + + current, err := semver.NewVersion(currentVersion) + if err != nil { + return err + } + + if previous.Major() == current.Major() { + return nil + } + + oldModule := fmt.Sprintf("github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/%s/%s", rpName, namespaceName) + if previous.Major() > 1 { + oldModule = fmt.Sprintf("%s/v%d", oldModule, previous.Major()) + } + + newModule := fmt.Sprintf("github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/%s/%s", rpName, namespaceName) + if current.Major() > 1 { + newModule = fmt.Sprintf("%s/v%d", newModule, current.Major()) + } + + if oldModule == newModule { + return nil + } + + return filepath.Walk(filepath.Join(path, subPath), func(path string, info fs.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + return nil + } + + suffix := false + for i := 0; i < len(suffixes) && !suffix; i++ { + suffix = strings.HasSuffix(info.Name(), suffixes[i]) + } + + if suffix { + b, err := os.ReadFile(path) + if err != nil { + return err + } + + newFile := strings.ReplaceAll(string(b), oldModule, newModule) + if newFile != string(b) { + if err = os.WriteFile(path, []byte(newFile), 0666); err != nil { + return err + } + } + } + + return nil + }) +} diff --git a/eng/tools/generator/cmd/v2/common/generation.go b/eng/tools/generator/cmd/v2/common/generation.go index 6d5a408b220c..ef78ab60318e 100644 --- a/eng/tools/generator/cmd/v2/common/generation.go +++ b/eng/tools/generator/cmd/v2/common/generation.go @@ -298,6 +298,14 @@ func (ctx *GenerateContext) GenerateForSingleRPNamespace(generateParam *Generate return nil, err } + if changelog.HasBreakingChanges() && isGenerateFake(packagePath) { + log.Printf("Replace fake module v2+...") + if err = replaceModuleImport(packagePath, generateParam.RPName, generateParam.NamespaceName, previousVersion, version.String(), + "fake", "_server.go"); err != nil { + return nil, err + } + } + // Example generation should be the last step because the package import relay on the new calculated version if !generateParam.SkipGenerateExample { log.Printf("Generate examples...")