Skip to content

Commit

Permalink
Refactor versioner (Azure#1461)
Browse files Browse the repository at this point in the history
* Ensures go.mod exists before doing actual work

* Fix wrong module name

* Add a new test scenario

* Some refinement on testcases for versioner

* Update version in version.go

* Only keep one line of tag in version.go

* Minor fix

* No need to print tag to stdout

* New test scenario

* Fix linter problem

* Return error from filepath.Abs

* Add test for update version.go file

* Remove method that ensures existence of go.mod file, and some redaudent
test cases

* Formatting code after versioning done

* Refine tests

* Fix a bug that when update applied to v2, the go.mod file does not
update as v2

* Clean up

* Purely file rename, nothing else changed

* Add functionality for run tool in root folder

* Add root command again

* Fix format

* Typo fix

* Add test for list all stage folders

* Add two functions for programmatically call

* Fix CI failure

* Add a new test case for more realistic

* Fix errors in the new test case

* Fix gofmt issues

* Add a new struct to cover this scenario

* Add test for go vet

* Add new test case for one line import

* Format stage folder first to avoid un-expected changes in code

* Fix a bug in regex used for multiline import statement

* Move getPkgs method to an internal package and exported

* Fix broken tests

* Add comment to silence golint error

* Init command

* Format change

* More minor tweak to satisfy golint
  • Loading branch information
ArcturusZhang authored and SDK Automation committed Oct 24, 2019
1 parent 69936f4 commit 7515ab9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 28 deletions.
12 changes: 2 additions & 10 deletions tools/internal/pkgs/pkgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,12 @@ func (p Pkg) GetAPIVersion() (string, error) {
if p.IsARMPkg() {
// management-plane
regex := regexp.MustCompile(`mgmt/(.+)/`)
groups := regex.FindStringSubmatch(dest)
if len(groups) < 2 {
return "", fmt.Errorf("cannot find api version in %s", dest)
}
versionString := groups[1]
versionString := regex.FindStringSubmatch(dest)[1]
return versionString, nil
}
// data-plane
regex := regexp.MustCompile(`/(\d{4}-\d{2}.*|v?\d+(\.\d+)?)/`)
groups := regex.FindStringSubmatch(dest)
if len(groups) < 2 {
return "", fmt.Errorf("cannot find api version in %s", dest)
}
versionString := groups[1]
versionString := regex.FindStringSubmatch(dest)[1]
if versionString == "" {
return "", fmt.Errorf("does not find api version in data plane package %s", dest)
}
Expand Down
17 changes: 1 addition & 16 deletions tools/versioner/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ func theInitCommand(args []string) error {
if err := createGoModFile(root, p); err != nil {
errs = append(errs, err)
}
if err := createChangeLogFile(root, p); err != nil {
errs = append(errs, err)
}
}
// handle errors
if len(errs) == 0 {
Expand All @@ -147,8 +144,7 @@ func createVersionFile(root string, p pkgs.Pkg, tagPrefix string) error {
return fmt.Errorf("failed to get api version of package %s: %+v", p.Dest, err)
}
tag := tagPrefix + "/" + startingModVer
ver := versionGoRegex.FindString(startingModVer)
content := fmt.Sprintf(initialVerGo, p.Package.Name, ver, p.Package.Name, apiVersion, ver, tag)
content := fmt.Sprintf(initialVerGo, p.Package.Name, startingModVer, p.Package.Name, apiVersion, startingModVer, tag)
err = ioutil.WriteFile(verFilePath, []byte(content), 0755)
if err != nil {
return fmt.Errorf("failed to write file %s: %+v", verFilePath, err)
Expand All @@ -172,17 +168,6 @@ func createGoModFile(root string, p pkgs.Pkg) error {
return nil
}

func createChangeLogFile(root string, p pkgs.Pkg) error {
logFilePath := filepath.Join(root, p.Dest, changeLogName)
log, err := os.Create(logFilePath)
if err != nil {
return fmt.Errorf("failed to create %s: %v", changeLogName, err)
}
defer log.Close()
_, err = log.WriteString("No changes to exported content compared to the previous release.\n")
return err
}

func loadExceptions(exceptFile string) (map[string]bool, error) {
result := make(map[string]bool)
if exceptFile == "" {
Expand Down
3 changes: 1 addition & 2 deletions tools/versioner/cmd/unstage.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ var (
getTagsHook TagsHookFunc
)

const changeLogName = "CHANGELOG.md"

// TagsHookFunc is a func used for get tags from remote
type TagsHookFunc func(string, string) ([]string, error)

Expand Down Expand Up @@ -402,6 +400,7 @@ func writeChangelog(stage string, mod modinfo.Provider) error {
if mod.NewModule() {
return nil
}
const changeLogName = "CHANGELOG.md"
rpt := mod.GenerateReport()
log, err := os.Create(filepath.Join(stage, changeLogName))
if err != nil {
Expand Down

0 comments on commit 7515ab9

Please sign in to comment.