Module: RDoc::PuppetParserCore

Included in:
PuppetParserRDoc1, PuppetParserRDoc2
Defined in:
lib/puppet/util/rdoc/parser/puppet_parser_core.rb

Overview

Functionality common to both our RDoc version 1 and 2 parsers.

Constant Summary collapse

SITE =
"__site__"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 6

def self.included(base)
  base.class_eval do
    attr_accessor :input_file_name, :top_level

    # parser registration into RDoc
    parse_files_matching(/\.(rb|pp)$/)
  end
end

Instance Method Details

#create_rdoc_preprocessObject

New instance of the appropriate PreProcess for our RDoc version.

Raises:

  • (NotImplementedError)


432
433
434
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 432

def create_rdoc_preprocess
  raise(NotImplementedError, "This method must be overwritten for whichever version of RDoc this parser is working with")
end

#document_class(name, klass, container) ⇒ Object

create documentation for a class named name



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 228

def document_class(name, klass, container)
  Puppet.debug "rdoc: found new class #{name}"
  container, name = get_class_or_module(container, name)

  superclass = klass.parent
  superclass = "" if superclass.nil? or superclass.empty?

  comment = klass.doc
  look_for_directives_in(container, comment) unless comment.empty?
  cls = container.add_class(RDoc::PuppetClass, name, superclass)
  # it is possible we already encountered this class, while parsing some namespaces
  # from other classes of other files. But at that time we couldn't know this class superclass
  # so, now we know it and force it.
  cls.superclass = superclass
  cls.record_location(@top_level)

  # scan class code for include
  code = klass.code.children if klass.code.is_a?(Puppet::Parser::AST::BlockExpression)
  code ||= klass.code
  unless code.nil?
    scan_for_include_or_require(cls, code)
    scan_for_realize(cls, code)
    scan_for_resource(cls, code) if Puppet.settings[:document_all]
  end

  cls.add_comment(comment, klass.file)
rescue => detail
  raise Puppet::ParseError, "impossible to parse class '#{name}' in #{klass.file} at line #{klass.line}: #{detail}", detail.backtrace
end

#document_define(name, define, container) ⇒ Object

create documentation for a define



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 284

def document_define(name, define, container)
  Puppet.debug "rdoc: found new definition #{name}"
  # find superclas if any

  # find the parent
  # split define name by :: to find the complete module hierarchy
  container, name = get_class_or_module(container,name)

  # build up declaration
  declaration = ""
  define.arguments.each do |arg,value|
    declaration << "\$#{arg}"
    unless value.nil?
      declaration << " => "
      case value
      when Puppet::Parser::AST::Leaf
        declaration << "'#{value.value}'"
      when Puppet::Parser::AST::BlockExpression
        declaration << "[#{value.children.collect { |v| "'#{v}'" }.join(", ")}]"
      else
        declaration << "#{value.to_s}"
      end
    end
    declaration << ", "
  end
  declaration.chop!.chop! if declaration.size > 1

  # register method into the container
  meth =  RDoc::AnyMethod.new(declaration, name)
  meth.comment = define.doc
  container.add_method(meth)
  look_for_directives_in(container, meth.comment) unless meth.comment.empty?
  meth.params = "( #{declaration} )"
  meth.visibility = :public
  meth.document_self = true
  meth.singleton = false
rescue => detail
  raise Puppet::ParseError, "impossible to parse definition '#{name}' in #{define.file} at line #{define.line}: #{detail}", detail.backtrace
end

#document_node(name, node, container) ⇒ Object

create documentation for a node



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 259

def document_node(name, node, container)
  Puppet.debug "rdoc: found new node #{name}"
  superclass = node.parent
  superclass = "" if superclass.nil? or superclass.empty?

  comment = node.doc
  look_for_directives_in(container, comment) unless comment.empty?
  n = container.add_node(name, superclass)
  n.record_location(@top_level)

  code = node.code.children if node.code.is_a?(Puppet::Parser::AST::BlockExpression)
  code ||= node.code
  unless code.nil?
    scan_for_include_or_require(n, code)
    scan_for_realize(n, code)
    scan_for_vardef(n, code)
    scan_for_resource(n, code) if Puppet.settings[:document_all]
  end

  n.add_comment(comment, node.file)
rescue => detail
  raise Puppet::ParseError, "impossible to parse node '#{name}' in #{node.file} at line #{node.line}: #{detail}", detail.backtrace
end

#find_object_named(container, name) ⇒ Object

Due to a bug in RDoc, we need to roll our own find_module_named The issue is that RDoc tries harder by asking the parent for a class/module of the name. But by doing so, it can mistakenly use a module of same name but from which we are not descendant.



48
49
50
51
52
53
54
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 48

def find_object_named(container, name)
  return container if container.name == name
  container.each_classmodule do |m|
    return m if m.name == name
  end
  nil
end

#get_class_or_module(container, name) ⇒ Object

walk down the namespace and lookup/create container as needed



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 57

def get_class_or_module(container, name)

  # class ::A -> A is in the top level
  if name =~ /^::/
    container = @top_level
  end

  names = name.split('::')

  final_name = names.pop
  names.each do |name|
    prev_container = container
    container = find_object_named(container, name)
    container ||= prev_container.add_class(RDoc::PuppetClass, name, nil)
  end
  [container, final_name]
end

#initialize(top_level, file_name, body, options, stats) ⇒ Object

called with the top level file



16
17
18
19
20
21
22
23
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 16

def initialize(top_level, file_name, body, options, stats)
  @options = options
  @stats   = stats
  @input_file_name = file_name
  @top_level = top_level
  @top_level.extend(RDoc::PuppetTopLevel)
  @progress = $stderr unless options.quiet
end

#look_for_directives_in(context, comment) ⇒ Object

look_for_directives_in scans the current comment for RDoc directives



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 437

def look_for_directives_in(context, comment)
  preprocess = create_rdoc_preprocess

  preprocess.handle(comment) do |directive, param|
    case directive
    when "stopdoc"
      context.stop_doc
      ""
    when "startdoc"
      context.start_doc
      context.force_documentation = true
      ""
    when "enddoc"
      #context.done_documenting = true
      #""
      throw :enddoc
    when "main"
      options = Options.instance
      options.main_page = param
      ""
    when "title"
      options = Options.instance
      options.title = param
      ""
    when "section"
      context.set_current_section(param, comment)
      comment.replace("") # 1.8 doesn't support #clear
      break
    else
      warn "Unrecognized directive '#{directive}'"
      break
    end
  end
  remove_private_comments(comment)
end

#parse_elements(container, known_resource_types) ⇒ Object

Traverse the AST tree and produce code-objects node that contains the documentation



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 326

def parse_elements(container, known_resource_types)
  Puppet.debug "rdoc: scanning manifest"

  known_resource_types.hostclasses.values.sort { |a,b| a.name <=> b.name }.each do |klass|
    name = klass.name
    if klass.file == @input_file_name
      unless name.empty?
        document_class(name,klass,container)
      else # on main class document vardefs
        code = klass.code.children if klass.code.is_a?(Puppet::Parser::AST::BlockExpression)
        code ||= klass.code
        scan_for_vardef(container, code) unless code.nil?
      end
    end
  end

  known_resource_types.definitions.each do |name, define|
    if define.file == @input_file_name
      document_define(name,define,container)
    end
  end

  known_resource_types.nodes.each do |name, node|
    if node.file == @input_file_name
      document_node(name.to_s,node,container)
    end
  end
end

#parse_fact(container) ⇒ Object

this is a poor man custom fact parser :-)



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 366

