]> git.lizzy.rs Git - micro.git/blob - tools/build-version.go
7a60307c56e3aceb1680449c52d6c28e7a4c5ee4
[micro.git] / tools / build-version.go
1 package main
2
3 import (
4         "fmt"
5         "os/exec"
6         "strings"
7
8         "github.com/zyedidia/micro/tools/semver"
9 )
10
11 func getTag(match ...string) (string, *semver.PRVersion) {
12         args := append([]string{
13                 "describe", "--tags",
14         }, match...)
15         if tag, err := exec.Command("git", args...).Output(); err != nil {
16                 return "", nil
17         } else {
18                 tagParts := strings.Split(string(tag), "-")
19                 if len(tagParts) == 3 {
20                         if ahead, err := semver.NewPRVersion(tagParts[1]); err == nil {
21                                 return tagParts[0], &ahead
22                         }
23                 }
24
25                 return tagParts[0], nil
26         }
27 }
28
29 func main() {
30         // Find the last vX.X.X Tag and get how many builds we are ahead of it.
31         versionStr, ahead := getTag("--match", "v*")
32         version, err := semver.ParseTolerant(versionStr)
33         if err != nil {
34                 // no version tag found so just return what ever we can find.
35                 fmt.Println("0.0.0-unknown")
36                 return
37         }
38         // Get the tag of the current revision.
39         tag, _ := getTag("--exact-match")
40         if tag == versionStr {
41                 // Seems that we are going to build a release.
42                 // So the version number should already be correct.
43                 fmt.Println(version.String())
44                 return
45         }
46
47         // If we don't have any tag assume "dev"
48         if tag == "" {
49                 tag = "dev"
50         }
51         // Get the most likely next version:
52         version.Patch = version.Patch + 1
53
54         if pr, err := semver.NewPRVersion(tag); err == nil {
55                 // append the tag as pre-release name
56                 version.Pre = append(version.Pre, pr)
57         }
58
59         if ahead != nil {
60                 // if we know how many commits we are ahead of the last release, append that too.
61                 version.Pre = append(version.Pre, *ahead)
62         }
63
64         fmt.Println(version.String())
65 }