github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/script/package (about)

     1  #!/usr/bin/env ruby
     2  # Usage: script/package
     3  #
     4  # Packages `hub` for release for current platform
     5  
     6  require "fileutils"
     7  include FileUtils
     8  
     9  module OS
    10    class << self
    11      def type
    12        if darwin?
    13          "darwin"
    14        elsif linux?
    15          "linux"
    16        elsif windows?
    17          "windows"
    18        else
    19          raise "Unknown OS type #{RUBY_PLATFORM}"
    20        end
    21      end
    22  
    23      def windows?
    24        (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
    25      end
    26  
    27      def darwin?
    28        (/darwin/ =~ RUBY_PLATFORM) != nil
    29      end
    30  
    31      def linux?
    32        (/linux/ =~ RUBY_PLATFORM) != nil
    33      end
    34    end
    35  end
    36  
    37  class Packer
    38    class << self
    39      def pack!
    40        self.new.pack!
    41      end
    42    end
    43  
    44    attr_reader :version
    45  
    46    def initialize
    47      @version = parse_version!
    48    end
    49  
    50    def pack!
    51      install_gox!
    52      build_toolchain!
    53      run_tests!
    54      build_hub!
    55      cp_assets
    56      tar_gzip
    57    end
    58  
    59    private
    60  
    61    def exec!(cmd)
    62      io = IO.popen(cmd)
    63      begin
    64        while line = io.gets
    65          puts line.chomp
    66        end
    67      ensure
    68        io.close
    69      end
    70  
    71      raise "Fail to execute #{cmd}" unless $?.to_i == 0
    72    end
    73  
    74    # Returns the root path to paths
    75    def root_path(*paths)
    76      current = File.expand_path(File.dirname(__FILE__)) # current is the target folder
    77      File.expand_path File.join(current, "..", paths)
    78    end
    79  
    80    def glob_dir(path)
    81      Dir[path].select { |d| File.directory?(d) }
    82    end
    83  
    84    def parse_version!
    85      content = File.read root_path("commands", "version.go")
    86      match = /const Version = "(.+)"/.match content
    87      raise "Fail to parse Hub version" unless match
    88  
    89      match[1]
    90    end
    91  
    92    def install_gox!
    93      puts "Installing github.com/mitchellh/gox"
    94      result = system "go get github.com/mitchellh/gox"
    95      raise "Fail to install gox" unless result
    96    end
    97  
    98    def build_toolchain!
    99      puts "Building Go toolchain"
   100      result = system "gox -build-toolchain -os=#{OS.type}"
   101      raise "Fail to build Go toolchain" unless result
   102    end
   103  
   104    def run_tests!
   105      puts "Running Hub tests"
   106  
   107      bootstrap_script = root_path("script", "bootstrap")
   108      exec!(bootstrap_script)
   109  
   110      test_script = root_path("script", "test")
   111      exec!(test_script)
   112    end
   113  
   114    def build_hub!
   115      puts "Building for #{OS.type}"
   116      exec!("script/godep gox -os=#{OS.type} -output=./target/{{.Dir}}_#{version}_{{.OS}}_{{.Arch}}/{{.Dir}} -tags=noupdate")
   117    end
   118  
   119    def cp_assets
   120      path = root_path("target", "*#{OS.type}*")
   121      glob_dir(path).each do |dir|
   122        puts "Copying assets to #{dir}"
   123        ["README.md", "LICENSE", "etc/"].each do |f|
   124          cp_r f, File.join(dir, f)
   125        end
   126      end
   127    end
   128  
   129    def tar_gzip
   130      path = root_path("target", "*#{OS.type}*")
   131      glob_dir(path).each do |dir|
   132        puts "Archiving #{dir}"
   133        Dir.chdir(root_path("target")) do
   134          exec!("tar -zcf #{File.basename(dir)}.gz.tar #{File.basename(dir)}")
   135        end
   136      end
   137    end
   138  end
   139  
   140  Packer.pack!