Class: PDK::Generate::Task
- Inherits:
-
PuppetObject
- Object
- PuppetObject
- PDK::Generate::Task
- 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
-
#check_if_task_already_exists ⇒ Object
private
Checks that the task has not already been defined with a different extension.
- #run ⇒ Object
-
#target_object_path ⇒ String
Calculates the path to the file where the new task will be written.
-
#target_spec_path ⇒ nil
Calculates the path to the file where the tests for the new task will be written.
-
#task_name ⇒ String
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).
-
#template_data ⇒ Hash{Symbol => Object}
Prepares the data needed to render the new task template.
-
#write_task_metadata ⇒ Object
private
Writes the <module>/tasks/<task_name>.json metadata file for the task.
Methods inherited from PuppetObject
#check_preconditions, #initialize, #module_metadata, #module_name, #object_type, #render_file, #target_type_path, #target_type_spec_path, #templates, #with_templates, #write_file
Constructor Details
This class inherits a constructor from PDK::Generate::PuppetObject
Instance Method Details
#check_if_task_already_exists ⇒ 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.
Checks that the task has not already been defined with a different extension.
task exist in the <module>/tasks/ directory
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 |
#run ⇒ Object
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_path ⇒ String
Calculates the path to the file where the new task will be written.
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_path ⇒ nil
Calculates the path to the file where the tests for the new task will be written.
34 35 36 |
# File 'lib/pdk/generate/task.rb', line 34 def target_spec_path nil end |
#task_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.
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).
81 82 83 |
# File 'lib/pdk/generate/task.rb', line 81 def task_name (object_name == module_name) ? 'init' : object_name end |
#template_data ⇒ Hash{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.
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: .fetch(:description, 'A short description of this task'), parameters: {}, } end |
#write_task_metadata ⇒ 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.
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 |