2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2010-02-21 21:52:35 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/local_remote_options'
|
|
|
|
require 'rubygems/gemcutter_utilities'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/package'
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
class Gem::Commands::PushCommand < Gem::Command
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
include Gem::LocalRemoteOptions
|
|
|
|
include Gem::GemcutterUtilities
|
|
|
|
|
|
|
|
def description # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
<<-EOF
|
|
|
|
The push command uploads a gem to the push server (the default is
|
|
|
|
https://rubygems.org) and adds it to the index.
|
|
|
|
|
2018-05-17 21:39:13 -04:00
|
|
|
The gem can be removed from the index and deleted from the server using the yank
|
2013-09-14 04:59:02 -04:00
|
|
|
command. For further discussion see the help for the yank command.
|
2019-01-22 01:28:04 -05:00
|
|
|
|
|
|
|
The push command will use ~/.gem/credentials to authenticate to a server, but you can use the RubyGems environment variable GEM_HOST_API_KEY to set the api key to authenticate.
|
2013-09-14 04:59:02 -04:00
|
|
|
EOF
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEM built gem to push up"
|
|
|
|
end
|
|
|
|
|
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} GEM"
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2013-09-14 04:59:02 -04:00
|
|
|
super 'push', 'Push a gem up to the gem server', :host => self.host
|
2013-07-08 18:41:03 -04:00
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
@user_defined_host = false
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
add_proxy_option
|
2011-03-01 04:41:32 -05:00
|
|
|
add_key_option
|
2018-12-01 06:01:00 -05:00
|
|
|
add_otp_option
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
add_option('--host HOST',
|
2017-10-07 21:32:18 -04:00
|
|
|
'Push to another gemcutter-compatible host',
|
|
|
|
' (e.g. https://rubygems.org)') do |value, options|
|
2011-01-18 19:08:49 -05:00
|
|
|
options[:host] = value
|
2018-08-27 06:05:04 -04:00
|
|
|
@user_defined_host = true
|
2011-01-18 19:08:49 -05:00
|
|
|
end
|
2013-02-04 21:37:35 -05:00
|
|
|
|
|
|
|
@host = nil
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-08-27 06:05:04 -04:00
|
|
|
gem_name = get_one_gem_name
|
|
|
|
default_gem_server, push_host = get_hosts_for(gem_name)
|
|
|
|
|
|
|
|
default_host = nil
|
|
|
|
user_defined_host = nil
|
|
|
|
|
|
|
|
if @user_defined_host
|
|
|
|
user_defined_host = options[:host]
|
|
|
|
else
|
|
|
|
default_host = options[:host]
|
|
|
|
end
|
|
|
|
|
|
|
|
@host = if user_defined_host
|
|
|
|
user_defined_host
|
|
|
|
elsif default_gem_server
|
|
|
|
default_gem_server
|
|
|
|
elsif push_host
|
|
|
|
push_host
|
|
|
|
else
|
|
|
|
default_host
|
|
|
|
end
|
2013-02-04 21:37:35 -05:00
|
|
|
|
|
|
|
sign_in @host
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
send_gem(gem_name)
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
def send_gem(name)
|
2011-01-18 19:08:49 -05:00
|
|
|
args = [:post, "api/v1/gems"]
|
|
|
|
|
2013-09-18 17:29:41 -04:00
|
|
|
gem_data = Gem::Package.new(name)
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
unless @host
|
2013-09-18 17:29:41 -04:00
|
|
|
@host = gem_data.spec.metadata['default_gem_server']
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2016-03-03 19:29:40 -05:00
|
|
|
push_host = nil
|
2013-09-18 17:29:41 -04:00
|
|
|
|
|
|
|
if gem_data.spec.metadata.has_key?('allowed_push_host')
|
2016-03-03 19:29:40 -05:00
|
|
|
push_host = gem_data.spec.metadata['allowed_push_host']
|
2013-09-18 17:29:41 -04:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2016-03-03 19:29:40 -05:00
|
|
|
@host ||= push_host
|
|
|
|
|
|
|
|
# Always include @host, even if it's nil
|
|
|
|
args += [ @host, push_host ]
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
say "Pushing gem to #{@host || Gem.host}..."
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2018-12-01 06:01:00 -05:00
|
|
|
response = send_push_request(name, args)
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
with_response response
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
private
|
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
def send_push_request(name, args)
|
2018-12-01 06:01:00 -05:00
|
|
|
rubygems_api_request(*args) do |request|
|
|
|
|
request.body = Gem.read_binary name
|
|
|
|
request.add_field "Content-Length", request.body.size
|
|
|
|
request.add_field "Content-Type", "application/octet-stream"
|
|
|
|
request.add_field "Authorization", api_key
|
2019-03-04 22:32:58 -05:00
|
|
|
request.add_field "OTP", options[:otp] if options[:otp]
|
2018-12-01 06:01:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
def get_hosts_for(name)
|
|
|
|
gem_metadata = Gem::Package.new(name).spec.metadata
|
|
|
|
|
|
|
|
[
|
|
|
|
gem_metadata["default_gem_server"],
|
|
|
|
gem_metadata["allowed_push_host"]
|
|
|
|
]
|
|
|
|
end
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|