Class: Gem::Commands::SboxCommand

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

Defined Under Namespace

Modules: Multipart

Instance Method Summary collapse

Constructor Details

#initializeSboxCommand

Returns a new instance of SboxCommand.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubygems/commands/sbox_command.rb', line 19

def initialize
  super 'sbox', description

  add_option('-c', '--configure', "Configure StrangeBox") 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



11
12
13
# File 'lib/rubygems/commands/sbox_command.rb', line 11

def arguments
  "GEM ... built gem to push up"
end

#config_pathObject



91
92
93
# File 'lib/rubygems/commands/sbox_command.rb', line 91

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

#configureObject



95
96
97
98
99
# File 'lib/rubygems/commands/sbox_command.rb', line 95

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

#descriptionObject



5
6
7
8
9
# File 'lib/rubygems/commands/sbox_command.rb', line 5

def description
  'Push a gem up to your StrangeBox:
$ gem build secretgem.gemspec      ...  build your Gem
$ gem sbox ./secretgem-0.0.1.gem   ...  push Gem to StrangeBox'
end

#executeObject



31
32
33
34
35
# File 'lib/rubygems/commands/sbox_command.rb', line 31

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

#find_gemObject

Raises:

  • (Gem::CommandLineError)


46
47
48
49
50
51
52
53
# File 'lib/rubygems/commands/sbox_command.rb', line 46

def find_gem
  say "StrangeBox:: 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

#handle_response(response) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rubygems/commands/sbox_command.rb', line 82

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

#proxyObject



74
75
76
77
78
79
80
# File 'lib/rubygems/commands/sbox_command.rb', line 74

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



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

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

  say "StrangeBox:: 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



37
38
39
40
41
42
43
44
# File 'lib/rubygems/commands/sbox_command.rb', line 37

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

#strangebox_hostObject



101
102
103
# File 'lib/rubygems/commands/sbox_command.rb', line 101

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

#strangebox_host=(host) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/rubygems/commands/sbox_command.rb', line 105

def strangebox_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

#usageObject



15
16
17
# File 'lib/rubygems/commands/sbox_command.rb', line 15

def usage
  "#{program_name} GEM"
end