Class: Gem::Commands::CleanstaleCommand

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

Instance Method Summary collapse

Constructor Details

#initializeCleanstaleCommand

Returns a new instance of CleanstaleCommand.



4
5
6
# File 'lib/rubygems/commands/cleanstale_command.rb', line 4

def initialize
  super('cleanstale', 'Remove gems without a given access time')
end

Instance Method Details

#argumentsObject



8
9
10
# File 'lib/rubygems/commands/cleanstale_command.rb', line 8

def arguments
  "DAYS        number of days to check, default 30"
end

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubygems/commands/cleanstale_command.rb', line 16

def execute
  days = get_one_optional_argument || '30'
  say "Querying for gems without access from #{days} days..."
  gem_to_atime = {}
  Gem::Specification.each do |spec|
    key = { :name => spec.name, :version => spec.version }
    Dir["#{spec.full_gem_path}/**/*.*"].each do |file|
      next if File.directory?(file)
      stat = File.stat(file)
      gem_to_atime[key] ||= stat.atime
      gem_to_atime[key] = stat.atime if gem_to_atime[key] < stat.atime
    end
  end

  gem_to_atime.sort_by { |_, atime| atime }.each do |spec, atime|
    break unless (Time.now.to_i-atime.to_i) > 3600 * 24 * days.to_i
    say "#{spec[:name]} #{spec[:version]} last access was on #{atime.strftime '%d/%m/%Y at %H:%M'}"
    cmd = "gem uninstall #{spec[:name]} -v #{spec[:version]}"
    say cmd
    system cmd
  end
end

#usageObject

:nodoc:



12
13
14
# File 'lib/rubygems/commands/cleanstale_command.rb', line 12

def usage # :nodoc:
  "#{program_name} [DAYS]"
end