-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from rodjek/pdk-468
(PDK-468) Task generation
- Loading branch information
Showing
10 changed files
with
347 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module PDK::CLI | ||
@new_task_cmd = @new_cmd.define_command do | ||
name 'task' | ||
usage _('task [options] <name>') | ||
summary _('Create a new task named <name> using given options') | ||
|
||
PDK::CLI.template_url_option(self) | ||
option nil, :description, _('A short description of the purpose of the task'), argument: :required | ||
|
||
run do |opts, args, _cmd| | ||
PDK::CLI::Util.ensure_in_module! | ||
|
||
task_name = args[0] | ||
module_dir = Dir.pwd | ||
|
||
if task_name.nil? || task_name.empty? | ||
puts command.help | ||
exit 1 | ||
end | ||
|
||
unless Util::OptionValidator.valid_task_name?(task_name) | ||
raise PDK::CLI::ExitWithError, _("'%{name}' is not a valid task name") % { name: task_name } | ||
end | ||
|
||
PDK::Generate::Task.new(module_dir, task_name, opts).run | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require 'pdk/generators/puppet_object' | ||
|
||
module PDK | ||
module Generate | ||
class Task < PuppetObject | ||
OBJECT_TYPE = :task | ||
|
||
# Prepares the data needed to render the new task template. | ||
# | ||
# @return [Hash{Symbol => Object}] a hash of information that will be | ||
# provided to the task template during rendering. Additionally, this hash | ||
# (with the :name key removed) makes up the task metadata. | ||
def template_data | ||
{ | ||
name: object_name, | ||
puppet_task_version: 1, | ||
supports_noop: true, | ||
description: options.fetch(:description, 'A short description of this task'), | ||
} | ||
end | ||
|
||
# Calculates the path to the file where the new task will be written. | ||
# | ||
# @return [String] the path to the task file. | ||
def target_object_path | ||
@target_object_path ||= File.join(module_dir, 'tasks', "#{task_name}.sh") | ||
end | ||
|
||
# Calculates the path to the file where the tests for the new task will | ||
# be written. | ||
# | ||
# @return [nil] as there is currently no test framework for Tasks. | ||
def target_spec_path | ||
nil | ||
end | ||
|
||
def run | ||
check_if_task_already_exists | ||
|
||
super | ||
|
||
write_task_metadata | ||
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 | ||
# | ||
# @api private | ||
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 | ||
|
||
# Writes the <module>/tasks/<task_name>.json metadata file for the task. | ||
# | ||
# @api private | ||
def write_task_metadata | ||
task_metadata = template_data.dup | ||
task_metadata.delete(:name) | ||
|
||
File.open(File.join(module_dir, 'tasks', "#{task_name}.json"), 'w') do |f| | ||
f.write(JSON.pretty_generate(task_metadata)) | ||
end | ||
end | ||
|
||
# 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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
require 'spec_helper' | ||
|
||
describe 'PDK::CLI new task' do | ||
let(:help_text) { a_string_matching(%r{^USAGE\s+pdk new task}m) } | ||
|
||
before(:each) do | ||
allow(PDK::Util).to receive(:module_root).and_return(module_root) | ||
end | ||
|
||
shared_examples 'it exits non-zero and prints the help text' do | ||
it 'exits non-zero and prints the `pdk new task` help' do | ||
expect { | ||
PDK::CLI.run(args) | ||
}.to raise_error(SystemExit) { |error| | ||
expect(error.status).not_to be_zero | ||
}.and output(help_text).to_stdout | ||
end | ||
end | ||
|
||
shared_examples 'it exits with an error' do |expected_error| | ||
it 'exits with an error' do | ||
expect(logger).to receive(:error).with(a_string_matching(expected_error)) | ||
|
||
expect { | ||
PDK::CLI.run(args) | ||
}.to raise_error(SystemExit) { |error| | ||
expect(error.status).not_to be_zero | ||
} | ||
end | ||
end | ||
|
||
context 'when not run from inside a module' do | ||
let(:module_root) { nil } | ||
let(:args) { %w[new task test_task] } | ||
|
||
it_behaves_like 'it exits with an error', %r{must be run from inside a valid module} | ||
end | ||
|
||
context 'when run from inside a module' do | ||
let(:module_root) { '/path/to/test/module' } | ||
|
||
context 'and not provided with a name for the new task' do | ||
let(:args) { %w[new task] } | ||
|
||
it_behaves_like 'it exits non-zero and prints the help text' | ||
end | ||
|
||
context 'and provided an empty string as the task name' do | ||
let(:args) { ['new', 'task', ''] } | ||
|
||
it_behaves_like 'it exits non-zero and prints the help text' | ||
end | ||
|
||
context 'and provided an invalid task name' do | ||
let(:args) { %w[new task test-task] } | ||
|
||
it_behaves_like 'it exits with an error', %r{'test-task' is not a valid task name} | ||
end | ||
|
||
context 'and provided a valid task name' do | ||
let(:generator) { PDK::Generate::Task } | ||
let(:generator_double) { instance_double(generator) } | ||
let(:generator_opts) { instance_of(Hash) } | ||
|
||
before(:each) do | ||
allow(generator).to receive(:new).with(anything, 'test_task', generator_opts).and_return(generator_double) | ||
end | ||
|
||
it 'generates the task' do | ||
expect(generator_double).to receive(:run) | ||
|
||
PDK::CLI.run(%w[new task test_task]) | ||
end | ||
|
||
context 'and a custom template URL' do | ||
let(:generator_opts) { { :'template-url' => 'https://custom/template' } } | ||
|
||
it 'generates the task from the custom template' do | ||
expect(generator_double).to receive(:run) | ||
|
||
PDK::CLI.run(%w[new task test_task --template-url https://custom/template]) | ||
end | ||
end | ||
|
||
context 'and provided a description for the task' do | ||
let(:generator_opts) do | ||
{ | ||
:description => 'test_task description', | ||
:'template-url' => anything, | ||
} | ||
end | ||
|
||
it 'generates the task with the specified description' do | ||
expect(generator_double).to receive(:run) | ||
|
||
PDK::CLI.run(['new', 'task', 'test_task', '--description', 'test_task description']) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.