Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major version upgrade when processing fake #21285

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions eng/tools/generator/cmd/v2/common/fileProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package common

import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -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
})
}
8 changes: 8 additions & 0 deletions eng/tools/generator/cmd/v2/common/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down