-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |