github.com/echohead/hub@v2.2.1+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 friendly_name
    24        if darwin?
    25          "mac"
    26        elsif linux?
    27          "linux"
    28        elsif windows?
    29          "windows"
    30        else
    31          raise "Unknown OS type #{RUBY_PLATFORM}"
    32        end
    33      end
    34  
    35      def windows?
    36        (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
    37      end
    38  
    39      def windows_64?
    40        windows? && /x64/ =~ RUBY_PLATFORM
    41      end
    42  
    43      def darwin?
    44        (/darwin/ =~ RUBY_PLATFORM) != nil
    45      end
    46  
    47      def linux?
    48        (/linux/ =~ RUBY_PLATFORM) != nil
    49      end
    50    end
    51  end
    52  
    53  class Packer
    54    class << self
    55      def pack!
    56        self.new.pack!
    57      end
    58    end
    59  
    60    attr_reader :version
    61  
    62    def initialize
    63      @version = parse_version!
    64    end
    65  
    66    def pack!
    67      unless ENV["SKIP_TOOLCHAIN"]
    68        install_gox!
    69        build_toolchain!
    70      end
    71      run_tests! unless OS.windows? || ENV["SKIP_TEST"] # cukes don't run on Windows
    72      build_hub!
    73      cp_assets
    74      tar_gzip
    75    end
    76  
    77    private
    78  
    79    def exec!(cmd)
    80      io = IO.popen(cmd)
    81      begin
    82        while line = io.gets
    83          puts line.chomp
    84        end
    85      ensure
    86        io.close
    87      end
    88  
    89      raise "Fail to execute #{cmd}" unless $?.to_i == 0
    90    end
    91  
    92    # Returns the root path to paths
    93    def root_path(*paths)
    94      current = File.expand_path(File.dirname(__FILE__)) # current is the target folder
    95      File.expand_path File.join(current, "..", paths)
    96    end
    97  
    98    def glob_dir(path)
    99      Dir[path].select { |d| File.directory?(d) }
   100    end
   101  
   102    def parse_version!
   103      content = File.read root_path("commands", "version.go")
   104      match = /var Version = "(.+)"/.match content
   105      raise "Fail to parse Hub version" unless match
   106  
   107      match[1]
   108    end
   109  
   110    def install_gox!
   111      puts "Installing github.com/mitchellh/gox"
   112      result = system "go get github.com/mitchellh/gox"
   113      raise "Fail to install gox" unless result
   114    end
   115  
   116    def build_toolchain!
   117      puts "Building Go toolchain"
   118      result = system "gox -build-toolchain -os=#{OS.type}"
   119      raise "Fail to build Go toolchain" unless result
   120    end
   121  
   122    def run_tests!
   123      puts "Running Hub tests"
   124  
   125      bootstrap_script = root_path("script", "bootstrap")
   126      exec!(bootstrap_script)
   127  
   128      test_script = root_path("script", "test")
   129      exec!(test_script)
   130    end
   131  
   132    def build_hub!
   133      puts "Building for #{OS.friendly_name}"
   134      release_version = `script/version`.strip
   135      output = root_path("target", "{{.Dir}}-#{OS.friendly_name}-{{.Arch}}-#{version}", "{{.Dir}}")
   136      # gox doesn't build for 64 bit and 32 bit on 64 bit Windows
   137      # specifying osarch for Windows
   138      # see https://github.com/mitchellh/gox/issues/19#issuecomment-68117016
   139      osarch = OS.windows? ? "windows/#{OS.windows_64? ? "amd64" : "386"}" : ""
   140      cmd = "gox -os=#{OS.type} -output=#{output} -ldflags \"-X github.com/github/hub/commands.Version #{release_version}\""
   141      cmd += " -osarch=#{osarch}" unless osarch.empty?
   142      exec!(cmd)
   143    end
   144  
   145    def cp_assets
   146      path = root_path("target", "*#{OS.friendly_name}*")
   147      glob_dir(path).each do |dir|
   148        puts "Copying assets to #{dir}"
   149        ["README.md", "LICENSE", "etc/", "man/"].each do |f|
   150          cp_r f, File.join(dir, f)
   151        end
   152      end
   153    end
   154  
   155    def tar_gzip
   156      path = root_path("target", "*#{OS.friendly_name}*")
   157      glob_dir(path).each do |dir|
   158        puts "Archiving #{dir}"
   159        Dir.chdir(root_path("target")) do
   160          exec!("tar -zcf #{File.basename(dir)}.tar.gz #{File.basename(dir)}")
   161        end
   162      end
   163    end
   164  end
   165  
   166  Packer.pack!