Skip to content

Commit dea9732

Browse files
Merge pull request #3983 from rubygems/improve-c-flag
Improve gem build -C flag (cherry picked from commit 2ad192e)
1 parent e5a9c8d commit dea9732

File tree

2 files changed

+230
-21
lines changed

2 files changed

+230
-21
lines changed

lib/rubygems/commands/build_command.rb

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ def usage # :nodoc:
6161
end
6262

6363
def execute
64-
gem_name = get_one_optional_argument || find_gemspec
65-
build_gem(gem_name)
64+
if build_path = options[:build_path]
65+
Dir.chdir(build_path) { build_gem }
66+
return
67+
end
68+
69+
build_gem
6670
end
6771

6872
private
6973

70-
def find_gemspec
71-
gemspecs = Dir.glob("*.gemspec").sort
74+
def find_gemspec(glob = "*.gemspec")
75+
gemspecs = Dir.glob(glob).sort
7276

7377
if gemspecs.size > 1
7478
alert_error "Multiple gemspecs found: #{gemspecs}, please specify one"
@@ -78,28 +82,19 @@ def find_gemspec
7882
gemspecs.first
7983
end
8084

81-
def build_gem(gem_name)
82-
gemspec = File.extname(gem_name) == ".gemspec" ? gem_name : "#{gem_name}.gemspec"
83-
84-
if File.exist?(gemspec)
85-
spec = Gem::Specification.load(gemspec)
86-
87-
if options[:build_path]
88-
Dir.chdir(File.dirname(gemspec)) do
89-
spec = Gem::Specification.load(File.basename(gemspec))
90-
build_package(spec)
91-
end
92-
else
93-
build_package(spec)
94-
end
85+
def build_gem
86+
gemspec = resolve_gem_name
9587

88+
if gemspec
89+
build_package(gemspec)
9690
else
97-
alert_error "Gemspec file not found: #{gemspec}"
91+
alert_error error_message
9892
terminate_interaction(1)
9993
end
10094
end
10195

102-
def build_package(spec)
96+
def build_package(gemspec)
97+
spec = Gem::Specification.load(gemspec)
10398
if spec
10499
Gem::Package.build(
105100
spec,
@@ -112,4 +107,26 @@ def build_package(spec)
112107
terminate_interaction 1
113108
end
114109
end
110+
111+
def resolve_gem_name
112+
return find_gemspec unless gem_name
113+
114+
if File.exist?(gem_name)
115+
gem_name
116+
else
117+
find_gemspec("#{gem_name}.gemspec") || find_gemspec(gem_name)
118+
end
119+
end
120+
121+
def error_message
122+
if gem_name
123+
"Couldn't find a gemspec file matching '#{gem_name}' in #{Dir.pwd}"
124+
else
125+
"Couldn't find a gemspec file in #{Dir.pwd}"
126+
end
127+
end
128+
129+
def gem_name
130+
get_one_optional_argument
131+
end
115132
end

test/rubygems/test_gem_commands_build_command.rb

Lines changed: 193 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_execute_missing_file
231231
end
232232

233233
assert_equal '', @ui.output
234-
assert_equal "ERROR: Gemspec file not found: some_gem.gemspec\n", @ui.error
234+
assert_equal "ERROR: Couldn't find a gemspec file matching 'some_gem' in #{@tempdir}\n", @ui.error
235235
end
236236

237237
def test_execute_outside_dir
@@ -272,6 +272,198 @@ def test_execute_outside_dir
272272
assert_equal "this is a summary", spec.summary
273273
end
274274

275+
def test_execute_outside_dir_with_glob_argument
276+
gemspec_dir = File.join @tempdir, 'build_command_gem'
277+
gemspec_file = File.join gemspec_dir, @gem.spec_name
278+
readme_file = File.join gemspec_dir, 'README.md'
279+
280+
FileUtils.mkdir_p gemspec_dir
281+
282+
File.open readme_file, 'w' do |f|
283+
f.write "My awesome gem"
284+
end
285+
286+
File.open gemspec_file, 'w' do |gs|
287+
gs.write @gem.to_ruby
288+
end
289+
290+
@cmd.options[:build_path] = gemspec_dir
291+
@cmd.options[:args] = ["*.gemspec"]
292+
293+
use_ui @ui do
294+
@cmd.execute
295+
end
296+
297+
output = @ui.output.split "\n"
298+
assert_equal " Successfully built RubyGem", output.shift
299+
assert_equal " Name: some_gem", output.shift
300+
assert_equal " Version: 2", output.shift
301+
assert_equal " File: some_gem-2.gem", output.shift
302+
assert_equal [], output
303+
304+
gem_file = File.join gemspec_dir, File.basename(@gem.cache_file)
305+
assert File.exist?(gem_file)
306+
307+
spec = Gem::Package.new(gem_file).spec
308+
309+
assert_equal "some_gem", spec.name
310+
assert_equal "this is a summary", spec.summary
311+
end
312+
313+
def test_execute_outside_dir_no_gemspec_present
314+
gemspec_dir = File.join @tempdir, 'build_command_gem'
315+
gemspec_file = File.join @tempdir, @gem.spec_name
316+
readme_file = File.join gemspec_dir, 'README.md'
317+
318+
FileUtils.mkdir_p gemspec_dir
319+
320+
File.open readme_file, 'w' do |f|
321+
f.write "My awesome gem"
322+
end
323+
324+
File.open gemspec_file, 'w' do |gs|
325+
gs.write @gem.to_ruby
326+
end
327+
328+
@cmd.options[:build_path] = gemspec_dir
329+
@cmd.options[:args] = ["*.gemspec"]
330+
331+
use_ui @ui do
332+
assert_raises Gem::MockGemUi::TermError do
333+
@cmd.execute
334+
end
335+
end
336+
337+
assert_equal "", @ui.output
338+
assert_equal "ERROR: Couldn't find a gemspec file matching '*.gemspec' in #{gemspec_dir}\n", @ui.error
339+
340+
gem_file = File.join gemspec_dir, File.basename(@gem.cache_file)
341+
refute File.exist?(gem_file)
342+
end
343+
344+
def test_execute_outside_dir_without_gem_name
345+
gemspec_dir = File.join(@tempdir, 'build_command_gem')
346+
gemspec_file = File.join(gemspec_dir, @gem.spec_name)
347+
348+
readme_file = File.join gemspec_dir, 'README.md'
349+
350+
FileUtils.mkdir_p(gemspec_dir)
351+
352+
File.open readme_file, 'w' do |f|
353+
f.write "My awesome gem"
354+
end
355+
356+
File.open(gemspec_file, "w") do |gs|
357+
gs.write(@gem.to_ruby)
358+
end
359+
360+
@cmd.options[:build_path] = gemspec_dir
361+
@cmd.options[:args] = []
362+
363+
use_ui @ui do
364+
Dir.chdir(gemspec_dir) do
365+
@cmd.execute
366+
end
367+
end
368+
369+
output = @ui.output.split("\n")
370+
assert_equal " Successfully built RubyGem", output.shift
371+
assert_equal " Name: some_gem", output.shift
372+
assert_equal " Version: 2", output.shift
373+
assert_equal " File: some_gem-2.gem", output.shift
374+
assert_equal [], output
375+
376+
gem_file = File.join gemspec_dir, File.basename(@gem.cache_file)
377+
assert File.exist?(gem_file)
378+
379+
spec = Gem::Package.new(gem_file).spec
380+
381+
assert_equal "some_gem", spec.name
382+
assert_equal "this is a summary", spec.summary
383+
end
384+
385+
def test_execute_outside_dir_with_external_gemspec
386+
gemspec_dir = File.join @tempdir, 'gemspec_dir'
387+
gemspec_file = File.join gemspec_dir, @gem.spec_name
388+
389+
gemcode_dir = File.join @tempdir, 'build_command_gem'
390+
readme_file = File.join gemcode_dir, 'README.md'
391+
392+
FileUtils.mkdir_p gemspec_dir
393+
FileUtils.mkdir_p gemcode_dir
394+
395+
File.open readme_file, 'w' do |f|
396+
f.write "My awesome gem in nested directory"
397+
end
398+
399+
File.open gemspec_file, 'w' do |gs|
400+
gs.write @gem.to_ruby
401+
end
402+
403+
@cmd.options[:build_path] = gemcode_dir
404+
@cmd.options[:args] = [gemspec_file]
405+
406+
use_ui @ui do
407+
@cmd.execute
408+
end
409+
410+
output = @ui.output.split "\n"
411+
assert_equal " Successfully built RubyGem", output.shift
412+
assert_equal " Name: some_gem", output.shift
413+
assert_equal " Version: 2", output.shift
414+
assert_equal " File: some_gem-2.gem", output.shift
415+
assert_equal [], output
416+
417+
gem_file = File.join gemcode_dir, File.basename(@gem.cache_file)
418+
assert File.exist?(gem_file)
419+
420+
spec = Gem::Package.new(gem_file).spec
421+
422+
assert_equal "some_gem", spec.name
423+
assert_equal "this is a summary", spec.summary
424+
end
425+
426+
def test_execute_outside_dir_with_external_relative_gemspec
427+
gemspec_dir = File.join @tempdir, 'gemspec_dir'
428+
gemspec_file = File.join gemspec_dir, @gem.spec_name
429+
430+
gemcode_dir = File.join @tempdir, 'build_command_gem'
431+
readme_file = File.join gemcode_dir, 'README.md'
432+
433+
FileUtils.mkdir_p gemspec_dir
434+
FileUtils.mkdir_p gemcode_dir
435+
436+
File.open readme_file, 'w' do |f|
437+
f.write "My awesome gem in nested directory"
438+
end
439+
440+
File.open gemspec_file, 'w' do |gs|
441+
gs.write @gem.to_ruby
442+
end
443+
444+
@cmd.options[:build_path] = gemcode_dir
445+
@cmd.options[:args] = [File.join("..", "gemspec_dir", @gem.spec_name)]
446+
447+
use_ui @ui do
448+
@cmd.execute
449+
end
450+
451+
output = @ui.output.split "\n"
452+
assert_equal " Successfully built RubyGem", output.shift
453+
assert_equal " Name: some_gem", output.shift
454+
assert_equal " Version: 2", output.shift
455+
assert_equal " File: some_gem-2.gem", output.shift
456+
assert_equal [], output
457+
458+
gem_file = File.join gemcode_dir, File.basename(@gem.cache_file)
459+
assert File.exist?(gem_file)
460+
461+
spec = Gem::Package.new(gem_file).spec
462+
463+
assert_equal "some_gem", spec.name
464+
assert_equal "this is a summary", spec.summary
465+
end
466+
275467
def test_can_find_gemspecs_without_dot_gemspec
276468
gemspec_file = File.join(@tempdir, @gem.name)
277469

0 commit comments

Comments
 (0)