Class: PDK::Generate::Task

Inherits:
PuppetObject show all
Defined in:
lib/pdk/generate/task.rb

Constant Summary collapse

OBJECT_TYPE =
:task

Instance Attribute Summary

Attributes inherited from PuppetObject

#module_dir, #object_name, #options

Instance Method Summary collapse

Methods inherited from PuppetObject

#can_run?, #check_preconditions, class_name_from_object_name, #initialize, #module_name, #object_type, puppet_strings_type, #render_file, #spec_only?, #target_device_path, #target_type_path, #target_type_spec_path, #targets, #templates, #with_templates, #write_file

Constructor Details

This class inherits a constructor from PDK::Generate::PuppetObject

Instance Method Details

#check_if_task_already_existsObject

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.

Checks that the task has not already been defined with a different extension.

task exist in the <module>/tasks/ directory

Raises:



53
54
55
56
57
58
59
60
61
62
# File 'lib/pdk/generate/task.rb', line 53

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

  Dir.glob(File.join(module_dir, '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

#runObject



38
39
40
41
42
43
44
# File 'lib/pdk/generate/task.rb', line 38

def run
  check_if_task_already_exists

  super

  
end

#target_object_pathString

Calculates the path to the file where the new task will be written.

Returns:

  • (String)

    the path to the task file.



26
27
28
# File 'lib/pdk/generate/task.rb', line 26

def target_object_path
  @target_object_path ||= File.join(module_dir, 'tasks', "#{task_name}.sh")
end

#target_spec_pathnil

Calculates the path to the file where the tests for the new task will be written.

Returns:

  • (nil)

    as there is currently no test framework for Tasks.



34
35
36
# File 'lib/pdk/generate/task.rb', line 34

def target_spec_path
  nil
end

#task_nameString

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.

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).

Returns:

  • (String)

    the base name of the file(s) for the task.



81
82
83
# File 'lib/pdk/generate/task.rb', line 81

def task_name
  (object_name == module_name) ? 'init' : object_name
end

#template_dataHash{Symbol => Object}

Prepares the data needed to render the new task template.

provided to the task template during rendering. Additionally, this hash (with the :name key removed) makes up the task metadata.

Returns:

  • (Hash{Symbol => Object})

    a hash of information that will be



13
14
15
16
17
18
19
20
21
# File 'lib/pdk/generate/task.rb', line 13

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

#write_task_metadataObject

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.

Writes the <module>/tasks/<task_name>.json metadata file for the task.



67
68
69
70
71
72
73
# File 'lib/pdk/generate/task.rb', line 67

def 
  write_file(File.join(module_dir, 'tasks', "#{task_name}.json")) do
     = template_data.dup
    .delete(:name)
    JSON.pretty_generate()
  end
end