在解析gemspec之前,如何安装gem(通过使用gemspec的捆绑程序)? [英] How can I install a gem (via bundler using gemspec) before parsing the gemspec?

查看:269
本文介绍了在解析gemspec之前,如何安装gem(通过使用gemspec的捆绑程序)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以帮助进行版本控制的宝石.在gemspec文件中定义版本时,使此gem可用非常有用.

I have a gem that exists for the purpose of helping with versioning. It's useful to have this gem available when defining the version in the gemspec file.

但是,问题在于,运行bundle install首先会导致gemspec解析,这会导致错误,因为尚未安装所需的gem.

The problem, however, is that running bundle install first causes the gemspec to be parsed, which results in an error because the required gem isn't installed yet.

我可以通过在bundle install之前运行gem install <other_gem>来解决它,但是我更喜欢捆绑程序来管理它,尤其是考虑到我正在使用自定义的gem服务器时.

I can get around it by running gem install <other_gem> before bundle install, but I'd much prefer bundler manage it, especially when taking into account that I'm using a custom gem server.

我尝试将gem添加到gemspec行之前的Gemfile中,但是没有运气.

I've tried adding the gem to the Gemfile directly before the gemspec line, but no luck.

宝石文件:

source 'https://my.gemserver.com/gems'

gemspec

mygem.gemspec:

mygem.gemspec:

require 'external/dependency'

Gem::Specification.new do |spec|
  spec.name = 'mygem'
  spec.version = External::Dependency.version_helper
  ....
  spec.add_development_dependency 'external-dependency'
end

另一个解决方法是抢救LoadError并在未加载依赖项的情况下指定默认版本.另外,也不理想

Another workaround is to rescue the LoadError and specify a default version if the dependency isn't loaded. Also, not ideal

begin
  require 'external/dependency'
rescue LoadError; end

Gem::Specification.new do |spec|
  spec.name = 'mygem'
  spec.version = defined?(External::Dependency) ? External::Dependency.version_helper : ''
  ....
  spec.add_development_dependency 'external-dependency'
end

推荐答案

我通过使gemspec在捆绑更新或安装期间安装gem来解决这个问题.

I got around it by making the gemspec install the gem during a bundle update or install.

EXTERNAL_DEPENDENCY = Gem::Dependency.new('external-dependency', '~> 0.1')
if File.basename($0) == 'bundle' && ARGV.include?('update') || ARGV.include?('install')
  require 'rubygems/dependency_installer'
  Gem::DependencyInstaller.new.install(EXTERNAL_DEPENDENCY)
end

然后...

spec.add_development_dependency EXTERNAL_DEPENDENCY.name, EXTERNAL_DEPENDENCY.requirements_list

这篇关于在解析gemspec之前,如何安装gem(通过使用gemspec的捆绑程序)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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