Skip to content

Instantly share code, notes, and snippets.

@Noofbiz
Last active December 8, 2018 21:57
Show Gist options
  • Save Noofbiz/c369a8093f4c0e90f25803ed92401abf to your computer and use it in GitHub Desktop.
Save Noofbiz/c369a8093f4c0e90f25803ed92401abf to your computer and use it in GitHub Desktop.
a script built in go that attempts to run tests then build engo demos to test if changes break the build
package main
import (
"bytes"
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
)
var (
path string
run, gopherjs, gomobile bool
)
type BuildCommand uint
const (
GoBuildCommand BuildCommand = iota
GopherjsBuildCommand
GomobileBuildCommand
)
func init() {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
flag.BoolVar(&run, "run", true, "run tries to build and run the demos")
flag.BoolVar(&gopherjs, "gopherjs", false, "gopherjs tries to build the demos via gopherjs")
flag.BoolVar(&gomobile, "gomobile", false, "gomobile tries to build the demos via gomobile")
flag.StringVar(&path, "path", gopath+"/src/engo.io/engo", "path is the path to your engo library folder")
}
func main() {
flag.Parse()
runTests("go")
if run {
fmt.Println("attempting to build demos")
buildDemos(GoBuildCommand)
fmt.Println("demos built successfully!")
}
if gopherjs {
runTests("gopherjs")
fmt.Println("attempting to build with gopherjs")
buildDemos(GopherjsBuildCommand)
fmt.Println("gopherjs built successfully!")
}
if gomobile {
fmt.Println("attempting to build with gomobile")
buildDemos(GomobileBuildCommand)
fmt.Println("gomobile built successfully!")
}
}
func runTests(tool string) {
cmd := exec.Command(tool, "test", "-v", "./...")
cmd.Dir = path
printCmdOutput(cmd)
}
func printCmdOutput(cmd *exec.Cmd) {
var out []byte
var err error
if out, err = cmd.Output(); err != nil {
switch outerr := err.(type) {
case *exec.ExitError:
buf := bytes.NewBuffer(outerr.Stderr)
fmt.Println(buf.String())
}
panic(err)
}
buf := bytes.NewBuffer(out)
fmt.Println(buf.String())
}
func buildDemos(bc BuildCommand) {
tmp, err := ioutil.TempDir(path+"/demos", "build")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp)
fi, err := ioutil.ReadDir(path + "/demos")
if err != nil {
panic(err)
}
for _, file := range fi {
if file.IsDir() {
if file.Name() != "demoutils" && path+"/demos/"+file.Name() != tmp {
fmt.Println("building " + file.Name())
var cmd *exec.Cmd
switch bc {
case GoBuildCommand:
cmd = exec.Command("go", "build", "-o="+tmp+"/"+file.Name(), "--tags=demo")
case GopherjsBuildCommand:
cmd = exec.Command("gopherjs", "build", "-o="+tmp+"/"+file.Name()+".js", "--tags=demo")
case GomobileBuildCommand:
cmd = exec.Command("gomobile", "build", "-o="+tmp+"/"+file.Name()+".apk", "--tags=demo")
default:
panic("build command not found")
}
cmd.Dir = path + "/demos/" + file.Name()
printCmdOutput(cmd)
fmt.Println("Sucessfully built " + file.Name())
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment