@@ -30,19 +30,26 @@ import (
3030 "os"
3131 "os/exec"
3232 "path/filepath"
33+ "strconv"
3334 "strings"
3435 "time"
3536
3637 "github.com/coreos/go-semver/semver"
3738)
3839
40+ const (
41+ preReleasePrefix = "preview-"
42+ )
43+
3944var (
4045 versionFile string // Full path of VERSION file
4146 releaseType string // What type of release to create (major|minor|patch)
4247 ghRelease string // Full path of github-release tool
4348 ghUser string // Github account name to create release in
4449 ghRepo string // Github repository name to create release in
4550 binFolder string // Folder containing binaries
51+ preRelease bool // If set, mark release as preview
52+ dryRun bool // If set, do not really push a release or any git changes
4653
4754 binaries = map [string ]string {
4855 "arangodb-darwin-amd64" : "darwin/amd64/arangodb" ,
@@ -60,20 +67,22 @@ func init() {
6067 flag .StringVar (& ghUser , "github-user" , "arangodb-helper" , "Github account name to create release in" )
6168 flag .StringVar (& ghRepo , "github-repo" , "arangodb" , "Github repository name to create release in" )
6269 flag .StringVar (& binFolder , "bin-folder" , "./bin" , "Folder containing binaries" )
70+ flag .BoolVar (& preRelease , "prerelease" , false , "If set, mark release as preview" )
71+ flag .BoolVar (& dryRun , "dryrun" , false , "If set, do not really push a release or any git changes" )
6372}
6473
6574func main () {
6675 flag .Parse ()
6776 ensureGithubToken ()
6877 checkCleanRepo ()
69- version := bumpVersion (releaseType )
78+ version := bumpVersionInFile (releaseType )
7079 make ("clean ")
7180 make ("binaries ")
7281 createSHA256Sums ()
73- make (" docker - push - version " )
82+ pushDockerImages ( )
7483 gitTag (version )
7584 githubCreateRelease (version )
76- bumpVersion ("devel" )
85+ bumpVersionInFile ("devel" )
7786}
7887
7988// ensureGithubToken makes sure the GITHUB_TOKEN envvar is set.
@@ -100,33 +109,36 @@ func checkCleanRepo() {
100109 }
101110}
102111
112+ func pushDockerImages () {
113+ if dryRun {
114+ log .Printf ("Skipping pushing docker images to registry" )
115+ } else {
116+ make ("docker - push - version ")
117+ }
118+ }
119+
103120func make (target string ) {
104121 if err := run ("make" , target ); err != nil {
105122 log .Fatalf ("Failed to make %s: %v\n " , target , err )
106123 }
107124}
108125
109- func bumpVersion (action string ) string {
110- contents , err := ioutil .ReadFile (versionFile )
126+ func bumpVersionInFile (action string ) string {
127+ contents , err := os .ReadFile (versionFile )
111128 if err != nil {
112129 log .Fatalf ("Cannot read '%s': %v\n " , versionFile , err )
113130 }
114131 version := semver .New (strings .TrimSpace (string (contents )))
132+ bumpVersion (version , action , preRelease )
115133
116- switch action {
117- case "patch" :
118- version .BumpPatch ()
119- case "minor" :
120- version .BumpMinor ()
121- case "major" :
122- version .BumpMajor ()
123- case "devel" :
124- version .Metadata = "git"
125- }
126134 contents = []byte (version .String ())
127135
128- if err := ioutil .WriteFile (versionFile , contents , 0755 ); err != nil {
129- log .Fatalf ("Cannot write '%s': %v\n " , versionFile , err )
136+ if dryRun {
137+ log .Printf ("Skipping writing VERSION file. Content: %s" , string (contents ))
138+ } else {
139+ if err := os .WriteFile (versionFile , contents , 0755 ); err != nil {
140+ log .Fatalf ("Cannot write '%s': %v\n " , versionFile , err )
141+ }
130142 }
131143
132144 gitCommitAll (fmt .Sprintf ("Updated to %s" , version ))
@@ -135,7 +147,70 @@ func bumpVersion(action string) string {
135147 return version .String ()
136148}
137149
150+ func bumpVersion (version * semver.Version , action string , isPreRelease bool ) {
151+ if action == "devel" {
152+ version .Metadata = "git"
153+ return
154+ }
155+ version .Metadata = ""
156+
157+ currVersionIsPreRelease := strings .HasPrefix (string (version .PreRelease ), preReleasePrefix )
158+ if isPreRelease {
159+ firstPreRelease := semver .PreRelease (fmt .Sprintf ("%s1" , preReleasePrefix ))
160+ switch action {
161+ case "patch" :
162+ if currVersionIsPreRelease {
163+ version .PreRelease = bumpPreReleaseIndex (version .PreRelease )
164+ } else {
165+ version .BumpPatch ()
166+ version .PreRelease = firstPreRelease
167+ }
168+ case "minor" :
169+ if currVersionIsPreRelease && version .Patch == 0 {
170+ version .PreRelease = bumpPreReleaseIndex (version .PreRelease )
171+ } else {
172+ version .BumpMinor ()
173+ version .PreRelease = firstPreRelease
174+ }
175+ case "major" :
176+ if currVersionIsPreRelease && version .Minor == 0 && version .Patch == 0 {
177+ version .PreRelease = bumpPreReleaseIndex (version .PreRelease )
178+ } else {
179+ version .BumpMajor ()
180+ version .PreRelease = firstPreRelease
181+ }
182+ }
183+ } else {
184+ version .PreRelease = ""
185+ if ! currVersionIsPreRelease {
186+ switch action {
187+ case "patch" :
188+ version .BumpPatch ()
189+ case "minor" :
190+ version .BumpMinor ()
191+ case "major" :
192+ version .BumpMajor ()
193+ }
194+ }
195+ }
196+ }
197+
198+ func bumpPreReleaseIndex (preReleaseStr semver.PreRelease ) semver.PreRelease {
199+ indexStr := strings .TrimPrefix (string (preReleaseStr ), preReleasePrefix )
200+ index , err := strconv .Atoi (indexStr )
201+ if err != nil {
202+ log .Fatalf ("Could not parse prerelease: %s" , preReleaseStr )
203+ }
204+ index ++
205+ return semver .PreRelease (fmt .Sprintf ("%s%d" , preReleasePrefix , index ))
206+ }
207+
138208func gitCommitAll (message string ) {
209+ if dryRun {
210+ log .Printf ("Skipping git commit and push with message '%s'" , message )
211+ return
212+ }
213+
139214 args := []string {
140215 "commit" ,
141216 "--all" ,
@@ -150,6 +225,11 @@ func gitCommitAll(message string) {
150225}
151226
152227func gitTag (version string ) {
228+ if dryRun {
229+ log .Printf ("Skipping git tag with name '%s'" , version )
230+ return
231+ }
232+
153233 if err := run ("git" , "tag" , version ); err != nil {
154234 log .Fatalf ("Failed to tag: %v\n " , err )
155235 }
@@ -176,6 +256,11 @@ func createSHA256Sums() {
176256}
177257
178258func githubCreateRelease (version string ) {
259+ if dryRun {
260+ log .Printf ("Skipping github release with tag name '%s'" , version )
261+ return
262+ }
263+
179264 // Create draft release
180265 args := []string {
181266 "release" ,
@@ -184,6 +269,9 @@ func githubCreateRelease(version string) {
184269 "--tag" , version ,
185270 "--draft" ,
186271 }
272+ if preRelease {
273+ args = append (args , "--pre-release" )
274+ }
187275 if err := run (ghRelease , args ... ); err != nil {
188276 log .Fatalf ("Failed to create github release: %v\n " , err )
189277 }
@@ -217,6 +305,9 @@ func githubCreateRelease(version string) {
217305 "--repo" , ghRepo ,
218306 "--tag" , version ,
219307 }
308+ if preRelease {
309+ args = append (args , "--pre-release" )
310+ }
220311 if err := run (ghRelease , args ... ); err != nil {
221312 log .Fatalf ("Failed to finalize github release: %v\n " , err )
222313 }
0 commit comments