Class: Gem::Commands::ManCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems_plugin.rb

Constant Summary collapse

MAN_DIR =
"/usr/share/man/"

Instance Method Summary collapse

Constructor Details

#initializeManCommand

Returns a new instance of ManCommand.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubygems_plugin.rb', line 7

def initialize
  super 'man', 'Manage man-files bundled with gems'
  options[:action] = :install
  
  add_option('-v', '--view', "Views the manual files included in",
                             "the gem.") do |value, options|
    options[:action] = :view
  end
  
  add_option('-i', '--install', "Installs all the gem's manual files globally") do |value, options|
    options[:action] = :install
  end
  
  add_option('-r', '--remove', "Removes the gem's manual files globally") do |value, options|
    options[:action] = :remove
  end
end

Instance Method Details

#add_watermark(text) ⇒ Object

Watermarks some groff text, so you can tell it’s been rubygem’d



106
107
108
# File 'lib/rubygems_plugin.rb', line 106

def add_watermark(text)
  "#{watermark}#{text}"
end

#dispatch(file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/rubygems_plugin.rb', line 49

def dispatch(file)
  case options[:action]
  when :install
    install(file)
  when :view
    view(file)
  when :remove
    remove(file)
  end
end

#executeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubygems_plugin.rb', line 25

def execute
  if Gem.win_platform? || !has_man?
    alert_error "You must have the 'man' command to use this extension."
    return
  end
  
  get_all_gem_names.each do |name|
    path = get_path name, options[:version]
    if path then
      man_path = File.join path, 'man'
      if File.exist?(man_path) && File.directory?(man_path) then
        Dir[File.join(man_path, "**")].each do |man_file|
          dispatch(man_file)
        end
      else
        alert_error "Gem '#{name}' does not appear to have packaged man files."
      end
    else
      alert_error "Gem '#{name}' not installed."
    end
  end
end

#get_path(gemname, version_req) ⇒ Object

Return the full path to the cached gem file matching the given name and version requirement. Returns ‘nil’ if no match.

Example:

get_path('rake', '> 0.4')   # -> '/usr/lib/ruby/gems/1.8/cache/rake-0.4.2.gem'
get_path('rake', '< 0.1')   # -> nil
get_path('rak')             # -> nil (exact name required)

– TODO: This should be refactored so that it’s a general service. I don’t think any of our existing classes are the right place though. Just maybe ‘Cache’?

TODO: It just uses Gem.dir for now. What’s an easy way to get the list of source directories?



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rubygems_plugin.rb', line 137

def get_path(gemname, version_req)
  return gemname if gemname =~ /\.gem$/i

  specs = Gem::source_index.find_name gemname, version_req

  selected = specs.sort_by { |s| s.version }.last

  return nil if selected.nil?

  # We expect to find (basename).gem in the 'cache' directory.
  # Furthermore, the name match must be exact (ignoring case).
  if gemname =~ /^#{selected.name}$/i
    filename = selected.full_name
    path = nil

    Gem.path.find do |gem_dir|
      path = File.join gem_dir, 'gems', filename
      File.exist? path
    end

    path
  else
    nil
  end
end

#has_man?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/rubygems_plugin.rb', line 163

def has_man?
  system("man 1>/dev/null 2>&1")
end

#install(source) ⇒ Object

Installs the given file into the appropriate man directory.



71
72
73
74
75
76
77
78
# File 'lib/rubygems_plugin.rb', line 71

def install(source)
  full_name = File.split(source).last
  section = full_name.split(".").last
  destination = File.join MAN_DIR, "man#{section}", full_name
  File.open(destination, "wb") do |out|
    out << watermark_file(source)
  end
end

#is_watermarked?(text) ⇒ Boolean

Is the given text watermarked?

Returns:

  • (Boolean)


118
119
120
# File 'lib/rubygems_plugin.rb', line 118

def is_watermarked?(text)
  text[0..(watermark.size-1)] == watermark
end

#is_watermarked_file?(file) ⇒ Boolean

Watermarks a man page. Assumes input file is not already compressed.

Returns:

  • (Boolean)


112
113
114
# File 'lib/rubygems_plugin.rb', line 112

def is_watermarked_file?(file)
  is_watermarked?(File.read(file))
end

#remove(source) ⇒ Object

Installs the given file into the appropriate man directory.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubygems_plugin.rb', line 82

def remove(source)
  full_name = File.split(source).last
  section = full_name.split(".").last
  destination = File.join MAN_DIR, "man#{section}", full_name
  if is_watermarked_file?(destination)
    FileUtils.unlink(destination)
  else
    alert_error "The man file at #{destination} was not installed by Rubygems. It has "+
                "not been deleted."
  end
end

#view(source) ⇒ Object

Views the man file in the man program



65
66
67
# File 'lib/rubygems_plugin.rb', line 65

def view(source)
  system("man #{source}")
end

#watermarkObject



94
95
96
# File 'lib/rubygems_plugin.rb', line 94

def watermark
  %Q{.\\" Installed by Rubygems' Man Extension\n}
end

#watermark_file(file) ⇒ Object

Watermarks a man page. Assumes input file is not already compressed.



100
101
102
# File 'lib/rubygems_plugin.rb', line 100

def watermark_file(file)
  add_watermark(File.read(file))
end