Class: Gem::Commands::ReadCommand

Inherits:
Gem::Command
  • Object
show all
Includes:
VersionOption, OpenGem::CommonOptions
Defined in:
lib/rubygems/commands/read_command.rb

Overview

ReadCommand will open a gem’s rdoc

Instance Method Summary collapse

Methods included from OpenGem::CommonOptions

#add_command_option, #add_exact_match_option, #add_latest_version_option, #get_spec

Constructor Details

#initializeReadCommand

Returns a new instance of ReadCommand.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rubygems/commands/read_command.rb', line 7

def initialize
  super 'read', "Opens the gem's documentation",
    :command => nil,
    :version=>  Gem::Requirement.default,
    :latest=>   false

  add_command_option "Application to read rdoc with"
  add_latest_version_option
  add_version_option
  add_exact_match_option
end

Instance Method Details

#argumentsObject

:nodoc:



19
20
21
# File 'lib/rubygems/commands/read_command.rb', line 19

def arguments # :nodoc:
  "GEMNAME       gem to read"
end

#executeObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubygems/commands/read_command.rb', line 23

def execute
  name = get_one_gem_name
  spec = get_spec(name){|s| s.has_rdoc? }
  if spec && path = get_path(spec)
    if File.exists? path
      read_gem path
    elsif ask_yes_no "The rdoc seems to be missing, would you like to generate one?", true
      generate_rdoc spec
      read_gem path
    end
  end
end

#generate_rdoc(spec) ⇒ Object



40
41
42
# File 'lib/rubygems/commands/read_command.rb', line 40

def generate_rdoc spec
  Gem::DocManager.new(spec).generate_rdoc
end

#get_path(spec) ⇒ Object



36
37
38
# File 'lib/rubygems/commands/read_command.rb', line 36

def get_path(spec)
  File.join(spec.installation_path, "doc", spec.full_name, 'rdoc','index.html')
end

#read_gem(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubygems/commands/read_command.rb', line 44

def read_gem(path)
  if options[:command]
    command_parts = Shellwords.shellwords(options[:command])
    command_parts << path
    success = system(*command_parts)
    if !success
      raise Gem::CommandLineError, "Could not run '#{rdoc_reader} #{path}', exit code: #{$?.exitstatus}"
    end
  else
    Launchy.open("file://"+path)
  end
end