提交 90b7d804 编写于 作者: J Jingwen Owen Ou

Add package task

上级 334a5ae1
......@@ -49,6 +49,7 @@ def bootstrap(box)
echo '#{profile}' >> /home/vagrant/.profile
chown -R vagrant:vagrant /home/vagrant/gocode
go get github.com/jingweno/gotask
echo "\nRun: vagrant ssh #{box} -c 'cd project/path; go test ./...'"
SCRIPT
......@@ -59,7 +60,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "linux" do |linux|
linux.vm.box = "precise64"
linux.vm.box_url = "http://files.vagrantup.com/precise64.box"
linux.vm.synced_folder "#{src_path}/src/github.com/jingweno/gh/target", "/home/vagrant/target"
linux.vm.synced_folder "#{src_path}/src/github.com/jingweno/gh", "/home/vagrant/gocode/src/github.com/jingweno/gh"
linux.vm.provision :shell, :inline => bootstrap("linux")
end
......
......@@ -3,54 +3,79 @@
package main
import (
"fmt"
"github.com/jingweno/gotask/tasking"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
)
// NAME
// cross-compile-all - cross-compiles gh for all supported platforms.
// install-deps - install dependencies with go get
//
// DESCRIPTION
// Cross-compiles gh for all supported platforms. Build artifacts will be in target/VERSION.
// This only works on darwin with Vagrant setup.
func TaskCrossCompileAll(t *tasking.T) {
t.Log("Removing build target...")
err := os.RemoveAll("target")
if err != nil {
t.Errorf("Can't remove build target: %s\n", err)
return
// Install dependencies with go get.
func TaskInstallDeps(t *tasking.T) {
deps := []string{
"github.com/kr/godep",
"github.com/laher/goxc",
"github.com/jingweno/gh",
}
// for current
t.Logf("Compiling for %s...\n", runtime.GOOS)
TaskCrossCompile(t)
if t.Failed() {
return
for _, dep := range deps {
t.Logf("Installing %s\n", dep)
err := t.Exec("go get", dep)
if err != nil {
t.Fatalf("Can't download dependency %s", err)
}
}
}
// NAME
// package - cross compile gh and package it
//
// DESCRIPTION
// Cross compile gh and package it into PWD/target
func TaskPackage(t *tasking.T) {
gopath, err := ioutil.TempDir("", "gh-build")
os.Setenv("GOPATH", gopath)
t.Logf("GOPATH=%s\n", gopath)
// for linux
t.Log("Compiling for linux...")
t.Log("Downloading gh...")
err = t.Exec("vagrant ssh -c 'rm -rf ~/gocode && go get github.com/jingweno/gh'")
pwd, err := os.Getwd()
if err != nil {
t.Errorf("Can't download gh on linux: %s\n", err)
return
t.Fatal(err)
}
t.Log("Cross-compiling gh...")
err = t.Exec("vagrant ssh -c 'cd ~/gocode/src/github.com/jingweno/gh && ./script/bootstrap && GOPATH=`godep path`:$GOPATH gotask cross-compile'")
t.Logf("Packaging for %s...\n", runtime.GOOS)
t.Log("Installing dependencies...")
TaskInstallDeps(t)
ghPath := filepath.Join(gopath, "src", "github.com", "jingweno", "gh")
err = os.Chdir(ghPath)
if err != nil {
t.Errorf("Can't cross-compile gh on linux: %s\n", err)
return
t.Fatal(err)
}
t.Log("Moving build artifacts...")
err = t.Exec("vagrant ssh -c 'cp -R ~/gocode/src/github.com/jingweno/gh/target/* ~/target/'")
t.Log("Cross-compiling...")
godepPath := filepath.Join(ghPath, "Godeps", "_workspace")
os.Setenv("GOPATH", fmt.Sprintf("%s:%s", godepPath, gopath))
TaskCrossCompile(t)
source := filepath.Join(ghPath, "target")
target := filepath.Join(pwd, "target")
t.Logf("Copying build artifacts from %s to %s...\n", source, target)
_, err = os.Stat(target)
if err != nil {
t.Errorf("Can't cross-compile gh on linux: %s\n", err)
return
err = os.Mkdir(target, 0777)
if err != nil {
t.Fatal(err)
}
}
t.Args = append(t.Args, source, target)
TaskCopyBuildArtifacts(t)
}
// NAME
......@@ -60,9 +85,63 @@ func TaskCrossCompileAll(t *tasking.T) {
// Cross-compiles gh for current platform. Build artifacts will be in target/VERSION
func TaskCrossCompile(t *tasking.T) {
t.Logf("Cross-compiling gh for %s...\n", runtime.GOOS)
err := t.Exec("goxc", "-wd=.", "-os="+runtime.GOOS, "-c="+runtime.GOOS)
err := t.Exec("goxc", "-wd=.", "-os="+runtime.GOOS, "-c="+runtime.GOOS)
if err != nil {
t.Fatalf("Can't cross-compile gh: %s\n", err)
}
}
// NAME
// copy-build-artifacts - copy build artifacts from source to target
//
// DESCRIPTION
// Copy build artifacts from source to target.
// For example, `gotask copy-build-artifacts src dest`
func TaskCopyBuildArtifacts(t *tasking.T) {
if len(t.Args) < 2 {
t.Fatal("Missing source or target")
}
srcDir := t.Args[0]
destDir := t.Args[1]
artifacts := findBuildArtifacts(srcDir)
for _, artifact := range artifacts {
target := filepath.Join(destDir, filepath.Base(artifact))
t.Logf("Copying %s to %s\n", artifact, target)
err := copyFile(artifact, target)
if err != nil {
t.Fatal(err)
}
}
}
func copyFile(src, dst string) error {
sf, err := os.Open(src)
if err != nil {
t.Errorf("Can't cross-compile gh: %s\n", err)
return
return err
}
defer sf.Close()
df, err := os.Create(dst)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
return err
}
func findBuildArtifacts(root string) (artifacts []string) {
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
ext := filepath.Ext(path)
if ext == ".deb" || ext == ".zip" || ext == ".gz" {
artifacts = append(artifacts, path)
}
return nil
})
return
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册