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_lib_files, #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 = {})
  super
  raise "NoManifestFileError" unless spec_object[:manifest_file]
  @context = OpenStruct.new(:manifest_file => spec_object[:manifest_file], :content => nil)
end

Class Method Details

.generate_spec_files(module_path, config_data) ⇒ Object



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

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::DefinitionGenerator.generate_spec_files(module_path, config_data)
  files.flatten
end

.manifest_files(module_path) ⇒ Object



57
58
59
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 57

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 trollop.rubyforge.org all options here are available in the config passed into config object returns the parameters



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 43

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

Instance Method Details

#astObject



197
198
199
200
201
202
203
204
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 197

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

#dumperObject



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

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

#find_all_resourcesObject



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

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



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

def generate_content
  content = dumper.dump(ast)
end

#generate_file_name(type_name) ⇒ Object

returns the filename of the type



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

def generate_file_name(type_name)
  tokens = type_name.split('::')
  file_name = tokens.pop
  "#{file_name}_spec.rb"
end

#generate_spec_fileObject



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

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



129
130
131
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 129

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

#item_spec_pathObject

generates a file path for spec tests based on the resource name. An added option is to generate directory names for each parent resource as a default option



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 177

def item_spec_path
  file_name = generate_file_name(type_name)
  tokens = type_name.split('::')
  # if there are only two tokens ie. tomcat::params we dont need to create a subdirectory
  if tokens.count > 2
    # this is a deep level resource ie. tomcat::config::server::connector
    # however we don't need the tomcat directory so we can just remove it
    # this should leave us with config/server/connector_spec.rb
    tokens.delete_at(0)
    # remove the last token since its the class name
    tokens.pop
    # so lets make a directory structure out of it
    dir_name = File.join(tokens) # config/server
    dir_name = File.join(spec_path, dir_name, file_name) # spec/classes/tomcat/config/server
  else
    dir_name = File.join(spec_path, file_name)
  end
  dir_name
end

#lib_pathObject



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

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

#load_context_dataObject



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

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



91
92
93
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 91

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

#manifest_fileObject



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

def manifest_file
  context.manifest_file
end

#parametersObject



115
116
117
118
119
120
121
122
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 115

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

#plural_nameObject



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

def plural_name
  unless @plural_name
    raise NotImplementedError
  end
  @plural_name
end

#resource_typeObject



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

def resource_type
  ast.eContents.first.class
end

#resource_type_nameObject

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



146
147
148
149
150
151
152
153
154
155
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 146

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



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

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

#runObject

run is the main method that gets called automatically



125
126
127
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 125

def run
  generate_spec_file
end

#singular_nameObject



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

def singular_name
  unless @singular_name
    raise NotImplementedError
  end
  @singular_name
end

#spec_pathObject



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

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

#spec_template_fileObject



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

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



160
161
162
163
164
165
166
# File 'lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb', line 160

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