Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pointlander committed Aug 26, 2020
1 parent 5cdb3ad commit b50b989
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
49 changes: 29 additions & 20 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,53 +50,62 @@ func main() {
}
}

const BuildinfoTemplate = `// Code Generated by "build.go buildinfo" DO NOT EDIT.
package main
const (
// VERSION is the version of peg
VERSION = "{{.Version}}"
// BUILDTIME is the build time of peg
BUILDTIME = "{{.Buildtime}}"
// COMMIT is the commit hash of peg
COMMIT = "{{.Commit}}"
// IS_TAGGED is there a version
IS_TAGGED = {{.IsTagged}}
)
`

func buildinfo() {
log.SetPrefix("buildinfo:")
type info struct {
Version string
Version string
Buildtime string
Commit string
IsTagged bool
Commit string
IsTagged bool
}
infFile, err := os.Create("buildinfo.go")
defer infFile.Close()
if err != nil {
log.Println("open buildinfo.go: fatal:",err)
return
log.Println("open buildinfo.go: fatal:", err)
}
var inf info = info{
Version:"unknown", // show this if we can't get the version
Version: "unknown", // show this if we can't get the version
}
vers, err := exec.Command("git","tag","--contains").Output()
vers, err := exec.Command("git", "tag", "--contains").Output()
if err != nil {
log.Println("error:", err)
} else if len(vers) > 1 { // ignore any single newlines that might exist
inf.IsTagged = true
inf.Version = strings.TrimSuffix(string(vers),"\n")
inf.Version = strings.TrimSuffix(string(vers), "\n")
} else {
vers, err = exec.Command("git","tag","--merged","--sort=v:refname").Output()
vers, err = exec.Command("git", "tag", "--merged", "--sort=v:refname").Output()
if err != nil {
log.Println("error:",err)
log.Println("error:", err)
} else if len(vers) > 1 {
tags := strings.Split(string(vers),"\n")
tags := strings.Split(string(vers), "\n")
inf.Version = tags[len(tags)-1]
}
}

cmit, err := exec.Command("git","rev-parse","HEAD").Output()
cmit, err := exec.Command("git", "rev-parse", "HEAD").Output()
if err != nil {
log.Println("error:",err)
log.Println("error:", err)
}
inf.Commit = strings.TrimSuffix(string(cmit),"\n")
inf.Commit = strings.TrimSuffix(string(cmit), "\n")
// slice the constant to remove the timezone specifier
inf.Buildtime = time.Now().UTC().Format(time.RFC3339[0:19])

templ := `// Code Generated by "build.go buildinfo" DO NOT EDIT.
package main
const (VERSION="{{.Version}}";BUILDTIME="{{.Buildtime}}";COMMIT="{{.Commit}}";IS_TAGGED={{.IsTagged}})`

err = template.Must(template.New("buildinfo").Parse(templ)).Execute(infFile,inf)
err = template.Must(template.New("buildinfo").Parse(BuildinfoTemplate)).Execute(infFile, inf)
if err != nil {
log.Println("error: template:", err)
}
Expand Down
11 changes: 10 additions & 1 deletion buildinfo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
// Code Generated by "build.go buildinfo" DO NOT EDIT.
package main

const (VERSION="";BUILDTIME="2020-08-23T02:42:17";COMMIT="97b325e916488e3c5cf8ce31b7cb7c32ca36472b";IS_TAGGED=false)
const (
// VERSION is the version of peg
VERSION = "unknown"
// BUILDTIME is the build time of peg
BUILDTIME = "2020-08-26T03:40:14"
// COMMIT is the commit hash of peg
COMMIT = "5cdb3adc061370cdd20392ffe2740cc8db104126"
// IS_TAGGED is there a version
IS_TAGGED = false
)
31 changes: 15 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,33 @@ import (
//go:generate build peg

var (
inline = flag.Bool("inline", false, "parse rule inlining")
_switch = flag.Bool("switch", false, "replace if-else if-else like blocks with switch blocks")
print = flag.Bool("print", false, "directly dump the syntax tree")
syntax = flag.Bool("syntax", false, "print out the syntax tree")
noast = flag.Bool("noast", false, "disable AST")
strict = flag.Bool("strict", false, "treat compiler warnings as errors")
filename = flag.String("output", "", "specify name of output file")
showVersion = flag.Bool("version", false, "print the version and exit")
inline = flag.Bool("inline", false, "parse rule inlining")
_switch = flag.Bool("switch", false, "replace if-else if-else like blocks with switch blocks")
print = flag.Bool("print", false, "directly dump the syntax tree")
syntax = flag.Bool("syntax", false, "print out the syntax tree")
noast = flag.Bool("noast", false, "disable AST")
strict = flag.Bool("strict", false, "treat compiler warnings as errors")
filename = flag.String("output", "", "specify name of output file")
showVersion = flag.Bool("version", false, "print the version and exit")
showBuildTime = flag.Bool("time", false, "show the last time `build.go buildinfo` was ran")
)

// whether running with -version should
// show the last time `build.go buildinfo` was ran
const Show_BUILDTIME = false

func main() {
runtime.GOMAXPROCS(2)
flag.Parse()

if *showVersion {
if IS_TAGGED {
fmt.Println("version:",VERSION)
fmt.Println("version:", VERSION)
} else {
fmt.Printf("version: %s-%s\n",VERSION,COMMIT)
fmt.Printf("version: %s-%s\n", VERSION, COMMIT)
}
if *showBuildTime {
fmt.Println("time:", BUILDTIME)
}
if Show_BUILDTIME {fmt.Println("time:",BUILDTIME)}
return
}

if flag.NArg() != 1 {
flag.Usage()
log.Fatalf("FILE: the peg file to compile")
Expand Down

0 comments on commit b50b989

Please sign in to comment.