gpt4 book ai didi

ruby - 如何找出 gem 的所有依赖项?

转载 作者:数据小太阳 更新时间:2023-10-29 07:21:01 25 4
gpt4 key购买 nike

我一直在尝试找出 ruby​​-gem 的依赖项。我知道 gem dependency 命令会让我知道 gem 的依赖关系。但我想做的不止于此。我还想知道由 gem dependency 命令生成的那些 gem 的依赖关系。我的意思是,我想找出所有 gem ,直到我的 gem 所依赖的最后一颗 gem 。

任何指针将不胜感激。提前致谢。

注意:我们的想法是构建类似 https://www.gemlou.pe/ 的东西

最佳答案

下面的类将递归地获取 gem 依赖项(请注意它的概念证明,因此它不会做任何花哨的事情,但它是一个很好的起点)。有关文档,请参阅 ruby​​docs:Gem::DependencyGem::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"=>{}}}}

关于ruby - 如何找出 gem 的所有依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21108109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com