package 2.7 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
  # 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 已提交
66

O
Owen Ou 已提交
67 68 69
  def glob_dir(path)
    Dir[path].select { |d| File.directory?(d) }
  end
J
Jingwen Owen Ou 已提交
70

O
Owen Ou 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  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"
    result = system "gox -build-toolchain -os=#{OS.type} -tags=noupdate"
    raise "Fail to build Go toolchain" unless result
  end

91 92
  def run_tests!
    puts "Running Hub tests"
O
Owen Ou 已提交
93 94 95 96

    bootstrap_script = root_path("script", "test")
    `#{bootstrap_script}`

97 98
    test_script = root_path("script", "test")
    `#{test_script}`
O
Owen Ou 已提交
99

100
    raise "Hub tests fail" unless $?.exitstatus == 0
101 102
  end

O
Owen Ou 已提交
103 104 105
  def build_hub!
    puts "Building for #{OS.type}"
    puts `script/godep gox -os=#{OS.type} -output=./target/{{.Dir}}_#{version}_{{.OS}}_{{.Arch}}/{{.Dir}}`
106 107

    raise "Fail to build hub" unless $?.exitstatus == 0
O
Owen Ou 已提交
108 109 110 111 112 113 114 115
  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 已提交
116 117
      end
    end
O
Owen Ou 已提交
118
  end
J
Jingwen Owen Ou 已提交
119

O
Owen Ou 已提交
120 121 122 123 124 125
  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)}`
J
Jingwen Owen Ou 已提交
126 127 128 129
      end
    end
  end
end
J
Jingwen Owen Ou 已提交
130

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