Created
May 27, 2011 06:48
-
-
Save mvz/994764 to your computer and use it in GitHub Desktop.
Improved gem cleanup: ignores Rails, tries to avoid user interaction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'rubygems/user_interaction' | |
require 'rubygems/uninstaller' | |
# New RubyGems? | |
#specs = Gem::Specification.find_all_by_name do |s| | |
# s.name != "rails" | |
#end | |
dep = Gem::Dependency.new //, Gem::Requirement.default | |
specs = Gem.source_index.search dep | |
candidates = specs.inject(Hash.new([])) {|acc, elm| | |
acc[elm.name] += [elm]; acc | |
}.delete_if {|k,v| v.count == 1 or k == "rails"} | |
deplist = Gem::DependencyList.from_source_index Gem.source_index | |
keys = candidates.keys.sort | |
keys.each do |k| | |
v = candidates[k] | |
sorted = v.sort_by {|s| s.version} | |
last = sorted.pop | |
ok = sorted.select {|s| deplist.ok_to_remove?(s.full_name)} | |
puts "Found: #{k}; latest: #{last.version}; #{sorted.count} other version(s). #{ok.count} OK to remove." | |
ok.each do |s| | |
puts "Attempting uninstall of #{s.full_name}" | |
opts = { | |
:executables => false, | |
:version => "= #{s.version}", | |
} | |
uninstaller = Gem::Uninstaller.new s.name, opts | |
uninstaller.uninstall | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment