Rubygems:指定对最新版本的gem的依赖 [英] Rubygems: specify dependency on the latest version of a gem
问题描述
我正在开发一个取决于另一个gem B的gem A.我需要我的gem用户拥有最新版本的B,不管它是什么。所以我想在我的gemspec中说一些类似于
spec.add_dependencyB,:latest
code>
原因是B包含一个数据库驱动程序,它需要使用最新版本,否则数据库会拒绝连接。 / p>
换句话说,我需要在使用我的gem之前强制 gem update
。有没有办法如何在gemspec中做到这一点?
我知道我可以指定版本大于或小于某个固定版本,但这不是我想要的。
另外我可以从git中获取宝石代码 - 但从master分支中获取它通常与将最新的stable版本推送到rubygems不同。 / p>
这样的事情如何:
require'json'
require'uri'
require'open-uri'
nokogiri_uri = URI('https://rubygems.org/api/v1 /gems/nokogiri.json')
version = nil
open(nokogiri_uri)do | f |
json_str = f.read
puts json_str
$ b $ hash = JSON.parse json_str
version = hash ['version']
puts version
结束
如果版本
spec.add_dependencynokogiri,版本
else
puts我的宝石将无法工作,因为它的依赖关系是错误的。
$ b - 输出: -
{name:nokogiri,downloads:33177312,version:1.6.6.2,...}
1.6.6.2
我用 open-uri
而不是 net / http
库,因为文章如何解决Net :: HTTP的危险默认HTTPS行为。我不知道在最新版本的ruby中是否解决了这个问题。
I'm developing a gem A that depends on another gem B. I need the user of my gem to have the latest version of B whatever it is. So I would like to say in my gemspec something like
spec.add_dependency "B", :latest
The reason for this is that B contains a database driver that needs to be in its latest version otherwise the database rejects the connection.
So in other words I need to force gem update
before my gem is used. Is there a way how to do this in gemspec?
I know I can specify the version to be greater or smaller then some fixed version, but that's not what I want.
Also I can take the gem code from git - but taking it from master branch usually isn't the same as taking the latest "stable" version pushed to rubygems.
How about something like this:
require 'json'
require 'uri'
require 'open-uri'
nokogiri_uri = URI('https://rubygems.org/api/v1/gems/nokogiri.json')
version = nil
open(nokogiri_uri) do |f|
json_str = f.read
puts json_str
hash = JSON.parse json_str
version = hash['version']
puts version
end
if version
spec.add_dependency "nokogiri", version
else
puts "My gem won't work because its dependencies are buggered."
--output:--
{"name":"nokogiri","downloads":33177312,"version":"1.6.6.2", ... }
1.6.6.2
I used open-uri
instead of the net/http
library because of the article How to Cure Net::HTTP’s Risky Default HTTPS Behavior. I don't know if that concern has been addressed in the latest versions of ruby.
这篇关于Rubygems:指定对最新版本的gem的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!