route/mage.go

80 lines
1.8 KiB
Go
Raw Normal View History

2017-09-30 13:30:11 +00:00
// +build mage
package main
import (
"context"
"log"
"os"
"os/exec"
"path/filepath"
2017-09-30 14:20:44 +00:00
"runtime"
2017-09-30 13:30:11 +00:00
"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
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) {
shouldWork(ctx, env, dir, "go", "build", pkgBase+pkgname)
}
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
}
shouldWork(ctx, nil, dir, "go", "build", "-buildmode=plugin", "-o=fname", pkgBase+pkgname)
}
2017-09-30 13:30:11 +00:00
// Build builds the binaries for route and routed.
func Build() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
d := filepath.Join(wd, "./bin")
2017-09-30 14:20:44 +00:00
for _, goos := range []string{"linux", "darwin", "windows"} {
os.Mkdir(filepath.Join(d, goos), 0777)
for _, pkg := range []string{"route-httpagent", "route", "routed"} {
2017-09-30 14:43:50 +00:00
goBuild(ctx, []string{"GOOS=" + goos}, filepath.Join(d, goos), "cmd/"+pkg)
2017-09-30 14:20:44 +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
// 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")
}