-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
248 lines (211 loc) · 6.78 KB
/
main.go
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
// Integration giddyup as a go generate tool means that each generate will increment the next
// dev version. For a different workflow, go generate might not be a good fit but you can still
// use giddyup independently
//go:generate giddyup
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"gopkg.in/alecthomas/kingpin.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
var (
app = kingpin.New("giddyup", "A go generate tool to increment an application's version.")
variable = kingpin.Flag("variable", "Name of the version variable.").Default("VERSION").String()
mode = kingpin.Flag("mode", "Increment mode (MAJOR, MINOR, PATCH)").Short('m').Default("PATCH").Enum("MAJOR", "MINOR", "PATCH")
paths = kingpin.Arg("paths", "directories or files").Strings()
currentVersion = kingpin.Flag("toolVersion", "Only prints the current version of the tool without incrementing for the next release").Short('t').Default("false").Bool()
verbose = kingpin.Flag("verbose", "Verbose output (prints current and next dev versions)").Short('v').Default("false").Bool()
lazyInit = kingpin.Flag("init", "Initialize version to 1.0.0 if no managed version found)").Short('i').Default("false").Bool()
)
type errWriter struct {
b *bytes.Buffer
err error
}
func (ew *errWriter) writeString(value string) {
if ew.err != nil {
return
}
_, ew.err = ew.b.WriteString(value)
}
func main() {
kingpin.Version(VERSION)
kingpin.Parse()
inputPaths := *paths
if len(inputPaths) == 0 {
// Default: process whole package in current directory.
inputPaths = []string{"."}
}
if *currentVersion {
if err := printCurrentVersion(*variable, inputPaths); err != nil {
fmt.Fprintf(os.Stderr, "Error getting current version: %v", err)
}
} else {
if err := run(*variable, inputPaths, *mode, *lazyInit); err != nil {
fmt.Fprintf(os.Stderr, "Error generating version: %v", err)
}
}
}
func printCurrentVersion(variable string, inputPaths []string) error {
for _, path := range inputPaths {
version, err := getCurrentVersion(path, false)
if err != nil {
return err
}
fmt.Printf("%s", version)
}
return nil
}
func run(variable string, inputPaths []string, mode string, lazyInit bool) error {
for _, path := range inputPaths {
version, err := getCurrentVersion(path, lazyInit)
if err != nil {
return err
}
if *verbose {
fmt.Printf("Current version is [%s]\n", version)
}
nextDevVersion, err := getNextVersion(version, mode)
if err != nil {
return err
}
if *verbose {
fmt.Printf("Next dev version is [%s]\n", nextDevVersion)
}
var buffer bytes.Buffer
if err := writeHeader(&buffer, variable); err != nil {
return err
}
pkg := parsePackage(path)
err = generateContent(pkg, nextDevVersion, variable, &buffer)
if err != nil {
return err
}
// Write to file.
output := fmt.Sprintf("%s/version.go", filepath.Dir(path))
err = ioutil.WriteFile(output, buffer.Bytes(), 0644)
if err != nil {
return err
}
}
return nil
}
// isFile reports whether the named file is a file (not a directory).
func isFile(name string) bool {
info, err := os.Stat(name)
if err != nil {
log.Fatal(err)
}
return !info.IsDir()
}
func getCurrentVersion(path string, lazyInit bool) (string, error) {
fset := token.NewFileSet()
versionFilePath := filepath.Join(path, "version.go")
f, err := parser.ParseFile(fset, versionFilePath, nil, 0)
if err != nil {
if lazyInit {
if *verbose {
fmt.Printf("Version file not found at [%s], initializing version to [1.0.0]\n", versionFilePath)
}
return "1.0.0", nil
}
return "", err
}
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
if decl.Tok == token.CONST {
for _, spec := range decl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
for _, name := range valueSpec.Names {
if name.String() == *variable {
for _, value := range valueSpec.Values {
if basicLiteral, ok := value.(*ast.BasicLit); ok {
return strings.Trim(basicLiteral.Value, "\""), nil
}
}
}
}
}
}
}
}
}
return "", fmt.Errorf("Could not find version constant [%s] in file [%s]", *variable, versionFilePath)
}
func getNextVersion(version string, mode string) (string, error) {
versionRegEx := regexp.MustCompile("\\A(\\d+)\\.(\\d+)\\.(\\d+)\\z")
if versionRegEx.MatchString(version) {
matches := versionRegEx.FindAllStringSubmatch(version, -1)[0]
majorVersion, err := strconv.Atoi(matches[1])
if err != nil {
return "", fmt.Errorf("Version format should be [number.number.number] but was [%s]: [%v]", version, err)
}
minorVersion, err := strconv.Atoi(matches[2])
if err != nil {
return "", fmt.Errorf("Version format should be [number.number.number] but was [%s]: [%v]", version, err)
}
patchVersion, err := strconv.Atoi(matches[3])
if err != nil {
return "", fmt.Errorf("Version format should be [number.number.number] but was [%s]: [%v]", version, err)
}
switch mode {
case "PATCH":
patchVersion = patchVersion + 1
case "MINOR":
minorVersion = minorVersion + 1
patchVersion = 0
case "MAJOR":
majorVersion = majorVersion + 1
minorVersion = 0
patchVersion = 0
}
return fmt.Sprintf("%d.%d.%d", majorVersion, minorVersion, patchVersion), nil
} else {
return "", fmt.Errorf("Version format should be [number.number.number] but was [%s]", version)
}
}
// writeHeader writes the header of the file (code generation warning as well as the go:generate line)
func writeHeader(buffer *bytes.Buffer, variable string) error {
ew := &errWriter{b: buffer}
ew.writeString(fmt.Sprintln("// GENERATED and MANAGED by giddyup (https://github.com/alexandre-normand/giddyup)"))
return ew.err
}
func generateContent(pkg string, version string, variable string, buffer *bytes.Buffer) error {
buffer.WriteString(fmt.Sprintf("package %s\n\nconst (\n\t%s = \"%s\"\n)\n", pkg, variable, version))
return nil
}
// parsePackage analyzes the single package constructed from the named files.
// If text is non-nil, it is a string to be used instead of the content of the file,
// to be used for testing. parsePackage exits if there is an error.
func parsePackage(directory string) string {
var astFiles []*ast.File
fs := token.NewFileSet()
files, err := ioutil.ReadDir(directory)
if err != nil {
log.Fatalf("Failed to read directory [%s]: %v", directory, err)
}
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".go") {
continue
}
parsedFile, err := parser.ParseFile(fs, file.Name(), nil, 0)
if err != nil {
log.Fatalf("parsing package: %s: %s", file.Name(), err)
}
astFiles = append(astFiles, parsedFile)
}
if len(astFiles) == 0 {
log.Fatalf("%s: no buildable Go files", directory)
}
return astFiles[0].Name.Name
}