Using bundler inside my gem

2019-04-15 19:59发布

问题:

I wanted to use bundler inside gem I wrote. I had Gemfile and in my_gem_file.rb I have

require 'rubygems'
require 'bundler'
Bundler.setup
Bundler.require(:default)

But when I build and install my gem I get exception Bundler::GemfileNotFound: Could not locate Gemfile. Is there any solution for using bundler inside gems?

回答1:

Since your gem has specified its dependencies in its .gemspec you can assume that they will be available at runtime. Don't involve Bundler.

You can, however, still make use of Bundler for the development of your gem. If you use this Gemfile:

source :rubygems
gemspec

Bundler will look in your .gemspec to determine which gems to install when you run bundle install. You can read more about that here: http://jeffkreeftmeijer.com/2010/bundler-because-your-gems-depend-on-gems-too/



回答2:

Bundler is not suitable for managment depending gems in gem source. Inside a gem, just require the libraries that you need.



回答3:

I disagree, Bundler is great for gem development. It helps make it easier to get started and collaborate. And keep the gemspec cleaner?

I'd eliminate the dev dependencies

Gem::Specification.new do |s|
  s.add_development_dependency("mocha", ["~>0.9.8"])

of course keep the s.add_dependency("i18n", ["~>0.4.1"]) and others that your gem depends on.

You might end up with a Gemfile (as Theo shows), like this

source :rubygems
gemspec

group :test do
  gem 'rails', '3.0.0'
  gem 'mocha'
  gem 'rspec-rails', "~>2.0.1"
end

Nice and clean, easy to read. Hope this helps.