1require 'rubygems/command'
2require 'rubygems/version_option'
3require 'rubygems/validator'
4require 'rubygems/doctor'
5
6class Gem::Commands::CheckCommand < Gem::Command
7
8  include Gem::VersionOption
9
10  def initialize
11    super 'check', 'Check a gem repository for added or missing files',
12          :alien => true, :doctor => false, :dry_run => false, :gems => true
13
14    add_option('-a', '--[no-]alien',
15               'Report "unmanaged" or rogue files in the',
16               'gem repository') do |value, options|
17      options[:alien] = value
18    end
19
20    add_option('--[no-]doctor',
21               'Clean up uninstalled gems and broken',
22               'specifications') do |value, options|
23      options[:doctor] = value
24    end
25
26    add_option('--[no-]dry-run',
27               'Do not remove files, only report what',
28               'would be removed') do |value, options|
29      options[:dry_run] = value
30    end
31
32    add_option('--[no-]gems',
33               'Check installed gems for problems') do |value, options|
34      options[:gems] = value
35    end
36
37    add_version_option 'check'
38  end
39
40  def check_gems
41    say 'Checking gems...'
42    say
43    gems = get_all_gem_names rescue []
44
45    Gem::Validator.new.alien(gems).sort.each do |key, val|
46      unless val.empty? then
47        say "#{key} has #{val.size} problems"
48        val.each do |error_entry|
49          say "  #{error_entry.path}:"
50          say "    #{error_entry.problem}"
51        end
52      else
53        say "#{key} is error-free" if Gem.configuration.verbose
54      end
55      say
56    end
57  end
58
59  def doctor
60    say 'Checking for files from uninstalled gems...'
61    say
62
63    Gem.path.each do |gem_repo|
64      doctor = Gem::Doctor.new gem_repo, options[:dry_run]
65      doctor.doctor
66    end
67  end
68
69  def execute
70    check_gems if options[:gems]
71    doctor if options[:doctor]
72  end
73
74  def arguments # :nodoc:
75    'GEMNAME       name of gem to check'
76  end
77
78  def defaults_str # :nodoc:
79    '--gems --alien'
80  end
81
82  def usage # :nodoc:
83    "#{program_name} [OPTIONS] [GEMNAME ...]"
84  end
85
86end
87