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

Added replaceReadmeModule function #22135

Merged
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
37 changes: 37 additions & 0 deletions eng/tools/generator/cmd/v2/common/fileProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package common

import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
Expand Down Expand Up @@ -592,3 +593,39 @@ func existSuffixFile(path, suffix string) bool {

return existed
}

func replaceReadmeModule(path, rpName, namespaceName, currentVersion string) error {
readmeFile, err := os.ReadFile(filepath.Join(path, "README.md"))
if err != nil {
return err
}

module := fmt.Sprintf("github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/%s/%s", rpName, namespaceName)

readmeModule := module
match := regexp.MustCompile(fmt.Sprintf(`%s/v\d`, module))
if match.Match(readmeFile) {
readmeModule = match.FindString(string(readmeFile))
}

newModule := module
current, err := semver.NewVersion(currentVersion)
if err != nil {
return err
}
if current.Major() > 1 {
newModule = fmt.Sprintf("%s/v%d", newModule, current.Major())
}

if newModule == readmeModule {
return nil
}

newReadmeFile := bytes.ReplaceAll(readmeFile, []byte(readmeModule), []byte(newModule))
err = os.WriteFile(filepath.Join(path, "README.md"), newReadmeFile, 0666)
if err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions eng/tools/generator/cmd/v2/common/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ func (ctx *GenerateContext) GenerateForSingleRPNamespace(generateParam *Generate
}
}

log.Printf("Replace README.md module...")
if err = replaceReadmeModule(packagePath, generateParam.RPName, generateParam.NamespaceName, version.String()); err != nil {
raych1 marked this conversation as resolved.
Show resolved Hide resolved
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