提交 69660ca4 编写于 作者: J Jingwen Owen Ou 提交者: Owen Ou

Clean up package script

* Prefer Ruby script to `gotask` script for packaging task
* Clean up Vagrant file
* Prefer `gox` to `goxc`
上级 3294fe55
{
"ArtifactsDest": "target",
"Arch": "386,amd64",
"Os": "darwin,linux,windows",
"ResourcesInclude": "INSTALL*,README*,LICENSE*",
"ResourcesExclude": "*.go",
"MainDirsExclude": "Godeps",
"PackageVersion": "2.0.0",
"Verbosity": "v",
"TaskSettings": {
"downloads-page": {
"fileheader": "",
"filename": ""
},
"pkg-build": {
"metadata": {
"description": "Fast GitHub command line client",
"maintainer": "Jingwen Owen Ou (http://owenou.com)"
},
"metadata-deb": {
"Depends": "",
"Homepage": "http://owenou.com/gh"
}
}
},
"ConfigVersion": "0.9"
}
......@@ -9,11 +9,8 @@
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Vagrantfile API/syntax version.
VAGRANTFILE_API_VERSION = "2"
GO_ARCHIVES = {
"linux" => "go1.2.linux-amd64.tar.gz",
"linux" => "go1.3.1.linux-amd64.tar.gz",
}
INSTALL = {
......@@ -34,38 +31,33 @@ def bootstrap(box)
profile = <<-PROFILE
export GOROOT=#{vagrant_home}/go
export GOPATH=#{vagrant_home}/gocode
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
export CDPATH=.:$GOPATH/src/github.com:$GOPATH/src/code.google.com/p:$GOPATH/src/bitbucket.org:$GOPATH/src/launchpad.net
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
PROFILE
<<-SCRIPT
#{install}
if ! [ -f /home/vagrant/#{archive} ]; then
curl -O# https://go.googlecode.com/files/#{archive}
curl -O# https://storage.googleapis.com/golang/#{archive}
fi
tar -C /home/vagrant -xzf #{archive}
chown -R vagrant:vagrant #{vagrant_home}/go
if ! grep -q GOPATH #{vagrant_home}/.profile; then
echo '#{profile}' >> #{vagrant_home}/.profile
if ! grep -q GOPATH #{vagrant_home}/.bashrc; then
echo '#{profile}' >> #{vagrant_home}/.bashrc
fi
source #{vagrant_home}/.profile
source #{vagrant_home}/.bashrc
chown -R vagrant:vagrant #{vagrant_home}/gocode
go get github.com/jingweno/gotask
echo "\nRun: vagrant ssh #{box} -c 'cd project/path; go test ./...'"
SCRIPT
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
Vagrant.configure("2") 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", "/home/vagrant/gocode/src/github.com/jingweno/gh"
linux.vm.box = "ubuntu/trusty64"
linux.vm.synced_folder "#{src_path}/src/github.com/github/hub", "/home/vagrant/gocode/src/github.com/github/hub"
linux.vm.provision :shell, :inline => bootstrap("linux")
end
end
{
"ConfigVersion": "0.9",
"BuildConstraints": "darwin",
"ResourcesInclude": "INSTALL*,README*,LICENSE*,etc/gh.*"
}
// +build gotask
package main
import (
"fmt"
"github.com/jingweno/gotask/tasking"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
)
// NAME
// install-deps - install dependencies with go get
//
// DESCRIPTION
// Install dependencies with go get.
func TaskInstallDeps(t *tasking.T) {
deps := []string{
"github.com/laher/goxc",
}
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)
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)
t.Logf("Packaging for %s...\n", runtime.GOOS)
t.Log("Installing dependencies...")
TaskInstallDeps(t)
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
ghPath := filepath.Join(gopath, "src", "github.com", "jingweno", "gh")
t.Logf("Copying source from %s to %s\n", pwd, ghPath)
err = copyDir(pwd, ghPath)
if err != nil {
t.Fatal(err)
}
err = os.Chdir(ghPath)
if err != nil {
t.Fatal(err)
}
t.Log("Cross-compiling...")
godepPath := filepath.Join(ghPath, "Godeps", "_workspace")
gopath = fmt.Sprintf("%s%c%s", gopath, os.PathListSeparator, godepPath)
os.Setenv("GOPATH", 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 = mkdirAll(target)
if err != nil {
t.Fatal(err)
}
err = copyBuildArtifacts(source, target)
if err != nil {
t.Fatal(err)
}
}
// 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")
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.
//
// DESCRIPTION
// 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)
t.Logf("GOPATH=%s\n", os.Getenv("GOPATH"))
err := t.Exec("goxc", "-wd=.", "-os="+runtime.GOOS, "-c="+runtime.GOOS)
if err != nil {
t.Fatalf("Can't cross-compile gh: %s\n", err)
}
}
func copyBuildArtifacts(srcDir, destDir string) error {
artifacts := findBuildArtifacts(srcDir)
for _, artifact := range artifacts {
target := filepath.Join(destDir, filepath.Base(artifact))
fmt.Printf("Copying %s to %s\n", artifact, target)
err := copyFile(artifact, target)
if err != nil {
return err
}
}
return nil
}
func copyFile(source, dest string) error {
sf, err := os.Open(source)
if err != nil {
return err
}
defer sf.Close()
df, err := os.Create(dest)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
if err == nil {
si, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, si.Mode())
}
}
return err
}
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
}
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
}
func mkdirAll(dir string) error {
return os.MkdirAll(dir, 0777)
}
{
"ConfigVersion": "0.9",
"BuildConstraints": "linux",
"TaskSettings": {
"archive-tar-gz": {
"platforms": "!linux"
},
"archive-zip": {
"platforms": "linux"
}
}
}
#!/usr/bin/env bash
# Usage: make
#!/usr/bin/env ruby
# Usage: script/package
#
# Package gh for release
#
# Author: Jingwen Owen Ou
# Packages `hub` for release for current platform
require "fileutils"
include FileUtils
module OS
class << self
def type
if darwin?
"darwin"
elsif linux?
"linux"
elsif windows?
"windows"
else
raise "Unknown OS type #{RUBY_PLATFORM}"
end
end
def windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
def darwin?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
def linux?
(/linux/ =~ RUBY_PLATFORM) != nil
end
end
end
module Packer
class << self
def pack!
install_gox!
build_toolchain!
build_hub
cp_assets
tar_gzip
end
private
# Returns the root path to paths
def root_path(*paths)
current = File.expand_path(File.dirname(__FILE__)) # current is the target folder
File.expand_path File.join(current, "..", paths)
end
def glob_dir(path)
Dir[path].select { |d| File.directory?(d) }
end
def install_gox!
puts "Installing github.com/mitchellh/gox"
result = system "go get github.com/mitchellh/gox"
raise "Fail to install gox" unless result
end
def build_toolchain!
puts "Building Go toolchain"
result = system "gox -build-toolchain -os=#{OS.type} -tags=noupdate"
raise "Fail to build Go toolchain" unless result
end
def build_hub
puts "Building for #{OS.type}"
puts `script/godep gox -os=#{OS.type} -output=./target/{{.OS}}_{{.Arch}}/{{.Dir}}`
raise "Fail to build hub" unless $?.exited?
end
def cp_assets
path = root_path("target", "#{OS.type}*")
glob_dir(path).each do |dir|
puts "Copying assets to #{dir}"
["README.md", "LICENSE", "etc/"].each do |f|
cp_r f, File.join(dir, f)
end
end
end
def tar_gzip
path = root_path("target", "#{OS.type}*")
glob_dir(path).each do |dir|
puts "Archiving #{dir}"
Dir.chdir(root_path("target")) do
`tar -zcf #{File.basename(dir)}.gz.tar #{File.basename(dir)}`
end
end
end
end
end
go get github.com/kr/godep
go get github.com/jingweno/gotask
GOPATH=`godep path`:$GOPATH gotask package
Packer.pack!
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册