hub/gh_task.go

239 строки
4.7 KiB
Go
Исходник Обычный вид История

2013-10-24 02:00:57 +04:00
// +build gotask
package main
import (
2013-12-18 09:28:54 +04:00
"fmt"
2013-10-24 02:00:57 +04:00
"github.com/jingweno/gotask/tasking"
2013-12-18 09:28:54 +04:00
"io"
"io/ioutil"
2013-10-24 02:00:57 +04:00
"os"
2013-12-18 09:28:54 +04:00
"path/filepath"
2013-10-24 02:00:57 +04:00
"runtime"
)
// NAME
2013-12-18 09:28:54 +04:00
// install-deps - install dependencies with go get
//
// DESCRIPTION
2013-12-18 09:28:54 +04:00
// Install dependencies with go get.
func TaskInstallDeps(t *tasking.T) {
deps := []string{
"github.com/laher/goxc",
}
2013-12-18 09:28:54 +04:00
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)
}
}
2013-12-18 09:28:54 +04:00
}
// 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)
2014-01-07 10:57:13 +04:00
path := fmt.Sprintf("%s%c%s", filepath.Join(gopath, "bin"), os.PathListSeparator, os.Getenv("PATH"))
os.Setenv("PATH", path)
t.Logf("PATH=%s\n", path)
2013-12-18 09:28:54 +04:00
t.Logf("Packaging for %s...\n", runtime.GOOS)
t.Log("Installing dependencies...")
TaskInstallDeps(t)
2014-01-07 10:57:13 +04:00
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
2014-01-07 11:29:49 +04:00
2013-12-18 09:28:54 +04:00
ghPath := filepath.Join(gopath, "src", "github.com", "jingweno", "gh")
2014-01-07 10:57:13 +04:00
t.Logf("Copying source from %s to %s\n", pwd, ghPath)
err = copyDir(pwd, ghPath)
if err != nil {
t.Fatal(err)
}
2013-12-18 09:28:54 +04:00
err = os.Chdir(ghPath)
if err != nil {
2013-12-18 09:28:54 +04:00
t.Fatal(err)
}
2013-12-18 09:28:54 +04:00
t.Log("Cross-compiling...")
godepPath := filepath.Join(ghPath, "Godeps", "_workspace")
2014-01-07 10:57:13 +04:00
gopath = fmt.Sprintf("%s%c%s", gopath, os.PathListSeparator, godepPath)
2013-12-18 10:46:19 +04:00
os.Setenv("GOPATH", gopath)
2013-12-18 09:28:54 +04:00
TaskCrossCompile(t)
source := filepath.Join(ghPath, "target")
target := filepath.Join(pwd, "target")
t.Logf("Copying build artifacts from %s to %s...\n", source, target)
2014-01-07 19:13:48 +04:00
err = mkdirAll(target)
if err != nil {
2014-01-07 19:13:48 +04:00
t.Fatal(err)
}
2014-01-07 19:13:48 +04:00
2014-01-07 10:57:13 +04:00
err = copyBuildArtifacts(source, target)
if err != nil {
t.Fatal(err)
}
2014-01-07 11:29:49 +04:00
}
2014-01-07 19:13:48 +04:00
// NAME
// bottle - build homebrew bottle for gh
//
// DESCRIPTION
// Build homebrew bottle for gh into PWD/target.
func TaskBottle(t *tasking.T) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
target := filepath.Join(pwd, "target")
err = mkdirAll(target)
if err != nil {
t.Fatal(err)
}
err = t.Exec("brew", "list", "gh")
2014-01-07 11:29:49 +04:00
if err == nil {
err := t.Exec("brew", "uninstall", "gh")
if err != nil {
t.Fatal(err)
}
}
err = t.Exec("brew", "install", "--build-from-source", "--build-bottle", "gh")
if err != nil {
t.Fatal(err)
}
err = os.Chdir(target)
if err != nil {
t.Fatal(err)
}
err = t.Exec("brew", "bottle", "gh")
if err != nil {
t.Fatal(err)
}
}
// NAME
// cross-compile - cross-compiles gh for current platform.
2013-10-24 02:00:57 +04:00
//
// DESCRIPTION
// Cross-compiles gh for current platform. Build artifacts will be in target/VERSION
2013-10-24 02:20:47 +04:00
func TaskCrossCompile(t *tasking.T) {
t.Logf("Cross-compiling gh for %s...\n", runtime.GOOS)
2014-01-07 10:57:13 +04:00
t.Logf("GOPATH=%s\n", os.Getenv("GOPATH"))
2013-12-18 09:28:54 +04:00
err := t.Exec("goxc", "-wd=.", "-os="+runtime.GOOS, "-c="+runtime.GOOS)
if err != nil {
t.Fatalf("Can't cross-compile gh: %s\n", err)
}
}
2014-01-07 10:57:13 +04:00
func copyBuildArtifacts(srcDir, destDir string) error {
2013-12-18 09:28:54 +04:00
artifacts := findBuildArtifacts(srcDir)
for _, artifact := range artifacts {
target := filepath.Join(destDir, filepath.Base(artifact))
2014-01-07 10:57:13 +04:00
fmt.Printf("Copying %s to %s\n", artifact, target)
2013-12-18 09:28:54 +04:00
err := copyFile(artifact, target)
if err != nil {
2014-01-07 10:57:13 +04:00
return err
2013-12-18 09:28:54 +04:00
}
}
2014-01-07 10:57:13 +04:00
return nil
2013-12-18 09:28:54 +04:00
}
2014-01-07 10:57:13 +04:00
func copyFile(source, dest string) error {
sf, err := os.Open(source)
2013-10-24 02:00:57 +04:00
if err != nil {
2013-12-18 09:28:54 +04:00
return err
2013-10-24 02:00:57 +04:00
}
2013-12-18 09:28:54 +04:00
defer sf.Close()
2014-01-07 10:57:13 +04:00
df, err := os.Create(dest)
2013-12-18 09:28:54 +04:00
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
2014-01-07 10:57:13 +04:00
if err == nil {
si, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, si.Mode())
}
}
2013-12-18 09:28:54 +04:00
return err
}
2014-01-07 10:57:13 +04:00
func copyDir(source, dest string) (err error) {
fi, err := os.Stat(source)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("Source is not a directory")
}
_, err = os.Open(dest)
if !os.IsNotExist(err) {
return fmt.Errorf("Destination already exists")
}
err = os.MkdirAll(dest, fi.Mode())
if err != nil {
return err
}
entries, err := ioutil.ReadDir(source)
for _, entry := range entries {
sfp := filepath.Join(source, entry.Name())
dfp := filepath.Join(dest, entry.Name())
if entry.IsDir() {
err = copyDir(sfp, dfp)
if err != nil {
return err
}
} else {
err = copyFile(sfp, dfp)
if err != nil {
return err
}
}
}
return
}
2013-12-18 09:28:54 +04:00
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
2013-10-24 02:00:57 +04:00
}
2014-01-07 19:13:48 +04:00
func mkdirAll(dir string) error {
return os.MkdirAll(dir, 0777)
}