使用bundler时如何在Gemfile中使用gem?

问题描述 投票:21回答:5

当一般使用带有项目的bundle和Rails时,你只能访问Gemfile中定义的gem。虽然这是有道理的,但它可能是有限的。当我想使用团队的其他成员不使用的某个RSpec格式化程序时,我发现它主要限制了它。除非它在Gemfile中,否则无法访问。

任何方式或我必须将其添加到Gemfile?

更新:我的问题不是Bundler而是Spork。在没有Spork的情况下运行RSpec时我没有使用任何格式化程序的问题。

更新#2:看起来使用Bundler仍然是导致问题的原因。使用Spork和不使用Spork之间的区别在于,在没有Spork的情况下运行RSpec时,它会在加载项目并进入Bundler“sandbox”之前加载格式化程序。

使用Bundler:

$ bundle exec irb
>> require 'fivemat'
LoadError: cannot load such file -- fivemat

from (irb):1:in `require'
from (irb):1
from /Users/arikfr/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

没有Bundler:

$ irb
>> require 'fivemat'
=> true
ruby rubygems bundler
5个回答
10
投票

我假设这些答案都没有被选为正确的,因为他们没有很好地解决问题:拥有额外的宝石,你可以使用默认情况下不需要对存储库中已有的文件进行任何更改来实现。也就是说,您不必修改任何文件,也不必记住不检查本地更改。这是我如何做到的。

这个想法基本上颠倒了Holger答案的依赖性,因此不需要修改共享的Gemfile。 Bundler允许指定which file is to be used as the gemfile,但奇怪的是记录的方法do not apparently work with its configuration file and will not be fixed。 Bundler有一个模糊的功能,任何配置选项都可以在环境变量中设置或在命令行上传递。以bundle [command] --gemfile [yourgemfile]BUNDLE_GEMFILE="[yourgemfile]" bundle [command]运行所有命令将使Bundler读取您想要的任何gemfile。我强烈建议使用环境变量方法,并为当前会话创建别名或导出变量,特别是因为我无法使用带有“exec”命令的命令行开关。

因此,我运行这样的rspec:BUNDLE_GEMFILE="[mygemfile]" bundle exec rspec [filename],我在我的bashrc中将这个别名的第一部分作为bem。奇迹般有效。

然后,你应该设置你的源代码控制来忽略你的Gemfile,无论是在项目的.gitignore中,还是为了让项目完全卫生而不改变它的.gitignore,你的个人全局忽略文件(默认情况下在~/.config/git/ignore中,并具有与项目的gitignore文件格式相同)。

另外需要注意的是Bundler将根据Gemfile的名称创建一个锁文件。这非常方便,因为它可以防止你覆盖你的项目的Gemfile.lock,如果它已经签入,但你也需要忽略这个新的锁文件。如果您的gemfile是Foo.bar,请查找Foo.bar.lock

最后,您可以在自定义Gemfile中执行与Holger建议类似的操作:

source "http://rubygems.org"
gem "fivemat"
instance_eval(File.read(File.dirname(__FILE__) + "/Gemfile"))

只要你记得指定你的Gemfile,你就会很高兴。


16
投票

ChiliProject,我们允许用户创建一个Gemfile.local,它在加载时包含在主要的Gemfile中。这允许用户指定其他宝石,而无需更改我们的Gemfile以简化更新。

为此,我们已经包含以下代码at the bottom of our Gemfile

gemfile_local = File.expand_path('Gemfile.local', __dir__)
if File.readable?(gemfile_local)
  puts "Loading #{gemfile_local}..." if $DEBUG
  instance_eval(File.read(gemfile_local))
end

Gemfile.local本身通过.gitignore从存储库中排除。


1
投票

你可以在你的Gemfile中使用这样的东西:

gem 'foo' if ENV['ENABLE_FOO_GEM']

然后在您的环境中设置ENABLE_FOO_GEM。

export ENABLE_FOO_GEM=1

默认情况下,gem将被禁用,但任何想要使用它的人都可以轻松打开(永久)。


0
投票

如果你仍然决定这样做(可怕的想法):

您可以将ruby代码添加到Gemfile中以加载〜/ .gem文件(或类似文件)(如果存在)。

就像是:

eval(IO.read('~/.gemfile'), binding) if FileTest.exists?("~/.gemfile")

0
投票

添加到.gitignore

Gemfile.local
Gemfile.local.lock

使用以下内容向项目添加Gemfile.local.sample文件:

# Include gems that are note meant to be part of the project but for development purposes
# That's why Gemfile.local and Gemfile.local.lock must be git-ignored

# To use these gems:
#   1. Create a "Gemfile.local" file (at same level of "Gemfile")
#   2. Prepend "BUNDLE_GEMFILE=Gemfile.local" before "bundle install" or "bundle exec rails c" and so forth.

eval_gemfile "./Gemfile"

group :development, :test do
  # Suggested gems
  gem "awesome_print", require:"ap"
  gem "hirb"
  gem "pry"
  gem "pry-byebug"
  gem "pry-rails"
  gem "meta_request"

  # My gems
  gem "fivemat"
end
© www.soinside.com 2019 - 2024. All rights reserved.