我如何找出宝石的所有依赖关系? [英] How do I find out all the dependencies of a gem?

查看:110
本文介绍了我如何找出宝石的所有依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出ruby-gem的依赖关系。我知道gem dependency命令会让我知道gem的依赖关系。但我想做的比这更多。我也想知道由gem dependency命令生成的那些gem的依赖关系。我的意思是,我想找出所有的宝石,直到最后一块,我的宝石取决于它。

任何指针都会被赞赏。提前致谢。



注意:这个想法是建立一些像 https://www.gemlou.pe/

解决方案

以下类将递归地获取gem依赖关系(注意其证明的概念,所以它不会做任何幻想,但它是一个很好的起点)。有关文档,请参阅rubydocs: Gem :: Dependency Gem :: Specification

  class GemRequirements 
def初始化(name,version = nil)
@gem = Gem :: Dependency.new(name,版本)
结束

def dependency_tree
@dependency_tree || = {} .merge(get_dependency(@gem))
结束

私人

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

你可以使用它编程方式在您的应用程序或从红宝石控制台:

  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 - 测试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 - 测试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 - 测试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=> {}}}}


I have been trying to find out dependencies for a ruby-gem. I know that gem dependency command will let me know about the dependencies of the gem. But I want to do more than that. I also want to know the dependencies of those gems generated by gem dependency command. I mean, I want to find out all the gems till the last one, on which my gem is depending.

Any pointers will be appreciated. Thanks in advance.

Note: The idea is to build something like https://www.gemlou.pe/

解决方案

following class will recursively get gem dependencies (note its proof of concept so it does not do anything fancy, but its a good starting point). for documentation please refer rubydocs: Gem::Dependency and 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

you can use it programatically in your app or from ruby console:

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"=>{}}}}

这篇关于我如何找出宝石的所有依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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