在使用捆绑器时如何使用不在Gemfile中的gem? [英] How to use gems not in a Gemfile when working with bundler?

查看:86
本文介绍了在使用捆绑器时如何使用不在Gemfile中的gem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用bundler与一般的项目以及Rails专用时,您只能访问Gemfile中定义的gem。虽然这是有道理的,但它可能是有限的。大多数情况下,当我想使用某个RSpec格式化器时,我发现它是有限制的,而其余的团队不使用它。除非它在Gemfile中,否则它是不可访问的。



任何方法都可以,或者我必须将它添加到Gemfile中?



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

更新#2:看起来使用Bundler仍然是问题的原因。使用Spork和不使用Spork的不同之处在于,当运行RSpec而不使用Spork时,它会在加载项目并加载Bundler沙箱之前加载格式化程序。



使用Bundler:

  $ bundle exec irb 
>>需要'fivemat'
LoadError:无法加载这样的文件 - (irb)中的fivemat

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

没有Bundler:

  $ irb 
>>要求'fivemat'
=> true


解决方案

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



这个想法基本上颠倒了Holger答案的依赖关系,因此不需要修改共享的Gemfile。 Bundler允许指定哪个文件将被用作gemfile ,但奇怪的是,记录的方法不明显与其配置文件一起工作,并且不会被修复。 Bundler的功能有些模糊,任何配置选项都可以在环境变量中设置或在命令行上传递。以 bundle命令的形式运行所有命令--gemfile [yourgemfile] BUNDLE_GEMFILE =[yourgemfile]bundle [命令] 将导致Bundler读取你想要的任何gem文件。我强烈建议使用环境变量方法,并为当前会话创建别名或导出变量,特别是因为我无法通过exec命令使用命令行开关。



因此,我运行如下的rspec: BUNDLE_GEMFILE =[mygemfile]bundle exec rspec [filename] ,并且我有这个别名的第一部分作为 bem 在我的bashrc中。就像魅力一样。

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



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



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

  sourcehttp: //www.ibm.com/developerworks/cn/webservices/default.asp?url=/rubygems.org
gemfivemat
instance_eval(File.read(File.dirname(__ FILE__)+/ Gemfile))

只要记得指定Gemfile,你就可以走了。


When using bundler with a project in general and Rails specifically, you have access only to gems defined in your Gemfile. While this makes sense, it can be limiting. Mostly I find it limiting when I want to use a certain RSpec formatter that the rest of the team doesn't use. Unless it's in the Gemfile, it isn't accessible.

Any way around it or I have to add it to Gemfile?

Update: my problem wasn't Bundler but Spork. When running RSpec without Spork I had no problem of using whatever formatter I wanted.

Update #2: it looks like that using Bundler is still the cause of the problem. The difference between using Spork and not using Spork, is that when running RSpec without Spork, it loads the formatter before loading your project and getting into the Bundler "sandbox".

With 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>'

Without Bundler:

$ irb
>> require 'fivemat'
=> true

解决方案

I assume that none of these answers have been chosen as correct because they don't do a great job of solving the problem: having additional gems that you can use that by default don't require any changes to files already in the repository to achieve. That is, you don't have to modify any files, and you don't have to live with remembering not to check in your local changes. Here's how I do it.

The idea is basically inverting the dependencies of Holger's answer, such that there's no need to modify the shared Gemfile. Bundler allows one to specify which file is to be used as the gemfile, but strangely the documented methods do not apparently work with its configuration file and will not be fixed. There is a somewhat obscured feature of Bundler that any of the configuration options can be set in an environment variable or passed on the command line. Running all of your commands as bundle [command] --gemfile [yourgemfile] or BUNDLE_GEMFILE="[yourgemfile]" bundle [command] will cause Bundler to read whatever gemfile you want it to. I highly recommend using the environment variable approach, and either creating an alias or exporting the variable for your current session, particularly as I was unable to use the command line switch with the "exec" command.

Therefore, I run rspec like this: BUNDLE_GEMFILE="[mygemfile]" bundle exec rspec [filename], and I have the first part of this aliased as bem in my bashrc. Works like a charm.

Then, you should setup your source control to ignore your Gemfile, either in the project's .gitignore or, to keep the project entirely hygienic without changing even its .gitignore, to your personal global ignore file (which is by default in ~/.config/git/ignore and has the same format as a project's gitignore file).

One other thing to note is that Bundler will create a lockfile based on the Gemfile's name. This is super handy, as it keeps you from overwriting your project's Gemfile.lock if it's checked in, but you need to ignore this new lock file as well. If your gemfile is Foo.bar, look for Foo.bar.lock.

Finally, you can do something similar to Holger's suggestion in your custom Gemfile:

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

and you're good to go, as long as you remember to specify your Gemfile.

这篇关于在使用捆绑器时如何使用不在Gemfile中的gem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