Class: Retrospec::Puppet::Generators::ResourceBaseGenerator

Inherits:
BaseGenerator
  • Object
show all
Defined in:
lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb

Instance Attribute Summary

Attributes inherited from BaseGenerator

#context, #generator_template_dir_name, #template_dir

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseGenerator

#generate_file_name, #generate_lib_files, #generate_path, #generate_spec_files, #get_binding, #item_name, #logger

Constructor Details

#initialize(module_path, spec_object = {}) ⇒ ResourceBaseGenerator

retrospec will initilalize this class so its up to you to set any additional variables you need to get the job done.



14
15
16
17
18
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 14

def initialize(module_path, spec_object = {})
  spec_object.merge!({:manifest_file => spec_object[:manifest_file],
    :content => nil, name: spec_object[:name]})
  super
end

Class Method Details

.generate_spec_files(module_path, config_data) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 68

def self.generate_spec_files(module_path, config_data)
  manifests = manifest_files(module_path)
  files = Retrospec::Puppet::Generators::HostClassGenerator.generate_spec_files(module_path, config_data)
  files << Retrospec::Puppet::Generators::DataTypeGenerator.generate_spec_files(module_path, config_data)
  files << Retrospec::Puppet::Generators::DefinitionGenerator.generate_spec_files(module_path, config_data)
  files.flatten
end

.manifest_files(module_path) ⇒ Object



64
65
66
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 64

def self.manifest_files(module_path)
  Dir.glob(File.join(module_path, 'manifests', '**', '*.pp'))
end

.run_cli(global_opts, args = ARGV) ⇒ Object

used to display subcommand options to the cli the global options are passed in for your usage optimist.rubyforge.org all options here are available in the config passed into config object returns the parameters



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 50

def self.run_cli(global_opts, args=ARGV)
  sub_command_opts = Optimist.options(args) do
    banner <<-EOS
    ""
    EOS
  end
  unless sub_command_opts[:manifest_file]
    Optimist.educate
    exit 1
  end
  plugin_data = global_opts.merge(sub_command_opts)
  plugin_data
end

Instance Method Details

#astObject



172
173
174
175
176
177
178
179
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 172

def ast
  unless @ast
    parser = ::Puppet::Pops::Parser::EvaluatingParser.new
    result = parser.parse_file(manifest_file)
    @ast = result.current
  end
  @ast
end

#dumperObject



114
115
116
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 114

def dumper
  @dumper ||= Retrospec::Puppet::RspecDumper.new
end

#find_all_resourcesObject



103
104
105
106
107
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 103

def find_all_resources
  res = manifest_body.eAllContents.find_all do |p|
    p.class.to_s == 'Puppet::Pops::Model::ResourceExpression'
  end
end

#generate_contentObject

this produces the content that will later be rendered in the template



119
120
121
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 119

def generate_content
  content = dumper.dump(ast)
end

#generate_spec_fileObject



86
87
88
89
90
91
92
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 86

def generate_spec_file
  template_file = File.join(template_dir,spec_template_file )
  context = load_context_data
  logger.debug("\nUsing template #{template_file}\n")
  safe_create_template_file(item_spec_path, template_file, context)
  item_spec_path
end

#item_pathObject



137
138
139
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 137

def item_path
  File.join(lib_path, "#{item_name}.pp")
end

#item_spec_pathObject



20
21
22
23
24
25
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 20

def item_spec_path
  iname = type_name || item_name
  file_name = generate_file_name(iname.downcase)
  path = generate_path("#{file_name}_spec.rb", iname)
  File.join(spec_path, path )
end

#lib_pathObject



145
146
147
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 145

def lib_path
  File.join(module_path, 'manifests')
end

#load_context_dataObject



76
77
78
79
80
81
82
83
84
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 76

def load_context_data
  context.content = generate_content
  context.parameters = parameters
  context.type_name = type_name
  context.resources = resources
  context.resource_type = resource_type
  context.resource_type_name = resource_type_name
  context
end

#manifest_bodyObject

return a manifest body object



99
100
101
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 99

def manifest_body
  ast.body.body || ast.body
end

#manifest_fileObject



94
95
96
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 94

def manifest_file
  context.manifest_file
end

#parametersObject



123
124
125
126
127
128
129
130
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 123

def parameters
  if ast.body.respond_to?(:parameters)
    args = ast.body.parameters || []
    dumper.dump(args)
  else
    []
  end
end

#plural_nameObject



34
35
36
37
38
39
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 34

def plural_name
  unless @plural_name
    raise NotImplementedError
  end
  @plural_name
end

#resource_typeObject



149
150
151
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 149

def resource_type
  ast.eContents.first.class
end

#resource_type_nameObject

returns the type of resource either the define, type, or class



154
155
156
157
158
159
160
161
162
163
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 154

def resource_type_name
  case ast.eContents.first
  when ::Puppet::Pops::Model::HostClassDefinition
    'class'
  when ::Puppet::Pops::Model::ResourceTypeDefinition
    type_name
  else
    resource_type
  end
end

#resourcesObject

returns all the found resources



110
111
112
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 110

def resources
  @resources ||= find_all_resources.map {|p| dumper.dump(p)}
end

#runObject

run is the main method that gets called automatically



133
134
135
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 133

def run
  generate_spec_file
end

#singular_nameObject



27
28
29
30
31
32
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 27

def singular_name
  unless @singular_name
    raise NotImplementedError
  end
  @singular_name
end

#spec_pathObject



141
142
143
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 141

def spec_path
  File.join(module_path, 'spec', plural_name)
end

#spec_template_fileObject



41
42
43
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 41

def spec_template_file
  NotImplementedError
end

#type_nameObject

returns the name of the first type found in the file for files that have multiple types, we just don’t care since it doesn’t follow the style guide



168
169
170
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 168

def type_name
  ast.eContents.first.name if ast.eContents.first.respond_to?(:name)
end