route/mage.go

112 lines
2.4 KiB
Go

// +build mage
package main
import (
"context"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/jtolds/qod"
)
var wd string
func init() {
lwd, err := os.Getwd()
qod.ANE(err)
wd = lwd
}
const pkgBase = "git.xeserv.us/xena/route/"
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
cmd.Env = append(env, os.Environ()...)
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) {
shouldWork(ctx, env, dir, "go", "build", "-v", pkgBase+pkgname)
}
func goInstall(ctx context.Context, env []string, pkgname string) {
shouldWork(ctx, nil, wd, "go", "install", pkgBase+pkgname)
}
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
}
shouldWork(ctx, nil, dir, "go", "build", "-v", "-buildmode=plugin", "-o="+fname, pkgBase+pkgname)
qod.Printlnf("built %s for %s", fname, runtime.GOOS)
}
func buildBins(goos string) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
d := filepath.Join(wd, "./bin")
os.Mkdir(filepath.Join(d, goos), 0777)
for _, pkg := range []string{"route-httpagent", "route", "routed"} {
goBuild(ctx, []string{"GOOS=" + goos}, filepath.Join(d, goos), "cmd/"+pkg)
if goos == runtime.GOOS {
goInstall(ctx, nil, "cmd/"+pkg)
}
qod.Printlnf("built binary for %s for os %s", pkg, goos)
}
}
// Linux builds binaries for linux
func Linux() {
buildBins("linux")
Plugin()
}
// 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)
if runtime.GOOS == "linux" {
Plugin()
}
}
// 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")
}