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

[Release Automation] Update Helm/SDK/Install Packages Version Numbers #3149

Merged
merged 13 commits into from
May 17, 2023
81 changes: 81 additions & 0 deletions build/scripts/version-update/after-release/after-release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)

func main() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than the heavy duplication between after-release and before-release, I would recommend using a flag. For a script like this, no worries using the standard flag library: https://pkg.go.dev/flag

Examples in our repo include: https://github.com/googleforgames/agones/blob/main/examples/simple-game-server/main.go#L41-L49

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to make it a single script using Flag. Thanks!

if len(os.Args) < 2 {
fmt.Println("Please provide the initial version as a command-line argument")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.Fatalf here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used log instead fmt package

return
}

initialVersion := os.Args[1]
files := []string{
"install/helm/agones/Chart.yaml",
"install/yaml/install.yaml",
"install/helm/agones/values.yaml",
"sdks/nodejs/package.json",
"sdks/nodejs/package-lock.json",
"sdks/unity/package.json",
"sdks/csharp/sdk/AgonesSDK.nuspec",
"sdks/csharp/sdk/csharp-sdk.csproj",
}

for _, filename := range files {
err := UpdateVersionInFile(filename, initialVersion)
if err != nil {
fmt.Printf("Error updating file %s: %s\n", filename, err.Error())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a log.Fatalf instead of just a Printf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used log instead fmt

}
}
}

func UpdateVersionInFile(filename string, initialVersion string) error {
fileBytes, err := ioutil.ReadFile(filename)
if err != nil {
return err
}

content := string(fileBytes)

// Check the file extension and update version accordingly
ext := filepath.Ext(filename)
switch ext {
case ".yaml", ".yml", ".nuspec", ".csproj":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this needs a case statement. It seems like you can just write it as

if ext := filepath.Ext(filename); ext == ".json" {
  updateJSONVersion()
} else {
  updateVersion()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to update the JSON files without a switch/case statement, but the syntax was slightly different. So created a switch/case statement to ensure that the files were updated correctly.

content = updateVersion(content, initialVersion)
case ".json":
content = updateJSONVersion(content, initialVersion)
}

err = ioutil.WriteFile(filename, []byte(content), 0644)
if err != nil {
return err
}

return nil
}

func updateVersion(content string, initialVersion string) string {
re := regexp.MustCompile(regexp.QuoteMeta(initialVersion))
newVersion := incrementVersion(initialVersion)
return re.ReplaceAllString(content, newVersion+"-dev")
}

func updateJSONVersion(content string, initialVersion string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the only difference here is handling "1.2.3" vs 1.2.3, is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that's correct

re := regexp.MustCompile(`"` + regexp.QuoteMeta(initialVersion) + `"`)
newVersion := incrementVersion(initialVersion) + "-dev"
return re.ReplaceAllString(content, `"`+newVersion+`"`)
}

func incrementVersion(version string) string {
segments := strings.Split(version, ".")
lastButOneSegment, _ := strconv.Atoi(segments[len(segments)-2])
segments[len(segments)-2] = strconv.Itoa(lastButOneSegment + 1)
return strings.Join(segments, ".")
}
71 changes: 71 additions & 0 deletions build/scripts/version-update/before-release/before-release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
)

func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide the value to update as a command-line argument")
return
}

valueToUpdate := os.Args[1]

files := []string{
"install/helm/agones/Chart.yaml",
"install/yaml/install.yaml",
"install/helm/agones/values.yaml",
"sdks/nodejs/package.json",
"sdks/nodejs/package-lock.json",
"sdks/unity/package.json",
"sdks/csharp/sdk/AgonesSDK.nuspec",
"sdks/csharp/sdk/csharp-sdk.csproj",
}

for _, filename := range files {
err := UpdateValueInFile(filename, valueToUpdate)
if err != nil {
fmt.Printf("Error updating file %s: %s\n", filename, err.Error())
}
}
}

func UpdateValueInFile(filename string, valueToUpdate string) error {
fileBytes, err := ioutil.ReadFile(filename)
if err != nil {
return err
}

content := string(fileBytes)

// Check the file extension and update values accordingly
ext := filepath.Ext(filename)
switch ext {
case ".yaml", ".yml", ".nuspec", ".csproj":
content = updateValues(content, valueToUpdate)
case ".json":
content = updateJSONValues(content, valueToUpdate)
}

err = ioutil.WriteFile(filename, []byte(content), 0644)
if err != nil {
return err
}

return nil
}

func updateValues(content string, valueToUpdate string) string {
re := regexp.MustCompile(`(\d+\.\d+\.\d+)-dev`)
return re.ReplaceAllString(content, "${1}")
}

func updateJSONValues(content string, valueToUpdate string) string {
re := regexp.MustCompile(`"(\d+\.\d+\.\d+)-dev"`)
return re.ReplaceAllString(content, `"$1"`)
}