diff --git a/eng/tools/generator/autorest/model/changelog.go b/eng/tools/generator/autorest/model/changelog.go index 101b36d1bd98..f4afdf6dde93 100644 --- a/eng/tools/generator/autorest/model/changelog.go +++ b/eng/tools/generator/autorest/model/changelog.go @@ -81,7 +81,7 @@ func writeChangelogForPackage(r *report.Package) string { } // write additional changes - md.WriteHeader("New Content") + md.WriteHeader("Features Added") for _, item := range getNewContents(r.AdditiveChanges) { md.WriteListItem(item) } diff --git a/eng/tools/generator/cmd/root.go b/eng/tools/generator/cmd/root.go index 52f134555f50..5ecf6fbb0580 100644 --- a/eng/tools/generator/cmd/root.go +++ b/eng/tools/generator/cmd/root.go @@ -14,6 +14,7 @@ import ( "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/release" "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/template" automation_v2 "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/automation" + refresh_v2 "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/refresh" release_v2 "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/release" "github.com/Azure/azure-sdk-for-go/eng/tools/generator/common" "github.com/spf13/cobra" @@ -41,12 +42,13 @@ func Command() *cobra.Command { rootCmd.AddCommand( automation.Command(), - automation_v2.Command(), - release_v2.Command(), issue.Command(), template.Command(), refresh.Command(), release.Command(), + automation_v2.Command(), + release_v2.Command(), + refresh_v2.Command(), ) return rootCmd diff --git a/eng/tools/generator/cmd/template/templateCmd.go b/eng/tools/generator/cmd/template/templateCmd.go index 50dd98df3f1d..4e4f28b71f54 100644 --- a/eng/tools/generator/cmd/template/templateCmd.go +++ b/eng/tools/generator/cmd/template/templateCmd.go @@ -59,26 +59,29 @@ func BindFlags(flagSet *pflag.FlagSet) { flagSet.String("package-title", "", "Specifies the title of this package") flagSet.String("commit", "", "Specifies the commit hash of azure-rest-api-specs") flagSet.String("release-date", "", "Specifies the release date in changelog") + flagSet.String("package-config", "", "Additional config for package") } // ParseFlags parses the flags to a Flags struct func ParseFlags(flagSet *pflag.FlagSet) Flags { return Flags{ - SDKRoot: flags.GetString(flagSet, "go-sdk-folder"), - TemplatePath: flags.GetString(flagSet, "template-path"), - PackageTitle: flags.GetString(flagSet, "package-title"), - Commit: flags.GetString(flagSet, "commit"), - ReleaseDate: flags.GetString(flagSet, "release-date"), + SDKRoot: flags.GetString(flagSet, "go-sdk-folder"), + TemplatePath: flags.GetString(flagSet, "template-path"), + PackageTitle: flags.GetString(flagSet, "package-title"), + Commit: flags.GetString(flagSet, "commit"), + ReleaseDate: flags.GetString(flagSet, "release-date"), + PackageConfig: flags.GetString(flagSet, "package-config"), } } // Flags ... type Flags struct { - SDKRoot string - TemplatePath string - PackageTitle string - Commit string - ReleaseDate string + SDKRoot string + TemplatePath string + PackageTitle string + Commit string + ReleaseDate string + PackageConfig string } // GeneratePackageByTemplate creates a new set of files based on the things in template directory @@ -99,7 +102,7 @@ func GeneratePackageByTemplate(rpName, packageName string, flags Flags) error { } // build the replaceMap - buildReplaceMap(rpName, packageName, flags.PackageTitle, flags.Commit, flags.ReleaseDate) + buildReplaceMap(rpName, packageName, flags.PackageConfig, flags.PackageTitle, flags.Commit, flags.ReleaseDate) // copy everything to destination directory for _, file := range fileList { @@ -123,11 +126,12 @@ func GeneratePackageByTemplate(rpName, packageName string, flags Flags) error { return nil } -func buildReplaceMap(rpName, packageName, packageTitle, commitID, releaseDate string) { +func buildReplaceMap(rpName, packageName, packageConfig, packageTitle, commitID, releaseDate string) { replaceMap = make(map[string]string) replaceMap[RPNameKey] = rpName replaceMap[PackageNameKey] = packageName + replaceMap[PackageConfigKey] = packageConfig replaceMap[PackageTitleKey] = packageTitle replaceMap[CommitIDKey] = commitID if releaseDate == "" { @@ -170,10 +174,11 @@ var ( ) const ( - RPNameKey = "{{rpName}}" - PackageNameKey = "{{packageName}}" - PackageTitleKey = "{{PackageTitle}}" - CommitIDKey = "{{commitID}}" - FilenameSuffix = ".tpl" - ReleaseDate = "{{releaseDate}}" + RPNameKey = "{{rpName}}" + PackageNameKey = "{{packageName}}" + PackageTitleKey = "{{PackageTitle}}" + CommitIDKey = "{{commitID}}" + FilenameSuffix = ".tpl" + ReleaseDate = "{{releaseDate}}" + PackageConfigKey = "{{packageConfig}}" ) diff --git a/eng/tools/generator/cmd/v2/common/fileProcessor.go b/eng/tools/generator/cmd/v2/common/fileProcessor.go index 857f96756113..04451c86b1ae 100644 --- a/eng/tools/generator/cmd/v2/common/fileProcessor.go +++ b/eng/tools/generator/cmd/v2/common/fileProcessor.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "os" + "path" "path/filepath" "regexp" "strings" @@ -30,11 +31,19 @@ var ( v2BeginRegex = regexp.MustCompile("^```\\s*yaml\\s*\\$\\(go\\)\\s*&&\\s*\\$\\((track2|v2)\\)") v2EndRegex = regexp.MustCompile("^\\s*```\\s*$") newClientMethodNameRegex = regexp.MustCompile("^New.+Client$") + versionLineRegex = regexp.MustCompile(`version\s*=\s*\".*v\d+\.\d+\.\d+\"`) + changelogVersionRegex = regexp.MustCompile(`##\s*(?P\d+\.\d+\.\d+)\s*\((\d{4}-\d{2}-\d{2}|Unreleased)\)`) + packageConfigRegex = regexp.MustCompile(`\$\((package-.+)\)`) ) +type PackageInfo struct { + Name string + Config string +} + // reads from readme.go.md, parses the `track2` section to get module and package name -func ReadV2ModuleNameToGetNamespace(path string) (map[string][]string, error) { - result := make(map[string][]string) +func ReadV2ModuleNameToGetNamespace(path string) (map[string][]PackageInfo, error) { + result := make(map[string][]PackageInfo) log.Printf("Reading from readme.go.md '%s'...", path) file, err := os.Open(path) if err != nil { @@ -79,7 +88,12 @@ func ReadV2ModuleNameToGetNamespace(path string) (map[string][]string, error) { } namespaceName := strings.TrimSuffix(strings.TrimSuffix(modules[3], "\n"), "\r") log.Printf("RP: %s Package: %s", modules[2], namespaceName) - result[modules[2]] = append(result[modules[2]], namespaceName) + packageConfig := "" + matchResults := packageConfigRegex.FindAllStringSubmatch(lines[start[i]], -1) + for _, matchResult := range matchResults { + packageConfig = matchResult[1] + ": true" + } + result[modules[2]] = append(result[modules[2]], PackageInfo{Name: namespaceName, Config: packageConfig}) } } } @@ -106,7 +120,7 @@ func CleanSDKGeneratedFiles(path string) error { return nil } -// replace repo commit with local path in autorest.md files +// replace repo commit with local path in autorest.md file func ChangeConfigWithLocalPath(path, specPath, specRPName string) error { log.Printf("Replacing repo commit with local path in autorest.md ...") b, err := ioutil.ReadFile(path) @@ -131,7 +145,7 @@ func ChangeConfigWithLocalPath(path, specPath, specRPName string) error { return ioutil.WriteFile(path, []byte(strings.Join(lines, "\n")), 0644) } -// replace repo URL and commit id in autorest.md files +// replace repo URL and commit id in autorest.md file func ChangeConfigWithCommitID(path, repoURL, commitID, specRPName string) error { log.Printf("Replacing repo URL and commit id in autorest.md ...") b, err := ioutil.ReadFile(path) @@ -151,25 +165,49 @@ func ChangeConfigWithCommitID(path, repoURL, commitID, specRPName string) error return ioutil.WriteFile(path, []byte(strings.Join(lines, "\n")), 0644) } -// get latest version according to `module-version: ` prefix in autorest.md file +// get swagger rp folder name from autorest.md file +func GetSpecRpName(packageRootPath string) (string, error) { + b, err := ioutil.ReadFile(path.Join(packageRootPath, "autorest.md")) + if err != nil { + return "", err + } + + lines := strings.Split(string(b), "\n") + for _, line := range lines { + if strings.Contains(line, autorest_md_file_suffix) { + allParts := strings.Split(line, "/") + for i, part := range allParts { + if part == "specification" { + return allParts[i+1], nil + } + } + } + } + return "", fmt.Errorf("cannot get sepc rp name from config") +} + +// get latest version from changelog file according to first line with: `## 0.2.1 (2021-11-22)` func GetLatestVersion(packageRootPath string) (*semver.Version, error) { - b, err := ioutil.ReadFile(filepath.Join(packageRootPath, "autorest.md")) + path := filepath.Join(packageRootPath, common.ChangelogFilename) + b, err := ioutil.ReadFile(path) if err != nil { - return nil, err + return nil, fmt.Errorf("cannot parse version from changelog") } lines := strings.Split(string(b), "\n") for _, line := range lines { - if strings.HasPrefix(line, autorest_md_module_version_prefix) { - versionString := strings.TrimSuffix(strings.TrimSuffix(line[len(autorest_md_module_version_prefix):], "\n"), "\r") - return semver.NewVersion(versionString) + matchResults := changelogVersionRegex.FindAllStringSubmatch(line, -1) + for _, matchResult := range matchResults { + if matchResult[2] != "Unreleased" { + return semver.NewVersion(matchResult[1]) + } } } - return nil, fmt.Errorf("cannot parse version from autorest.md") + return nil, fmt.Errorf("cannot parse version from changelog") } -// replace version according to `module-version: ` prefix in autorest.md file +// replace version: use `module-version: ` prefix to locate version in autorest.md file, use version = "v*.*.*" regrex to locate version in constants.go file func ReplaceVersion(packageRootPath string, newVersion string) error { path := filepath.Join(packageRootPath, "autorest.md") b, err := ioutil.ReadFile(path) @@ -185,7 +223,17 @@ func ReplaceVersion(packageRootPath string, newVersion string) error { } } - return ioutil.WriteFile(path, []byte(strings.Join(lines, "\n")), 0644) + if err = ioutil.WriteFile(path, []byte(strings.Join(lines, "\n")), 0644); err != nil { + return err + } + + path = filepath.Join(packageRootPath, sdk_generated_file_prefix+"constants.go") + if b, err = ioutil.ReadFile(path); err != nil { + return err + } + contents := versionLineRegex.ReplaceAllString(string(b), "version = \"v"+newVersion+"\"") + + return ioutil.WriteFile(path, []byte(contents), 0644) } // calculate new version by changelog using semver package @@ -226,13 +274,27 @@ func AddChangelogToFile(changelog *model.Changelog, version *semver.Version, pac if err != nil { return "", err } + oldChangelog := string(b) - insertPos := strings.Index(oldChangelog, "##") + newChangelog := "" + matchResults := changelogVersionRegex.FindAllStringSubmatchIndex(oldChangelog, -1) additionalChangelog := changelog.ToCompactMarkdown() if releaseDate == "" { releaseDate = time.Now().Format("2006-01-02") } - newChangelog := oldChangelog[:insertPos] + "## " + version.String() + " (" + releaseDate + ")\n" + additionalChangelog + "\n\n" + oldChangelog[insertPos:] + + for _, matchResult := range matchResults { + if oldChangelog[matchResult[4]:matchResult[5]] == "Unreleased" { + newChangelog = newChangelog + oldChangelog[0:matchResult[0]] + } else { + if newChangelog == "" { + newChangelog = newChangelog + oldChangelog[0:matchResult[0]] + } + newChangelog = newChangelog + "## " + version.String() + " (" + releaseDate + ")\r\n" + additionalChangelog + "\r\n\r\n" + oldChangelog[matchResult[0]:] + break + } + } + err = ioutil.WriteFile(path, []byte(newChangelog), 0644) if 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 a832c961aff7..86059ed18359 100644 --- a/eng/tools/generator/cmd/v2/common/generation.go +++ b/eng/tools/generator/cmd/v2/common/generation.go @@ -39,6 +39,7 @@ type GenerateResult struct { type GenerateParam struct { RPName string NamespaceName string + NamespaceConfig string SpecficVersion string SpecficPackageTitle string SpecRPName string @@ -62,14 +63,15 @@ func (ctx GenerateContext) GenerateForAutomation(readme, repo string) ([]Generat } } - for rpName, namespaceNames := range rpMap { - for _, namespaceName := range namespaceNames { - log.Printf("Process rp: %s, namespace: %s", rpName, namespaceName) + for rpName, packageInfos := range rpMap { + for _, packageInfo := range packageInfos { + log.Printf("Process rp: %s, namespace: %s", rpName, packageInfo.Name) singleResult, err := ctx.GenerateForSingleRPNamespace(&GenerateParam{ RPName: rpName, - NamespaceName: namespaceName, + NamespaceName: packageInfo.Name, SpecRPName: specRPName, SkipGenerateExample: true, + NamespaceConfig: packageInfo.Config, }) if err != nil { errors = append(errors, err) @@ -97,10 +99,11 @@ func (ctx GenerateContext) GenerateForSingleRPNamespace(generateParam *GenerateP log.Printf("Use template to generate new rp folder and basic package files...") if err = template.GeneratePackageByTemplate(generateParam.RPName, generateParam.NamespaceName, template.Flags{ - SDKRoot: ctx.SDKPath, - TemplatePath: "eng/tools/generator/template/rpName/packageName", - PackageTitle: generateParam.SpecficPackageTitle, - Commit: ctx.SpecCommitHash, + SDKRoot: ctx.SDKPath, + TemplatePath: "eng/tools/generator/template/rpName/packageName", + PackageTitle: generateParam.SpecficPackageTitle, + Commit: ctx.SpecCommitHash, + PackageConfig: generateParam.NamespaceConfig, }); err != nil { return nil, err } @@ -192,21 +195,11 @@ func (ctx GenerateContext) GenerateForSingleRPNamespace(generateParam *GenerateP return nil, err } - log.Printf("Remove all the files that start with `zz_generated_`...") - if err = CleanSDKGeneratedFiles(packagePath); err != nil { - return nil, err - } - - log.Printf("Replace version in autorest.md...") + log.Printf("Replace version in autorest.md and constants...") if err = ReplaceVersion(packagePath, version.String()); err != nil { return nil, err } - log.Printf("Run `go generate` to regenerate the code for new version...") - if err = ExecuteGoGenerate(packagePath); err != nil { - return nil, err - } - return &GenerateResult{ Version: version.String(), RPName: generateParam.RPName, diff --git a/eng/tools/generator/cmd/v2/common/workspaceProcessor.go b/eng/tools/generator/cmd/v2/common/workspaceProcessor.go new file mode 100644 index 000000000000..b396487fa0cd --- /dev/null +++ b/eng/tools/generator/cmd/v2/common/workspaceProcessor.go @@ -0,0 +1,63 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package common + +import ( + "fmt" + "path/filepath" + "regexp" + + "github.com/Azure/azure-sdk-for-go/eng/tools/generator/repo" +) + +var ( + commitIDRegex = regexp.MustCompile("^[0-9a-f]{40}$") +) + +func GetSDKRepo(sdkRepoParam, sdkRepoURL string) (repo.SDKRepository, error) { + var err error + var sdkRepo repo.SDKRepository + // create sdk git repo ref + if commitIDRegex.Match([]byte(sdkRepoParam)) { + sdkRepo, err = repo.CloneSDKRepository(sdkRepoURL, sdkRepoParam) + if err != nil { + return nil, fmt.Errorf("failed to get sdk repo: %+v", err) + } + } else { + path, err := filepath.Abs(sdkRepoParam) + if err != nil { + return nil, fmt.Errorf("failed to get the directory of azure-sdk-for-go: %v", err) + } + + sdkRepo, err = repo.OpenSDKRepository(path) + if err != nil { + return nil, fmt.Errorf("failed to get sdk repo: %+v", err) + } + } + return sdkRepo, nil +} + +func GetSpecCommit(specRepoParam string) (string, error) { + specCommitHash := "" + // create spec git repo ref + if commitIDRegex.Match([]byte(specRepoParam)) { + specCommitHash = specRepoParam + } else { + path, err := filepath.Abs(specRepoParam) + if err != nil { + return "", fmt.Errorf("failed to get the directory of azure-rest-api-specs: %v", err) + } + specRepo, err := repo.OpenSpecRepository(path) + if err != nil { + return "", fmt.Errorf("failed to get spec repo: %+v", err) + } + specHeader, err := specRepo.Head() + if err != nil { + return "", fmt.Errorf("failed to get HEAD ref of azure-rest-api-specs: %+v", err) + } + specCommitHash = specHeader.Hash().String() + } + + return specCommitHash, nil +} diff --git a/eng/tools/generator/cmd/v2/refresh/refreshCmd.go b/eng/tools/generator/cmd/v2/refresh/refreshCmd.go new file mode 100644 index 000000000000..ad5c6775ed39 --- /dev/null +++ b/eng/tools/generator/cmd/v2/refresh/refreshCmd.go @@ -0,0 +1,148 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package refresh + +import ( + "fmt" + "io/ioutil" + "log" + "path" + "time" + + "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/common" + "github.com/Azure/azure-sdk-for-go/eng/tools/generator/flags" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +var ( + releaseBranchNamePattern = "release-%s-%s-%s-%v" +) + +// Release command +func Command() *cobra.Command { + releaseCmd := &cobra.Command{ + Use: "refresh-v2 ", + Short: "Regenerate all v2 release of azure-sdk-for-go", + Long: `This command will regenerate all v2 release for azure-sdk-for-go. + +azure-sdk-for-go directory/commitid: the directory path of the azure-sdk-for-go with git control or just a commitid for remote repo +azure-rest-api-specs directory: the directory path of the azure-rest-api-specs with git control or just a commitid for remote repo +`, + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + + ctx := commandContext{ + flags: ParseFlags(cmd.Flags()), + } + return ctx.execute(args[0], args[1]) + }, + } + + BindFlags(releaseCmd.Flags()) + + return releaseCmd +} + +type Flags struct { + SwaggerRepo string + SDKRepo string + ReleaseDate string + SkipCreateBranch bool + SkipGenerateExample bool +} + +func BindFlags(flagSet *pflag.FlagSet) { + flagSet.String("sdk-repo", "https://github.com/Azure/azure-sdk-for-go", "Specifies the sdk repo URL for generation") + flagSet.String("spec-repo", "https://github.com/Azure/azure-rest-api-specs", "Specifies the swagger repo URL for generation") + flagSet.String("release-date", "", "Specifies the release date in changelog") + flagSet.Bool("skip-create-branch", false, "Skip create release branch after generation") + flagSet.Bool("skip-generate-example", false, "Skip generate example for SDK in the same time") +} + +func ParseFlags(flagSet *pflag.FlagSet) Flags { + return Flags{ + SDKRepo: flags.GetString(flagSet, "sdk-repo"), + SwaggerRepo: flags.GetString(flagSet, "spec-repo"), + ReleaseDate: flags.GetString(flagSet, "release-date"), + SkipCreateBranch: flags.GetBool(flagSet, "skip-create-branch"), + SkipGenerateExample: flags.GetBool(flagSet, "skip-generate-example"), + } +} + +type commandContext struct { + flags Flags +} + +func (c *commandContext) execute(sdkRepoParam, specRepoParam string) error { + sdkRepo, err := common.GetSDKRepo(sdkRepoParam, c.flags.SDKRepo) + if err != nil { + return err + } + + specCommitHash, err := common.GetSpecCommit(specRepoParam) + if err != nil { + return err + } + + generateCtx := common.GenerateContext{ + SDKPath: sdkRepo.Root(), + SDKRepo: &sdkRepo, + SpecCommitHash: specCommitHash, + SpecRepoURL: c.flags.SwaggerRepo, + } + + rps, err := ioutil.ReadDir(path.Join(generateCtx.SDKPath, "sdk", "resourcemanager")) + if err != nil { + return fmt.Errorf("failed to get all rps: %+v", err) + } + + if !c.flags.SkipCreateBranch { + log.Printf("Create new branch for release") + releaseBranchName := fmt.Sprintf(releaseBranchNamePattern, "refresh", "all", "package", time.Now().Unix()) + if err := sdkRepo.CreateReleaseBranch(releaseBranchName); err != nil { + return fmt.Errorf("failed to create release branch: %+v", err) + } + } + + for _, rp := range rps { + namespaces, err := ioutil.ReadDir(path.Join(generateCtx.SDKPath, "sdk", "resourcemanager", rp.Name())) + if err != nil { + continue + } + + for _, namespace := range namespaces { + log.Printf("Release generation for rp: %s, namespace: %s", rp.Name(), namespace.Name()) + specRpName, err := common.GetSpecRpName(path.Join(generateCtx.SDKPath, "sdk", "resourcemanager", rp.Name(), namespace.Name())) + if err != nil { + continue + } + result, err := generateCtx.GenerateForSingleRPNamespace(&common.GenerateParam{ + RPName: rp.Name(), + NamespaceName: namespace.Name(), + SpecficPackageTitle: "", + SpecficVersion: "", + SpecRPName: specRpName, + ReleaseDate: c.flags.ReleaseDate, + SkipGenerateExample: c.flags.SkipGenerateExample, + }) + if err != nil { + fmt.Printf("failed to finish release generation process: %+v", err) + continue + } + // print generation result + log.Printf("Generation result: %s", result) + + if !c.flags.SkipCreateBranch { + log.Printf("Include the packages that is about to release in this release and do release commit...") + // append a time in long to avoid collision of branch names + if err := sdkRepo.AddReleaseCommit(rp.Name(), namespace.Name(), generateCtx.SpecCommitHash, result.Version); err != nil { + return fmt.Errorf("failed to add release package or do release commit: %+v", err) + } + } + } + } + + return nil +} diff --git a/eng/tools/generator/cmd/v2/release/releaseCmd.go b/eng/tools/generator/cmd/v2/release/releaseCmd.go index db06b3b9a7a5..5f6ea8d286ab 100644 --- a/eng/tools/generator/cmd/v2/release/releaseCmd.go +++ b/eng/tools/generator/cmd/v2/release/releaseCmd.go @@ -6,19 +6,15 @@ package release import ( "fmt" "log" - "path/filepath" - "regexp" "time" "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/common" "github.com/Azure/azure-sdk-for-go/eng/tools/generator/flags" - "github.com/Azure/azure-sdk-for-go/eng/tools/generator/repo" "github.com/spf13/cobra" "github.com/spf13/pflag" ) var ( - commitIDRegex = regexp.MustCompile("^[0-9a-f]{40}$") releaseBranchNamePattern = "release-%s-%s-%s-%v" ) @@ -66,6 +62,7 @@ type Flags struct { ReleaseDate string SkipCreateBranch bool SkipGenerateExample bool + PackageConfig string } func BindFlags(flagSet *pflag.FlagSet) { @@ -77,6 +74,7 @@ func BindFlags(flagSet *pflag.FlagSet) { flagSet.String("release-date", "", "Specifies the release date in changelog") flagSet.Bool("skip-create-branch", false, "Skip create release branch after generation") flagSet.Bool("skip-generate-example", false, "Skip generate example for SDK in the same time") + flagSet.String("package-config", "", "Additional config for package") } func ParseFlags(flagSet *pflag.FlagSet) Flags { @@ -89,6 +87,7 @@ func ParseFlags(flagSet *pflag.FlagSet) Flags { ReleaseDate: flags.GetString(flagSet, "release-date"), SkipCreateBranch: flags.GetBool(flagSet, "skip-create-branch"), SkipGenerateExample: flags.GetBool(flagSet, "skip-generate-example"), + PackageConfig: flags.GetString(flagSet, "package-config"), } } @@ -99,43 +98,14 @@ type commandContext struct { } func (c *commandContext) execute(sdkRepoParam, specRepoParam string) error { - var err error - var sdkRepo repo.SDKRepository - // create sdk and spec git repo ref - if commitIDRegex.Match([]byte(sdkRepoParam)) { - sdkRepo, err = repo.CloneSDKRepository(c.flags.SDKRepo, sdkRepoParam) - if err != nil { - return fmt.Errorf("failed to get sdk repo: %+v", err) - } - } else { - path, err := filepath.Abs(sdkRepoParam) - if err != nil { - return fmt.Errorf("failed to get the directory of azure-sdk-for-go: %v", err) - } - - sdkRepo, err = repo.OpenSDKRepository(path) - if err != nil { - return fmt.Errorf("failed to get sdk repo: %+v", err) - } + sdkRepo, err := common.GetSDKRepo(sdkRepoParam, c.flags.SDKRepo) + if err != nil { + return err } - specCommitHash := "" - if commitIDRegex.Match([]byte(specRepoParam)) { - specCommitHash = specRepoParam - } else { - path, err := filepath.Abs(specRepoParam) - if err != nil { - return fmt.Errorf("failed to get the directory of azure-rest-api-specs: %v", err) - } - specRepo, err := repo.OpenSpecRepository(path) - if err != nil { - return fmt.Errorf("failed to get spec repo: %+v", err) - } - specHeader, err := specRepo.Head() - if err != nil { - return fmt.Errorf("failed to get HEAD ref of azure-rest-api-specs: %+v", err) - } - specCommitHash = specHeader.Hash().String() + specCommitHash, err := common.GetSpecCommit(specRepoParam) + if err != nil { + return err } log.Printf("Release generation for rp: %s, namespace: %s", c.rpName, c.namespaceName) @@ -152,6 +122,7 @@ func (c *commandContext) execute(sdkRepoParam, specRepoParam string) error { result, err := generateCtx.GenerateForSingleRPNamespace(&common.GenerateParam{ RPName: c.rpName, NamespaceName: c.namespaceName, + NamespaceConfig: c.flags.PackageConfig, SpecficPackageTitle: c.flags.PackageTitle, SpecficVersion: c.flags.VersionNumber, SpecRPName: c.flags.SpecRPName, diff --git a/eng/tools/generator/repo/sdk.go b/eng/tools/generator/repo/sdk.go index a7bf5c35ece2..813854b2db6f 100644 --- a/eng/tools/generator/repo/sdk.go +++ b/eng/tools/generator/repo/sdk.go @@ -85,6 +85,7 @@ func (s *sdkRepository) AddReleaseCommit(rpName, namespaceName, specHash, versio return nil } + func (s *sdkRepository) CreateReleaseBranch(releaseBranchName string) error { log.Printf("Checking out to %s", plumbing.NewBranchReferenceName(releaseBranchName)) return s.Checkout(&CheckoutOptions{ diff --git a/eng/tools/generator/template/rpName/packageName/autorest.md.tpl b/eng/tools/generator/template/rpName/packageName/autorest.md.tpl index 26ee22bf9c9c..b49e4eafb55f 100644 --- a/eng/tools/generator/template/rpName/packageName/autorest.md.tpl +++ b/eng/tools/generator/template/rpName/packageName/autorest.md.tpl @@ -9,4 +9,5 @@ require: - https://github.com/Azure/azure-rest-api-specs/blob/{{commitID}}/specification/{{rpName}}/resource-manager/readme.go.md license-header: MICROSOFT_MIT_NO_VERSION module-version: 0.1.0 +{{packageConfig}} ``` \ No newline at end of file