Class: Gem::Commands::RemoveStaleCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/remove_stale_command.rb

Instance Method Summary collapse

Constructor Details

#initializeRemoveStaleCommand

Returns a new instance of RemoveStaleCommand.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rubygems/commands/remove_stale_command.rb', line 4

def initialize
  super 'remove_stale', 'Remove gems for which last use time is too old'

  add_option('-y', '--yes', 'Don\'t ask for confirmation') do |value, options|
    options[:yes] = true
  end

  add_option('-d', '--days DAYS', 'Consider stale after no access for this number of days (Default: 40)') do |value, options|
    options[:days] = value.to_i
  end
end

Instance Method Details

#executeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubygems/commands/remove_stale_command.rb', line 28

def execute
  days = options[:days] || 40
  border_time = Time.now - days  * (24 * 60 * 60)

  stale_gems = Gem::Specification.select do |spec|
    stale_gem?(spec, border_time)
  end

  to_uninstall = stale_gems.select do |spec|
    spec.dependent_gems.all? do |dependent, depency, satlist|
      stale_gems.include?(dependent) || (satlist - stale_gems).length > 0
    end
  end

  if to_uninstall.empty?
    puts 'No stale gems found.'
  else
    puts 'Stale gems:'
    to_uninstall.group_by(&:name).sort.each do |name, specs|
      all_versions = Gem::Specification.find_all_by_name(name).map(&:version)
      versions_to_uninstall = specs.map(&:version).sort
      versions_to_leave = all_versions - versions_to_uninstall
      line = "  #{name}: #{versions_to_uninstall.join(', ')}"
      unless versions_to_leave.empty?
        line << " (leaving versions: #{versions_to_leave.join(', ')})"
      end
      puts line
    end
    if options[:yes] || ask_yes_no('Remove gems?')
      to_uninstall.each do |spec|
        say "Attempting to uninstall #{spec.full_name}"
        begin
          Gem::Uninstaller.new(spec.name, {
            :version => "= #{spec.version}",
            :executables => true,
            :ignore => true
          }).uninstall
        rescue Gem::DependencyRemovalException, Gem::InstallError, Gem::GemNotInHomeException => e
          say "Unable to uninstall #{spec.full_name}:"
          say "  #{e.class}: #{e.message}"
        end
      end
    end
  end
end

#gem_atime(spec) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rubygems/commands/remove_stale_command.rb', line 16

def gem_atime(spec)
  Dir["#{spec.full_gem_path}/**/*.*"].reject do |path|
    File.directory?(path)
  end.map do |path|
    File.stat(path).atime
  end.max
end

#stale_gem?(spec, border_time) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rubygems/commands/remove_stale_command.rb', line 24

def stale_gem?(spec, border_time)
  (atime = gem_atime(spec)) && (atime < border_time)
end