是否有可能需要在Gemfile之外的文件?[英] Is it possible to require files outside the Gemfile?

本文是小编为大家收集整理的关于是否有可能需要在Gemfile之外的文件?的处理方法,想解了是否有可能需要在Gemfile之外的文件?的问题怎么解决?是否有可能需要在Gemfile之外的文件?问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

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

source :rubygems

gemspec

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

然后人们可以使用:

$ bundle install --without extras

但我希望它是一个默认的未安装.什么是完美的,即他们Gemfile中的不是,但我仍然需要它们(如果它们存在于计算机上).这个解决方案是可以的,因为我不在乎他们锁定的版本.可以完成吗?

推荐答案

您可以添加任意加载路径,然后需要它们的宝石.查看全局变量$:

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

其他推荐答案

bundle install是"选择退出" - 无数您指定--without some_group,它安装一切.

如果你绝对不想在你的Gemfile中有一个特定的宝石,你可以只是gem install捆绑在你的捆绑外的流氓宝石.然后,它会在irb和bright ruby下面可见(但显然您将在require在bundle exec下运行的代码中尝试require,因此您会得到错误.

其他推荐答案

您可以将根据环境变量添加到Gemfile中的条件.示例:

source :rubygems

gemspec

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

如果将环境变量设置为'1',则只能安装/加载宝石. WITH_EXTRAS=1 bundle install.

本文地址:https://www.itbaoku.cn/post/786757.html