From 795b4a76e6804ad8e2d376b23436ef1a6e1b7760 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 30 Sep 2017 11:43:09 -0700 Subject: [PATCH] mage: add mage package --- doc/README.md | 0 mage.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 doc/README.md diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..e69de29 diff --git a/mage.go b/mage.go index ee3cfa4..efaf11d 100644 --- a/mage.go +++ b/mage.go @@ -4,13 +4,17 @@ package main import ( "context" + "fmt" + "io/ioutil" "log" "os" "os/exec" "path/filepath" "runtime" + "strings" "github.com/jtolds/qod" + "github.com/pkg/errors" ) var wd string @@ -24,6 +28,31 @@ func init() { const pkgBase = "git.xeserv.us/xena/route/" +func output(cmd string, args ...string) (string, error) { + c := exec.Command(cmd, args...) + c.Env = os.Environ() + c.Stderr = os.Stderr + b, err := c.Output() + if err != nil { + return "", errors.Wrapf(err, `failed to run %v %q`, cmd, args) + } + return string(b), nil +} + +func gitTag() (string, error) { + s, err := output("git", "describe", "--tags") + if err != nil { + ee, ok := errors.Cause(err).(*exec.ExitError) + if ok && ee.Exited() { + // probably no git tag + return "dev", nil + } + return "", err + } + + return strings.TrimSuffix(s, "\n"), nil +} + func shouldWork(ctx context.Context, env []string, dir string, cmdName string, args ...string) { loc, err := exec.LookPath(cmdName) qod.ANE(err) @@ -109,3 +138,37 @@ func Plugin() { goBuildPlugin(ctx, filepath.Join(wd, "./bin/linux"), "plugins/autohttpagent", "autohttpagent.so") } + +// Package builds all binaries for all platforms and assembles a package of +// documentation, useful tools and all route components. +func Package() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + //os.RemoveAll("./bin/darwin") + //os.RemoveAll("./bin/linux") + //os.RemoveAll("./bin/windows") + + //mg.Deps(Linux, Windows, Darwin) + + ver, err := gitTag() + qod.ANE(err) + + for _, osname := range []string{"linux", "windows", "darwin"} { + dir, err := ioutil.TempDir("", "route-package-"+osname) + qod.ANE(err) + qod.Printlnf("%s", dir) + + for _, file := range []string{"route", "route-httpagent", "routed"} { + if osname == "windows" { + file = file + ".exe" + } + shouldWork(ctx, nil, filepath.Join(wd, "bin", osname), "cp", "-vrf", file, dir) + } + + shouldWork(ctx, nil, wd, "cp", "-vrf", "doc", dir) + shouldWork(ctx, nil, wd, "cp", "-vrf", "README.md", dir) + + shouldWork(ctx, nil, dir, "tar", "czf", filepath.Join(wd, "bin", fmt.Sprintf("route-%s-%s.tar.gz", ver, osname)), ".") + } +}