Skip to content

Instantly share code, notes, and snippets.

@lsegal
Created July 7, 2010 04:30
Show Gist options
  • Save lsegal/466308 to your computer and use it in GitHub Desktop.
Save lsegal/466308 to your computer and use it in GitHub Desktop.
require 'json'
require 'open-uri'
class DependencyChecker
def initialize(*gems)
@gems = gems.flatten
@dep_cache = {}
end
def all_dependencies
deps = @gems.flatten
loop do
size = deps.size
deps.dup.each {|dep| deps |= dependencies_for(dep) }
break if size == deps.size
end
deps.sort - @gems
end
private
def dependencies_for(gem)
return @dep_cache[gem] if @dep_cache[gem]
#puts "Checking dependencies for #{gem}"
json = JSON.parse(open("http://rubygems.org/api/v1/gems/#{gem}.json").read)
@dep_cache[gem] = json['dependencies']['runtime'].map {|g| g['name'] }.uniq
end
end
if ARGV.empty?
print "Enter gem names: "
deps = gets.strip.split(/\s+|,/).map {|x| x.empty? ? nil : x }.compact
else
deps = ARGV
end
puts "Recursive dependencies for #{deps.join(', ')}:"
puts DependencyChecker.new(*deps).all_dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment