Class: PDK::Generate::PuppetObject
- Inherits:
-
Object
- Object
- PDK::Generate::PuppetObject
- Defined in:
- lib/pdk/generators/puppet_object.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#module_dir ⇒ Object
readonly
Returns the value of attribute module_dir.
-
#object_name ⇒ Object
readonly
Returns the value of attribute object_name.
Instance Method Summary collapse
-
#initialize(module_dir, object_name, options = {}) ⇒ PuppetObject
constructor
Initialises the PDK::Generate::PuppetObject object.
-
#module_metadata ⇒ PDK::Module::Metadata
private
Parses the metadata.json file for the module.
-
#module_name ⇒ String
private
Retrieves the name of the module (without the forge username) from the module metadata.
-
#object_type ⇒ Symbol
private
Retrieves the type of the object being generated, e.g.
-
#render_file(dest_path, template_path, data) ⇒ void
private
Render a file using the provided template and write it to disk.
-
#run ⇒ Object
Check that the target files do not exist, find an appropriate template and create the target files from the template.
- #target_object_path ⇒ Object abstract
- #target_spec_path ⇒ Object abstract
- #template_data ⇒ Object abstract
-
#templates ⇒ Array<Hash{Symbol => Object}>
private
Provides the possible template directory locations in the order in which they should be searched for a valid template.
-
#with_templates {|template_paths, config_hash| ... } ⇒ Object
private
Search the possible template directories in order of preference to find a template that can be used to render a new object of the specified type.
Constructor Details
#initialize(module_dir, object_name, options = {}) ⇒ PuppetObject
Initialises the PDK::Generate::PuppetObject object.
In general, this object should never be instantiated directly. Instead, one of the subclasses should be used e.g. PDK::Generate::Klass.
New subclasses generally only need to inherit this class, set the OBJECT_TYPE constant and implement the #template_data, #target_object_path and #target_spec_path methods.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pdk/generators/puppet_object.rb', line 30 def initialize(module_dir, object_name, = {}) @module_dir = module_dir @options = if [:class, :defined_type].include?(object_type) # rubocop:disable Style/GuardClause object_name_parts = object_name.split('::') @object_name = if object_name_parts.first == module_name object_name else [module_name, object_name].join('::') end end end |
Instance Attribute Details
#module_dir ⇒ Object (readonly)
Returns the value of attribute module_dir.
12 13 14 |
# File 'lib/pdk/generators/puppet_object.rb', line 12 def module_dir @module_dir end |
#object_name ⇒ Object (readonly)
Returns the value of attribute object_name.
13 14 15 |
# File 'lib/pdk/generators/puppet_object.rb', line 13 def object_name @object_name end |
Instance Method Details
#module_metadata ⇒ PDK::Module::Metadata
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses the metadata.json file for the module.
203 204 205 206 207 208 209 |
# File 'lib/pdk/generators/puppet_object.rb', line 203 def @module_metadata ||= begin PDK::Module::Metadata.from_file(File.join(module_dir, 'metadata.json')) rescue ArgumentError => e raise PDK::CLI::FatalError, _("'%{dir}' does not contain valid Puppet module metadata: %{msg}") % { dir: module_dir, msg: e. } end end |
#module_name ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the name of the module (without the forge username) from the module metadata.
191 192 193 |
# File 'lib/pdk/generators/puppet_object.rb', line 191 def module_name @module_name ||= .data['name'].rpartition('-').last end |
#object_type ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the type of the object being generated, e.g. :class, :defined_type, etc. This is specified in the subclass’ OBJECT_TYPE constant.
73 74 75 |
# File 'lib/pdk/generators/puppet_object.rb', line 73 def object_type self.class::OBJECT_TYPE end |
#render_file(dest_path, template_path, data) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Render a file using the provided template and write it to disk.
111 112 113 114 115 116 |
# File 'lib/pdk/generators/puppet_object.rb', line 111 def render_file(dest_path, template_path, data) PDK.logger.info(_("Creating '%{file}' from template.") % { file: dest_path }) file_content = PDK::TemplateFile.new(template_path, data).render FileUtils.mkdir_p(File.dirname(dest_path)) File.open(dest_path, 'w') { |f| f.write file_content } end |
#run ⇒ Object
Check that the target files do not exist, find an appropriate template and create the target files from the template. This is the main entry point for the class.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/pdk/generators/puppet_object.rb', line 84 def run [target_object_path, target_spec_path].each do |target_file| if File.exist?(target_file) raise PDK::CLI::FatalError, _("Unable to generate class, '%{file}' already exists.") % { file: target_file } end end with_templates do |template_path, config_hash| data = template_data.merge(configs: config_hash) render_file(target_object_path, template_path[:object], data) render_file(target_spec_path, template_path[:spec], data) if template_path[:spec] end end |
#target_object_path ⇒ Object
Subclass and implement #target_object_path. Implementations of this method should return a String containing the destination path of the object being generated.
55 56 57 |
# File 'lib/pdk/generators/puppet_object.rb', line 55 def target_object_path raise NotImplementedError end |
#target_spec_path ⇒ Object
Subclass and implement #target_spec_path. Implementations of this method should return a String containing the destination path of the tests for the object being generated.
62 63 64 |
# File 'lib/pdk/generators/puppet_object.rb', line 62 def target_spec_path raise NotImplementedError end |
#template_data ⇒ Object
Subclass and implement #template_data to provide data to the templates during rendering. Implementations of this method should return a Hash[=> Object].
48 49 50 |
# File 'lib/pdk/generators/puppet_object.rb', line 48 def template_data raise NotImplementedError end |
#templates ⇒ Array<Hash{Symbol => Object}>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Provides the possible template directory locations in the order in which they should be searched for a valid template.
If a template-url has been specified on in the options hash (e.g. from a CLI parameter), then this template directory will be checked first and we do not fall back to the next possible template directory.
If we have not been provided a specific template directory to use, we try the template specified in the module metadata (as set during PDK::Generate::Module) and fall back to the default template if necessary.
176 177 178 179 180 181 182 |
# File 'lib/pdk/generators/puppet_object.rb', line 176 def templates @templates ||= [ { type: 'CLI', url: @options[:'template-url'], allow_fallback: false }, { type: 'metadata', url: .data['template-url'], allow_fallback: true }, { type: 'default', url: PDK::Generate::Module::DEFAULT_TEMPLATE, allow_fallback: false }, ] end |
#with_templates {|template_paths, config_hash| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Search the possible template directories in order of preference to find a template that can be used to render a new object of the specified type.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/pdk/generators/puppet_object.rb', line 132 def with_templates templates.each do |template| if template[:url].nil? PDK.logger.debug(_('No %{dir_type} template specified; trying next template directory.') % { dir_type: template[:type] }) next end PDK::Module::TemplateDir.new(template[:url]) do |template_dir| template_paths = template_dir.object_template_for(object_type) if template_paths config_hash = template_dir.object_config yield template_paths, config_hash # TODO: refactor to a search-and-execute form instead return # work is done # rubocop:disable Lint/NonLocalExitFromIterator elsif template[:allow_fallback] PDK.logger.debug(_('Unable to find a %{type} template in %{url}, trying next template directory') % { type: object_type, url: template[:url] }) else raise PDK::CLI::FatalError, _('Unable to find the %{type} template in %{url}.') % { type: object_type, url: template[:url] } end end end end |