DEV Community

Phil Schalm
Phil Schalm

Posted on

Advanced Bundler

The following will be a (hopefully often) updated article with tricks to help utilize Bundler to its fullest.

DRY multi-platform gemspecs (2019-06-14)

  • Note the .dup call after loading the common specification. This is important as Bundler actually loads all (*.gemspec) files and Rubygems caches the results of evaling a Gem::Specification.load call. Without the .dup this means you'll end up modifying the same spec instance for every custom gemspec you want to have.
gemname.gemspec.common
Gem::Specification.new do |gem|
  gem.authors     = ["Phil Schalm"]
  gem.email       = ["notmyrealemail@nowhere.com"]
  gem.description = "Sample"
  gem.summary     = "Longer sample"
  gem.files       = %w[gemname.gemspec gemname-java.gemspec gemname.gemspec.common Gemfile]

  # Common dependencies here
end
gemname.gemspec
Gem::Specification.load(File.expand_path("gemname.gemspec.common", __dir__)).dup.tap do |gem|
  gem.instance_exec do
    self.name = "gemname"
    # 'Everything else' specific dependencies here
  end
end
gemname-java.gemspec
Gem::Specification.load(File.expand_path("gemname.gemspec.common", __dir__)).dup.tap do |gem|
  gem.instance_exec do
    self.name = "gemname-java"
    # Java specific dependencies here
  end
end
Gemfile
source 'https://rubygems.org'

gemspec name: RUBY_PLATFORM == "java" ? "gemname-java" : "gemname"

Top comments (0)