Skip to content

Instantly share code, notes, and snippets.

@epitron
Last active August 29, 2015 14:12
Show Gist options
  • Save epitron/ced20a132b9a953f1a7c to your computer and use it in GitHub Desktop.
Save epitron/ced20a132b9a953f1a7c to your computer and use it in GitHub Desktop.
Alias for 'require' which tracks which modules get created and displays them in a tree.
command "req-verbose", "Requires gem(s). No need for quotes! (If the gem isn't installed, it will ask if you want to install it.)" do |*gems|
def tree_to_array(hash, indent=0)
result = []
dent = " " * indent
hash.each do |key,val|
result << dent+key
result += tree_to_array(val, indent+1) if val.any?
end
result
end
def print_module_tree mods
mods = mods.select { |mod| not mod < Exception }
mods = mods.map { |mod| mod.to_s.split("::") }
mod_tree = {}
mods.sort.each { |path| mod_tree.mkdir_p(path) }
results = tree_to_array(mod_tree)
table = Term::Table.new(results, :cols=>3, :vertically => true)
puts table
end
gems = gems.join(' ').gsub(',', '').split(/\s+/)
gems.each do |gem|
begin
before_modules = ObjectSpace.each_object(Module).to_a
if require gem
output.puts "#{text.bright_yellow(gem)} loaded"
loaded_modules = ObjectSpace.each_object(Module).to_a - before_modules
print_module_tree(loaded_modules)
else
output.puts "#{text.bright_white(gem)} already loaded"
end
# rescue LoadError => e
# if gem_installed? gem
# output.puts e.inspect
# else
# output.puts "#{gem.bright_red} not found"
# if prompt("Install the gem?") == "y"
# run "gem-install", gem
# run "req", gem
# end
# end
end # rescue
end # gems
end
alias_command "require", "req-verbose"
alias_command "req", "req-verbose"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment