【问题标题】:How do I find out all the dependencies of a gem?如何找出 gem 的所有依赖项?
【发布时间】:2014-01-14 07:33:10
【问题描述】:

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

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

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

【问题讨论】:

  • 查看Gemfile.lock,开发依赖查看rubygems.org
  • 但是您如何使用gem dependency [] -R 做到这一点? @Pseeyush Gupta,如果没有答案(几天),我会开始赏金

标签: ruby rubygems


【解决方案1】:

下面的类将递归地获取 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"=>{}}}}

【讨论】:

    【解决方案2】:
    $ 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 dependencyhttp://guides.rubygems.org/command-reference/#gem_dependency

    更新

    根据我在网络上所做的研究,在向我的同事和其他人询问了这个主题后,我得出结论,目前没有办法使用$ gem dependency 来找出完整的递归列表一个 gem 依赖项。

    编写与您在更新问题中指出的类似的红宝石宝石应该不是很复杂。我相信人们可以通过编写一个简单的递归循环并获取 gem 信息来做到这一点,也许是一种理想的格式。

    我想我放弃了这个主题,但是有人可以为其他人(不是我)开始赏金,他们可以想到 “获得递归 gem 依赖项的最佳方法” https://www.gemlou.pe 我觉得不错。

    【讨论】:

    • 谢谢。但正如我所说的 gem 依赖只会给我一个我的 gem 所依赖的 gem 的列表。我还想知道列出的宝石所依赖的宝石。就像 rdoc 依赖于 json。类似于递归依赖搜索。
    • 我不太明白你在问什么,但我也对此感兴趣。 $ gem dependency rdoc -R 列出 json。你能告诉一个 gem 有一个“递归”依赖并且它不会在那里列出吗?
    • 好的,如果我不清楚,对不起。你看,当你搜索 nokogiri 的依赖时,它会显示你的 rdoc。但是对于rdoc,还需要json,这里没有列出来。我想要做的是当我搜索 nokogiri 的依赖项时,我得到了所有的依赖项,即 rdoc 和 json。我希望我现在清楚了。
    • 哦,我现在明白了。让我尝试一下。
    • 是的,我想递归循环是现在要走的路。无论如何,非常感谢您的帮助!
    猜你喜欢
    • 2011-05-23
    • 2012-04-11
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多