I'd like to conditionally set require:
in my Gemfile definition depending on the environment to work in conjunction with Bundler.require
. E.g. i'd like pry to be available in all environments but only have require: true
set in development and test. I'm currently doing something like this:
# make pry available for anyone who wants it
# but not automatically required
# e.g. in a console
gem 'pry', require: false
# automatically require pry
# for easy usage in tests
group :development, :test do
gem 'pry', require: true
end
Which works but results in a nice warning regarding a duplicate gem definition:
Your Gemfile lists the gem pry (>= 0) more than once. You should probably keep only one of them. While it's not a problem now, it could cause errors if you change the version of just one of them later.
Alternately, I can try to do something like this:
gem 'pry', require: ENV["RACK_ENV"] != "production"
but that feels much less declarative (and a little gross).
To clarify,
Bundler.require
in the development and test environments Bundler.require
答案 0 :(得分:1)
如果希望安装gem而不是“必需”,则使用:require => false
。如果你不打算在生产中使用'pry'宝石,那么将它安装到生产中是没有意义的。
您可以通过显式指定gem来使gem可用于任何环境。 例如。在开发和测试环境中使用pry
group :development, :test do
gem 'pry'
end
希望,这有帮助!
答案 1 :(得分:1)
对依赖项进行分组允许您对整个组执行操作。
group :development, :test do
gem 'pry', require: true
end
上述代码本身仅在pry gem
和development
环境中需要test
。不在生产中。因此,您不需要指定生产中不需要它。
如果你在gem文件中提到gem 'pry'
而没有指定任何内容,那么在所有环境默认情况下都需要它。
如需了解更多信息,请参阅此链接:http://bundler.io/groups.html
发件人: The How and Why of Bundler Groups
指定组允许您执行两项操作。首先,你可以安装 Gemfile中的gem,减去特定的组。例如,Rails 把mysql和pg放在数据库组中,这样如果你只是在工作 在ActionPack上,你可以捆绑安装--without db并运行 ActionPack测试,无需担心获得宝石 安装。
其次,您可以列出要使用的自动查询的特定组 Bundler.require。默认情况下,Bundler.require需要所有的宝石 默认组(这是没有显式组的所有gem)。 你也可以说Bundler.require(:default,:another_group)要求 特定群体。
答案 2 :(得分:0)
如果您在Bundler.require
中添加了群组名称,那么也会加载宝石(明确指出require: false
的除外)。
例如,拥有包含以下内容的Gemfile:
gem 'redis'
group :test do
gem 'pry'
gem 'simplecov', require: false
end
如果你Bundler.require
redis
只需要Bundler.require(:default, :test)
和,pry
它将同时需要redis
和simplecov
};请注意,即使您明确指出:test
群组,也不会要求simplecov
。因此,要使用require 'simplecov'
,您仍需要明确调用{{1}}