forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
204 lines (183 loc) · 5.53 KB
/
version.go
File metadata and controls
204 lines (183 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ttnmage
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
"github.com/TheThingsIndustries/magepkg/git"
"github.com/blang/semver"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Version namespace.
type Version mg.Namespace
var goVersionFile = `// Code generated by Makefile. DO NOT EDIT.
package version
// TTN Version
var TTN = "%s-dev"
`
var currentVersion string
func (Version) getCurrent() error {
_, _, tag, err := git.Info()
if err != nil {
return err
}
currentVersion = tag
return nil
}
// Current returns the current version.
func (Version) Current() error {
mg.Deps(Version.getCurrent)
fmt.Println(currentVersion)
return nil
}
const (
goVersionFilePath = "pkg/version/ttn.go"
docVersionFilePath = "doc/config.toml"
docVersionTemplateFilePath = docVersionFilePath + ".tmpl"
)
var packageJSONFilePaths = []string{"package.json", "sdk/js/package.json", "doc/themes/the-things-stack/package.json"}
// Files writes the current version to files that contain version info and adds them to the Git index.
func (Version) Files() error {
if mg.Verbose() {
fmt.Println("Writing version files")
}
mg.Deps(Version.getCurrent)
version := strings.TrimPrefix(currentVersion, "v")
err := ioutil.WriteFile(goVersionFilePath, []byte(fmt.Sprintf(goVersionFile, version)), 0644)
if err != nil {
return err
}
for _, packageJSONFile := range packageJSONFilePaths {
err = sh.Run(
nodeBin("json"),
"-f", packageJSONFile,
"-I",
"-e", fmt.Sprintf(`this.version="%s"`, version),
)
if err != nil {
return err
}
}
docTmpl, err := template.New(filepath.Base(docVersionTemplateFilePath)).ParseFiles(docVersionTemplateFilePath)
if err != nil {
return err
}
target, err := os.OpenFile(docVersionFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil
}
fmt.Fprintln(target, "# File generated by mage. DO NOT EDIT.")
fmt.Fprintf(target, "# You can edit the template in %s\n", filepath.Base(docVersionTemplateFilePath))
fmt.Fprintln(target)
defer target.Close()
err = docTmpl.Execute(target, struct {
CurrentVersion string
}{
CurrentVersion: currentVersion,
})
if err != nil {
return err
}
return git.Add(append(packageJSONFilePaths, goVersionFilePath, docVersionFilePath)...)
}
func bumpVersion(bump string) error {
mg.Deps(Version.getCurrent)
version, err := semver.Parse(strings.TrimPrefix(currentVersion, "v"))
if err != nil {
return err
}
var newVersion semver.Version
switch bump {
case "release":
newVersion.Major = version.Major
newVersion.Minor = version.Minor
newVersion.Patch = version.Patch
newVersion.Pre = nil
case "major":
newVersion.Major = version.Major + 1
case "minor":
newVersion.Major = version.Major
newVersion.Minor = version.Minor + 1
case "patch":
newVersion.Major = version.Major
newVersion.Minor = version.Minor
newVersion.Patch = version.Patch + 1
case "rc":
newVersion.Major = version.Major
newVersion.Minor = version.Minor
newVersion.Patch = version.Patch
rc := 0
if len(version.Pre) > 0 {
rc, err = strconv.Atoi(strings.TrimPrefix(version.Pre[0].VersionStr, "rc"))
if err != nil {
return err
}
}
pre, err := semver.NewPRVersion(fmt.Sprintf("rc%d", rc+1))
if err != nil {
return err
}
newVersion.Pre = []semver.PRVersion{pre}
}
if mg.Verbose() {
fmt.Printf("Bumping version from %s to %s", version, newVersion)
}
currentVersion = fmt.Sprintf("v%s", newVersion)
return nil
}
// BumpRelease bumps a pre-release to a release version.
func (Version) BumpRelease() error { return bumpVersion("release") }
// BumpMajor bumps a major version.
func (Version) BumpMajor() error { return bumpVersion("major") }
// BumpMinor bumps a minor version.
func (Version) BumpMinor() error { return bumpVersion("minor") }
// BumpPatch bumps a patch version.
func (Version) BumpPatch() error { return bumpVersion("patch") }
// BumpRC bumps a release candidate version.
func (Version) BumpRC() error { return bumpVersion("rc") }
// CommitBump creates a git commit for the version bump.
func (Version) CommitBump() error {
mg.Deps(Version.getCurrent)
if mg.Verbose() {
fmt.Println("Creating version bump commit")
}
return git.Commit(fmt.Sprintf("all: Bump to version %s", strings.TrimPrefix(currentVersion, "v")))
}
// Tag creates a git tag for the current version.
func (Version) Tag() error {
mg.Deps(Version.getCurrent)
if mg.Verbose() {
fmt.Println("Creating version tag")
}
version, err := semver.Parse(strings.TrimPrefix(currentVersion, "v"))
if err != nil {
return err
}
versionType := "Release version"
if len(version.Pre) > 0 {
pre := version.Pre[0].VersionStr
versionType = "Pre-release for version"
if strings.HasPrefix(pre, "rc") {
versionType = fmt.Sprintf("Release candidate %s for version", strings.TrimPrefix(pre, "rc"))
}
version.Pre = nil
}
return git.Tag(currentVersion, fmt.Sprintf("%s %s", versionType, version))
}