Skip to content

Commit

Permalink
Major version upgrade when processing fake (#21285)
Browse files Browse the repository at this point in the history
* add replaceFakeImport function

* generic func

* remove breakingchange judgment

* fix
  • Loading branch information
Alancere authored Aug 3, 2023
1 parent 90bbc9e commit 00e9b6c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
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

0 comments on commit 00e9b6c

Please sign in to comment.