twitter/spitball

View on GitHub
bin/spitball

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'spitball'
require 'optparse'
require 'net/http'
require 'fileutils'

STDOUT.sync = true

args = {}
opts = OptionParser.new do |opts|
  opts.banner = "Usage: spitball [options] GEMFILE ARCHIVE"
  opts.separator ""
  opts.separator "options:"

  opts.on('-h', '--host HOST', 'Get the tarball from a remote spitball server') do |host|
    args[:host] = host
  end

  opts.on('-p', '--port PORT', 'Specify the remote server port. Default 8080') do |port|
    args[:port] = port
  end

  opts.on('-v', '--version', 'Display the version and quit') do
    puts Spitball::VERSION
    exit!(0)
  end

  opts.on('--without a,b,c', Array, 'Excluded groups in the tarball. Does not apply to remote spitballs') do |without|
    args[:without] = without
  end

  opts.on('--bundle-config CONFIG_FILE', 'Pass a bundle-config styled configuration file with build instructions') do |bundle_config|
    args[:bundle_config] = bundle_config
  end

  opts.on('-g', '--generate-only', "Only generate, don't download") do
    args[:generate] = true
  end

  opts.separator ""
  opts.separator "environment variables:"
  opts.separator "\tSPITBALL_CACHE\t\t     Specifies the cache dir. Defaults to /tmp/spitball-#{ENV['USER']}"
  opts.separator ""
end

opts.permute!(ARGV)

args[:gemfile] = ARGV[0]
args[:destination] = ARGV[1]

unless args[:gemfile] and args[:destination]
  puts opts.help
  exit 1
end

gemfile = File.read(args[:gemfile])
gemfile_lock = File.read("#{args[:gemfile]}.lock")
bundle_config = File.read(args[:bundle_config]) if args[:bundle_config]

ball = args[:host] ?
  Spitball::Remote.new(gemfile, gemfile_lock, :host => args[:host], :port => (args[:port] || 8080).to_i,
    :without => args[:without], :bundle_config => bundle_config) :
  Spitball.new(gemfile, gemfile_lock, :without => args[:without], :bundle_config => bundle_config)

args[:generate] ? ball.cache! : ball.copy_to(args[:destination])