Class: Retrospec::Puppet::Generators::ProviderGenerator

Inherits:
Retrospec::Plugins::V1::Plugin
  • Object
show all
Defined in:
lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

retrospec will initaialize this class so its up to you to set any additional variables you need in the context in feed the templates.



11
12
13
14
15
16
17
18
19
20
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 11

def initialize(module_path, spec_object = {})
  super
  # below is the Spec Object which serves as a context for template rendering
  # you will need to initialize this object, so the erb templates can get the binding
  # the SpecObject can be customized to your liking as its different for every plugin gem.
  @context = OpenStruct.new(:provider_name => spec_object[:name],
                            :type_name => spec_object[:type],
                            :properties => [])
  @provider_type = context.type_name
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 6

def context
  @context
end

#provider_typeObject

Returns the value of attribute provider_type.



7
8
9
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 7

def provider_type
  @provider_type
end

#template_dirObject (readonly)

returns the path to the templates first looks inside the external templates directory for specific file then looks inside the gem path templates directory, which is really only useful when developing new templates.



26
27
28
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 26

def template_dir
  @template_dir
end

Class Method Details

.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



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

def self.run_cli(global_opts, args=ARGV)
  sub_command_opts = Optimist.options(args) do
    banner <<-EOS
Generates a new provider with the given name.

    EOS
    opt :name, 'The name of the provider you wish to create', :type => :string, :required => true, :short => '-n'
    opt :type, 'The type name of the provider', :type => :string, :required => true, :short => '-t'
  end
  unless sub_command_opts[:name]
    Optimist.educate
    exit 1
  end
  plugin_data = global_opts.merge(sub_command_opts)
  plugin_data
end

Instance Method Details

#generate_provider_filesObject



89
90
91
92
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 89

def generate_provider_files
  safe_create_template_file(provider_name_path, File.join(template_dir, 'provider_template.rb.retrospec.erb'), context)
  provider_name_path
end

#generate_provider_spec_filesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 94

def generate_provider_spec_files
  provider_files = Dir.glob(File.join(provider_dir, '**', '*.rb')).sort
  spec_files = []
  provider_files.each do |provider_file|
    t_name = File.basename(File.dirname(provider_file))
    provider_file_data = Retrospec::Puppet::Type.load_type(type_file(t_name), provider_file)
    provider_file_data.type_name = t_name # add the provider type
    # because many facts can be in a single file we want to create a unique file for each fact
    provider_spec_path = File.join(provider_spec_dir, t_name, "#{provider_file_data.name}_spec.rb")
    spec_files << provider_spec_path
    safe_create_template_file(provider_spec_path, File.join(template_dir, 'provider_spec.rb.retrospec.erb'), provider_file_data)
  end
  spec_files
end

#provider_dirObject



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

def provider_dir
  File.join(module_path, 'lib', 'puppet', 'provider')
end

#provider_nameObject



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

def provider_name
  context.provider_name
end

#provider_name_pathObject



81
82
83
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 81

def provider_name_path
  File.join(provider_dir, provider_type, "#{provider_name}.rb")
end

#provider_spec_dirObject



77
78
79
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 77

def provider_spec_dir
  File.join(module_path, 'spec', 'unit', 'puppet', 'provider')
end

#type_dirObject



61
62
63
# File 'lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb', line 61

def type_dir
  File.join(module_path, 'lib', 'puppet', 'type')
end

#type_file(p_type = provider_type) ⇒ Object

returns the type file that the provider uses if the type file does not exist it assumes a core puppet type because we could potentially dealing with multiple



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

def type_file(p_type = provider_type)
  if TypeGenerator::CORE_TYPES.include?(p_type)
    type_file = "puppet/type/#{p_type}.rb"
  else
    type_file = File.join(type_dir, "#{p_type}.rb")
  end
  type_file
end