Commit c185dfdb authored by Oscar Campos's avatar Oscar Campos Committed by Oscar Campos
Browse files

chore: add version information command

parent 7336dbcb
......@@ -15,10 +15,6 @@
package main
import (
"github.com/alecthomas/kong"
)
type context struct {
Path string
Verbose bool
......@@ -32,14 +28,8 @@ type listCmd struct{}
var cli struct {
Generate generateCmd `cmd help:"Generates autotoregistration boilerplate Go code for user defined structures"` //nolint:govet
List listCmd `cmd help:"List user defined autoregistrable data structures"` //nolint:govet
Version versionCmd `cmd help:"Show version information and exit"`
Path string `type:"path" default:"." help:"Path where execute the command"`
Verbose bool `help:"Verbose output"`
}
func main() {
ctx := kong.Parse(&cli)
err := ctx.Run(&context{Path: cli.Path, Verbose: cli.Verbose})
ctx.FatalIfErrorf(err)
}
......@@ -9,12 +9,13 @@ import (
"os"
"path/filepath"
"runtime"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var ldflags = "-s -w"
var ldflags = "-s -w -X main.version=$VERSION_TAG -X main.commit=$COMMIT_HASH -X main.date=$BUILD_TIME -X main.builtBy=$BUILDER"
var Default = Generate
......@@ -86,7 +87,7 @@ func RetrieveGodotDocumentation() error {
return fmt.Errorf("could not activate core.sparseCheckout: %w", err)
}
sparseCheckoutsConfigFile := filepath.Join(".", ".git", "info", "sparse-checkout")
writeErr := ioutil.WriteFile(sparseCheckoutsConfigFile, []byte("doc/classes"), 0655)
writeErr := ioutil.WriteFile(sparseCheckoutsConfigFile, []byte("doc/classes"), 0600)
if writeErr != nil {
return fmt.Errorf("could not write .git/info/sparse-checkout file: %w", writeErr)
}
......@@ -97,14 +98,14 @@ func RetrieveGodotDocumentation() error {
return nil
}
// Build builds the library to make sure everything is fine
// Build builds the gdnative-go compiler gogdc (also builds the library)
func Build() error {
return sh.Run("go", "install", "-ldflags", ldflags, "-x", "./gdnative/...")
return sh.RunWith(flagEnv(), "go", "build", "-ldflags", ldflags, "-x", "./cmd/gogdc")
}
// Gdnativego builds the gdnativego compiler and installs it
func Gdnativego() error {
return sh.Run("go", "install", "-ldflags", ldflags, "-x", "./cmd/gdnativego")
// Install builds and installs the gdnative-go compiler gogdc in $GOPATH/bin
func Install() error {
return sh.RunWith(flagEnv(), "go", "install", "-ldflags", ldflags, "-x", "./cmd/gogdc")
}
// getCurrentFilePath constructs and returns the current file path on the drive
......@@ -117,3 +118,22 @@ func getCurrentFilePath() string {
return filepath.Join(filepath.Dir(filename))
}
// fills environment with build data
func flagEnv() map[string]string {
commitHash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
buildAuthor, _ := sh.Output("git", "log", "-1", "--pretty=format:%ae")
versionTag, _ := sh.Output("git", "describe", "--tags", "--abbrev=0")
if versionTag == "" {
versionTag = "v0.0.0-dev"
}
return map[string]string{
"COMMIT_HASH": commitHash,
"VERSION_TAG": versionTag,
"BUILD_TIME": time.Now().Format("2006-01-02T15:04:05Z0700"),
"BUILDER": buildAuthor,
"CGO_ENABLED": "1",
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment