Skip to content

Commit d313891

Browse files
tenderlovematzbot
authored andcommitted
[ruby/rubygems] build gems directly instead of shelling out
I'm trying to speed up the bundler tests. The tests shell out a lot in order to build gems. We can build gems without creating a sub-process. This change reduced the test suite time from ~24 minutes, to about ~21 minutes on my machine. Once we have more of these "asset generation" routines done in the same process, I think we can start caching the outputs for further improvements ruby/rubygems@ebf27056c6
1 parent 71fecfa commit d313891

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

spec/bundler/quality_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ def check_for_specific_pronouns(filename)
184184
end
185185

186186
it "can still be built" do
187-
with_built_bundler do |_gem_path|
188-
expect(err).to be_empty, "bundler should build as a gem without warnings, but\n#{err}"
187+
with_built_bundler do |gem_path|
188+
expect(File.exist?(gem_path)).to be true
189189
end
190190
end
191191

spec/bundler/support/builders.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require "bundler/shared_helpers"
44
require "shellwords"
5+
require "fileutils"
6+
require "rubygems/package"
57

68
require_relative "build_metadata"
79

@@ -423,21 +425,30 @@ def required_ruby_version=(*reqs)
423425
end
424426

425427
class BundlerBuilder
426-
attr_writer :required_ruby_version
428+
SPEC_FILE = File.join File.dirname(__FILE__), "..", "..", "bundler.gemspec"
429+
SPEC = Gem::Specification.load(SPEC_FILE)
427430

428431
def initialize(context, name, version)
429432
raise "can only build bundler" unless name == "bundler"
430433

431434
@context = context
432-
@version = version || Bundler::VERSION
435+
@spec = SPEC.dup
436+
@spec.version = version || Bundler::VERSION
437+
end
438+
439+
def required_ruby_version
440+
@spec.required_ruby_version
441+
end
442+
443+
def required_ruby_version=(x)
444+
@spec.required_ruby_version = x
433445
end
434446

435447
def _build(options = {})
436-
full_name = "bundler-#{@version}"
448+
full_name = "bundler-#{@spec.version}"
437449
build_path = (options[:build_path] || @context.tmp) + full_name
438450
bundler_path = build_path + "#{full_name}.gem"
439451

440-
require "fileutils"
441452
FileUtils.mkdir_p build_path
442453

443454
@context.shipped_files.each do |shipped_file|
@@ -449,13 +460,14 @@ def _build(options = {})
449460
FileUtils.cp File.expand_path(shipped_file, @context.source_root), target_shipped_file, preserve: true
450461
end
451462

452-
@context.replace_version_file(@version, dir: build_path)
453-
@context.replace_changelog(@version, dir: build_path) if options[:released]
454-
@context.replace_required_ruby_version(@required_ruby_version, dir: build_path) if @required_ruby_version
463+
@context.replace_version_file(@spec.version, dir: build_path)
464+
@context.replace_changelog(@spec.version, dir: build_path) if options[:released]
455465

456-
Spec::BuildMetadata.write_build_metadata(dir: build_path, version: @version)
466+
Spec::BuildMetadata.write_build_metadata(dir: build_path, version: @spec.version.to_s)
457467

458-
@context.gem_command "build #{@context.relative_gemspec}", dir: build_path
468+
Dir.chdir build_path do
469+
Gem::Package.build(@spec)
470+
end
459471

460472
if block_given?
461473
yield(bundler_path)
@@ -659,7 +671,7 @@ def _build(opts)
659671
elsif opts[:skip_validation]
660672
@context.gem_command "build --force #{@spec.name}", dir: lib_path
661673
else
662-
@context.gem_command "build #{@spec.name}", dir: lib_path, allowed_warning: opts[:allowed_warning]
674+
Dir.chdir(lib_path) { Gem::Package.build(@spec) }
663675
end
664676

665677
gem_path = File.expand_path("#{@spec.full_name}.gem", lib_path)

0 commit comments

Comments
 (0)