package 2.8 KB
Newer Older
J
Jingwen Owen Ou 已提交
1 2
#!/usr/bin/env ruby
# Usage: script/package
J
Jingwen Owen Ou 已提交
3
#
J
Jingwen Owen Ou 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# 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

O
Owen Ou 已提交
37
class Packer
J
Jingwen Owen Ou 已提交
38 39
  class << self
    def pack!
O
Owen Ou 已提交
40
      self.new.pack!
J
Jingwen Owen Ou 已提交
41
    end
O
Owen Ou 已提交
42
  end
J
Jingwen Owen Ou 已提交
43

O
Owen Ou 已提交
44
  attr_reader :version
J
Jingwen Owen Ou 已提交
45

O
Owen Ou 已提交
46 47 48
  def initialize
    @version = parse_version!
  end
J
Jingwen Owen Ou 已提交
49

O
Owen Ou 已提交
50 51 52
  def pack!
    install_gox!
    build_toolchain!
53
    run_tests!
O
Owen Ou 已提交
54 55 56 57
    build_hub!
    cp_assets
    tar_gzip
  end
J
Jingwen Owen Ou 已提交
58

O
Owen Ou 已提交
59
  private
J
Jingwen Owen Ou 已提交
60

O
Owen Ou 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73
  def exec!(cmd)
    io = IO.popen(cmd)
    begin
      while line = io.gets
        puts line.chomp
      end
    ensure
      io.close
    end

    raise "Fail to execute #{cmd}" unless $?.to_i == 0
  end

O
Owen Ou 已提交
74 75 76 77 78
  # 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
J
Jingwen Owen Ou 已提交
79

O
Owen Ou 已提交
80 81 82
  def glob_dir(path)
    Dir[path].select { |d| File.directory?(d) }
  end
J
Jingwen Owen Ou 已提交
83

O
Owen Ou 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  def parse_version!
    content = File.read root_path("commands", "version.go")
    match = /const Version = "(.+)"/.match content
    raise "Fail to parse Hub version" unless match

    match[1]
  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"
100
    result = system "gox -build-toolchain -os=#{OS.type}"
O
Owen Ou 已提交
101 102 103
    raise "Fail to build Go toolchain" unless result
  end

104 105
  def run_tests!
    puts "Running Hub tests"
O
Owen Ou 已提交
106

O
Owen Ou 已提交
107 108
    bootstrap_script = root_path("script", "bootstrap")
    exec!(bootstrap_script)
O
Owen Ou 已提交
109

110
    test_script = root_path("script", "test")
O
Owen Ou 已提交
111
    exec!(test_script)
112 113
  end

O
Owen Ou 已提交
114 115
  def build_hub!
    puts "Building for #{OS.type}"
116
    exec!("script/godep gox -os=#{OS.type} -output=./target/{{.Dir}}_#{version}_{{.OS}}_{{.Arch}}/{{.Dir}} -tags=noupdate")
O
Owen Ou 已提交
117 118 119 120 121 122 123 124
  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)
J
Jingwen Owen Ou 已提交
125 126
      end
    end
O
Owen Ou 已提交
127
  end
J
Jingwen Owen Ou 已提交
128

O
Owen Ou 已提交
129 130 131 132 133
  def tar_gzip
    path = root_path("target", "*#{OS.type}*")
    glob_dir(path).each do |dir|
      puts "Archiving #{dir}"
      Dir.chdir(root_path("target")) do
O
Owen Ou 已提交
134
        exec!("tar -zcf #{File.basename(dir)}.gz.tar #{File.basename(dir)}")
J
Jingwen Owen Ou 已提交
135 136 137 138
      end
    end
  end
end
J
Jingwen Owen Ou 已提交
139

J
Jingwen Owen Ou 已提交
140
Packer.pack!