-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e70d6b
commit cc31ad1
Showing
6 changed files
with
116 additions
and
1 deletion.
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,29 @@ | ||
module PDK::CLI | ||
@new_function_cmd = @new_cmd.define_command do | ||
name 'function' | ||
usage _('function [options] <name>') | ||
summary _('Create a new function named <name> using given options') | ||
option :t, :type, _('The function type, (native or v4)'), argument: :required, default: 'native' | ||
|
||
run do |opts, args, _cmd| | ||
PDK::CLI::Util.ensure_in_module! | ||
|
||
function_name = args[0] | ||
|
||
if function_name.nil? || function_name.empty? | ||
puts command.help | ||
exit 1 | ||
end | ||
|
||
unless Util::OptionValidator.valid_function_name?(function_name) | ||
raise PDK::CLI::ExitWithError, _("'%{name}' is not a valid function name") % { name: function_name } | ||
end | ||
|
||
PDK::CLI::Util.analytics_screen_view('new_function', opts) | ||
|
||
require 'pdk/generate/function' | ||
updates = PDK::Generate::Function.new(PDK.context, function_name, opts).run | ||
PDK::CLI::Util::UpdateManagerPrinter.print_summary(updates, tense: :past) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'pdk' | ||
|
||
module PDK | ||
module Generate | ||
class Function < PuppetObject | ||
def initialize(*_args) | ||
super | ||
object_name_parts = @object_name.split('::') | ||
|
||
@object_name = if object_name_parts.first == module_name | ||
object_name | ||
else | ||
[module_name, object_name].join('::') | ||
end | ||
end | ||
|
||
def friendly_name | ||
'Function'.freeze | ||
end | ||
|
||
def template_files | ||
# Calculate the function tests name | ||
func_name_parts = object_name.split('::') | ||
# Drop the module name if the object name contains multiple parts | ||
func_name_parts.delete_at(0) if func_name_parts.length > 1 | ||
files = { | ||
File.join('functions', 'function_spec.erb') => File.join('spec', 'functions', *func_name_parts) + '_spec.rb', | ||
} | ||
return files if spec_only? | ||
func_name_parts = object_name.split('::')[1..-1] | ||
template_file = File.join('functions', "#{options[:type]}_function.erb") | ||
|
||
files[template_file] = if options[:type].eql?('v4') | ||
File.join('lib', 'puppet', 'functions', module_name, *func_name_parts) + '.rb' | ||
else | ||
File.join('functions', *func_name_parts) + '.pp' | ||
end | ||
files | ||
end | ||
|
||
def template_data | ||
func_name = object_name.split('::').last | ||
namespace = object_name.split('::')[0...-1].join('::') | ||
{ name: object_name, func_name: func_name, namespace: namespace } | ||
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module PDK | ||
VERSION = '1.19.0.pre'.freeze | ||
VERSION = '1.19.1.pre'.freeze | ||
TEMPLATE_REF = VERSION | ||
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,36 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'pdk new function', module_command: true do | ||
shared_examples 'it creates a function' do |_options| | ||
context 'in a new module' do | ||
include_context 'in a new module', 'new_function' | ||
context 'when creating the function' do | ||
describe command('pdk new function -t v4 abs') do | ||
its(:exit_status) { is_expected.to eq 0 } | ||
its(:stdout) { is_expected.to match(%r{Files added}) } | ||
its(:stdout) { is_expected.to match(%r{#{File.join('lib', 'puppet', 'functions', 'abs.rb')}}) } | ||
its(:stdout) { is_expected.to match(%r{#{ File.join('dspec', 'functions', 'abs_spec.rb')}}) } | ||
its(:stderr) { is_expected.to have_no_output } | ||
|
||
it_behaves_like 'it creates a function', | ||
name: 'abs', | ||
file: File.join('lib', 'puppet', 'functions', 'abs.rb'), | ||
spec: File.join('spec', 'functions', 'abs_spec.rb') | ||
end | ||
|
||
describe command('pdk new function -t native abs') do | ||
its(:exit_status) { is_expected.to eq 0 } | ||
its(:stdout) { is_expected.to match(%r{Files added}) } | ||
its(:stdout) { is_expected.to match(%r{#{File.join('functions', 'abs.pp')}}) } | ||
its(:stdout) { is_expected.to match(%r{#{ File.join('spec', 'functions', 'abs_spec.rb')}}) } | ||
its(:stderr) { is_expected.to have_no_output } | ||
|
||
it_behaves_like 'it creates a function', | ||
name: 'abs', | ||
file: File.join('functions', 'abs.rb'), | ||
spec: File.join('spec', 'functions', 'abs_spec.rb') | ||
end | ||
end | ||
end | ||
end | ||
end |