route/mage.go

173 lines
4.0 KiB
Go
Raw Normal View History

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
"os"
"path/filepath"
2017-09-30 14:20:44 +00:00
"runtime"
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 13:30:11 +00:00
)
var wd string
var arches []string
2017-09-30 13:30:11 +00:00
func init() {
lwd, err := os.Getwd()
qod.ANE(err)
wd = lwd
arches = []string{"amd64", "ppc64", "386", "arm", "arm64"}
2017-09-30 13:30:11 +00:00
}
const pkgBase = "git.xeserv.us/xena/route/"
2017-12-02 23:45:56 +00:00
func buildBins(goos, goarch string) {
2017-09-30 13:30:11 +00:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
d := filepath.Join(wd, "./bin")
2017-12-02 23:59:27 +00:00
os.MkdirAll(filepath.Join(d, goos, goarch), 0777)
2017-12-02 23:45:56 +00:00
for _, pkg := range []string{"route-httpagent", "route-cli", "routed", "terraform-provider-route"} {
env := []string{"GOOS=" + goos, "GOARCH=" + goarch}
goBuild(ctx, env, filepath.Join(d, goos, goarch), "cmd/"+pkg)
goInstall(ctx, env, "cmd/"+pkg)
2017-09-30 14:20:44 +00:00
2017-12-02 23:45:56 +00:00
qod.Printlnf("built binary for %s for %s/%s", pkg, goos, goarch)
2017-09-30 13:30:11 +00:00
}
}
2017-09-30 14:20:44 +00:00
2017-12-11 18:38:19 +00:00
// Dep reruns `dep`.
func Dep() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
shouldWork(ctx, nil, wd, "dep", "ensure")
shouldWork(ctx, nil, wd, "dep", "prune")
}
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)
shouldWork(ctx, nil, wd, "docker", "tag", "xena/route:thick", "xena/route:thick-"+ver)
2017-09-30 20:17:50 +00:00
}
// Linux builds binaries for linux
func Linux() {
for _, arch := range arches {
buildBins("linux", arch)
}
2017-09-30 17:11:04 +00:00
Plugin()
}
// Windows builds binaries for windows
func Windows() {
2017-12-02 23:45:56 +00:00
buildBins("windows", "amd64")
}
// Darwin builds binaries for darwin
func Darwin() {
2017-12-02 23:45:56 +00:00
buildBins("darwin", "amd64")
}
// Build builds the binaries for route and routed.
func Build() {
2017-12-02 23:45:56 +00:00
buildBins(runtime.GOOS, runtime.GOARCH)
2017-09-30 17:11:04 +00:00
if runtime.GOOS == "linux" {
Plugin()
}
}
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 _, arch := range arches {
for _, osname := range []string{"linux", "windows", "darwin"} {
if arch != "amd64" && osname != "linux" {
continue
}
dir, err := ioutil.TempDir("", "route-package-"+osname+"-"+arch)
qod.ANE(err)
qod.Printlnf("%s", dir)
2017-09-30 18:43:09 +00:00
for _, file := range []string{"route-cli", "route-httpagent", "routed"} {
if osname == "windows" {
file = file + ".exe"
}
shouldWork(ctx, nil, filepath.Join(wd, "bin", osname, arch), "cp", "-rf", file, dir)
2017-09-30 18:43:09 +00:00
}
shouldWork(ctx, nil, wd, "cp", "-rf", "doc", dir)
shouldWork(ctx, nil, wd, "cp", "-rf", "README.md", dir)
shouldWork(ctx, nil, wd, "cp", "-rf", "LICENSE", dir)
shouldWork(ctx, nil, wd, "cp", "-rf", "Gopkg.lock", dir)
2017-09-30 18:43:09 +00:00
shouldWork(ctx, nil, dir, "tar", "czf", filepath.Join(wd, "bin", fmt.Sprintf("route-%s-%s-%s.tar.gz", ver, osname, arch)), ".")
}
2017-09-30 18:43:09 +00:00
}
}
2017-10-01 17:13:25 +00:00
2017-10-02 05:24:19 +00:00
// Version is the version as git reports.
2017-10-01 17:13:25 +00:00
func Version() {
ver, err := gitTag()
qod.ANE(err)
qod.Printlnf("route-%s", ver)
}
2017-10-02 05:24:19 +00:00
// Test runs all of the functional and unit tests for the project.
func Test() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2017-10-04 07:26:43 +00:00
shouldWork(ctx, nil, wd, "go", "test", "-v", "./...")
2017-10-02 05:24:19 +00:00
}
2018-01-17 05:35:18 +00:00
// Tools installs all of the needed tools for the project.
func Tools(ctx context.Context) {
tools := []string{
"github.com/golang/dep/cmd/dep",
"github.com/golang/protobuf/protoc-gen-go",
}
for _, t := range tools {
shouldWork(ctx, nil, wd, "go", "get", "-u", t)
}
}