agones.dev/agones@v1.53.0/build/scripts/sdk-update-version/main.go (about) 1 // Copyright 2023 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package main implements a program that updates the version of files in the sdks and install directories. 16 package main 17 18 import ( 19 "flag" 20 "log" 21 "os" 22 "path/filepath" 23 "regexp" 24 "strconv" 25 "strings" 26 ) 27 28 var releaseStage string 29 var version string 30 31 const jsonExt string = ".json" 32 33 func init() { 34 flag.StringVar(&releaseStage, "release-stage", "", "Specify the release stage ('before', 'after', or 'patch')") 35 flag.StringVar(&version, "version", "", "Specify the initial version") 36 } 37 38 func main() { 39 flag.Parse() 40 41 if releaseStage == "" || version == "" { 42 log.Fatalf("Please provide the release stage ('before', 'after', or 'patch') and the version as command-line arguments") 43 } 44 45 log.Printf("Release Stage: %s", releaseStage) 46 log.Printf("Version: %s", version) 47 48 files := []string{ 49 "build/Makefile", 50 "install/helm/agones/Chart.yaml", 51 "install/yaml/install.yaml", 52 "install/helm/agones/values.yaml", 53 "sdks/nodejs/package.json", 54 "sdks/nodejs/package-lock.json", 55 "sdks/unity/package.json", 56 "sdks/csharp/sdk/AgonesSDK.nuspec", 57 "sdks/csharp/sdk/csharp-sdk.csproj", 58 "sdks/rust/Cargo.toml", 59 } 60 61 for _, filename := range files { 62 // Print the directory path 63 dir := filepath.Dir(filename) 64 log.Printf("Directory: %s", dir) 65 66 err := updateFile(filename, version) 67 if err != nil { 68 log.Fatalf("Error updating file %s: %s\n", filename, err.Error()) 69 } 70 } 71 } 72 73 // updateFile updates the specified file to the current release version before and after the release process. 74 func updateFile(filename string, version string) error { 75 fileBytes, err := os.ReadFile(filename) 76 if err != nil { 77 return err 78 } 79 80 content := string(fileBytes) 81 82 ext := filepath.Ext(filename) 83 84 switch releaseStage { 85 case "before": 86 if ext == jsonExt { 87 re := regexp.MustCompile(`"(\d+\.\d+\.\d+)-dev"`) 88 content = re.ReplaceAllString(content, `"$1"`) 89 } else { 90 re := regexp.MustCompile(`(\d+\.\d+\.\d+)-dev`) 91 content = re.ReplaceAllString(content, "${1}") 92 } 93 case "after": 94 if ext != jsonExt { 95 re := regexp.MustCompile(regexp.QuoteMeta(version)) 96 newVersion := incrementVersionAfterRelease(version) 97 if strings.HasSuffix(filename, "build/Makefile") { 98 content = re.ReplaceAllString(content, newVersion) 99 } else { 100 content = re.ReplaceAllString(content, newVersion+"-dev") 101 } 102 } else { 103 re := regexp.MustCompile(`"` + regexp.QuoteMeta(version) + `"`) 104 newVersion := incrementVersionAfterRelease(version) + "-dev" 105 content = re.ReplaceAllString(content, `"`+newVersion+`"`) 106 } 107 case "patch": 108 newVersion := incrementPatchVersion(version) 109 if ext != jsonExt { 110 re := regexp.MustCompile(regexp.QuoteMeta(version)) 111 content = re.ReplaceAllString(content, newVersion) 112 } else { 113 re := regexp.MustCompile(`"` + regexp.QuoteMeta(version) + `"`) 114 content = re.ReplaceAllString(content, `"`+newVersion+`"`) 115 } 116 default: 117 log.Fatalf("Invalid release stage. Please specify 'before', 'after', or 'patch'.") 118 } 119 120 err = os.WriteFile(filename, []byte(content), 0o644) 121 if err != nil { 122 return err 123 } 124 125 return nil 126 } 127 128 func incrementVersionAfterRelease(version string) string { 129 segments := strings.Split(version, ".") 130 if len(segments) < 3 { 131 log.Fatalf("Invalid version format: %s\n", version) 132 } 133 134 lastButOneSegment, err := strconv.Atoi(segments[len(segments)-2]) 135 if err != nil { 136 log.Fatalf("Error converting version segment to integer: %s\n", err.Error()) 137 } 138 segments[len(segments)-2] = strconv.Itoa(lastButOneSegment + 1) 139 return strings.Join(segments, ".") 140 } 141 142 func incrementPatchVersion(version string) string { 143 segments := strings.Split(version, ".") 144 if len(segments) < 3 { 145 log.Fatalf("Invalid version format: %s\n", version) 146 } 147 148 patchSegment, err := strconv.Atoi(segments[len(segments)-1]) 149 if err != nil { 150 log.Fatalf("Error converting version segment to integer: %s\n", err.Error()) 151 } 152 segments[len(segments)-1] = strconv.Itoa(patchSegment + 1) 153 return strings.Join(segments, ".") 154 }