Class: Gem::Commands::ReadmeCommand

Inherits:
Gem::Command
  • Object
show all
Includes:
VersionOption
Defined in:
lib/gem_readme/readme_command.rb

Constant Summary collapse

OPTIONS =
{
  :version => Gem::Requirement.default,
  :editor => 'less'
}

Instance Method Summary collapse

Constructor Details

#initializeReadmeCommand

Returns a new instance of ReadmeCommand.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gem_readme/readme_command.rb', line 13

def initialize
  super 'readme', 'Open a README of an installed gem', OPTIONS

  add_version_option

  add_option('-e', '--editor EDITOR', String,
             'The editor to use to open the gems',
             "Default: #{OPTIONS[:editor]}") do |editor, options|
    options[:editor] = editor
  end
end

Instance Method Details

#argumentsObject

:nodoc:



25
26
27
# File 'lib/gem_readme/readme_command.rb', line 25

def arguments # :nodoc:
  "GEMNAME       name of gem to open README"
end

#defaults_strObject

:nodoc:



29
30
31
# File 'lib/gem_readme/readme_command.rb', line 29

def defaults_str # :nodoc:
  "--version '#{OPTIONS[:version]}' --editor #{OPTIONS[:editor]}"
end

#executeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gem_readme/readme_command.rb', line 37

def execute
  version = options[:version] || OPTIONS[:version]

  gem_specs = get_all_gem_names.map { |gem_name| Gem.source_index.find_name(gem_name, version).last }.compact

  if gem_specs.size > 0
    paths = gem_specs.map { |spec| spec.full_gem_path }

    readmes = paths.inject([]) do |result, path|
      result + Dir[File.join(path, '*')].select{ |i| File.basename(i) =~ /^readme/i }
    end

    if readmes.empty?
      say "README not found!"
      exit!
    end

    cmd = "#{options[:editor]} #{readmes.join(' ')}"
    exec cmd unless options[:dryrun]
  else
    say "No gems found for #{get_all_gem_names.join(', ')}"
    raise Gem::SystemExitException, 1
  end
end

#usageObject

:nodoc:



33
34
35
# File 'lib/gem_readme/readme_command.rb', line 33

def usage # :nodoc:
  "#{program_name} [options] GEMNAME [GEMNAME ...]"
end