rename outfile xversion.go to lower init() priority, add outfile option

This commit is contained in:
AJ ONeal 2019-06-21 00:01:02 -06:00
parent 47b8ec43ae
commit 9f05e2304c
12 changed files with 85 additions and 8 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
generated-version.go
xversion.go
zversion.go
# ---> Go
# Binaries for programs and plugins

View File

@ -12,13 +12,17 @@ Goal: Either use an exact version like v1.0.0
# Demo
Generate a `generated-version.go` file:
Generate an `xversion.go` file:
```bash
go run git.rootprojects.org/root/go-gitver
cat generated-version.go
cat xversion.go
```
<small>**Note**: The file is named `xversion.go` by default so that the
generated file's `init()` will come later, and thus take priority, over
most other files.</small>
See `go-gitver`s self-generated version:
```bash
@ -60,6 +64,7 @@ You don't have to use `mod vendor`, but I highly recommend it.
version print version and exit
--fail exit with non-zero status code on failure
--package <name> will set the package name
--outfile <name> will replace `xversion.go` with the given file path
```
ENVs

View File

@ -25,7 +25,7 @@ go build -mod=vendor -o hello *.go
```
Note: If the source is distributed in a non-git tarball then
`generated-version.go` will not be output, and whatever
`version-generated.go` will not be output, and whatever
version info is in `package main` will remain as-is.
If you would prefer the build process to fail (i.e. in a CI/CD pipeline),

View File

@ -2,4 +2,4 @@ module example.com/hello
go 1.12
require git.rootprojects.org/root/go-gitver v1.0.1
require git.rootprojects.org/root/go-gitver v1.1.0

View File

@ -2,4 +2,4 @@ module example.com/hello
go 1.12
require git.rootprojects.org/root/go-gitver v1.0.1
require git.rootprojects.org/root/go-gitver v1.1.0

View File

@ -0,0 +1,24 @@
# Example
Prints the version or a nice message
# Doesn't have a separate tools package
This is just like `examples/sub-package` except that its `//go:generate` is in `main.go`
and it outputs `./version/zversion.go` instead of `xversion.go`.
See `examples/basic` for more details.
# Demo
```bash
go mod tidy
go mod vendor
```
```bash
go generate -mod=vendor ./...
go build -mod=vendor -o hello *.go
./hello
./hello --version
```

View File

@ -0,0 +1,5 @@
module example.com/hello
go 1.12
require git.rootprojects.org/root/go-gitver v1.1.0

View File

@ -0,0 +1,24 @@
//go:generate go run -mod=vendor git.rootprojects.org/root/go-gitver --package version --outfile ./version/zversion.go
package main
import (
"flag"
"fmt"
"example.com/hello/version"
)
func main() {
showVersion := flag.Bool("version", false, "Print version and exit")
flag.Parse()
if *showVersion {
fmt.Println(version.GitRev)
fmt.Println(version.GitVersion)
fmt.Println(version.GitTimestamp)
return
}
fmt.Println("Hello, World!")
}

View File

@ -0,0 +1,8 @@
// +build tools
// This is a dummy package for build tooling
package tools
import (
_ "git.rootprojects.org/root/go-gitver"
)

View File

@ -0,0 +1,7 @@
package version
var (
GitRev = "0000000"
GitVersion = "v0.0.0-pre0+0000000"
GitTimestamp = "0000-00-00T00:00:00+0000"
)

View File

@ -2,4 +2,4 @@ module example.com/hello
go 1.12
require git.rootprojects.org/root/go-gitver v1.0.1
require git.rootprojects.org/root/go-gitver v1.1.0

View File

@ -19,7 +19,7 @@ import (
var exitCode int
var exactVer *regexp.Regexp
var gitVer *regexp.Regexp
var verFile = "generated-version.go"
var verFile = "xversion.go"
var (
GitRev = "0000000"
@ -44,6 +44,9 @@ func main() {
arg := args[i]
if "-f" == arg || "--fail" == arg {
exitCode = 1
} else if ("--outfile" == arg || "-o" == arg) && len(args) > i+1 {
verFile = args[i+1]
args[i+1] = ""
} else if "--package" == arg && len(args) > i+1 {
pkg = args[i+1]
args[i+1] = ""