Class: Retrospec::Puppet::Generators::TaskGenerator

Inherits:
BaseGenerator
  • Object
show all
Defined in:
lib/retrospec/plugins/v1/plugin/generators/task_generator.rb

Constant Summary collapse

EXT_TYPES =
{
  'ruby' => 'rb',
  'generic' => 'generic',
  'python' => 'py',
  'powershell' => 'ps1',
  'bash' => 'sh',
  'node' => 'js'
}.freeze

Instance Attribute Summary

Attributes inherited from BaseGenerator

#context, #generator_template_dir_name, #plural_name, #singular_name, #template_dir

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseGenerator

#generate_file_name, #generate_lib_files, #generate_path, #generate_spec_files, #get_binding, #item_name, #item_path, #item_spec_path, #lib_path, #logger, #spec_path

Constructor Details

#initialize(module_path, spec_object = {}) ⇒ TaskGenerator

retrospec will initilalize this class so its up to you to set any additional variables you need to get the job done.



18
19
20
21
22
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 18

def initialize(module_path, spec_object = {})
  super
  @plural_name = 'tasks'
  @singular_name = 'task'
end

Class Method Details

.run_cli(global_opts, args = ARGV) ⇒ Object

used to display subcommand options to the cli the global options are passed in for your usage optimist.rubyforge.org all options here are available in the config passed into config object returns the parameters



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 85

def self.run_cli(global_opts, args = ARGV)
  task_types = %w(bash generic ruby python node powershell)
  task_type  = global_opts['plugins::puppet::default_task_type'] || 'bash'
  sub_command_opts = Optimist.options(args) do
    banner <<-EOS
Creates a new puppet bolt task for your module

Example: retrospec puppet new_task -n reboot -p "name, ttl, message"

    EOS
    opt :name, 'The name of the task you wish to create', :type => :string, :required => true, :short => '-n'
    opt :task_params, 'The task parameter names separated by commas', :short => '-p', :type => :string, :required => false, default: 'name'
    opt :task_type, "The task type of the task (#{task_types.join(', ')})", :type => :string, :required => false, :short => '-t',
                                                                            :default => task_type
  end
  unless sub_command_opts[:name]
    Optimist.educate
    exit 1
  end
  plugin_data = global_opts.merge(sub_command_opts)
  plugin_data
end

Instance Method Details

#enable_beaker_tasks?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 61

def enable_beaker_tasks?
  false
end

#generate_task_filesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 65

def generate_task_files
  context.task_type = task_type
  context.shebang = shebang
  context.task_params_output = task_params_output
  context.task_params = task_params
  parameter_template = File.join(template_dir, 'task_parameters.json.retrospec.erb')
  task_template = Dir.glob(File.join(template_dir, 'types', task_type, '*')).first
  unless task_template
    task_template = Dir.glob(File.join(template_dir, 'types', 'task.retrospec.erb')).first
  end
  safe_create_template_file(task_filepath, task_template, context)
  safe_create_template_file(task_params_filepath, parameter_template, context)
  [task_filepath, task_params_filepath]
end

#runObject



46
47
48
49
50
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 46

def run
  files = []
  files << generate_task_files
  files
end

#shebangObject



24
25
26
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 24

def shebang
  "#!/usr/bin/env #{task_type}"
end

#task_filepathObject



52
53
54
55
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 52

def task_filepath
  ext = EXT_TYPES.fetch(task_type, task_type)
  File.join(module_path, 'tasks', "#{item_name}.#{ext}")
end

#task_paramsObject



28
29
30
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 28

def task_params
  config_data[:task_params].gsub(/\s/, '').split(',')
end

#task_params_filepathObject



57
58
59
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 57

def task_params_filepath
  File.join(module_path, 'tasks', "#{item_name}.json")
end

#task_params_outputObject



32
33
34
35
36
37
38
39
40
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 32

def task_params_output
  params = {}
  task_params.each_with_object({}) do |item, obj|
    obj['description'] = "The description of the #{item} parameter"
    obj['type'] = 'String'
    params[item] = obj
  end
  params
end

#task_typeObject



42
43
44
# File 'lib/retrospec/plugins/v1/plugin/generators/task_generator.rb', line 42

def task_type
  config_data[:task_type]
end