我一直在尝试查找一个Ruby-gem的依赖关系。我知道gem dependency命令可以让我了解gem的依赖关系。但我想做更多的事情。我还想知道由gem dependency命令生成的那些gems的依赖关系。我的意思是,我想找出所有的gems,直到最后一个,它依赖于我的gem。
任何指针都会受到赞赏。提前感谢。
注:目标是构建类似于https://www.gemlou.pe/的东西。
任何指针都会受到赞赏。提前感谢。
注:目标是构建类似于https://www.gemlou.pe/的东西。
以下类将递归获取宝石依赖项(请注意,这只是概念验证,因此它不做任何花哨的事情,但它是一个很好的起点)。有关文档,请参阅RubyDocs:Gem :: Dependency 和Gem :: Specification
class GemRequirements
def initialize(name, version = nil)
@gem = Gem::Dependency.new(name, version)
end
def dependency_tree
@dependency_tree ||= {}.merge(get_dependency(@gem))
end
private
def get_dependency(gem_dependency)
spec = gem_dependency.matching_specs.first
dep_key = "#{gem_dependency.name} #{spec.version}"
hash = { dep_key => {} }
spec.runtime_dependencies.each do |spec_dependency|
spec_dependency_spec = spec_dependency.matching_specs.first
spec_dep_key = "#{spec_dependency.name} #{spec_dependency_spec.version}"
hash[dep_key][spec_dep_key] = get_dependency(spec_dependency)
end
hash
end
end
您可以在应用程序中编程使用它,也可以从Ruby控制台中使用:
r = GemRequirements.new 'rails'
r.dependency_tree
=> {"rails 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"actionpack 3.2.12"=>
{"actionpack 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"activemodel 3.2.12"=>
{"activemodel 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"builder 3.0.4"=>{"builder 3.0.4"=>{}}}},
"rack-cache 1.2"=>
{"rack-cache 1.2"=>{"rack 1.4.5"=>{"rack 1.4.5"=>{}}}},
"builder 3.0.4"=>{"builder 3.0.4"=>{}},
"rack 1.4.5"=>{"rack 1.4.5"=>{}},
"rack-test 0.6.2"=>
{"rack-test 0.6.2"=>{"rack 1.4.5"=>{"rack 1.4.5"=>{}}}},
"journey 1.0.4"=>{"journey 1.0.4"=>{}},
"sprockets 2.2.2"=>
{"sprockets 2.2.2"=>
{"hike 1.2.1"=>{"hike 1.2.1"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}},
"rack 1.4.5"=>{"rack 1.4.5"=>{}},
"tilt 1.4.1"=>{"tilt 1.4.1"=>{}}}},
"erubis 2.7.0"=>{"erubis 2.7.0"=>{}}}},
"activerecord 3.2.12"=>
{"activerecord 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"activemodel 3.2.12"=>
{"activemodel 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"builder 3.0.4"=>{"builder 3.0.4"=>{}}}},
"arel 3.0.2"=>{"arel 3.0.2"=>{}},
"tzinfo 0.3.37"=>{"tzinfo 0.3.37"=>{}}}},
"activeresource 3.2.12"=>
{"activeresource 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"activemodel 3.2.12"=>
{"activemodel 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"builder 3.0.4"=>{"builder 3.0.4"=>{}}}}}},
"actionmailer 3.2.12"=>
{"actionmailer 3.2.12"=>
{"actionpack 3.2.12"=>
{"actionpack 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"activemodel 3.2.12"=>
{"activemodel 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"builder 3.0.4"=>{"builder 3.0.4"=>{}}}},
"rack-cache 1.2"=>
{"rack-cache 1.2"=>{"rack 1.4.5"=>{"rack 1.4.5"=>{}}}},
"builder 3.0.4"=>{"builder 3.0.4"=>{}},
"rack 1.4.5"=>{"rack 1.4.5"=>{}},
"rack-test 0.6.2"=>
{"rack-test 0.6.2"=>{"rack 1.4.5"=>{"rack 1.4.5"=>{}}}},
"journey 1.0.4"=>{"journey 1.0.4"=>{}},
"sprockets 2.2.2"=>
{"sprockets 2.2.2"=>
{"hike 1.2.1"=>{"hike 1.2.1"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}},
"rack 1.4.5"=>{"rack 1.4.5"=>{}},
"tilt 1.4.1"=>{"tilt 1.4.1"=>{}}}},
"erubis 2.7.0"=>{"erubis 2.7.0"=>{}}}},
"mail 2.4.4"=>
{"mail 2.4.4"=>
{"mime-types 1.21"=>{"mime-types 1.21"=>{}},
"treetop 1.4.12"=>
{"treetop 1.4.12"=>{"polyglot 0.3.3"=>{"polyglot 0.3.3"=>{}}}},
"i18n 0.6.4"=>{"i18n 0.6.4"=>{}}}}}},
"railties 3.2.12"=>
{"railties 3.2.12"=>
{"rake 10.1.0"=>{"rake 10.1.0"=>{}},
"rack-ssl 1.3.3"=>
{"rack-ssl 1.3.3"=>{"rack 1.4.5"=>{"rack 1.4.5"=>{}}}},
"thor 0.18.1"=>{"thor 0.18.1"=>{}},
"rdoc 3.12.2"=>{"rdoc 3.12.2"=>{"json 1.8.1"=>{"json 1.8.1"=>{}}}},
"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"actionpack 3.2.12"=>
{"actionpack 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"activemodel 3.2.12"=>
{"activemodel 3.2.12"=>
{"activesupport 3.2.12"=>
{"activesupport 3.2.12"=>
{"i18n 0.6.4"=>{"i18n 0.6.4"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}}}},
"builder 3.0.4"=>{"builder 3.0.4"=>{}}}},
"rack-cache 1.2"=>
{"rack-cache 1.2"=>{"rack 1.4.5"=>{"rack 1.4.5"=>{}}}},
"builder 3.0.4"=>{"builder 3.0.4"=>{}},
"rack 1.4.5"=>{"rack 1.4.5"=>{}},
"rack-test 0.6.2"=>
{"rack-test 0.6.2"=>{"rack 1.4.5"=>{"rack 1.4.5"=>{}}}},
"journey 1.0.4"=>{"journey 1.0.4"=>{}},
"sprockets 2.2.2"=>
{"sprockets 2.2.2"=>
{"hike 1.2.1"=>{"hike 1.2.1"=>{}},
"multi_json 1.8.2"=>{"multi_json 1.8.2"=>{}},
"rack 1.4.5"=>{"rack 1.4.5"=>{}},
"tilt 1.4.1"=>{"tilt 1.4.1"=>{}}}},
"erubis 2.7.0"=>{"erubis 2.7.0"=>{}}}}}},
"bundler 1.3.5"=>{"bundler 1.3.5"=>{}}}}
$ gem dependency nokogiri -R
Gem nokogiri-1.6.0
hoe (~> 2.16, development)
hoe-bundler (>= 1.1, development)
hoe-debugging (>= 1.0.3, development)
hoe-gemspec (>= 1.0, development)
hoe-git (>= 1.4, development)
mini_portile (~> 0.5.0)
minitest (~> 2.2.2, development)
racc (>= 1.4.6, development)
rake (>= 0.9, development)
rake-compiler (~> 0.8.0, development)
rdoc (~> 3.10, development)
rexical (>= 1.0.5, development)
Used by
haml-4.0.3 (nokogiri (>= 0, development))
mime-types-1.23 (nokogiri (~> 1.5, development))
redcarpet-2.3.0 (nokogiri (>= 0, development))
tilt-1.4.1 (nokogiri (>= 0, development))
在输出中包括反向依赖项
更多关于$ gem dependency
的信息,请参见http://guides.rubygems.org/command-reference/#gem_dependency
更新
根据我在网上所做的研究以及向同事和其他人咨询了这个问题后,我得出的结论是,目前没有办法使用 $ gem dependency
来查找 gem 依赖项的完整递归列表。
编写一个类似于您在更新的问题中指示的 Ruby gem 应该不会很复杂。我相信可以通过编写一个简单的递归循环并获取 gem 信息来完成,可能以一种期望的格式。
我想我放弃这个问题,但是有人可以为其他人(而不是我)开始一个悬赏,以想出“获得递归 gem 依赖项的最佳方法”,除了我认为不错的https://www.gemlou.pe。
$ gem dependency rdoc -R
列出了 json
。你能告诉一个具有“递归”依赖关系的 gem,它不会被列在那里吗? - dachi
gem dependency [] -R
实现这一点呢?@Pseeyush Gupta,如果在(几天)内没有答案,我将开始悬赏。 - Davit