Class: RDocF95::RI::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc-f95/ri/driver.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Driver

Returns a new instance of Driver.


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rdoc-f95/ri/driver.rb', line 203

def initialize(options)
  @names = options[:names]

  @class_cache_name = 'classes'
  @all_dirs = RDocF95::RI::Paths.path(true, true, true, true)
  @homepath = RDocF95::RI::Paths.raw_path(false, false, true, false).first
  @homepath = @homepath.sub(/\.rdoc/, '.ri')
  @sys_dirs = RDocF95::RI::Paths.raw_path(true, false, false, false)

  FileUtils.mkdir_p cache_file_path unless File.directory? cache_file_path

  @class_cache = nil

  @display = RDocF95::RI::DefaultDisplay.new(options[:formatter],
                                          options[:width],
                                          options[:use_stdout])
end

Class Method Details

.process_args(argv) ⇒ Object


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rdoc-f95/ri/driver.rb', line 14

def self.process_args(argv)
  options = {}
  options[:use_stdout] = !$stdout.tty?
  options[:width] = 72
  options[:formatter] = RDocF95::RI::Formatter.for 'plain'
  options[:list_classes] = false
  options[:list_names] = false

  # By default all paths are used.  If any of these are true, only those
  # directories are used.
  use_system = false
  use_site = false
  use_home = false
  use_gems = false
  doc_dirs = []

  opts = OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = RDocF95::VERSION
    opt.summary_indent = ' ' * 4

    directories = [
      RDocF95::RI::Paths::SYSDIR,
      RDocF95::RI::Paths::SITEDIR,
      RDocF95::RI::Paths::HOMEDIR
    ]

    if RDocF95::RI::Paths::GEMDIRS then
      Gem.path.each do |dir|
        directories << "#{dir}/doc/*/ri"
      end
    end

    opt.banner = <<-EOT
Usage: #{opt.program_name} [options] [names...]

Where name can be:

Class | Class::method | Class#method | Class.method | method

All class names may be abbreviated to their minimum unambiguous form. If a name
is ambiguous, all valid options will be listed.

The form '.' method matches either class or instance methods, while #method
matches only instance and ::method matches only class methods.

For example:

  #{opt.program_name} Fil
  #{opt.program_name} File
  #{opt.program_name} File.new
  #{opt.program_name} zip

Note that shell quoting may be required for method names containing
punctuation:

  #{opt.program_name} 'Array.[]'
  #{opt.program_name} compact\\!

By default ri searches for documentation in the following directories:

  #{directories.join "\n    "}

Specifying the --system, --site, --home, --gems or --doc-dir options will
limit ri to searching only the specified directories.

Options may also be set in the 'RI' environment variable.
    EOT

    opt.separator nil
    opt.separator "Options:"
    opt.separator nil

    opt.on("--classes", "-c",
           "Display the names of classes and modules we",
           "know about.") do |value|
      options[:list_classes] = value
    end

    opt.separator nil

    opt.on("--doc-dir=DIRNAME", "-d", Array,
           "List of directories to search for",
           "documentation. If not specified, we search",
           "the standard rdoc/ri directories. May be",
           "repeated.") do |value|
      value.each do |dir|
        unless File.directory? dir then
          raise OptionParser::InvalidArgument, "#{dir} is not a directory"
        end
      end

      doc_dirs.concat value
    end

    opt.separator nil

    opt.on("--fmt=FORMAT", "--format=FORMAT", "-f",
           RDocF95::RI::Formatter::FORMATTERS.keys,
           "Format to use when displaying output:",
           "   #{RDocF95::RI::Formatter.list}",
           "Use 'bs' (backspace) with most pager",
           "programs. To use ANSI, either disable the",
           "pager or tell the pager to allow control",
           "characters.") do |value|
      options[:formatter] = RDocF95::RI::Formatter.for value
    end

    opt.separator nil

    unless RDocF95::RI::Paths::GEMDIRS.empty? then
      opt.on("--[no-]gems",
             "Include documentation from RubyGems.") do |value|
        use_gems = value
      end
    end

    opt.separator nil

    opt.on("--[no-]home",
           "Include documentation stored in ~/.rdoc.") do |value|
      use_home = value
    end

    opt.separator nil

    opt.on("--[no-]list-names", "-l",
           "List all the names known to RDoc, one per",
           "line.") do |value|
      options[:list_names] = value
    end

    opt.separator nil

    opt.on("--no-pager", "-T",
           "Send output directly to stdout.") do |value|
      options[:use_stdout] = !value
    end

    opt.separator nil

    opt.on("--[no-]site",
           "Include documentation from libraries",
           "installed in site_lib.") do |value|
      use_site = value
    end

    opt.separator nil

    opt.on("--[no-]system",
           "Include documentation from Ruby's standard",
           "library.") do |value|
      use_system = value
    end

    opt.separator nil

    opt.on("--width=WIDTH", "-w", OptionParser::DecimalInteger,
           "Set the width of the output.") do |value|
      options[:width] = value
    end
  end

  argv = ENV['RI'].to_s.split.concat argv

  opts.parse! argv

  options[:names] = argv

  options[:path] = RDocF95::RI::Paths.path(use_system, use_site, use_home,
                                        use_gems, *doc_dirs)
  options[:raw_path] = RDocF95::RI::Paths.raw_path(use_system, use_site,
                                                use_home, use_gems, *doc_dirs)

  options

rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => e
  puts opts
  puts
  puts e
  exit 1
end

.run(argv = ARGV) ⇒ Object


197
198
199
200
201
# File 'lib/rdoc-f95/ri/driver.rb', line 197

def self.run(argv = ARGV)
  options = process_args argv
  ri = new options
  ri.run
end

Instance Method Details

#cache_file_for(klassname) ⇒ Object


251
252
253
# File 'lib/rdoc-f95/ri/driver.rb', line 251

def cache_file_for(klassname)
  File.join cache_file_path, klassname.gsub(/:+/, "-")
end

#cache_file_pathObject


255
256
257
# File 'lib/rdoc-f95/ri/driver.rb', line 255

def cache_file_path
  File.join @homepath, 'cache'
end

#class_cacheObject


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rdoc-f95/ri/driver.rb', line 221

def class_cache
  return @class_cache if @class_cache

  newest = map_dirs('created.rid', :all) do |f|
    File.mtime f if test ?f, f
  end.max

  up_to_date = (File.exist?(class_cache_file_path) and
                newest < File.mtime(class_cache_file_path))

  @class_cache = if up_to_date then
                   load_cache_for @class_cache_name
                 else
                   class_cache = {}

                   classes = map_dirs('**/cdesc*.yaml', :sys) { |f| Dir[f] }
                   populate_class_cache class_cache, classes

                   classes = map_dirs('**/cdesc*.yaml') { |f| Dir[f] }
                   warn "Updating class cache with #{classes.size} classes..."

                   populate_class_cache class_cache, classes, true
                   write_cache class_cache, class_cache_file_path
                 end
end

#class_cache_file_pathObject


247
248
249
# File 'lib/rdoc-f95/ri/driver.rb', line 247

def class_cache_file_path
  File.join cache_file_path, @class_cache_name
end

#display_class(name) ⇒ Object


259
260
261
262
# File 'lib/rdoc-f95/ri/driver.rb', line 259

def display_class(name)
  klass = class_cache[name]
  @display.display_class_info klass, class_cache
end

#load_cache_for(klassname) ⇒ Object


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rdoc-f95/ri/driver.rb', line 264

