Reputation: 1010
How can I find out all dependencies of a given Gem in a .rb file without having to rely on system() or similar external calls?
Consider this project:
https://rubygems.org/gems/diamond_shell
It has about 20 - 25 dependencies.
I need to traverse all dependencies, on all the linked dependencies too, and populate an array with all of them.
Edit: Thanks for the answers.
Upvotes: 2
Views: 1006
Reputation: 22375
You can yank the code out of lib/rubygems/commands/dependency_command.rb
. Here's a simple method I made.
require 'rubygems/commands/dependency_command'
def get_dependencies name, local = true
cmd = Gem::Commands::DependencyCommand.new
dependency = cmd.gem_dependency name, nil, nil
specs =
if local
dependency.matching_specs.uniq.sort
else
cmd.fetch_remote_specs(dependency).uniq.sort
end
dependencies = []
specs.each do |spec|
dependencies.concat spec.dependencies.sort_by { |dep| dep.name }.map { |dep| [dep.name, dep.requirement] }
end
dependencies
end
puts get_dependencies('diamond_shell', false)
Upvotes: 3
Reputation: 2711
If you happen to be using Bundler for ruby gem management (http://bundler.io), the bundle install
run generates a Gemfile.lock manifest file with all project gems and their dependencies. An example is below:
sunspot (2.0.0)
pr_geohash (~> 1.0)
rsolr (~> 1.0.7)
sunspot_rails (2.0.0)
nokogiri
sunspot (= 2.0.0)
teaspoon (0.7.8)
phantomjs (>= 1.8.1.1)
railties (>= 3.2.5, < 5)
This file can be easily parsed to get your dependency tree.
Upvotes: 2