Class: GemsDepLevel

Inherits:
Object
  • Object
show all
Defined in:
lib/gems_deplevel.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir = nil, debug: false) ⇒ GemsDepLevel

Returns a new instance of GemsDepLevel.



14
15
16
# File 'lib/gems_deplevel.rb', line 14

def initialize(dir=nil, debug: false)
  @dir, @debug = dir, debug
end

Instance Method Details

#dep_level(name, gemlist = gem_deps(name)) ⇒ Object

Returns the dependency depth level for a gem’s transitive dependencies

  • gemlist contains the a list gem names, each containing the gem dependencies

    - use gem_deps() to make the gemlist or scan_gems() if using a
      local gem_src directory
    

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gems_deplevel.rb', line 27

def dep_level(name, gemlist=gem_deps(name))

  raise GemDepLevelErr, 'supply the name of a gem' unless name

  gemtally = Hash.new(0)
  scan(name, gemlist, gemtally)

  gemtally.sort_by(&:last).group_by(&:last).map do |key, value|
    [key, value.map(&:first)]
  end.to_h

end

#gem_deps(name) ⇒ Object

returns a Hash object of transitive gem dependencies for a specific gem

name => [gem1, gem2, …], => [gem1.1, gem1.2]


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gems_deplevel.rb', line 43

def gem_deps(name)

  return unless name

  puts 'name: ' + name.inspect if @debug
  gem = Gems.info name

  a = gem['dependencies']['runtime'].map {|x| x['name']}
  h = {name => a}

  a.inject(h) do |r,x|
    puts 'r.keys: ' + r.keys.inspect if @debug
    r.keys.include?(x) ? r : r.merge!(gem_deps(x))
  end

end

#scan_gems(dir = @dir) ⇒ Object

Returns the dependencies for each gem as a Hash object

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gems_deplevel.rb', line 63

def scan_gems(dir=@dir)

  raise GemDepLevelErr, 'please provde a directory path' unless dir

  dtx = DirToXML.new dir
  names = dtx.directories

  gems = names.map do |name|

    filename = "#{name}/#{name}.gemspec"
    gemspec = File.join(dir, filename)

    if File.exists? gemspec then

      s = File.read gemspec
      dependencies = s.scan(/add_runtime_dependency\(['"]([^'"]+)/)
                            .map(&:first)

      [name, dependencies]

    else
      nil
    end

  end

  return Hash[gems.compact]

end