2017-09-30 13:30:11 +00:00
|
|
|
// +build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-09-30 18:43:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-09-30 13:30:11 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2017-09-30 14:20:44 +00:00
|
|
|
"runtime"
|
2017-09-30 18:43:09 +00:00
|
|
|
"strings"
|
2017-09-30 13:30:11 +00:00
|
|
|
|
|
|
|
"github.com/jtolds/qod"
|
2017-09-30 18:47:04 +00:00
|
|
|
"github.com/magefile/mage/mg"
|
2017-09-30 18:43:09 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-09-30 13:30:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var wd string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
lwd, err := os.Getwd()
|
|
|
|
qod.ANE(err)
|
|
|
|
|
|
|
|
wd = lwd
|
|
|
|
}
|
|
|
|
|
|
|
|
const pkgBase = "git.xeserv.us/xena/route/"
|
|
|
|
|
2017-09-30 18:43:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-09-30 13:30:11 +00:00
|
|
|
func shouldWork(ctx context.Context, env []string, dir string, cmdName string, args ...string) {
|
|
|
|
loc, err := exec.LookPath(cmdName)
|
|
|
|
qod.ANE(err)
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, loc, args...)
|
|
|
|
cmd.Dir = dir
|
2017-09-30 14:20:44 +00:00
|
|
|
cmd.Env = append(env, os.Environ()...)
|
2017-09-30 13:30:11 +00:00
|
|
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
log.Printf("starting process, env: %v, pwd: %s, cmd: %s, args: %v", env, dir, loc, args)
|
|
|
|
err = cmd.Run()
|
|
|
|
qod.ANE(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func goBuild(ctx context.Context, env []string, dir string, pkgname string) {
|
2017-09-30 14:46:38 +00:00
|
|
|
shouldWork(ctx, env, dir, "go", "build", "-v", pkgBase+pkgname)
|
2017-09-30 15:00:25 +00:00
|
|
|
}
|
2017-09-30 14:49:03 +00:00
|
|
|
|
2017-09-30 15:00:25 +00:00
|
|
|
func goInstall(ctx context.Context, env []string, pkgname string) {
|
|
|
|
shouldWork(ctx, nil, wd, "go", "install", pkgBase+pkgname)
|
2017-09-30 13:30:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:20:44 +00:00
|
|
|
func goBuildPlugin(ctx context.Context, dir, pkgname, fname string) {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
qod.Printlnf("plugins don't work on non-linux machines yet :(")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-30 17:15:21 +00:00
|
|
|
shouldWork(ctx, nil, dir, "go", "build", "-v", "-buildmode=plugin", "-o="+fname, pkgBase+pkgname)
|
|
|
|
qod.Printlnf("built %s for %s", fname, runtime.GOOS)
|
2017-09-30 14:20:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 15:00:25 +00:00
|
|
|
func buildBins(goos string) {
|
2017-09-30 13:30:11 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
d := filepath.Join(wd, "./bin")
|
|
|
|
|
2017-09-30 15:00:25 +00:00
|
|
|
os.Mkdir(filepath.Join(d, goos), 0777)
|
|
|
|
|
2017-09-30 20:28:10 +00:00
|
|
|
for _, pkg := range []string{"route-httpagent", "route-cli", "routed"} {
|
2017-09-30 15:00:25 +00:00
|
|
|
goBuild(ctx, []string{"GOOS=" + goos}, filepath.Join(d, goos), "cmd/"+pkg)
|
2017-09-30 14:20:44 +00:00
|
|
|
|
2017-09-30 15:00:25 +00:00
|
|
|
if goos == runtime.GOOS {
|
|
|
|
goInstall(ctx, nil, "cmd/"+pkg)
|
2017-09-30 14:20:44 +00:00
|
|
|
}
|
2017-09-30 15:00:25 +00:00
|
|
|
|
|
|
|
qod.Printlnf("built binary for %s for os %s", pkg, goos)
|
2017-09-30 13:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-30 14:20:44 +00:00
|
|
|
|
2017-09-30 20:17:50 +00:00
|
|
|
// Docker builds docker images
|
|
|
|
func Docker() {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ver, err := gitTag()
|
|
|
|
qod.ANE(err)
|
|
|
|
|
|
|
|
shouldWork(ctx, nil, wd, "box", "box.rb")
|
2017-09-30 21:20:50 +00:00
|
|
|
shouldWork(ctx, nil, wd, "docker", "tag", "xena/route:latest", "xena/route:"+ver)
|
2017-09-30 20:17:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 15:00:25 +00:00
|
|
|
// Linux builds binaries for linux
|
|
|
|
func Linux() {
|
|
|
|
buildBins("linux")
|
2017-09-30 17:11:04 +00:00
|
|
|
Plugin()
|
2017-09-30 15:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Windows builds binaries for windows
|
|
|
|
func Windows() {
|
|
|
|
buildBins("windows")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Darwin builds binaries for darwin
|
|
|
|
func Darwin() {
|
|
|
|
buildBins("darwin")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build builds the binaries for route and routed.
|
|
|
|
func Build() {
|
|
|
|
buildBins(runtime.GOOS)
|
2017-09-30 17:11:04 +00:00
|
|
|
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
Plugin()
|
|
|
|
}
|
2017-09-30 15:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:20:44 +00:00
|
|
|
// Plugin builds all of the plugins for programs wanting to augment themselves with route.
|
|
|
|
func Plugin() {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
goBuildPlugin(ctx, filepath.Join(wd, "./bin/linux"), "plugins/autohttpagent", "autohttpagent.so")
|
|
|
|
}
|
2017-09-30 18:43:09 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
2017-09-30 18:47:04 +00:00
|
|
|
os.RemoveAll("./bin/darwin")
|
|
|
|
os.RemoveAll("./bin/linux")
|
|
|
|
os.RemoveAll("./bin/windows")
|
2017-09-30 18:43:09 +00:00
|
|
|
|
2017-09-30 18:47:04 +00:00
|
|
|
mg.Deps(Linux, Windows, Darwin)
|
2017-09-30 18:43:09 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-09-30 20:28:10 +00:00
|
|
|
for _, file := range []string{"route-cli", "route-httpagent", "routed"} {
|
2017-09-30 18:43:09 +00:00
|
|
|
if osname == "windows" {
|
|
|
|
file = file + ".exe"
|
|
|
|
}
|
2017-09-30 21:20:50 +00:00
|
|
|
shouldWork(ctx, nil, filepath.Join(wd, "bin", osname), "cp", "-rf", file, dir)
|
2017-09-30 18:43:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 21:20:50 +00:00
|
|
|
shouldWork(ctx, nil, wd, "cp", "-rf", "doc", dir)
|
|
|
|
shouldWork(ctx, nil, wd, "cp", "-rf", "README.md", dir)
|
2017-09-30 18:43:09 +00:00
|
|
|
|
|
|
|
shouldWork(ctx, nil, dir, "tar", "czf", filepath.Join(wd, "bin", fmt.Sprintf("route-%s-%s.tar.gz", ver, osname)), ".")
|
|
|
|
}
|
|
|
|
}
|