Add build time, hash and version info to binaries

Add urfave/cli to the doodle.exe binary to add a --version command. Both
doodle.exe and doodad.exe will now print their git commit hash and build
date along with their version number when asked.

% ./bin/doodle --version
doodle version 0.0.7-alpha build a316baf. Built on 2018-12-30T14:44:43-08:00
pull/1/head
Noah 2018-12-30 14:46:29 -08:00
parent a316bafb12
commit f473609bb5
7 changed files with 102 additions and 43 deletions

View File

@ -2,10 +2,11 @@ SHELL := /bin/bash
VERSION=$(shell grep -e 'Version' doodle.go | head -n 1 | cut -d '"' -f 2)
BUILD=$(shell git describe --always)
BUILD_DATE=$(shell date -Iseconds)
CURDIR=$(shell curdir)
# Inject the build version (commit hash) into the executable.
LDFLAGS := -ldflags "-X main.Build=$(BUILD)"
LDFLAGS := -ldflags "-X main.Build=$(BUILD) -X main.BuildDate=$(BUILD_DATE)"
# `make setup` to set up a new environment, pull dependencies, etc.
.PHONY: setup

View File

@ -11,10 +11,10 @@ import (
"image/png"
"git.kirsle.net/apps/doodle"
"git.kirsle.net/apps/doodle/doodads"
"git.kirsle.net/apps/doodle/level"
"git.kirsle.net/apps/doodle/lib/render"
"git.kirsle.net/apps/doodle/src"
"git.kirsle.net/apps/doodle/src/doodads"
"git.kirsle.net/apps/doodle/src/level"
"github.com/urfave/cli"
"golang.org/x/image/bmp"
)

View File

@ -2,22 +2,38 @@
package main
import (
"fmt"
"log"
"os"
"sort"
"time"
"git.kirsle.net/apps/doodle"
"git.kirsle.net/apps/doodle/cmd/doodad/commands"
"git.kirsle.net/apps/doodle/src"
"github.com/urfave/cli"
)
var Build = "N/A"
// Build number is the git commit hash.
var (
Build = "<dynamic>"
BuildDate string
)
func init() {
if BuildDate == "" {
BuildDate = time.Now().Format(time.RFC3339)
}
}
func main() {
app := cli.NewApp()
app.Name = "doodad"
app.Usage = "command line interface for Doodle"
app.Version = doodle.Version + " build " + Build
app.Version = fmt.Sprintf("%s build %s. Built on %s",
doodle.Version,
Build,
BuildDate,
)
app.Flags = []cli.Flag{
cli.BoolFlag{

View File

@ -1,59 +1,101 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"sort"
"time"
"git.kirsle.net/apps/doodle/lib/render/sdl"
"git.kirsle.net/apps/doodle/src"
"git.kirsle.net/apps/doodle/src/balance"
"github.com/urfave/cli"
_ "image/png"
)
// Build number is the git commit hash.
var Build string
// Command line args
var (
debug bool
edit bool
guitest bool
Build = "<dynamic>"
BuildDate string
)
func init() {
flag.BoolVar(&debug, "debug", false, "Debug mode")
flag.BoolVar(&edit, "edit", false, "Edit the map given on the command line. Default is to play the map.")
flag.BoolVar(&guitest, "guitest", false, "Enter the GUI Test scene.")
if BuildDate == "" {
BuildDate = time.Now().Format(time.RFC3339)
}
}
func main() {
runtime.LockOSThread()
flag.Parse()
args := flag.Args()
var filename string
if len(args) > 0 {
filename = args[0]
}
// SDL engine.
engine := sdl.New(
"Doodle v"+doodle.Version,
balance.Width,
balance.Height,
app := cli.NewApp()
app.Name = "doodle"
app.Usage = "command line interface for Doodle"
app.Version = fmt.Sprintf("%s build %s. Built on %s",
doodle.Version,
Build,
BuildDate,
)
app := doodle.New(debug, engine)
app.SetupEngine()
if guitest {
app.Goto(&doodle.GUITestScene{})
} else if filename != "" {
if edit {
app.EditFile(filename)
} else {
app.PlayLevel(filename)
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "enable debug level logging",
},
cli.BoolFlag{
Name: "edit, e",
Usage: "edit the map given on the command line (instead of play it)",
},
cli.BoolFlag{
Name: "guitest",
Usage: "enter the GUI Test scene on startup",
},
}
app.Action = func(c *cli.Context) error {
if c.Bool("version") {
fmt.Printf("This is Project Doodle, v%s build %s ",
doodle.Version,
Build,
)
return nil
}
var filename string
if c.NArg() > 0 {
filename = c.Args().Get(0)
}
// SDL engine.
engine := sdl.New(
"Doodle v"+doodle.Version,
balance.Width,
balance.Height,
)
game := doodle.New(c.Bool("debug"), engine)
game.SetupEngine()
if c.Bool("guitest") {
game.Goto(&doodle.GUITestScene{})
} else if filename != "" {
if c.Bool("edit") {
game.EditFile(filename)
} else {
game.PlayLevel(filename)
}
}
game.Run()
return nil
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
app.Run()
}

View File

@ -7,8 +7,8 @@ import (
"image"
"os"
"git.kirsle.net/apps/doodle/pkg/userdir"
"git.kirsle.net/apps/doodle/lib/render"
"git.kirsle.net/apps/doodle/pkg/userdir"
"golang.org/x/image/bmp"
)

View File

@ -4,9 +4,9 @@ import (
"fmt"
"git.kirsle.net/apps/doodle/lib/render"
"git.kirsle.net/apps/doodle/lib/ui"
"git.kirsle.net/apps/doodle/src/balance"
"git.kirsle.net/apps/doodle/src/events"
"git.kirsle.net/apps/doodle/lib/ui"
)
// GUITestScene implements the main menu of Doodle.

View File

@ -2,9 +2,9 @@ package doodle
import (
"git.kirsle.net/apps/doodle/lib/render"
"git.kirsle.net/apps/doodle/lib/ui"
"git.kirsle.net/apps/doodle/src/balance"
"git.kirsle.net/apps/doodle/src/events"
"git.kirsle.net/apps/doodle/lib/ui"
)
// MainScene implements the main menu of Doodle.