package 2.4 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 53 54 55 56
  def pack!
    install_gox!
    build_toolchain!
    build_hub!
    cp_assets
    tar_gzip
  end
J
Jingwen Owen Ou 已提交
57

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

O
Owen Ou 已提交
60 61 62 63 64
  # 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 已提交
65

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

O
Owen Ou 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  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

  def build_hub!
    puts "Building for #{OS.type}"
    puts `script/godep gox -os=#{OS.type} -output=./target/{{.Dir}}_#{version}_{{.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)
J
Jingwen Owen Ou 已提交
102 103
      end
    end
O
Owen Ou 已提交
104
  end
J
Jingwen Owen Ou 已提交
105

O
Owen Ou 已提交
106 107 108 109 110 111
  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 已提交
112 113 114 115
      end
    end
  end
end
J
Jingwen Owen Ou 已提交
116

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