From 46110044389b524f088337d3a86ea658bcaa7799 Mon Sep 17 00:00:00 2001 From: Alancere <804873052@qq.com> Date: Fri, 28 Jul 2023 17:37:21 +0800 Subject: [PATCH 1/4] add replaceFakeImport function --- .../generator/cmd/v2/common/fileProcessor.go | 52 +++++++++++++++++++ .../generator/cmd/v2/common/generation.go | 7 +++ 2 files changed, 59 insertions(+) diff --git a/eng/tools/generator/cmd/v2/common/fileProcessor.go b/eng/tools/generator/cmd/v2/common/fileProcessor.go index 50fb05f00bf2..beb721f79377 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,54 @@ 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 replaceFakeImport(path, rpName, namespaceName string, previousVersion, currentVersion string) error { + previous, err := semver.NewVersion(previousVersion) + if err != nil { + return err + } + + current, err := semver.NewVersion(currentVersion) + if err != nil { + return err + } + + 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()) + } + + if current.Major() < 1 { + return nil + } + newModule := fmt.Sprintf("github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/%s/%s/v%d", rpName, namespaceName, current.Major()) + + if previous.Major() == current.Major() { + return nil + } + + return filepath.Walk(filepath.Join(path, "fake"), func(path string, info fs.FileInfo, err error) error { + if !info.IsDir() && strings.HasSuffix(info.Name(), "_server.go") { + b, err := os.ReadFile(path) + if err != nil { + return err + } + + newFile := strings.ReplaceAll(string(b), oldModule, newModule) + 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..0f7b22ce7b3d 100644 --- a/eng/tools/generator/cmd/v2/common/generation.go +++ b/eng/tools/generator/cmd/v2/common/generation.go @@ -298,6 +298,13 @@ func (ctx *GenerateContext) GenerateForSingleRPNamespace(generateParam *Generate return nil, err } + if changelog.HasBreakingChanges() && isGenerateFake(packagePath) { + log.Printf("Replace fake module v2+...") + if err = replaceFakeImport(packagePath, generateParam.RPName, generateParam.NamespaceName, previousVersion, version.String()); 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...") From b28493c50f06a369998051a8a574813fd0b90881 Mon Sep 17 00:00:00 2001 From: Alancere <804873052@qq.com> Date: Mon, 31 Jul 2023 14:35:20 +0800 Subject: [PATCH 2/4] generic func --- .../generator/cmd/v2/common/fileProcessor.go | 37 ++++++++++++++----- .../generator/cmd/v2/common/generation.go | 3 +- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/eng/tools/generator/cmd/v2/common/fileProcessor.go b/eng/tools/generator/cmd/v2/common/fileProcessor.go index beb721f79377..30e625af72e3 100644 --- a/eng/tools/generator/cmd/v2/common/fileProcessor.go +++ b/eng/tools/generator/cmd/v2/common/fileProcessor.go @@ -506,7 +506,7 @@ func isGenerateFake(path string) bool { return false } -func replaceFakeImport(path, rpName, namespaceName string, previousVersion, currentVersion string) error { +func replaceModuleImport(path, rpName, namespaceName, previousVersion, currentVersion, subPath string, suffixes ...string) error { previous, err := semver.NewVersion(previousVersion) if err != nil { return err @@ -517,30 +517,49 @@ func replaceFakeImport(path, rpName, namespaceName string, previousVersion, curr 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()) } - if current.Major() < 1 { - return nil + 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()) } - newModule := fmt.Sprintf("github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/%s/%s/v%d", rpName, namespaceName, current.Major()) - if previous.Major() == current.Major() { + if oldModule == newModule { return nil } - return filepath.Walk(filepath.Join(path, "fake"), func(path string, info fs.FileInfo, err error) error { - if !info.IsDir() && strings.HasSuffix(info.Name(), "_server.go") { + 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 err = os.WriteFile(path, []byte(newFile), 0666); err != nil { - return err + if newFile != string(b) { + if err = os.WriteFile(path, []byte(newFile), 0666); err != nil { + return err + } } } diff --git a/eng/tools/generator/cmd/v2/common/generation.go b/eng/tools/generator/cmd/v2/common/generation.go index 0f7b22ce7b3d..ef78ab60318e 100644 --- a/eng/tools/generator/cmd/v2/common/generation.go +++ b/eng/tools/generator/cmd/v2/common/generation.go @@ -300,7 +300,8 @@ func (ctx *GenerateContext) GenerateForSingleRPNamespace(generateParam *Generate if changelog.HasBreakingChanges() && isGenerateFake(packagePath) { log.Printf("Replace fake module v2+...") - if err = replaceFakeImport(packagePath, generateParam.RPName, generateParam.NamespaceName, previousVersion, version.String()); err != nil { + if err = replaceModuleImport(packagePath, generateParam.RPName, generateParam.NamespaceName, previousVersion, version.String(), + "fake", "_server.go"); err != nil { return nil, err } } From 294aec05a11019e811349ff80bc89f565582d944 Mon Sep 17 00:00:00 2001 From: Alancere <804873052@qq.com> Date: Mon, 31 Jul 2023 15:11:16 +0800 Subject: [PATCH 3/4] remove breakingchange judgment --- eng/tools/generator/cmd/v2/common/generation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/tools/generator/cmd/v2/common/generation.go b/eng/tools/generator/cmd/v2/common/generation.go index ef78ab60318e..f47feabafd69 100644 --- a/eng/tools/generator/cmd/v2/common/generation.go +++ b/eng/tools/generator/cmd/v2/common/generation.go @@ -298,7 +298,7 @@ func (ctx *GenerateContext) GenerateForSingleRPNamespace(generateParam *Generate return nil, err } - if changelog.HasBreakingChanges() && isGenerateFake(packagePath) { + if isGenerateFake(packagePath) { log.Printf("Replace fake module v2+...") if err = replaceModuleImport(packagePath, generateParam.RPName, generateParam.NamespaceName, previousVersion, version.String(), "fake", "_server.go"); err != nil { From ce640f88774a1557f7f7ebc828bbe8fc61ca536e Mon Sep 17 00:00:00 2001 From: Alancere <804873052@qq.com> Date: Mon, 31 Jul 2023 15:20:04 +0800 Subject: [PATCH 4/4] fix --- eng/tools/generator/cmd/v2/common/generation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/tools/generator/cmd/v2/common/generation.go b/eng/tools/generator/cmd/v2/common/generation.go index f47feabafd69..ef78ab60318e 100644 --- a/eng/tools/generator/cmd/v2/common/generation.go +++ b/eng/tools/generator/cmd/v2/common/generation.go @@ -298,7 +298,7 @@ func (ctx *GenerateContext) GenerateForSingleRPNamespace(generateParam *Generate return nil, err } - if isGenerateFake(packagePath) { + 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 {