Class: Gem::Commands::InaboxCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/inabox_command.rb

Defined Under Namespace

Modules: Multipart

Instance Method Summary collapse

Constructor Details

#initializeInaboxCommand

Returns a new instance of InaboxCommand.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubygems/commands/inabox_command.rb', line 17

def initialize
  super 'inabox', description

  add_option('-c', '--configure', "Configure GemInABox") do |value, options|
    options[:configure] = true
  end

  add_option('-g', '--host HOST', "Host to upload to.") do |value, options|
    options[:host] = value
  end
end

Instance Method Details

#argumentsObject



9
10
11
# File 'lib/rubygems/commands/inabox_command.rb', line 9

def arguments
  "GEM       built gem to push up"
end

#config_pathObject



89
90
91
# File 'lib/rubygems/commands/inabox_command.rb', line 89

def config_path
  File.join(Gem.user_home, '.gem', 'geminabox')
end

#configureObject



93
94
95
96
97
# File 'lib/rubygems/commands/inabox_command.rb', line 93

def configure
  say "Enter the root url for your personal geminabox instance. (E.g. http://gems/)"
  host = ask("Host:")
  self.geminabox_host = host
end

#descriptionObject



5
6
7
# File 'lib/rubygems/commands/inabox_command.rb', line 5

def description
  'Push a gem up to your GemInABox'
end

#executeObject



29
30
31
32
33
# File 'lib/rubygems/commands/inabox_command.rb', line 29

def execute
  return configure if options[:configure]
  setup
  send_gem
end

#find_gemObject

Raises:

  • (Gem::CommandLineError)


44
45
46
47
48
49
50
51
# File 'lib/rubygems/commands/inabox_command.rb', line 44

def find_gem
  say "You didn't specify a gem, looking for one in pkg..."
  path, directory = File.split(Dir.pwd)
  possible_gems = Dir.glob("pkg/#{directory}-*.gem")
  raise Gem::CommandLineError, "Couldn't find a gem in pkg, please specify a gem name on the command line (e.g. gem inabox GEMNAME)" unless possible_gems.any?
  name_regexp = Regexp.new("^pkg/#{directory}-")
  possible_gems.sort_by{ |a| Gem::Version.new(a.sub(name_regexp,'')) }.last
end

#geminabox_hostObject



99
100
101
# File 'lib/rubygems/commands/inabox_command.rb', line 99

def geminabox_host
  @geminabox_host ||= options[:host] || Gem.configuration.load_file(config_path)[:host]
end

#geminabox_host=(host) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/rubygems/commands/inabox_command.rb', line 103

def geminabox_host=(host)
  config = Gem.configuration.load_file(config_path).merge(:host => host)

  dirname = File.dirname(config_path)
  Dir.mkdir(dirname) unless File.exists?(dirname)

  File.open(config_path, 'w') do |f|
    f.write config.to_yaml
  end
end

#handle_response(response) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/rubygems/commands/inabox_command.rb', line 80

def handle_response(response)
  case response
  when Net::HTTPSuccess, Net::HTTPRedirection
    puts response.body
  else
    response.error!
  end
end

#proxyObject



72
73
74
75
76
77
78
# File 'lib/rubygems/commands/inabox_command.rb', line 72

def proxy
  if proxy_info = ENV['http_proxy'] || ENV['HTTP_PROXY'] and uri = URI.parse(proxy_info)
    Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
  else
    Net::HTTP
  end
end

#send_gemObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubygems/commands/inabox_command.rb', line 53

def send_gem
  # sanitize printed URL if a password is present
  url = URI.parse(geminabox_host)
  url_for_presentation = url.clone
  url_for_presentation.password = '***' if url_for_presentation.password

  say "Pushing #{File.split(@gemfile).last} to #{url_for_presentation}..."

  File.open(@gemfile, "rb") do |file|
    request_body, request_headers = Multipart::MultipartPost.new.prepare_query("file" => file)

    proxy.start(url.host, url.port) {|con|
      req = Net::HTTP::Post.new('/upload', request_headers)
      req.basic_auth(url.user, url.password) if url.user
      handle_response(con.request(req, request_body))
    }
  end
end

#setupObject



35
36
37
38
39
40
41
42
# File 'lib/rubygems/commands/inabox_command.rb', line 35

def setup
  @gemfile = if options[:args].size == 0
    find_gem
  else
    get_one_gem_name
  end
  configure unless geminabox_host
end

#usageObject



13
14
15
# File 'lib/rubygems/commands/inabox_command.rb', line 13

def usage
  "#{program_name} GEM"
end