2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/package'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
class Gem::Commands::BuildCommand < Gem::Command
|
|
|
|
|
|
|
|
def initialize
|
2012-04-17 20:04:12 -04:00
|
|
|
super 'build', 'Build a gem from a gemspec'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
add_option '-C PATH', '', 'Run as if gem build was started in <PATH> instead of the current working directory.' do |value, options|
|
|
|
|
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
|
2019-08-17 10:54:17 -04:00
|
|
|
build_gem
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2019-08-18 16:04:08 -04:00
|
|
|
def build_gem(gem_name = get_one_optional_argument)
|
|
|
|
gemspec = File.exist?(gem_name) ? gem_name : "#{gem_name}.gemspec"
|
2016-02-01 07:43:26 -05:00
|
|
|
|
2019-08-17 10:54:17 -04:00
|
|
|
if File.exist?(gemspec)
|
2019-01-22 01:28:04 -05:00
|
|
|
spec = Gem::Specification.load(gemspec)
|
|
|
|
|
|
|
|
if options[:build_path]
|
|
|
|
Dir.chdir(File.dirname(gemspec)) do
|
|
|
|
spec = Gem::Specification.load File.basename(gemspec)
|
|
|
|
build_package(spec)
|
2018-05-30 09:01:35 -04:00
|
|
|
end
|
2019-01-22 01:28:04 -05:00
|
|
|
else
|
|
|
|
build_package(spec)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2019-01-22 01:28:04 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
else
|
|
|
|
alert_error "Gemspec file not found: #{gemspec}"
|
2019-08-17 10:54:17 -04:00
|
|
|
terminate_interaction(1)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-22 01:28:04 -05:00
|
|
|
def build_package(spec)
|
|
|
|
if spec
|
|
|
|
Gem::Package.build(
|
|
|
|
spec,
|
|
|
|
options[:force],
|
|
|
|
options[:strict],
|
|
|
|
options[:output]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
alert_error "Error loading gemspec. Aborting."
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
|
|
|
end
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|