例如,我正在开发一个gem,在我开发的时候,我使用pry
代替IRB,使用debugger
进行调试。但是,我不希望可能的贡献者必须安装它们(因为它们可能不需要它们)。我的第一个想法是把它们放在Bundler小组中:
source :rubygems
gemspec
group :extras do
gem "pry"
gem "debugger"
end
然后人们可以使用:
$ bundle install --without extras
但我希望它是默认的,它们没有安装。什么是完美的,他们在Gemfile
中 ,但我仍然需要它们(如果它们存在于计算机上)。这个解决方案没问题,因为我不关心他们被锁定的版本。可以吗?
答案 0 :(得分:1)
您可以添加任意加载路径,然后从中获取宝石。检查全局变量$:
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
答案 1 :(得分:0)
bundle install
是“选择退出” - 除非您指定--without some_group
,否则会安装所有内容。
如果你绝对不想在你的Gemfile中拥有一个给定的gem,你可以只是gem install
你的捆绑之外的那个流氓gem。然后,您会在irb
和ruby
下看到它(但显然,如果您在require
下运行的代码中尝试bundle exec
,则会出现错误。
答案 2 :(得分:0)
您可以根据环境变量将条件添加到Gemfile中。例如:
source :rubygems
gemspec
if ENV['WITH_EXTRAS'] == '1'
gem "pry"
gem "debugger"
end
如果您将环境变量设置为'1'
,则仅安装/加载宝石。 WITH_EXTRAS=1 bundle install
。