class Retrospec::Puppet::Generators::ResourceBaseGenerator

Public Class Methods

generate_spec_files(module_path, config_data) click to toggle source
# 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) click to toggle source
# 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
new(module_path, spec_object = {}) click to toggle source

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

# 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
run_cli(global_opts, args=ARGV) click to toggle source

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

# 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

Public Instance Methods

ast() click to toggle source
# 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
dumper() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb, line 114
def dumper
  @dumper ||= Retrospec::Puppet::RspecDumper.new
end
find_all_resources() click to toggle source
# 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_content() click to toggle source

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

# File lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb, line 119
def generate_content
  content = dumper.dump(ast)
end
generate_spec_file() click to toggle source
# 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_path() click to toggle source
# 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_path() click to toggle source
# 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_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb, line 145
def lib_path
  File.join(module_path, 'manifests')
end
load_context_data() click to toggle source
# 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_body() click to toggle source

return a manifest body object

# File lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb, line 99
def manifest_body
  ast.body.body || ast.body
end
manifest_file() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb, line 94
def manifest_file
  context.manifest_file
end
parameters() click to toggle source
# 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_name() click to toggle source
# 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_type() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb, line 149
def resource_type
  ast.eContents.first.class
end
resource_type_name() click to toggle source

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

# 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
resources() click to toggle source

returns all the found resources

# 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
run() click to toggle source

run is the main method that gets called automatically

# File lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb, line 133
def run
  generate_spec_file
end
singular_name() click to toggle source
# 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_path() click to toggle source
# 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_file() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/resource_base_generator.rb, line 41
def spec_template_file
  NotImplementedError
end
type_name() click to toggle source

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

# 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