5

例如,我正在开发一个gem,而在我开发的时候,我使用pry了IRB,而不是debugger用于调试。但是,我不希望可能的贡献者必须安装它们(因为他们可能不需要它们)。我的第一个想法是将它们放在一个 Bundler 组中:

source :rubygems

gemspec

group :extras do
  gem "pry"
  gem "debugger"
end

然后人们可以使用:

$ bundle install --without extras

但我希望它是未安装的默认值。完美的是它们不在my 中Gemfile,但我仍然可以要求它们(如果它们存在于计算机上)。这个解决方案可以,因为我不在乎他们被锁定在哪个版本。可以做到吗?

4

4 回答 4

2

您可以添加任意加载路径,然后从它们中获取 gem。查看全局变量$:

puts $:.inspect
# ["/var/myproject/releases/20200918191637/lib", "/var/myproject/releases/20200918191637/vendor", "/var/myproject/releases/20200918191637/app/assets", "/var/myproject/releases/20200918191637/app/controllers", "/var/myproject/releases/20200918191637/app/helpers", "/var/myproject/releases/20200918191637/app/mailers", "/var/myproject/releases/20200918191637/app/models", "/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-2.0.2/lib", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/json-1.8.3/lib", "/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-2.0.2/lib/gems/bundler-2.0.2/lib", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/x86_64-linux", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0/x86_64-linux", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-linux"]

现在让我们追加$:并要求一个宝石

require 'method_source'
# LoadError: cannot load such file -- method_source
$: << '/home/deploy/.rvm/gems/ruby-2.2.4@myset/gems/method_source-1.0.0/lib'
# ["/var/myproject/releases/20200918191637/lib", "/var/myproject/releases/20200918191637/vendor", "/var/myproject/releases/20200918191637/app/assets", "/var/myproject/releases/20200918191637/app/controllers", "/var/myproject/releases/20200918191637/app/helpers", "/var/myproject/releases/20200918191637/app/mailers", "/var/myproject/releases/20200918191637/app/models", "/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-2.0.2/lib", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/json-1.8.3/lib", "/home/deploy/.rvm/gems/ruby-2.3.0/gems/bundler-2.0.2/lib/gems/bundler-2.0.2/lib", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/x86_64-linux", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0/x86_64-linux", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0", "/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-linux", "/home/deploy/.rvm/gems/ruby-2.2.4@myset/gems/method_source-1.0.0/lib"]
require 'method_source'
# true

但是,如果您有很多依赖项,那可能会很乏味,因此您可以尝试以下方法:

Dir['/home/deploy/.rvm/gems/ruby-2.2.4@myset/gems/*'].each do |gem_path|
  $: << File.join(gem_path, 'lib')
end
于 2020-09-18T19:44:16.817 回答
0

bundle install是“选择退出”——除非您指定--without some_group,否则它会安装所有内容。

如果你绝对不想在你的 Gemfile 中有一个给定的 gem,你可以在你的包之外只是gem install那个流氓 gem。然后它会在下面irb直接对你可见(但如果你在运行的代码中ruby尝试它,显然你会得到错误)。requirebundle exec

于 2012-10-22T17:16:52.227 回答
0

您可以将基于环境变量的条件添加到 Gemfile。例子:

source :rubygems

gemspec

if ENV['WITH_EXTRAS'] == '1'
  gem "pry"
  gem "debugger"
end

'1'如果您将环境变量设置为eg ,则仅安装/加载 gems WITH_EXTRAS=1 bundle install

于 2012-10-22T19:43:08.047 回答
0

现在你可以使用“可选”参数,它会按照你的要求做:它不会安装额外的,除非人们bundle --with extras

group :extras, optional:true do
   ...
end

https://bundler.io/v2.3/guides/groups.html#optional-groups

于 2022-02-15T14:25:32.760 回答