lib/pdk/generate/puppet_object.rb in pdk-1.13.0 vs lib/pdk/generate/puppet_object.rb in pdk-1.14.0

- old
+ new

@@ -1,14 +1,5 @@ -require 'fileutils' - -require 'pdk' -require 'pdk/logger' -require 'pdk/module/metadata' -require 'pdk/module/templatedir' -require 'pdk/template_file' -require 'pdk/util/filesystem' - module PDK module Generate class PuppetObject attr_reader :module_dir attr_reader :object_name @@ -139,20 +130,34 @@ # # @raise [PDK::CLI::ExitWithError] if the target files already exist. # # @api public def check_preconditions + require 'pdk/util/filesystem' + targets.each do |target_file| - next unless File.exist?(target_file) + next unless PDK::Util::Filesystem.exist?(target_file) raise PDK::CLI::ExitWithError, _("Unable to generate %{object_type}; '%{file}' already exists.") % { file: target_file, object_type: spec_only? ? 'unit test' : object_type, } end end + # Check the preconditions of this template group, behaving as a + # predicate rather than raising an exception. + # + # @return [Boolean] true if the generator is safe to run, otherwise + # false. + def can_run? + check_preconditions + true + rescue PDK::CLI::ExitWithError + false + end + # Check that the templates can be rendered. Find an appropriate template # and create the target files from the template. This is the main entry # point for the class. # # @raise [PDK::CLI::ExitWithError] if the target files already exist. @@ -189,10 +194,12 @@ # # @return [void] # # @api private def render_file(dest_path, template_path, data) + require 'pdk/template_file' + write_file(dest_path) do PDK::TemplateFile.new(template_path, data).render end end @@ -209,16 +216,19 @@ # # @return [void] # # @api private def write_file(dest_path) + require 'pdk/logger' + require 'pdk/util/filesystem' + PDK.logger.info(_("Creating '%{file}' from template.") % { file: dest_path }) file_content = yield begin - FileUtils.mkdir_p(File.dirname(dest_path)) + PDK::Util::Filesystem.mkdir_p(File.dirname(dest_path)) rescue SystemCallError => e raise PDK::CLI::FatalError, _("Unable to create directory '%{path}': %{message}") % { path: File.dirname(dest_path), message: e.message, } @@ -245,10 +255,14 @@ # # @raise [PDK::CLI::FatalError] if no suitable template could be found. # # @api private def with_templates + require 'pdk/logger' + require 'pdk/module/templatedir' + require 'pdk/util/template_uri' + templates.each do |template| if template[:uri].nil? PDK.logger.debug(_('No %{dir_type} template found; trying next template directory.') % { dir_type: template[:type] }) next end @@ -291,37 +305,26 @@ # the lookup process should proceed to the next template directory if # the template file is not in this template directory. # # @api private def templates + require 'pdk/util/template_uri' + @templates ||= PDK::Util::TemplateURI.templates(@options) end # Retrieves the name of the module (without the forge username) from the # module metadata. # - # @raise (see #module_metadata) # @return [String] The name of the module. # # @api private def module_name - @module_name ||= module_metadata.data['name'].rpartition('-').last - end + require 'pdk/util' - # Parses the metadata.json file for the module. - # - # @raise [PDK::CLI::FatalError] if the metadata.json file does not exist, - # can not be read, or contains invalid metadata. - # - # @return [PDK::Module::Metadata] the parsed module metadata. - # - # @api private - def module_metadata - @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.message } - end + @module_name ||= PDK::Util.module_metadata['name'].rpartition('-').last + rescue ArgumentError => e + raise PDK::CLI::FatalError, e end # transform a object name into a ruby class name def self.class_name_from_object_name(object_name) object_name.to_s.split('_').map(&:capitalize).join