forked from adamsanderson/open_gem
/
read_command.rb
executable file
·60 lines (51 loc) · 1.57 KB
/
read_command.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# ReadCommand will open a gem's rdoc
class Gem::Commands::ReadCommand < Gem::Command
include OpenGem::CommonOptions
include Gem::VersionOption
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
end
def arguments # :nodoc:
"GEMNAME gem to read"
end
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
def get_path(spec)
File.join(spec.installation_path, "doc", spec.full_name, 'rdoc','index.html')
end
def generate_rdoc spec
Gem::DocManager.new(spec).generate_rdoc
end
def rdoc_reader
options[:command] || case RUBY_PLATFORM.downcase
when /darwin/ then 'open'
when /mswin/ then 'explorer'
when /linux/ then 'firefox'
else 'firefox' # Come on, if you write ruby, you probably have firefox installed ;)
end
end
def read_gem(path)
command_parts = Shellwords.shellwords(rdoc_reader)
command_parts << path
success = system(*command_parts)
if !success
raise Gem::CommandLineError, "Could not run '#{rdoc_reader} #{path}', exit code: #{$?.exitstatus}"
end
end
end