Skip to content

Instantly share code, notes, and snippets.

@thypon thypon/Extract Deps
Created Apr 24, 2015

Embed
What would you like to do?
Extract Deps in Ruby
#!/usr/bin/env ruby
require 'rubygems'
require 'gems'
require 'json'
class Package < Struct.new(:name, :language, :version, :hash, :source, :homepage, :depends)
end
class Dependencies < Struct.new(:hostmake, :make, :runtime)
end
class Struct
def to_map
map = Hash.new
self.members.each { |m| map[m] = self[m] }
map
end
def to_json(*a)
to_map.to_json(*a)
end
end
class Traverser
attr_accessor :memo
def initialize
@memo = Hash.new
end
def data(gem)
res = Package.new(gem["name"], "ruby", gem["version"], gem["sha"], gem["source_code_uri"], gem["homepage_uri"], nil)
res.depends = Dependencies.new(
gem['dependencies']['development'].map { |e| e['name'] },
nil,
gem['dependencies']['runtime'].map { |e| e['name'] }
)
res
end
def traverse(name)
gem = Gems.info name
memo[name] = data gem
deps = gem["dependencies"]
#deps["development"].each do |dep|
# p @memo.keys
# unless @memo[dep["name"]]
# gem = Gems.info dep["name"]
# traverse(dep["name"])
# end
#end
deps["runtime"].each do |dep|
p @memo.keys
unless @memo[dep["name"]]
gem = Gems.info dep["name"]
traverse(dep["name"])
end
end
end
end
t = Traverser.new
t.traverse ARGV[0]
puts t.memo.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.