def parse_fact(container)
  comments = ""
  current_fact = nil
  parsed_facts = []
  File.open(@input_file_name) do |of|
    of.each do |line|
      # fetch comments
      if line =~ /^[ \t]*# ?(.*)$/
        comments += $1 + "\n"
      elsif line =~ /^[ \t]*Facter.add\(['"](.*?)['"]\)/
        current_fact = RDoc::Fact.new($1,{})
        look_for_directives_in(container, comments) unless comments.empty?
        current_fact.comment = comments
        parsed_facts << current_fact
        comments = ""
        Puppet.debug "rdoc: found custom fact #{current_fact.name}"
      elsif line =~ /^[ \t]*confine[ \t]*:(.*?)[ \t]*=>[ \t]*(.*)$/
        current_fact.confine = { :type => $1, :value => $2 } unless current_fact.nil?
      else # unknown line type
        comments =""
      end
    end
  end
  parsed_facts.each do |f|
    container.add_fact(f)
    f.record_location(@top_level)
  end
end

#parse_plugins(container) ⇒ Object

create documentation for plugins



356
357
358
359
360
361
362
363
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 356

def parse_plugins(container)
  Puppet.debug "rdoc: scanning plugin or fact"
  if @input_file_name =~ /\/facter\/[^\/]+\.rb$/
    parse_fact(container)
  else
    parse_puppet_plugin(container)
  end
end

#parse_puppet_plugin(container) ⇒ Object

this is a poor man puppet plugin parser :-) it doesn’t extract doc nor desc :-(



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 397

def parse_puppet_plugin(container)
  comments = ""
  current_plugin = nil

  File.open(@input_file_name) do |of|
    of.each do |line|
      # fetch comments
      if line =~ /^[ \t]*# ?(.*)$/
        comments += $1 + "\n"
      elsif line =~ /^[ \t]*(?:Puppet::Parser::Functions::)?newfunction[ \t]*\([ \t]*:(.*?)[ \t]*,[ \t]*:type[ \t]*=>[ \t]*(:rvalue|:lvalue)/
        current_plugin = RDoc::Plugin.new($1, "function")
        look_for_directives_in(container, comments) unless comments.empty?
        current_plugin.comment = comments
        current_plugin.record_location(@top_level)
        container.add_plugin(current_plugin)
        comments = ""
        Puppet.debug "rdoc: found new function plugins #{current_plugin.name}"
      elsif line =~ /^[ \t]*Puppet::Type.newtype[ \t]*\([ \t]*:(.*?)\)/
        current_plugin = RDoc::Plugin.new($1, "type")
        look_for_directives_in(container, comments) unless comments.empty?
        current_plugin.comment = comments
        current_plugin.record_location(@top_level)
        container.add_plugin(current_plugin)
        comments = ""
        Puppet.debug "rdoc: found new type plugins #{current_plugin.name}"
      elsif line =~ /module Puppet::Parser::Functions/
        # skip
      else # unknown line type
        comments =""
      end
    end
  end
end

#remove_private_comments(comment) ⇒ Object



473
474
475
476
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 473

def remove_private_comments(comment)
  comment.gsub!(/^#--.*?^#\+\+/m, '')
  comment.sub!(/^#--.*/m, '')
end

#scanObject

main entry point



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 26

def scan
  environment = Puppet.lookup(:current_environment)
  known_resource_types = environment.known_resource_types
  unless known_resource_types.watching_file?(@input_file_name)
    Puppet.info "rdoc: scanning #{@input_file_name}"
    if @input_file_name =~ /\.pp$/
      @parser = Puppet::Parser::Parser.new(environment)
      @parser.file = @input_file_name
      @parser.parse.instantiate('').each do |type|
        known_resource_types.add type
      end
    end
  end

  scan_top_level(@top_level, environment)
  @top_level
end

#scan_for_include_or_require(container, code) ⇒ Object

create documentation for include statements we can find in code and associate it with container



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 151

def scan_for_include_or_require(container, code)
  code = [code] unless code.is_a?(Array)
  code.each do |stmt|
    scan_for_include_or_require(container,stmt.children) if stmt.is_a?(Puppet::Parser::AST::BlockExpression)

    if stmt.is_a?(Puppet::Parser::AST::Function) and ['include','require'].include?(stmt.name)
      stmt.arguments.each do |included|
        Puppet.debug "found #{stmt.name}: #{included}"
        container.send("add_#{stmt.name}", RDoc::Include.new(included.to_s, stmt.doc))
      end
    end
  end
end

#scan_for_realize(container, code) ⇒ Object

create documentation for realize statements we can find in code and associate it with container



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 167

def scan_for_realize(container, code)
  code = [code] unless code.is_a?(Array)
  code.each do |stmt|
    scan_for_realize(container,stmt.children) if stmt.is_a?(Puppet::Parser::AST::BlockExpression)

    if stmt.is_a?(Puppet::Parser::AST::Function) and stmt.name == 'realize'
      stmt.arguments.each do |realized|
        Puppet.debug "found #{stmt.name}: #{realized}"
        container.add_realize( RDoc::Include.new(realized.to_s, stmt.doc))
      end
    end
  end
end

#scan_for_resource(container, code) ⇒ Object

create documentation for resources we can find in code and associate it with container



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 197

def scan_for_resource(container, code)
  code = [code] unless code.is_a?(Array)
  code.each do |stmt|
    scan_for_resource(container,stmt.children) if stmt.is_a?(Puppet::Parser::AST::BlockExpression)

    if stmt.is_a?(Puppet::Parser::AST::Resource) and !stmt.type.nil?
      begin
        type = stmt.type.split("::").collect { |s| s.capitalize }.join("::")
        stmt.instances.each do |inst|
          title = inst.title.is_a?(Puppet::Parser::AST::ASTArray) ? inst.title.to_s.gsub(/\[(.*)\]/,'\1') : inst.title.to_s
          Puppet.debug "rdoc: found resource: #{type}[#{title}]"

          param = []
          inst.parameters.children.each do |p|
            res = {}
            res["name"] = p.param
            res["value"] = "#{p.value.to_s}" unless p.value.nil?

            param << res
          end

          container.add_resource(RDoc::PuppetResource.new(type, title, stmt.doc, param))
        end
      rescue => detail
        raise Puppet::ParseError, "impossible to parse resource in #{stmt.file} at line #{stmt.line}: #{detail}", detail.backtrace
      end
    end
  end
end

#scan_for_vardef(container, code) ⇒ Object

create documentation for global variables assignements we can find in code and associate it with container



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 183

def scan_for_vardef(container, code)
  code = [code] unless code.is_a?(Array)
  code.each do |stmt|
    scan_for_vardef(container,stmt.children) if stmt.is_a?(Puppet::Parser::AST::BlockExpression)

    if stmt.is_a?(Puppet::Parser::AST::VarDef)
      Puppet.debug "rdoc: found constant: #{stmt.name} = #{stmt.value}"
      container.add_constant(RDoc::Constant.new(stmt.name.to_s, stmt.value.to_s, stmt.doc))
    end
  end
end

#scan_top_level(container, environment) ⇒ Object

create documentation for the top level container



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
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 114

def scan_top_level(container, environment)
  # use the module README as documentation for the module
  comment = ""
  %w{README README.rdoc}.each do |rfile|
    readme = File.join(File.dirname(File.dirname(@input_file_name)), rfile)
    comment = File.open(readme,"r") { |f| f.read } if FileTest.readable?(readme)
  end
  look_for_directives_in(container, comment) unless comment.empty?

  # infer module name from directory
  name = split_module(@input_file_name, environment)
  if name.nil?
    # skip .pp files that are not in manifests directories as we can't guarantee they're part
    # of a module or the global configuration.
    container.document_self = false
    return
  end

  Puppet.debug "rdoc: scanning for #{name}"

  container.module_name = name
  container.global=true if name == SITE

  container, name  = get_class_or_module(container,name)
  mod = container.add_module(RDoc::PuppetModule, name)
  mod.record_location(@top_level)
  mod.add_comment(comment, @input_file_name)

  if @input_file_name =~ /\.pp$/
    parse_elements(mod, environment.known_resource_types)
  elsif @input_file_name =~ /\.rb$/
    parse_plugins(mod)
  end
end

#split_module(path, environment) ⇒ Object

split_module tries to find if path belongs to the module path if it does, it returns the module name, otherwise if we are sure it is part of the global manifest path, “__site__” is returned. And finally if this path couldn’t be mapped anywhere, nil is returned.



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
# File 'lib/puppet/util/rdoc/parser/puppet_parser_core.rb', line 79

def split_module(path, environment)
  # find a module
  fullpath = File.expand_path(path)
  Puppet.debug "rdoc: testing #{fullpath}"
  if fullpath =~ /(.*)\/([^\/]+)\/(?:manifests|plugins|lib)\/.+\.(pp|rb)$/
    modpath = $1
    name = $2
    Puppet.debug "rdoc: module #{name} into #{modpath} ?"
    environment.modulepath.each do |mp|
      if File.identical?(modpath,mp)
        Puppet.debug "rdoc: found module #{name}"
        return name
      end
    end
  end
  if fullpath =~ /\.(pp|rb)$/
    # there can be paths we don't want to scan under modules
    # imagine a ruby or manifest that would be distributed as part as a module
    # but we don't want those to be hosted under <site>
    environment.modulepath.each do |mp|
      # check that fullpath is a descendant of mp
      dirname = fullpath
      previous = dirname
      while (dirname = File.dirname(previous)) != previous
        previous = dirname
        return nil if File.identical?(dirname,mp)
      end
    end
  end
  # we are under a global manifests
  Puppet.debug "rdoc: global manifests"
  SITE
end