def load_cache_for(klassname)
  path = cache_file_for klassname

  if File.exist? path and
     File.mtime(path) >= File.mtime(class_cache_file_path) then
    File.open path, 'rb' do |fp|
      Marshal.load fp.read
    end
  else
    class_cache = nil

    File.open class_cache_file_path, 'rb' do |fp|
      class_cache = Marshal.load fp.read
    end

    klass = class_cache[klassname]
    return nil unless klass

    method_files = klass["sources"]
    cache = {}

    sys_dir = @sys_dirs.first
    method_files.each do |f|
      system_file = f.index(sys_dir) == 0
      Dir[File.join(File.dirname(f), "*")].each do |yaml|
        next unless yaml =~ /yaml$/
        next if yaml =~ /cdesc-[^\/]+yaml$/
        method = read_yaml yaml
        name = method["full_name"]
        ext_path = f
        ext_path = "gem #{$1}" if f =~ %r%gems/[\d.]+/doc/([^/]+)%
        method["source_path"] = ext_path unless system_file
        cache[name] = method
      end
    end

    write_cache cache, path
  end
end

#map_dirs(file_name, system = false) ⇒ Object


304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/rdoc-f95/ri/driver.rb', line 304

def map_dirs(file_name, system=false)
  dirs = if system == :all then
           @all_dirs
         else
           if system then
             @sys_dirs
           else
             @all_dirs - @sys_dirs
           end
         end

  dirs.map { |dir| yield File.join(dir, file_name) }.flatten.compact
end

#populate_class_cache(class_cache, classes, extension = false) ⇒ Object


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/rdoc-f95/ri/driver.rb', line 318

def populate_class_cache(class_cache, classes, extension = false)
  classes.each do |cdesc|
    desc = read_yaml cdesc
    klassname = desc["full_name"]

    unless class_cache.has_key? klassname then
      desc["display_name"] = "Class"
      desc["sources"] = [cdesc]
      desc["instance_method_extensions"] = []
      desc["class_method_extensions"] = []
      class_cache[klassname] = desc
    else
      klass = class_cache[klassname]

      if extension then
        desc["instance_method_extensions"] = desc.delete "instance_methods"
        desc["class_method_extensions"] = desc.delete "class_methods"
      end

      klass.merge_enums desc
      klass["sources"] << cdesc
    end
  end
end

#read_yaml(path) ⇒ Object


343
344
345
# File 'lib/rdoc-f95/ri/driver.rb', line 343

def read_yaml(path)
  YAML.load File.read(path).gsub(/ \!ruby\/(object|struct):(RDocF95::RI|RI|SM).*/, '')
end

#runObject


347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rdoc-f95/ri/driver.rb', line 347

def run
  if @names.empty? then
    @display.list_known_classes class_cache.keys.sort
  else
    @names.each do |name|
      case name
      when /::|\#|\./ then
        if class_cache.key? name then
          display_class name
        else
          meth = nil

          parts = name.split(/::|\#|\./)
          meth = parts.pop unless parts.last =~ /^[A-Z]/
          klass = parts.join '::'

          cache = load_cache_for klass
          # HACK Does not support F.n
          abort "Nothing known about #{name}" unless cache
          method = cache[name.gsub(/\./, '#')]
          abort "Nothing known about #{name}" unless method
          @display.display_method_info method
        end
      else
        if class_cache.key? name then
          display_class name
        else
          methods = select_methods(/^#{name}/)
          if methods.size == 0
            abort "Nothing known about #{name}"
          elsif methods.size == 1
            @display.display_method_info methods.first
          else
            @display.display_method_list methods
          end
        end
      end
    end
  end
end

#select_methods(pattern) ⇒ Object


388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/rdoc-f95/ri/driver.rb', line 388

def select_methods(pattern)
  methods = []
  class_cache.keys.sort.each do |klass|
    class_cache[klass]["instance_methods"].map{|h|h["name"]}.grep(pattern) do |name|
      method = load_cache_for(klass)[klass+'#'+name]
      methods << method if method
    end
    class_cache[klass]["class_methods"].map{|h|h["name"]}.grep(pattern) do |name|
      method = load_cache_for(klass)[klass+'::'+name]
      methods << method if method
    end
  end
  methods
end

#write_cache(cache, path) ⇒ Object


403
404
405
406
407
408
409
# File 'lib/rdoc-f95/ri/driver.rb', line 403

def write_cache(cache, path)
  File.open path, "wb" do |cache_file|
    Marshal.dump cache, cache_file
  end

  cache
end