Sha256: 197eeafd978ba5ab649c6c9a213fb9d58fe79576ff548b245626d047688ed0a7

Contents?: true

Size: 1.82 KB

Versions: 10

Compression:

Stored size: 1.82 KB

Contents

require 'pdk'

module PDK
  module Generate
    class Task < PuppetObject
      def friendly_name
        'Task'
      end

      def template_files
        return {} if spec_only?
        {
          'task.erb' => File.join('tasks', task_name + '.sh'),
        }
      end

      def template_data
        {
          name: object_name,
        }
      end

      # Checks that the task has not already been defined with a different
      # extension.
      #
      # @raise [PDK::CLI::ExitWithError] if files with the same name as the
      # task exist in the <module>/tasks/ directory
      def check_preconditions
        super

        error = _("A task named '%{name}' already exists in this module; defined in %{file}")
        allowed_extensions = %w[.md .conf]

        PDK::Util::Filesystem.glob(File.join(context.root_path, 'tasks', task_name + '.*')).each do |file|
          next if allowed_extensions.include?(File.extname(file))

          raise PDK::CLI::ExitWithError, error % { name: task_name, file: file }
        end
      end

      def non_template_files
        task_metadata_file = File.join('tasks', task_name + '.json')
        { task_metadata_file => JSON.pretty_generate(task_metadata) }
      end

      private

      # Calculates the file name of the task files ('init' if the task has the
      # same name as the module, otherwise use the specified task name).
      #
      # @return [String] the base name of the file(s) for the task.
      #
      # @api private
      def task_name
        (object_name == module_name) ? 'init' : object_name
      end

      def task_metadata
        {
          puppet_task_version: 1,
          supports_noop:       false,
          description:         options.fetch(:description, 'A short description of this task'),
          parameters:          {},
        }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pdk-2.6.1 lib/pdk/generate/task.rb
pdk-2.6.0 lib/pdk/generate/task.rb
pdk-2.5.0 lib/pdk/generate/task.rb
pdk-2.3.0 lib/pdk/generate/task.rb
pdk-2.2.0 lib/pdk/generate/task.rb
pdk-2.1.1 lib/pdk/generate/task.rb
pdk-2.1.0 lib/pdk/generate/task.rb
pdk-2.0.0 lib/pdk/generate/task.rb
pdk-1.18.1 lib/pdk/generate/task.rb
pdk-1.18.0 lib/pdk/generate/task.rb