Class: PuppetModuleParser

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/puppet_module_parser.rb

Overview

Based on ideas from puppet-parse by Johan van den Dorpe

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PuppetModuleParser

Returns a new instance of PuppetModuleParser.

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kafo/puppet_module_parser.rb', line 19

def initialize(file)
  raise ModuleName, "File not found #{file}, check you answer file" unless File.exists?(file)
  parser = Puppet::Parser::Parser.new('production')
  values = Puppet.settings.instance_variable_get('@values')
  values[:production][:confdir] ||= '/' # just some stubbing
  parser.import(file)

  # Find object in list of hostclasses
  parser.environment.known_resource_types.hostclasses.each do |x|
    @object = x.last if x.last.file == file
  end
  # Find object in list of definitions
  parser.environment.known_resource_types.definitions.each do |x|
    @object = x.last if x.last.file == file
  end
end

Class Method Details

.parse(file) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/kafo/puppet_module_parser.rb', line 9

def self.parse(file)
  content = new(file)

  {
      'parameters'  => content.parameters,
      'docs'        => content.docs,
      'validations' => content.validations
  }
end

Instance Method Details

#docsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kafo/puppet_module_parser.rb', line 52

def docs
  docs = {}
  if !@object.doc.nil?
    if RDoc::Markup.respond_to?(:parse)
      rdoc  = RDoc::Markup.parse(@object.doc)
    else # RDoc < 3.10.0
      rdoc = RDoc::Markup::Parser.parse(@object.doc)
    end
    items = rdoc.parts.select { |part| part.respond_to?(:items) }.map(&:items).flatten
    items.each do |item|
      # Skip rdoc items that aren't paragraphs
      next unless (item.parts.to_s.scan("RDoc::Markup::Paragraph") == ["RDoc::Markup::Paragraph"])
      # RDoc (>= 4) makes label an array
      label = item.label.is_a?(Array) ? item.label.first : item.label
      # Documentation must be a list - if there's no label then skip
      next if label.nil?
      key       = label.tr('^A-Za-z0-9_-', '')
      docs[key] = item.parts.first.parts.map!(&:strip)
    end
  end
  docs
end

#klassObject



44
45
46
# File 'lib/kafo/puppet_module_parser.rb', line 44

def klass
  @object.name if @object.class.respond_to?(:name)
end

#parametersObject

TODO - store parsed object type (Puppet::Parser::AST::Variable must be dumped later)



37
38
39
40
41
42
# File 'lib/kafo/puppet_module_parser.rb', line 37

def parameters
  parameters = {}
  arguments  = @object.respond_to?(:arguments) ? @object.arguments : {}
  arguments.each { |k, v| parameters[k] = v.respond_to?(:value) ? v.value : nil }
  parameters
end

#validations(param = nil) ⇒ Object



48
49
50
# File 'lib/kafo/puppet_module_parser.rb', line 48

def validations(param = nil)
  @object.code.select { |stmt| stmt.is_a?(Puppet::Parser::AST::Function) && stmt.name =~ /^validate_/ }
end