package 3.6 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
# 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

J
Jingwen Owen Ou 已提交
23 24 25 26 27 28 29 30 31 32 33 34
    def friendly_name
      if darwin?
        "mac"
      elsif linux?
        "linux"
      elsif windows?
        "windows"
      else
        raise "Unknown OS type #{RUBY_PLATFORM}"
      end
    end

J
Jingwen Owen Ou 已提交
35 36 37 38
    def windows?
      (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
    end

J
Jingwen Owen Ou 已提交
39 40 41 42
    def windows_64?
      windows? && /x64/ =~ RUBY_PLATFORM
    end

J
Jingwen Owen Ou 已提交
43 44 45 46 47 48 49 50 51 52
    def darwin?
      (/darwin/ =~ RUBY_PLATFORM) != nil
    end

    def linux?
      (/linux/ =~ RUBY_PLATFORM) != nil
    end
  end
end

O
Owen Ou 已提交
53
class Packer
J
Jingwen Owen Ou 已提交
54 55
  class << self
    def pack!
O
Owen Ou 已提交
56
      self.new.pack!
J
Jingwen Owen Ou 已提交
57
    end
O
Owen Ou 已提交
58
  end
J
Jingwen Owen Ou 已提交
59

O
Owen Ou 已提交
60
  attr_reader :version
J
Jingwen Owen Ou 已提交
61

O
Owen Ou 已提交
62 63 64
  def initialize
    @version = parse_version!
  end
J
Jingwen Owen Ou 已提交
65

O
Owen Ou 已提交
66
  def pack!
J
Jingwen Owen Ou 已提交
67 68 69 70 71
    unless ENV["SKIP_TOOLCHAIN"]
      install_gox!
      build_toolchain!
    end
    run_tests! unless OS.windows? || ENV["SKIP_TEST"] # cukes don't run on Windows
O
Owen Ou 已提交
72 73 74 75
    build_hub!
    cp_assets
    tar_gzip
  end
J
Jingwen Owen Ou 已提交
76

O
Owen Ou 已提交
77
  private
J
Jingwen Owen Ou 已提交
78

O
Owen Ou 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
  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 已提交
92 93 94 95 96
  # 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 已提交
97

O
Owen Ou 已提交
98 99 100
  def glob_dir(path)
    Dir[path].select { |d| File.directory?(d) }
  end
J
Jingwen Owen Ou 已提交
101

O
Owen Ou 已提交
102 103
  def parse_version!
    content = File.read root_path("commands", "version.go")
104
    match = /var Version = "(.+)"/.match content
O
Owen Ou 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
    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"
118
    result = system "gox -build-toolchain -os=#{OS.type}"
O
Owen Ou 已提交
119 120 121
    raise "Fail to build Go toolchain" unless result
  end

122 123
  def run_tests!
    puts "Running Hub tests"
O
Owen Ou 已提交
124

O
Owen Ou 已提交
125 126
    bootstrap_script = root_path("script", "bootstrap")
    exec!(bootstrap_script)
O
Owen Ou 已提交
127

128
    test_script = root_path("script", "test")
O
Owen Ou 已提交
129
    exec!(test_script)
130 131
  end

O
Owen Ou 已提交
132
  def build_hub!
J
Jingwen Owen Ou 已提交
133 134 135 136 137 138 139 140 141 142
    puts "Building for #{OS.friendly_name}"
    release_version = `script/version`.strip
    output = root_path("target", "{{.Dir}}_#{version}_#{OS.friendly_name}_{{.Arch}}", "{{.Dir}}")
    # gox doesn't build for 64 bit and 32 bit on 64 bit Windows
    # specifying osarch for Windows
    # see https://github.com/mitchellh/gox/issues/19#issuecomment-68117016
    osarch = OS.windows? ? "windows/#{OS.windows_64? ? "amd64" : "386"}" : ""
    cmd = "gox -os=#{OS.type} -output=#{output} -ldflags \"-X github.com/github/hub/commands.Version #{release_version}\""
    cmd += " -osarch=#{osarch}" unless osarch.empty?
    exec!(cmd)
O
Owen Ou 已提交
143 144 145
  end

  def cp_assets
J
Jingwen Owen Ou 已提交
146
    path = root_path("target", "*#{OS.friendly_name}*")
O
Owen Ou 已提交
147 148 149 150
    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 已提交
151 152
      end
    end
O
Owen Ou 已提交
153
  end
J
Jingwen Owen Ou 已提交
154

O
Owen Ou 已提交
155
  def tar_gzip
J
Jingwen Owen Ou 已提交
156
    path = root_path("target", "*#{OS.friendly_name}*")
O
Owen Ou 已提交
157 158 159
    glob_dir(path).each do |dir|
      puts "Archiving #{dir}"
      Dir.chdir(root_path("target")) do
O
Owen Ou 已提交
160
        exec!("tar -zcf #{File.basename(dir)}.gz.tar #{File.basename(dir)}")
J
Jingwen Owen Ou 已提交
161 162 163 164
      end
    end
  end
end
J
Jingwen Owen Ou 已提交
165

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