2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2019-04-22 07:56:16 -04:00
|
|
|
require_relative '../command'
|
|
|
|
require_relative '../package'
|
|
|
|
require_relative '../version_option'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
class Gem::Commands::BuildCommand < Gem::Command
|
2020-01-10 06:18:31 -05:00
|
|
|
include Gem::VersionOption
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def initialize
|
2012-04-17 20:04:12 -04:00
|
|
|
super 'build', 'Build a gem from a gemspec'
|
|
|
|
|
2020-01-10 06:18:31 -05:00
|
|
|
add_platform_option
|
|
|
|
|
2012-04-17 20:04:12 -04:00
|
|
|
add_option '--force', 'skip validation of the spec' do |value, options|
|
|
|
|
options[:force] = true
|
|
|
|
end
|
2018-08-27 06:05:04 -04:00
|
|
|
|
|
|
|
add_option '--strict', 'consider warnings as errors when validating the spec' do |value, options|
|
|
|
|
options[:strict] = true
|
|
|
|
end
|
2018-12-12 00:07:50 -05:00
|
|
|
|
|
|
|
add_option '-o', '--output FILE', 'output gem with the given filename' do |value, options|
|
|
|
|
options[:output] = value
|
|
|
|
end
|
2019-01-22 01:28:04 -05:00
|
|
|
|
2021-05-28 06:47:49 -04:00
|
|
|
add_option '-C PATH', 'Run as if gem build was started in <PATH> instead of the current working directory.' do |value, options|
|
2019-01-22 01:28:04 -05:00
|
|
|
options[:build_path] = value
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEMSPEC_FILE gemspec file name to build a gem for"
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def description # :nodoc:
|
|
|
|
<<-EOF
|
|
|
|
The build command allows you to create a gem from a ruby gemspec.
|
|
|
|
|
|
|
|
The best way to build a gem is to use a Rakefile and the Gem::PackageTask
|
|
|
|
which ships with RubyGems.
|
|
|
|
|
|
|
|
The gemspec can either be created by hand or extracted from an existing gem
|
|
|
|
with gem spec:
|
|
|
|
|
|
|
|
$ gem unpack my_gem-1.0.gem
|
|
|
|
Unpacked gem: '.../my_gem-1.0'
|
|
|
|
$ gem spec my_gem-1.0.gem --ruby > my_gem-1.0/my_gem-1.0.gemspec
|
|
|
|
$ cd my_gem-1.0
|
|
|
|
[edit gem contents]
|
|
|
|
$ gem build my_gem-1.0.gemspec
|
2018-12-12 00:07:50 -05:00
|
|
|
|
|
|
|
Gems can be saved to a specified filename with the output option:
|
|
|
|
|
|
|
|
$ gem build my_gem-1.0.gemspec --output=release.gem
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} GEMSPEC_FILE"
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2020-12-08 02:33:39 -05:00
|
|
|
if build_path = options[:build_path]
|
|
|
|
Dir.chdir(build_path) { build_gem }
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
build_gem
|
2019-08-17 10:54:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
def find_gemspec(glob = "*.gemspec")
|
|
|
|
gemspecs = Dir.glob(glob).sort
|
2019-08-22 01:35:39 -04:00
|
|
|
|
|
|
|
if gemspecs.size > 1
|
|
|
|
alert_error "Multiple gemspecs found: #{gemspecs}, please specify one"
|
|
|
|
terminate_interaction(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
gemspecs.first
|
2019-08-18 16:57:41 -04:00
|
|
|
end
|
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
def build_gem
|
|
|
|
gemspec = resolve_gem_name
|
2019-01-22 01:28:04 -05:00
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
if gemspec
|
|
|
|
build_package(gemspec)
|
2007-11-10 02:48:56 -05:00
|
|
|
else
|
2020-12-08 02:33:39 -05:00
|
|
|
alert_error error_message
|
2019-08-17 10:54:17 -04:00
|
|
|
terminate_interaction(1)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
def build_package(gemspec)
|
|
|
|
spec = Gem::Specification.load(gemspec)
|
2019-01-22 01:28:04 -05:00
|
|
|
if spec
|
|
|
|
Gem::Package.build(
|
|
|
|
spec,
|
|
|
|
options[:force],
|
|
|
|
options[:strict],
|
|
|
|
options[:output]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
alert_error "Error loading gemspec. Aborting."
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
|
|
|
end
|
2020-12-08 02:33:39 -05:00
|
|
|
|
|
|
|
def resolve_gem_name
|
|
|
|
return find_gemspec unless gem_name
|
|
|
|
|
|
|
|
if File.exist?(gem_name)
|
|
|
|
gem_name
|
|
|
|
else
|
|
|
|
find_gemspec("#{gem_name}.gemspec") || find_gemspec(gem_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
|
|
|
if gem_name
|
|
|
|
"Couldn't find a gemspec file matching '#{gem_name}' in #{Dir.pwd}"
|
|
|
|
else
|
|
|
|
"Couldn't find a gemspec file in #{Dir.pwd}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gem_name
|
|
|
|
get_one_optional_argument
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|