38: def execute
39: say "Cleaning up installed gems..."
40: primary_gems = {}
41:
42: Gem.source_index.each do |name, spec|
43: if primary_gems[spec.name].nil? or
44: primary_gems[spec.name].version < spec.version then
45: primary_gems[spec.name] = spec
46: end
47: end
48:
49: gems_to_cleanup = []
50:
51: unless options[:args].empty? then
52: options[:args].each do |gem_name|
53: dep = Gem::Dependency.new gem_name, Gem::Requirement.default
54: specs = Gem.source_index.search dep
55: specs.each do |spec|
56: gems_to_cleanup << spec
57: end
58: end
59: else
60: Gem.source_index.each do |name, spec|
61: gems_to_cleanup << spec
62: end
63: end
64:
65: gems_to_cleanup = gems_to_cleanup.select { |spec|
66: primary_gems[spec.name].version != spec.version
67: }
68:
69: deplist = Gem::DependencyList.new
70: gems_to_cleanup.uniq.each do |spec| deplist.add spec end
71:
72: deps = deplist.strongly_connected_components.flatten.reverse
73:
74: deps.each do |spec|
75: if options[:dryrun] then
76: say "Dry Run Mode: Would uninstall #{spec.full_name}"
77: else
78: say "Attempting to uninstall #{spec.full_name}"
79:
80: options[:args] = [spec.name]
81:
82: uninstall_options = {
83: :executables => false,
84: :version => "= #{spec.version}",
85: }
86:
87: if Gem.user_dir == spec.installation_path then
88: uninstall_options[:install_dir] = spec.installation_path
89: end
90:
91: uninstaller = Gem::Uninstaller.new spec.name, uninstall_options
92:
93: begin
94: uninstaller.uninstall
95: rescue Gem::DependencyRemovalException, Gem::InstallError,
96: Gem::GemNotInHomeException => e
97: say "Unable to uninstall #{spec.full_name}:"
98: say "\t#{e.class}: #{e.message}"
99: end
100: end
101: end
102:
103: say "Clean Up Complete"
104: end