Skip to content

Commit

Permalink
start using a magefile (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
natefinch committed Sep 22, 2017
1 parent 2f19124 commit bc1de9a
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 168 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ go get gnorm.org/gnorm
However, using go get to install will mean that `gnorm version` doesn't report
the correct data, and `gnorm docs` won't show you the docs in your browser.

For best results, use the go "makefile" in the root of the repo, which will do
all the build-time magic:
For best results, use the [magefile](https://github.com/magefile/mage) in the
root of the repo, which will do all the build-time magic. To run it, install
mage, then just run mage build.

```
go run make.go
$ go get github.com/magefile/mage
$ mage build
```

If you want to git clone instead (which should work fine since we vendor all
Expand Down
58 changes: 58 additions & 0 deletions mage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//+build mage

// This is the "magefile" for gnorm. To install mage, run go get github.com/magefile/mage.
// To build gnorm, just mage build.

package main

import (
"log"
"os"
)

// Runs go install for gnorm. This generates the embedded docs and the version
// info into the binary.
func Build() error {
Deps()
cleanup := genSite()
defer cleanup()

log.Print("running go install")
return run("go", "install", "-tags", "make", "--ldflags="+flags(), "gnorm.org/gnorm")
}

// Generates binaries for all supported versions. Currently that means a
// combination of windows, linux, and OSX in 32 bit and 64 bit formats. The
// files will be dumped in the local directory with names according to their
// supported platform.
func All() error {
Deps()
cleanup := genSite()
defer cleanup()

ldf := flags()
for _, OS := range []string{"windows", "darwin", "linux"} {
if err := os.Setenv("GOOS", OS); err != nil {
return err
}
for _, ARCH := range []string{"amd64", "386"} {
if err := os.Setenv("GOOS", OS); err != nil {
return err
}
log.Printf("running go build for GOOS=%s GOARCH=%s", OS, ARCH)
err := run("go", "build", "-tags", "make", "-o", "gnorm_"+OS+"_"+ARCH, "--ldflags="+ldf)
if err != nil {
return err
}
}
}
return nil
}

// Downloads commands needed to build gnorm.
func Deps() {
log.Print("downloading hugo")
must(run("go", "get", "github.com/gohugoio/hugo"))
log.Print("downloading statik")
must(run("go", "get", "github.com/rakyll/statik"))
}
90 changes: 90 additions & 0 deletions mage_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// +build mage

package main

import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
)

func flags() string {
timestamp := time.Now().Format(time.RFC3339)
hash := output("git", "rev-parse", "HEAD")
version := gitTag()
return fmt.Sprintf(`-X "gnorm.org/gnorm/cli.timestamp=%s" -X "gnorm.org/gnorm/cli.commitHash=%s" -X "gnorm.org/gnorm/cli.version=%s"`, timestamp, hash, version)
}

func gitTag() string {
c := exec.Command("git", "describe", "--tags")
c.Stderr = os.Stderr
b, err := c.Output()
if err != nil {
exit, ok := err.(*exec.ExitError)
if ok && exit.Exited() {
// probably no git tag
return "dev"
}
must(err)
}

return strings.TrimSuffix(string(b), "\n")
}

func genSite() (cleanup func()) {
log.Print("cleaning up any existing hugo generated files")
mustRemove("./cli/public")
log.Print("generating docs site")
must(run("hugo", "-s", "./site", "-d", "../cli/public"))
log.Print("removing fonts from generated site")
// fonts are BIG
mustRemove("./cli/public/fonts")
mustRemove("./cli/public/revealjs/lib/font")

log.Print("generating statik embedded files")
must(run("statik", "-f", "-src", "./cli/public", "-dest", "./cli"))
return func() {
log.Print("removing generated hugo site")
mustRemove("./cli/public")
log.Print("removing generated statik package")
mustRemove("./cli/statik")
}
}

func mustRemove(s string) {
err := os.RemoveAll(s)
if !os.IsNotExist(err) && err != nil {
log.Fatal(err)
}
}

func run(cmd string, args ...string) error {
c := exec.Command(cmd, args...)
c.Stderr = os.Stderr
if os.Getenv("MAGEFILE_VERBOSE") != "" {
c.Stdout = os.Stdout
}
err := c.Run()
if err != nil {
return err
}
return nil
}

func must(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func output(cmd string, args ...string) string {
c := exec.Command(cmd, args...)
c.Stderr = os.Stderr
b, err := c.Output()
must(err)
return string(b)
}
165 changes: 0 additions & 165 deletions make.go

This file was deleted.

0 comments on commit bc1de9a

Please sign in to comment.