pdk-akerl 1.8.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +826 -0
- data/LICENSE +201 -0
- data/README.md +133 -0
- data/exe/pdk +10 -0
- data/lib/pdk.rb +10 -0
- data/lib/pdk/answer_file.rb +121 -0
- data/lib/pdk/cli.rb +113 -0
- data/lib/pdk/cli/build.rb +76 -0
- data/lib/pdk/cli/bundle.rb +42 -0
- data/lib/pdk/cli/convert.rb +41 -0
- data/lib/pdk/cli/errors.rb +23 -0
- data/lib/pdk/cli/exec.rb +246 -0
- data/lib/pdk/cli/exec_group.rb +67 -0
- data/lib/pdk/cli/module.rb +14 -0
- data/lib/pdk/cli/module/build.rb +14 -0
- data/lib/pdk/cli/module/generate.rb +45 -0
- data/lib/pdk/cli/new.rb +17 -0
- data/lib/pdk/cli/new/class.rb +32 -0
- data/lib/pdk/cli/new/defined_type.rb +30 -0
- data/lib/pdk/cli/new/module.rb +41 -0
- data/lib/pdk/cli/new/provider.rb +27 -0
- data/lib/pdk/cli/new/task.rb +31 -0
- data/lib/pdk/cli/test.rb +12 -0
- data/lib/pdk/cli/test/unit.rb +88 -0
- data/lib/pdk/cli/update.rb +32 -0
- data/lib/pdk/cli/util.rb +193 -0
- data/lib/pdk/cli/util/command_redirector.rb +26 -0
- data/lib/pdk/cli/util/interview.rb +63 -0
- data/lib/pdk/cli/util/option_normalizer.rb +53 -0
- data/lib/pdk/cli/util/option_validator.rb +56 -0
- data/lib/pdk/cli/validate.rb +124 -0
- data/lib/pdk/generate.rb +11 -0
- data/lib/pdk/generate/defined_type.rb +49 -0
- data/lib/pdk/generate/module.rb +318 -0
- data/lib/pdk/generate/provider.rb +82 -0
- data/lib/pdk/generate/puppet_class.rb +48 -0
- data/lib/pdk/generate/puppet_object.rb +288 -0
- data/lib/pdk/generate/task.rb +86 -0
- data/lib/pdk/i18n.rb +4 -0
- data/lib/pdk/logger.rb +28 -0
- data/lib/pdk/module.rb +21 -0
- data/lib/pdk/module/build.rb +214 -0
- data/lib/pdk/module/convert.rb +209 -0
- data/lib/pdk/module/metadata.rb +193 -0
- data/lib/pdk/module/templatedir.rb +313 -0
- data/lib/pdk/module/update.rb +111 -0
- data/lib/pdk/module/update_manager.rb +210 -0
- data/lib/pdk/report.rb +112 -0
- data/lib/pdk/report/event.rb +357 -0
- data/lib/pdk/template_file.rb +89 -0
- data/lib/pdk/tests/unit.rb +213 -0
- data/lib/pdk/util.rb +271 -0
- data/lib/pdk/util/bundler.rb +253 -0
- data/lib/pdk/util/filesystem.rb +12 -0
- data/lib/pdk/util/git.rb +74 -0
- data/lib/pdk/util/puppet_version.rb +242 -0
- data/lib/pdk/util/ruby_version.rb +147 -0
- data/lib/pdk/util/vendored_file.rb +88 -0
- data/lib/pdk/util/version.rb +42 -0
- data/lib/pdk/util/windows.rb +13 -0
- data/lib/pdk/util/windows/api_types.rb +57 -0
- data/lib/pdk/util/windows/file.rb +36 -0
- data/lib/pdk/util/windows/string.rb +16 -0
- data/lib/pdk/validate.rb +14 -0
- data/lib/pdk/validate/base_validator.rb +209 -0
- data/lib/pdk/validate/metadata/metadata_json_lint.rb +86 -0
- data/lib/pdk/validate/metadata/metadata_syntax.rb +109 -0
- data/lib/pdk/validate/metadata_validator.rb +30 -0
- data/lib/pdk/validate/puppet/puppet_lint.rb +67 -0
- data/lib/pdk/validate/puppet/puppet_syntax.rb +112 -0
- data/lib/pdk/validate/puppet_validator.rb +30 -0
- data/lib/pdk/validate/ruby/rubocop.rb +77 -0
- data/lib/pdk/validate/ruby_validator.rb +29 -0
- data/lib/pdk/validate/tasks/metadata_lint.rb +126 -0
- data/lib/pdk/validate/tasks/name.rb +88 -0
- data/lib/pdk/validate/tasks_validator.rb +33 -0
- data/lib/pdk/version.rb +4 -0
- data/locales/config.yaml +21 -0
- data/locales/pdk.pot +1283 -0
- metadata +304 -0
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module PDK
|
4
|
+
class TemplateFile < OpenStruct
|
5
|
+
# Initialises the TemplateFile object with the path to the template file
|
6
|
+
# and the data to be used when rendering the template.
|
7
|
+
#
|
8
|
+
# @param template_file [String] The path on disk to the template file.
|
9
|
+
# @param data [Hash{Symbol => Object}] The data that should be provided to
|
10
|
+
# the template when rendering.
|
11
|
+
# @option data [Object] :configs The value of this key will be provided to
|
12
|
+
# the template as an instance variable `@configs` in order to maintain
|
13
|
+
# compatibility with modulesync.
|
14
|
+
#
|
15
|
+
# @api public
|
16
|
+
def initialize(template_file, data = {})
|
17
|
+
@template_file = template_file
|
18
|
+
|
19
|
+
if data.key?(:configs)
|
20
|
+
@configs = data[:configs]
|
21
|
+
end
|
22
|
+
|
23
|
+
super(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Renders the template by calling the appropriate engine based on the file
|
27
|
+
# extension.
|
28
|
+
#
|
29
|
+
# If the template has an `.erb` extension, the content of the template
|
30
|
+
# file will be treated as an ERB template. All other extensions are treated
|
31
|
+
# as plain text.
|
32
|
+
#
|
33
|
+
# @return [String] The rendered template
|
34
|
+
#
|
35
|
+
# @raise (see #template_content)
|
36
|
+
#
|
37
|
+
# @api public
|
38
|
+
def render
|
39
|
+
case File.extname(@template_file)
|
40
|
+
when '.erb'
|
41
|
+
render_erb
|
42
|
+
else
|
43
|
+
render_plain
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
# Reads the content of the template file into memory.
|
50
|
+
#
|
51
|
+
# @return [String] The content of the template file.
|
52
|
+
#
|
53
|
+
# @raise [ArgumentError] If the template file does not exist or can not be
|
54
|
+
# read.
|
55
|
+
#
|
56
|
+
# @api private
|
57
|
+
def template_content
|
58
|
+
if File.file?(@template_file) && File.readable?(@template_file)
|
59
|
+
return File.read(@template_file)
|
60
|
+
end
|
61
|
+
|
62
|
+
raise ArgumentError, _("'%{template}' is not a readable file") % { template: @template_file }
|
63
|
+
end
|
64
|
+
|
65
|
+
# Renders the content of the template file as an ERB template.
|
66
|
+
#
|
67
|
+
# @return [String] The rendered template.
|
68
|
+
#
|
69
|
+
# @raise (see #template_content)
|
70
|
+
#
|
71
|
+
# @api private
|
72
|
+
def render_erb
|
73
|
+
renderer = ERB.new(template_content, nil, '-')
|
74
|
+
renderer.filename = @template_file
|
75
|
+
renderer.result(binding)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Renders the content of the template file as plain text.
|
79
|
+
#
|
80
|
+
# @return [String] The rendered template.
|
81
|
+
#
|
82
|
+
# @raise (see #template_content)
|
83
|
+
#
|
84
|
+
# @api private
|
85
|
+
def render_plain
|
86
|
+
template_content
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,213 @@
|
|
1
|
+
require 'pdk'
|
2
|
+
require 'pdk/cli/exec'
|
3
|
+
require 'pdk/util/bundler'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module PDK
|
7
|
+
module Test
|
8
|
+
class Unit
|
9
|
+
def self.cmd(tests, opts = {})
|
10
|
+
rake_args = opts.key?(:parallel) ? 'parallel_spec_standalone' : 'spec_standalone'
|
11
|
+
rake_args += "[#{tests}]" unless tests.nil?
|
12
|
+
rake_args
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.rake_bin
|
16
|
+
@rake ||= File.join(PDK::Util.module_root, 'bin', 'rake')
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.rake(task, spinner_text, environment = {})
|
20
|
+
argv = [rake_bin, task]
|
21
|
+
argv.unshift(File.join(PDK::Util::RubyVersion.bin_path, 'ruby.exe')) if Gem.win_platform?
|
22
|
+
|
23
|
+
command = PDK::CLI::Exec::Command.new(*argv).tap do |c|
|
24
|
+
c.context = :module
|
25
|
+
c.add_spinner(spinner_text)
|
26
|
+
c.environment = environment
|
27
|
+
end
|
28
|
+
|
29
|
+
command.execute!
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.parallel_with_no_tests?(ran_in_parallel, json_result, result)
|
33
|
+
ran_in_parallel && json_result.empty? &&
|
34
|
+
((!result[:exit_code].zero? && result[:stderr].strip =~ %r{Pass files or folders to run$}) ||
|
35
|
+
result[:stderr].strip =~ %r{No files for parallel_spec to run against$})
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.print_failure(result, exception)
|
39
|
+
$stderr.puts ''
|
40
|
+
result[:stdout].each_line { |line| $stderr.puts line.rstrip } unless result[:stdout].nil?
|
41
|
+
result[:stderr].each_line { |line| $stderr.puts line.rstrip } unless result[:stderr].nil?
|
42
|
+
$stderr.puts ''
|
43
|
+
raise PDK::CLI::FatalError, exception
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.tear_down
|
47
|
+
result = rake('spec_clean', _('Cleaning up after running unit tests.'))
|
48
|
+
|
49
|
+
return if result[:exit_code].zero?
|
50
|
+
|
51
|
+
PDK.logger.error(_('The spec_clean rake task failed with the following error(s):'))
|
52
|
+
print_failure(result, _('Failed to clean up after running unit tests'))
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.setup
|
56
|
+
result = rake('spec_prep', _('Preparing to run the unit tests.'))
|
57
|
+
|
58
|
+
return if result[:exit_code].zero?
|
59
|
+
|
60
|
+
tear_down
|
61
|
+
|
62
|
+
PDK.logger.error(_('The spec_prep rake task failed with the following error(s):'))
|
63
|
+
print_failure(result, _('Failed to prepare to run the unit tests.'))
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.invoke(report, options = {})
|
67
|
+
PDK::Util::Bundler.ensure_binstubs!('rake', 'rspec-core')
|
68
|
+
|
69
|
+
setup
|
70
|
+
|
71
|
+
tests = options.fetch(:tests)
|
72
|
+
|
73
|
+
environment = { 'CI_SPEC_OPTIONS' => '--format j' }
|
74
|
+
environment['PUPPET_GEM_VERSION'] = options[:puppet] if options[:puppet]
|
75
|
+
spinner_msg = options.key?(:parallel) ? _('Running unit tests in parallel.') : _('Running unit tests.')
|
76
|
+
result = rake(cmd(tests, options), spinner_msg, environment)
|
77
|
+
|
78
|
+
json_result = if options.key?(:parallel)
|
79
|
+
PDK::Util.find_all_json_in(result[:stdout])
|
80
|
+
else
|
81
|
+
PDK::Util.find_first_json_in(result[:stdout])
|
82
|
+
end
|
83
|
+
|
84
|
+
if parallel_with_no_tests?(options.key?(:parallel), json_result, result)
|
85
|
+
json_result = [{ 'messages' => ['No examples found.'] }]
|
86
|
+
result[:exit_code] = 0
|
87
|
+
end
|
88
|
+
|
89
|
+
raise PDK::CLI::FatalError, _('Unit test output did not contain a valid JSON result: %{output}') % { output: result[:stdout] } if json_result.nil? || json_result.empty?
|
90
|
+
|
91
|
+
json_result = merge_json_results(json_result) if options.key?(:parallel)
|
92
|
+
|
93
|
+
parse_output(report, json_result, result[:duration])
|
94
|
+
|
95
|
+
result[:exit_code]
|
96
|
+
ensure
|
97
|
+
tear_down if options[:'clean-fixtures']
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.parse_output(report, json_data, duration)
|
101
|
+
# Output messages to stderr.
|
102
|
+
json_data['messages'] && json_data['messages'].each { |msg| $stderr.puts msg }
|
103
|
+
|
104
|
+
example_results = {
|
105
|
+
# Only possibilities are passed, failed, pending:
|
106
|
+
# https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example.rb#L548
|
107
|
+
'passed' => [],
|
108
|
+
'failed' => [],
|
109
|
+
'pending' => [],
|
110
|
+
}
|
111
|
+
|
112
|
+
json_data['examples'] && json_data['examples'].each do |ex|
|
113
|
+
example_results[ex['status']] << ex if example_results.key?(ex['status'])
|
114
|
+
end
|
115
|
+
|
116
|
+
example_results.each do |result, examples|
|
117
|
+
# Translate rspec example results to JUnit XML testcase results
|
118
|
+
state = case result
|
119
|
+
when 'passed' then :passed
|
120
|
+
when 'failed' then :failure
|
121
|
+
when 'pending' then :skipped
|
122
|
+
end
|
123
|
+
|
124
|
+
examples.each do |ex|
|
125
|
+
report.add_event(
|
126
|
+
source: 'rspec',
|
127
|
+
state: state,
|
128
|
+
file: ex['file_path'],
|
129
|
+
line: ex['line_number'],
|
130
|
+
test: ex['full_description'],
|
131
|
+
severity: ex['status'],
|
132
|
+
message: ex['pending_message'] || (ex['exception'] && ex['exception']['message']) || nil,
|
133
|
+
trace: (ex['exception'] && ex['exception']['backtrace']) || nil,
|
134
|
+
)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
return unless json_data['summary']
|
139
|
+
|
140
|
+
# TODO: standardize summary output
|
141
|
+
$stderr.puts ' ' << _('Evaluated %{total} tests in %{duration} seconds: %{failures} failures, %{pending} pending.') % {
|
142
|
+
total: json_data['summary']['example_count'],
|
143
|
+
duration: duration,
|
144
|
+
failures: json_data['summary']['failure_count'],
|
145
|
+
pending: json_data['summary']['pending_count'],
|
146
|
+
}
|
147
|
+
end
|
148
|
+
|
149
|
+
def self.merge_json_results(json_data)
|
150
|
+
merged_json_result = {}
|
151
|
+
|
152
|
+
# Merge messages
|
153
|
+
message_set = Set.new
|
154
|
+
json_data.each do |json|
|
155
|
+
next unless json['messages']
|
156
|
+
message_set |= json['messages']
|
157
|
+
end
|
158
|
+
merged_json_result['messages'] = message_set.to_a
|
159
|
+
|
160
|
+
# Merge examples
|
161
|
+
all_examples = []
|
162
|
+
json_data.each do |json|
|
163
|
+
next unless json['examples']
|
164
|
+
all_examples.concat json['examples']
|
165
|
+
end
|
166
|
+
merged_json_result['examples'] = all_examples
|
167
|
+
|
168
|
+
# Merge summaries
|
169
|
+
summary_hash = {
|
170
|
+
'example_count' => 0,
|
171
|
+
'failure_count' => 0,
|
172
|
+
'pending_count' => 0,
|
173
|
+
}
|
174
|
+
json_data.each do |json|
|
175
|
+
next unless json['summary']
|
176
|
+
summary_hash['example_count'] += json['summary']['example_count']
|
177
|
+
summary_hash['failure_count'] += json['summary']['failure_count']
|
178
|
+
summary_hash['pending_count'] += json['summary']['pending_count']
|
179
|
+
end
|
180
|
+
merged_json_result['summary'] = summary_hash
|
181
|
+
|
182
|
+
merged_json_result
|
183
|
+
end
|
184
|
+
|
185
|
+
# @return array of { :id, :full_description }
|
186
|
+
def self.list
|
187
|
+
PDK::Util::Bundler.ensure_binstubs!('rake')
|
188
|
+
|
189
|
+
command_argv = [File.join(PDK::Util.module_root, 'bin', 'rake'), 'spec_list_json']
|
190
|
+
command_argv.unshift(File.join(PDK::Util::RubyVersion.bin_path, 'ruby.exe')) if Gem.win_platform?
|
191
|
+
|
192
|
+
list_command = PDK::CLI::Exec::Command.new(*command_argv)
|
193
|
+
list_command.context = :module
|
194
|
+
output = list_command.execute!
|
195
|
+
|
196
|
+
rspec_json = PDK::Util.find_first_json_in(output[:stdout])
|
197
|
+
raise PDK::CLI::FatalError, _('Failed to find valid JSON in output from rspec: %{output}' % { output: output[:stdout] }) unless rspec_json
|
198
|
+
if rspec_json['examples'].empty?
|
199
|
+
rspec_message = rspec_json['messages'][0]
|
200
|
+
return [] if rspec_message == 'No examples found.'
|
201
|
+
|
202
|
+
raise PDK::CLI::FatalError, _('Unable to enumerate examples. rspec reported: %{message}' % { message: rspec_message })
|
203
|
+
else
|
204
|
+
examples = []
|
205
|
+
rspec_json['examples'].each do |example|
|
206
|
+
examples << { file_path: example['file_path'], id: example['id'], full_description: example['full_description'] }
|
207
|
+
end
|
208
|
+
examples
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
data/lib/pdk/util.rb
ADDED
@@ -0,0 +1,271 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
require 'pdk/util/version'
|
5
|
+
require 'pdk/util/windows'
|
6
|
+
require 'pdk/util/vendored_file'
|
7
|
+
require 'pdk/util/filesystem'
|
8
|
+
|
9
|
+
module PDK
|
10
|
+
module Util
|
11
|
+
MODULE_FOLDERS = %w[
|
12
|
+
manifests
|
13
|
+
lib
|
14
|
+
tasks
|
15
|
+
facts.d
|
16
|
+
functions
|
17
|
+
types
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
# Searches upwards from current working directory for the given target file.
|
21
|
+
#
|
22
|
+
# @param target [String] Name of file to search for.
|
23
|
+
# @param start_dir [String] Directory to start searching from, defaults to Dir.pwd
|
24
|
+
#
|
25
|
+
# @return [String, nil] Fully qualified path to the given target file if found,
|
26
|
+
# nil if the target file could not be found.
|
27
|
+
def find_upwards(target, start_dir = nil)
|
28
|
+
previous = nil
|
29
|
+
current = File.expand_path(start_dir || Dir.pwd)
|
30
|
+
|
31
|
+
until !File.directory?(current) || current == previous
|
32
|
+
filename = File.join(current, target)
|
33
|
+
return filename if File.file?(filename)
|
34
|
+
previous = current
|
35
|
+
current = File.expand_path('..', current)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
module_function :find_upwards
|
39
|
+
|
40
|
+
# Generate a name for a temporary directory.
|
41
|
+
#
|
42
|
+
# @param base [String] A string to base the name generation off.
|
43
|
+
#
|
44
|
+
# @return [String] The temporary directory path.
|
45
|
+
def make_tmpdir_name(base)
|
46
|
+
t = Time.now.strftime('%Y%m%d')
|
47
|
+
name = "#{base}#{t}-#{Process.pid}-#{rand(0x100000000).to_s(36)}"
|
48
|
+
File.join(Dir.tmpdir, name)
|
49
|
+
end
|
50
|
+
module_function :make_tmpdir_name
|
51
|
+
|
52
|
+
# Return an expanded, absolute path
|
53
|
+
#
|
54
|
+
# @param path [String] Existing path that may not be canonical
|
55
|
+
#
|
56
|
+
# @return [String] Canonical path
|
57
|
+
def canonical_path(path)
|
58
|
+
if Gem.win_platform?
|
59
|
+
unless File.exist?(path)
|
60
|
+
raise PDK::CLI::FatalError, _("Cannot resolve a full path to '%{path}', as it does not currently exist.") % { path: path }
|
61
|
+
end
|
62
|
+
PDK::Util::Windows::File.get_long_pathname(path)
|
63
|
+
else
|
64
|
+
File.expand_path(path)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
module_function :canonical_path
|
68
|
+
|
69
|
+
def package_install?
|
70
|
+
!PDK::Util::Version.version_file.nil?
|
71
|
+
end
|
72
|
+
module_function :package_install?
|
73
|
+
|
74
|
+
def development_mode?
|
75
|
+
(!PDK::Util::Version.git_ref.nil? || PDK::VERSION.end_with?('.pre'))
|
76
|
+
end
|
77
|
+
module_function :development_mode?
|
78
|
+
|
79
|
+
def gem_install?
|
80
|
+
!(package_install? || development_mode?)
|
81
|
+
end
|
82
|
+
module_function :gem_install?
|
83
|
+
|
84
|
+
def pdk_package_basedir
|
85
|
+
raise PDK::CLI::FatalError, _('Package basedir requested for non-package install.') unless package_install?
|
86
|
+
|
87
|
+
File.dirname(PDK::Util::Version.version_file)
|
88
|
+
end
|
89
|
+
module_function :pdk_package_basedir
|
90
|
+
|
91
|
+
def package_cachedir
|
92
|
+
File.join(pdk_package_basedir, 'share', 'cache')
|
93
|
+
end
|
94
|
+
module_function :package_cachedir
|
95
|
+
|
96
|
+
# Returns the fully qualified path to a per-user PDK cachedir.
|
97
|
+
#
|
98
|
+
# @return [String] Fully qualified path to per-user PDK cachedir.
|
99
|
+
def cachedir
|
100
|
+
if Gem.win_platform?
|
101
|
+
File.join(ENV['LOCALAPPDATA'], 'PDK', 'cache')
|
102
|
+
else
|
103
|
+
File.join(Dir.home, '.pdk', 'cache')
|
104
|
+
end
|
105
|
+
end
|
106
|
+
module_function :cachedir
|
107
|
+
|
108
|
+
# Returns path to the root of the module being worked on.
|
109
|
+
#
|
110
|
+
# @return [String, nil] Fully qualified base path to module, or nil if
|
111
|
+
# the current working dir does not appear to be within a module.
|
112
|
+
def module_root
|
113
|
+
metadata_path = find_upwards('metadata.json')
|
114
|
+
if metadata_path
|
115
|
+
File.dirname(metadata_path)
|
116
|
+
elsif in_module_root?
|
117
|
+
Dir.pwd
|
118
|
+
else
|
119
|
+
nil
|
120
|
+
end
|
121
|
+
end
|
122
|
+
module_function :module_root
|
123
|
+
|
124
|
+
# Returns true or false depending on if any of the common directories in a module
|
125
|
+
# are found in the current directory
|
126
|
+
#
|
127
|
+
# @return [boolean] True if any folders from MODULE_FOLDERS are found in the current dir,
|
128
|
+
# false otherwise.
|
129
|
+
def in_module_root?
|
130
|
+
PDK::Util::MODULE_FOLDERS.any? { |dir| File.directory?(dir) }
|
131
|
+
end
|
132
|
+
module_function :in_module_root?
|
133
|
+
|
134
|
+
# Iterate through possible JSON documents until we find one that is valid.
|
135
|
+
#
|
136
|
+
# @param [String] text the text in which to find a JSON document
|
137
|
+
# @return [Hash, nil] subset of text as Hash of first valid JSON found, or nil if no valid
|
138
|
+
# JSON found in the text
|
139
|
+
def find_first_json_in(text)
|
140
|
+
find_valid_json_in(text)
|
141
|
+
end
|
142
|
+
module_function :find_first_json_in
|
143
|
+
|
144
|
+
# Iterate through possible JSON documents for all valid JSON
|
145
|
+
#
|
146
|
+
# @param [String] text the text in which to find JSON document(s)
|
147
|
+
# @return [Array<Hash>] subset of text as Array of all JSON object found, empty Array if none are found
|
148
|
+
# JSON found in the text
|
149
|
+
def find_all_json_in(text)
|
150
|
+
find_valid_json_in(text, break_on_first: false)
|
151
|
+
end
|
152
|
+
module_function :find_all_json_in
|
153
|
+
|
154
|
+
# Iterate through possible JSON documents until we find one that is valid.
|
155
|
+
#
|
156
|
+
# @param [String] text the text in which to find a JSON document
|
157
|
+
# @param [Hash] opts options
|
158
|
+
# @option opts [Boolean] :break_on_first Whether or not to break after valid JSON is found, defaults to true
|
159
|
+
#
|
160
|
+
# @return [Hash, Array<Hash>, nil] subset of text as Hash of first valid JSON found, array of all valid JSON found, or nil if no valid
|
161
|
+
# JSON found in the text
|
162
|
+
#
|
163
|
+
# @private
|
164
|
+
def find_valid_json_in(text, opts = {})
|
165
|
+
break_on_first = opts.key?(:break_on_first) ? opts[:break_on_first] : true
|
166
|
+
|
167
|
+
json_result = break_on_first ? nil : []
|
168
|
+
|
169
|
+
text.scan(%r{\{(?:[^{}]|(?:\g<0>))*\}}x) do |str|
|
170
|
+
begin
|
171
|
+
if break_on_first
|
172
|
+
json_result = JSON.parse(str)
|
173
|
+
break
|
174
|
+
else
|
175
|
+
json_result.push(JSON.parse(str))
|
176
|
+
end
|
177
|
+
rescue JSON::ParserError
|
178
|
+
next
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
json_result
|
183
|
+
end
|
184
|
+
module_function :find_valid_json_in
|
185
|
+
|
186
|
+
# Returns the targets' paths relative to the working directory
|
187
|
+
#
|
188
|
+
# @return [Array<String>] The absolute or path to the target
|
189
|
+
def targets_relative_to_pwd(targets)
|
190
|
+
targets.map do |t|
|
191
|
+
if Pathname.new(t).absolute?
|
192
|
+
Pathname.new(t).relative_path_from(Pathname.pwd)
|
193
|
+
else
|
194
|
+
t
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
module_function :targets_relative_to_pwd
|
199
|
+
|
200
|
+
def default_template_url
|
201
|
+
answer_file_url = PDK.answers['template-url']
|
202
|
+
|
203
|
+
return puppetlabs_template_url if answer_file_url.nil?
|
204
|
+
|
205
|
+
# Ignore answer file template-url if the value is the old or new default.
|
206
|
+
return puppetlabs_template_url if answer_file_url == 'https://github.com/puppetlabs/pdk-module-template'
|
207
|
+
return puppetlabs_template_url if answer_file_url == puppetlabs_template_url
|
208
|
+
|
209
|
+
unless PDK::Util::Git.repo?(answer_file_url)
|
210
|
+
PDK.logger.warn(_("Unable to access the previously used template '%{template}', using the default template instead.") % { template: answer_file_url })
|
211
|
+
PDK.answers.update!('template-url' => nil)
|
212
|
+
return puppetlabs_template_url
|
213
|
+
end
|
214
|
+
|
215
|
+
answer_file_url
|
216
|
+
end
|
217
|
+
module_function :default_template_url
|
218
|
+
|
219
|
+
def puppetlabs_template_url
|
220
|
+
if package_install?
|
221
|
+
'file://' + File.join(package_cachedir, 'pdk-templates.git')
|
222
|
+
else
|
223
|
+
'https://github.com/puppetlabs/pdk-templates'
|
224
|
+
end
|
225
|
+
end
|
226
|
+
module_function :puppetlabs_template_url
|
227
|
+
|
228
|
+
def default_template_ref
|
229
|
+
# TODO: This should respect a --template-ref option if we add that
|
230
|
+
return 'origin/master' if default_template_url != puppetlabs_template_url
|
231
|
+
|
232
|
+
puppetlabs_template_ref
|
233
|
+
end
|
234
|
+
module_function :default_template_ref
|
235
|
+
|
236
|
+
def puppetlabs_template_ref
|
237
|
+
if PDK::Util.development_mode?
|
238
|
+
'origin/master'
|
239
|
+
else
|
240
|
+
PDK::TEMPLATE_REF
|
241
|
+
end
|
242
|
+
end
|
243
|
+
module_function :puppetlabs_template_ref
|
244
|
+
|
245
|
+
# TO-DO: Refactor replacement of lib/pdk/module/build.rb:metadata to use this function instead
|
246
|
+
def module_metadata
|
247
|
+
PDK::Module::Metadata.from_file(File.join(module_root, 'metadata.json')).data
|
248
|
+
end
|
249
|
+
module_function :module_metadata
|
250
|
+
|
251
|
+
# TO-DO: Refactor replacement of lib/pdk/module/build.rb:module_pdk_compatible? to use this function instead
|
252
|
+
def module_pdk_compatible?
|
253
|
+
['pdk-version', 'template-url'].any? { |key| module_metadata.key?(key) }
|
254
|
+
end
|
255
|
+
module_function :module_pdk_compatible?
|
256
|
+
|
257
|
+
def module_pdk_version
|
258
|
+
metadata = module_metadata
|
259
|
+
|
260
|
+
if metadata.nil? || metadata.fetch('pdk-version', nil).nil?
|
261
|
+
nil
|
262
|
+
else
|
263
|
+
metadata['pdk-version'].split.first
|
264
|
+
end
|
265
|
+
rescue ArgumentError => e
|
266
|
+
PDK.logger.error(e)
|
267
|
+
nil
|
268
|
+
end
|
269
|
+
module_function :module_pdk_version
|
270
|
+
end
|
271
|
+
end
|