Skip to content

Commit

Permalink
Add ability to generate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
logicminds committed Jan 11, 2021
1 parent 2e70d6b commit cc31ad1
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/pdk/cli/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ module PDK::CLI
require 'pdk/cli/new/test'
require 'pdk/cli/new/transport'
require 'pdk/cli/new/fact'
require 'pdk/cli/new/function'
29 changes: 29 additions & 0 deletions lib/pdk/cli/new/function.rb
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
1 change: 1 addition & 0 deletions lib/pdk/cli/util/option_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def self.valid_namespace?(string)

!(string =~ %r{\A([a-z][a-z0-9_]*)(::[a-z][a-z0-9_]*)*\Z}).nil?
end
singleton_class.send(:alias_method, :valid_function_name?, :valid_namespace?)

singleton_class.send(:alias_method, :valid_class_name?, :valid_namespace?)
singleton_class.send(:alias_method, :valid_defined_type_name?, :valid_namespace?)
Expand Down
48 changes: 48 additions & 0 deletions lib/pdk/generate/function.rb
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
2 changes: 1 addition & 1 deletion lib/pdk/version.rb
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
36 changes: 36 additions & 0 deletions spec/acceptance/new_function_spec.rb
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

0 comments on commit cc31ad1

Please sign in to comment.