pdk 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1336 -1329
- data/LICENSE +201 -201
- data/README.md +163 -163
- data/exe/pdk +10 -10
- data/lib/pdk/analytics/client/google_analytics.rb +143 -143
- data/lib/pdk/analytics/client/noop.rb +25 -25
- data/lib/pdk/analytics/util.rb +19 -19
- data/lib/pdk/analytics.rb +30 -30
- data/lib/pdk/answer_file.rb +12 -12
- data/lib/pdk/bolt.rb +19 -19
- data/lib/pdk/cli/build.rb +82 -82
- data/lib/pdk/cli/bundle.rb +48 -48
- data/lib/pdk/cli/config/get.rb +26 -26
- data/lib/pdk/cli/config.rb +22 -22
- data/lib/pdk/cli/console.rb +148 -148
- data/lib/pdk/cli/convert.rb +52 -52
- data/lib/pdk/cli/env.rb +52 -52
- data/lib/pdk/cli/errors.rb +25 -25
- data/lib/pdk/cli/exec/command.rb +293 -293
- data/lib/pdk/cli/exec/interactive_command.rb +114 -114
- data/lib/pdk/cli/exec.rb +84 -84
- data/lib/pdk/cli/exec_group.rb +104 -104
- data/lib/pdk/cli/get/config.rb +24 -24
- data/lib/pdk/cli/get.rb +20 -20
- data/lib/pdk/cli/module/build.rb +12 -12
- data/lib/pdk/cli/module/generate.rb +47 -47
- data/lib/pdk/cli/module.rb +14 -14
- data/lib/pdk/cli/new/class.rb +32 -32
- data/lib/pdk/cli/new/defined_type.rb +32 -32
- data/lib/pdk/cli/new/fact.rb +29 -29
- data/lib/pdk/cli/new/function.rb +29 -29
- data/lib/pdk/cli/new/module.rb +53 -53
- data/lib/pdk/cli/new/provider.rb +29 -29
- data/lib/pdk/cli/new/task.rb +34 -34
- data/lib/pdk/cli/new/test.rb +52 -52
- data/lib/pdk/cli/new/transport.rb +27 -27
- data/lib/pdk/cli/new.rb +21 -21
- data/lib/pdk/cli/release/prep.rb +39 -39
- data/lib/pdk/cli/release/publish.rb +50 -50
- data/lib/pdk/cli/release.rb +194 -194
- data/lib/pdk/cli/remove/config.rb +80 -80
- data/lib/pdk/cli/remove.rb +20 -20
- data/lib/pdk/cli/set/config.rb +119 -119
- data/lib/pdk/cli/set.rb +20 -20
- data/lib/pdk/cli/test/unit.rb +90 -90
- data/lib/pdk/cli/test.rb +11 -11
- data/lib/pdk/cli/update.rb +64 -64
- data/lib/pdk/cli/util/command_redirector.rb +27 -27
- data/lib/pdk/cli/util/interview.rb +72 -72
- data/lib/pdk/cli/util/option_normalizer.rb +55 -55
- data/lib/pdk/cli/util/option_validator.rb +68 -68
- data/lib/pdk/cli/util/spinner.rb +13 -13
- data/lib/pdk/cli/util/update_manager_printer.rb +82 -82
- data/lib/pdk/cli/util.rb +305 -305
- data/lib/pdk/cli/validate.rb +116 -116
- data/lib/pdk/cli.rb +175 -175
- data/lib/pdk/config/analytics_schema.json +26 -26
- data/lib/pdk/config/errors.rb +5 -5
- data/lib/pdk/config/ini_file.rb +183 -183
- data/lib/pdk/config/ini_file_setting.rb +39 -39
- data/lib/pdk/config/json.rb +34 -34
- data/lib/pdk/config/json_schema_namespace.rb +142 -142
- data/lib/pdk/config/json_schema_setting.rb +53 -53
- data/lib/pdk/config/json_with_schema.rb +49 -49
- data/lib/pdk/config/namespace.rb +354 -354
- data/lib/pdk/config/setting.rb +135 -135
- data/lib/pdk/config/validator.rb +31 -31
- data/lib/pdk/config/yaml.rb +46 -46
- data/lib/pdk/config/yaml_with_schema.rb +59 -59
- data/lib/pdk/config.rb +390 -390
- data/lib/pdk/context/control_repo.rb +60 -60
- data/lib/pdk/context/module.rb +28 -28
- data/lib/pdk/context/none.rb +22 -22
- data/lib/pdk/context.rb +99 -99
- data/lib/pdk/control_repo.rb +90 -90
- data/lib/pdk/generate/defined_type.rb +43 -43
- data/lib/pdk/generate/fact.rb +25 -25
- data/lib/pdk/generate/function.rb +48 -48
- data/lib/pdk/generate/module.rb +352 -352
- data/lib/pdk/generate/provider.rb +28 -28
- data/lib/pdk/generate/puppet_class.rb +43 -43
- data/lib/pdk/generate/puppet_object.rb +232 -232
- data/lib/pdk/generate/task.rb +68 -68
- data/lib/pdk/generate/transport.rb +33 -33
- data/lib/pdk/generate.rb +24 -24
- data/lib/pdk/i18n.rb +4 -4
- data/lib/pdk/logger.rb +45 -45
- data/lib/pdk/module/build.rb +322 -322
- data/lib/pdk/module/convert.rb +296 -296
- data/lib/pdk/module/metadata.rb +202 -202
- data/lib/pdk/module/release.rb +260 -260
- data/lib/pdk/module/update.rb +131 -131
- data/lib/pdk/module/update_manager.rb +227 -227
- data/lib/pdk/module.rb +30 -30
- data/lib/pdk/report/event.rb +370 -370
- data/lib/pdk/report.rb +121 -121
- data/lib/pdk/template/fetcher/git.rb +85 -85
- data/lib/pdk/template/fetcher/local.rb +28 -28
- data/lib/pdk/template/fetcher.rb +98 -98
- data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -116
- data/lib/pdk/template/renderer/v1/renderer.rb +132 -132
- data/lib/pdk/template/renderer/v1/template_file.rb +102 -102
- data/lib/pdk/template/renderer/v1.rb +25 -25
- data/lib/pdk/template/renderer.rb +96 -96
- data/lib/pdk/template/template_dir.rb +67 -67
- data/lib/pdk/template.rb +59 -59
- data/lib/pdk/tests/unit.rb +252 -252
- data/lib/pdk/util/bundler.rb +259 -259
- data/lib/pdk/util/changelog_generator.rb +137 -137
- data/lib/pdk/util/env.rb +47 -47
- data/lib/pdk/util/filesystem.rb +138 -138
- data/lib/pdk/util/git.rb +179 -179
- data/lib/pdk/util/json_finder.rb +85 -85
- data/lib/pdk/util/puppet_strings.rb +125 -125
- data/lib/pdk/util/puppet_version.rb +266 -266
- data/lib/pdk/util/ruby_version.rb +179 -179
- data/lib/pdk/util/template_uri.rb +295 -295
- data/lib/pdk/util/vendored_file.rb +93 -93
- data/lib/pdk/util/version.rb +43 -43
- data/lib/pdk/util/windows/api_types.rb +82 -82
- data/lib/pdk/util/windows/file.rb +36 -36
- data/lib/pdk/util/windows/process.rb +79 -79
- data/lib/pdk/util/windows/string.rb +16 -16
- data/lib/pdk/util/windows.rb +15 -15
- data/lib/pdk/util.rb +278 -278
- data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -23
- data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -98
- data/lib/pdk/validate/external_command_validator.rb +208 -208
- data/lib/pdk/validate/internal_ruby_validator.rb +100 -100
- data/lib/pdk/validate/invokable_validator.rb +228 -228
- data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -86
- data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -78
- data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -20
- data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -133
- data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -66
- data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -137
- data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -21
- data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -80
- data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -19
- data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -88
- data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -50
- data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -20
- data/lib/pdk/validate/validator.rb +118 -118
- data/lib/pdk/validate/validator_group.rb +104 -104
- data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -95
- data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -19
- data/lib/pdk/validate.rb +94 -94
- data/lib/pdk/version.rb +4 -4
- data/lib/pdk.rb +76 -76
- data/locales/config.yaml +21 -21
- data/locales/pdk.pot +2111 -2094
- metadata +3 -3
data/lib/pdk/module/convert.rb
CHANGED
@@ -1,296 +1,296 @@
|
|
1
|
-
require 'pdk'
|
2
|
-
|
3
|
-
module PDK
|
4
|
-
module Module
|
5
|
-
class Convert
|
6
|
-
def self.invoke(module_dir, options)
|
7
|
-
new(module_dir, options).run
|
8
|
-
end
|
9
|
-
|
10
|
-
attr_reader :module_dir
|
11
|
-
|
12
|
-
attr_reader :options
|
13
|
-
|
14
|
-
def initialize(module_dir, options = {})
|
15
|
-
@module_dir = module_dir
|
16
|
-
@options = options
|
17
|
-
end
|
18
|
-
|
19
|
-
def convert?
|
20
|
-
instance_of?(PDK::Module::Convert)
|
21
|
-
end
|
22
|
-
|
23
|
-
def run
|
24
|
-
stage_changes!
|
25
|
-
|
26
|
-
unless update_manager.changes?
|
27
|
-
if adding_tests?
|
28
|
-
add_tests!
|
29
|
-
print_result 'Convert completed'
|
30
|
-
else
|
31
|
-
require 'pdk/report'
|
32
|
-
|
33
|
-
PDK::Report.default_target.puts(_('No changes required.'))
|
34
|
-
end
|
35
|
-
|
36
|
-
return
|
37
|
-
end
|
38
|
-
|
39
|
-
print_summary
|
40
|
-
|
41
|
-
full_report('convert_report.txt') unless update_manager.changes[:modified].empty?
|
42
|
-
|
43
|
-
return if noop?
|
44
|
-
|
45
|
-
unless force?
|
46
|
-
require 'pdk/cli/util'
|
47
|
-
|
48
|
-
PDK.logger.info _(
|
49
|
-
'Module conversion is a potentially destructive action. ' \
|
50
|
-
'Ensure that you have committed your module to a version control ' \
|
51
|
-
'system or have a backup, and review the changes above before continuing.',
|
52
|
-
)
|
53
|
-
continue = PDK::CLI::Util.prompt_for_yes(_('Do you want to continue and make these changes to your module?'))
|
54
|
-
return unless continue
|
55
|
-
end
|
56
|
-
|
57
|
-
# Remove these files straight away as these changes are not something
|
58
|
-
# that the user needs to review.
|
59
|
-
update_manager.unlink_file('Gemfile.lock')
|
60
|
-
update_manager.unlink_file(File.join('.bundle', 'config'))
|
61
|
-
|
62
|
-
update_manager.sync_changes!
|
63
|
-
|
64
|
-
require 'pdk/util/bundler'
|
65
|
-
PDK::Util::Bundler.ensure_bundle!
|
66
|
-
|
67
|
-
add_tests! if adding_tests?
|
68
|
-
|
69
|
-
print_result 'Convert completed'
|
70
|
-
end
|
71
|
-
|
72
|
-
def noop?
|
73
|
-
options[:noop]
|
74
|
-
end
|
75
|
-
|
76
|
-
def force?
|
77
|
-
options[:force]
|
78
|
-
end
|
79
|
-
|
80
|
-
def add_tests?
|
81
|
-
options[:'add-tests']
|
82
|
-
end
|
83
|
-
|
84
|
-
def adding_tests?
|
85
|
-
add_tests? && missing_tests?
|
86
|
-
end
|
87
|
-
|
88
|
-
def missing_tests?
|
89
|
-
!available_test_generators.empty?
|
90
|
-
end
|
91
|
-
|
92
|
-
def available_test_generators
|
93
|
-
# Only select generators which can run and have no pre-existing files
|
94
|
-
test_generators.select do |gen|
|
95
|
-
if gen.can_run?
|
96
|
-
gen.template_files.none? { |_, dst_path| PDK::Util::Filesystem.exist?(File.join(gen.context.root_path, dst_path)) }
|
97
|
-
else
|
98
|
-
false
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_generators(context = PDK.context)
|
104
|
-
return @test_generators unless @test_generators.nil?
|
105
|
-
require 'pdk/util/puppet_strings'
|
106
|
-
|
107
|
-
test_gens = PDK::Util::PuppetStrings.all_objects.map do |generator, objects|
|
108
|
-
(objects || []).map do |obj|
|
109
|
-
generator.new(context, obj['name'], spec_only: true)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
@test_generators = test_gens.flatten
|
114
|
-
end
|
115
|
-
|
116
|
-
def stage_tests!(manager)
|
117
|
-
available_test_generators.each do |gen|
|
118
|
-
gen.stage_changes(manager)
|
119
|
-
end
|
120
|
-
manager
|
121
|
-
end
|
122
|
-
|
123
|
-
def add_tests!
|
124
|
-
update_manager.clear!
|
125
|
-
stage_tests!(update_manager)
|
126
|
-
|
127
|
-
if update_manager.changes?
|
128
|
-
update_manager.sync_changes!
|
129
|
-
print_summary
|
130
|
-
else
|
131
|
-
PDK::Report.default_target.puts(_('No test changes required.'))
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
def stage_changes!(context = PDK.context)
|
136
|
-
require 'pdk/util/filesystem'
|
137
|
-
|
138
|
-
metadata_path = File.join(module_dir, 'metadata.json')
|
139
|
-
|
140
|
-
PDK::Template.with(template_uri, context) do |template_dir|
|
141
|
-
new_metadata = update_metadata(metadata_path, template_dir.metadata)
|
142
|
-
if options[:noop] && new_metadata.nil?
|
143
|
-
update_manager.add_file(metadata_path, '')
|
144
|
-
elsif PDK::Util::Filesystem.file?(metadata_path)
|
145
|
-
update_manager.modify_file(metadata_path, new_metadata.to_json)
|
146
|
-
else
|
147
|
-
update_manager.add_file(metadata_path, new_metadata.to_json)
|
148
|
-
end
|
149
|
-
|
150
|
-
# new_metadata == nil when creating a new module but with --noop@
|
151
|
-
module_name = new_metadata.nil? ? 'new-module' : new_metadata.data['name']
|
152
|
-
metadata_for_render = new_metadata.nil? ? {} : new_metadata.data
|
153
|
-
|
154
|
-
template_dir.render_new_module(module_name, metadata_for_render) do |relative_file_path, file_content, file_status|
|
155
|
-
absolute_file_path = File.join(module_dir, relative_file_path)
|
156
|
-
case file_status
|
157
|
-
when :unmanage
|
158
|
-
PDK.logger.debug(_("skipping '%{path}'") % { path: absolute_file_path })
|
159
|
-
when :delete
|
160
|
-
update_manager.remove_file(absolute_file_path)
|
161
|
-
when :init
|
162
|
-
if convert? && !PDK::Util::Filesystem.exist?(absolute_file_path)
|
163
|
-
update_manager.add_file(absolute_file_path, file_content)
|
164
|
-
end
|
165
|
-
when :manage
|
166
|
-
if PDK::Util::Filesystem.exist?(absolute_file_path)
|
167
|
-
update_manager.modify_file(absolute_file_path, file_content)
|
168
|
-
else
|
169
|
-
update_manager.add_file(absolute_file_path, file_content)
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
|
-
rescue ArgumentError => e
|
175
|
-
raise PDK::CLI::ExitWithError, e
|
176
|
-
end
|
177
|
-
|
178
|
-
def update_manager
|
179
|
-
require 'pdk/module/update_manager'
|
180
|
-
|
181
|
-
@update_manager ||= PDK::Module::UpdateManager.new
|
182
|
-
end
|
183
|
-
|
184
|
-
def template_uri
|
185
|
-
require 'pdk/util/template_uri'
|
186
|
-
|
187
|
-
@template_uri ||= PDK::Util::TemplateURI.new(options)
|
188
|
-
end
|
189
|
-
|
190
|
-
def update_metadata(metadata_path, template_metadata)
|
191
|
-
require 'pdk/generate/module'
|
192
|
-
require 'pdk/util/filesystem'
|
193
|
-
require 'pdk/module/metadata'
|
194
|
-
|
195
|
-
if PDK::Util::Filesystem.file?(metadata_path)
|
196
|
-
unless PDK::Util::Filesystem.readable?(metadata_path)
|
197
|
-
raise PDK::CLI::ExitWithError, _('Unable to update module metadata; %{path} exists but it is not readable.') % {
|
198
|
-
path: metadata_path,
|
199
|
-
}
|
200
|
-
end
|
201
|
-
|
202
|
-
begin
|
203
|
-
metadata = PDK::Module::Metadata.from_file(metadata_path)
|
204
|
-
new_values = PDK::Module::Metadata::DEFAULTS.select do |key, _|
|
205
|
-
!metadata.data.key?(key) || metadata.data[key].nil? ||
|
206
|
-
(key == 'requirements' && metadata.data[key].empty?)
|
207
|
-
end
|
208
|
-
metadata.update!(new_values)
|
209
|
-
rescue ArgumentError
|
210
|
-
metadata = PDK::Generate::Module.prepare_metadata(options) unless options[:noop]
|
211
|
-
end
|
212
|
-
elsif PDK::Util::Filesystem.exist?(metadata_path)
|
213
|
-
raise PDK::CLI::ExitWithError, _('Unable to update module metadata; %{path} exists but it is not a file.') % {
|
214
|
-
path: metadata_path,
|
215
|
-
}
|
216
|
-
else
|
217
|
-
return if options[:noop]
|
218
|
-
|
219
|
-
project_dir = File.basename(Dir.pwd)
|
220
|
-
options[:module_name] = project_dir.split('-', 2).compact[-1]
|
221
|
-
options[:prompt] = false
|
222
|
-
options[:'skip-interview'] = true if options[:force]
|
223
|
-
|
224
|
-
metadata = PDK::Generate::Module.prepare_metadata(options)
|
225
|
-
end
|
226
|
-
|
227
|
-
metadata.update!(template_metadata)
|
228
|
-
metadata
|
229
|
-
end
|
230
|
-
|
231
|
-
def summary
|
232
|
-
summary = {}
|
233
|
-
update_manager.changes.each do |category, update_category|
|
234
|
-
if update_category.respond_to?(:keys)
|
235
|
-
updated_files = update_category.keys
|
236
|
-
else
|
237
|
-
begin
|
238
|
-
updated_files = update_category.map { |file| file[:path] }
|
239
|
-
rescue TypeError
|
240
|
-
updated_files = update_category.to_a
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
summary[category] = updated_files
|
245
|
-
end
|
246
|
-
|
247
|
-
summary
|
248
|
-
end
|
249
|
-
|
250
|
-
def print_summary
|
251
|
-
require 'pdk/report'
|
252
|
-
|
253
|
-
footer = false
|
254
|
-
|
255
|
-
summary.keys.each do |category|
|
256
|
-
next if summary[category].empty?
|
257
|
-
|
258
|
-
PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner("Files to be #{category}", 40) })
|
259
|
-
PDK::Report.default_target.puts(summary[category])
|
260
|
-
footer = true
|
261
|
-
end
|
262
|
-
|
263
|
-
PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner('', 40) }) if footer
|
264
|
-
end
|
265
|
-
|
266
|
-
def print_result(banner_text)
|
267
|
-
require 'pdk/report'
|
268
|
-
|
269
|
-
PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner(banner_text, 40) })
|
270
|
-
summary_to_print = summary.map { |k, v| "#{v.length} files #{k}" unless v.empty? }.compact
|
271
|
-
PDK::Report.default_target.puts(_("\n%{summary}\n\n") % { summary: "#{summary_to_print.join(', ')}." })
|
272
|
-
end
|
273
|
-
|
274
|
-
def full_report(path)
|
275
|
-
require 'pdk/report'
|
276
|
-
|
277
|
-
report = ["/* Report generated by PDK at #{Time.now} */"]
|
278
|
-
report.concat(update_manager.changes[:modified].map { |_, diff| "\n\n\n#{diff}" })
|
279
|
-
PDK::Util::Filesystem.write_file(path, report.join)
|
280
|
-
PDK::Report.default_target.puts(_("\nYou can find a report of differences in %{path}.\n\n") % { path: path })
|
281
|
-
end
|
282
|
-
|
283
|
-
def generate_banner(text, width = 80)
|
284
|
-
padding = width - text.length
|
285
|
-
banner = ''
|
286
|
-
padding_char = '-'
|
287
|
-
|
288
|
-
(padding / 2.0).ceil.times { banner << padding_char }
|
289
|
-
banner << text
|
290
|
-
(padding / 2.0).floor.times { banner << padding_char }
|
291
|
-
|
292
|
-
banner
|
293
|
-
end
|
294
|
-
end
|
295
|
-
end
|
296
|
-
end
|
1
|
+
require 'pdk'
|
2
|
+
|
3
|
+
module PDK
|
4
|
+
module Module
|
5
|
+
class Convert
|
6
|
+
def self.invoke(module_dir, options)
|
7
|
+
new(module_dir, options).run
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :module_dir
|
11
|
+
|
12
|
+
attr_reader :options
|
13
|
+
|
14
|
+
def initialize(module_dir, options = {})
|
15
|
+
@module_dir = module_dir
|
16
|
+
@options = options
|
17
|
+
end
|
18
|
+
|
19
|
+
def convert?
|
20
|
+
instance_of?(PDK::Module::Convert)
|
21
|
+
end
|
22
|
+
|
23
|
+
def run
|
24
|
+
stage_changes!
|
25
|
+
|
26
|
+
unless update_manager.changes?
|
27
|
+
if adding_tests?
|
28
|
+
add_tests!
|
29
|
+
print_result 'Convert completed'
|
30
|
+
else
|
31
|
+
require 'pdk/report'
|
32
|
+
|
33
|
+
PDK::Report.default_target.puts(_('No changes required.'))
|
34
|
+
end
|
35
|
+
|
36
|
+
return
|
37
|
+
end
|
38
|
+
|
39
|
+
print_summary
|
40
|
+
|
41
|
+
full_report('convert_report.txt') unless update_manager.changes[:modified].empty?
|
42
|
+
|
43
|
+
return if noop?
|
44
|
+
|
45
|
+
unless force?
|
46
|
+
require 'pdk/cli/util'
|
47
|
+
|
48
|
+
PDK.logger.info _(
|
49
|
+
'Module conversion is a potentially destructive action. ' \
|
50
|
+
'Ensure that you have committed your module to a version control ' \
|
51
|
+
'system or have a backup, and review the changes above before continuing.',
|
52
|
+
)
|
53
|
+
continue = PDK::CLI::Util.prompt_for_yes(_('Do you want to continue and make these changes to your module?'))
|
54
|
+
return unless continue
|
55
|
+
end
|
56
|
+
|
57
|
+
# Remove these files straight away as these changes are not something
|
58
|
+
# that the user needs to review.
|
59
|
+
update_manager.unlink_file('Gemfile.lock')
|
60
|
+
update_manager.unlink_file(File.join('.bundle', 'config'))
|
61
|
+
|
62
|
+
update_manager.sync_changes!
|
63
|
+
|
64
|
+
require 'pdk/util/bundler'
|
65
|
+
PDK::Util::Bundler.ensure_bundle!
|
66
|
+
|
67
|
+
add_tests! if adding_tests?
|
68
|
+
|
69
|
+
print_result 'Convert completed'
|
70
|
+
end
|
71
|
+
|
72
|
+
def noop?
|
73
|
+
options[:noop]
|
74
|
+
end
|
75
|
+
|
76
|
+
def force?
|
77
|
+
options[:force]
|
78
|
+
end
|
79
|
+
|
80
|
+
def add_tests?
|
81
|
+
options[:'add-tests']
|
82
|
+
end
|
83
|
+
|
84
|
+
def adding_tests?
|
85
|
+
add_tests? && missing_tests?
|
86
|
+
end
|
87
|
+
|
88
|
+
def missing_tests?
|
89
|
+
!available_test_generators.empty?
|
90
|
+
end
|
91
|
+
|
92
|
+
def available_test_generators
|
93
|
+
# Only select generators which can run and have no pre-existing files
|
94
|
+
test_generators.select do |gen|
|
95
|
+
if gen.can_run?
|
96
|
+
gen.template_files.none? { |_, dst_path| PDK::Util::Filesystem.exist?(File.join(gen.context.root_path, dst_path)) }
|
97
|
+
else
|
98
|
+
false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_generators(context = PDK.context)
|
104
|
+
return @test_generators unless @test_generators.nil?
|
105
|
+
require 'pdk/util/puppet_strings'
|
106
|
+
|
107
|
+
test_gens = PDK::Util::PuppetStrings.all_objects.map do |generator, objects|
|
108
|
+
(objects || []).map do |obj|
|
109
|
+
generator.new(context, obj['name'], spec_only: true)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
@test_generators = test_gens.flatten
|
114
|
+
end
|
115
|
+
|
116
|
+
def stage_tests!(manager)
|
117
|
+
available_test_generators.each do |gen|
|
118
|
+
gen.stage_changes(manager)
|
119
|
+
end
|
120
|
+
manager
|
121
|
+
end
|
122
|
+
|
123
|
+
def add_tests!
|
124
|
+
update_manager.clear!
|
125
|
+
stage_tests!(update_manager)
|
126
|
+
|
127
|
+
if update_manager.changes?
|
128
|
+
update_manager.sync_changes!
|
129
|
+
print_summary
|
130
|
+
else
|
131
|
+
PDK::Report.default_target.puts(_('No test changes required.'))
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def stage_changes!(context = PDK.context)
|
136
|
+
require 'pdk/util/filesystem'
|
137
|
+
|
138
|
+
metadata_path = File.join(module_dir, 'metadata.json')
|
139
|
+
|
140
|
+
PDK::Template.with(template_uri, context) do |template_dir|
|
141
|
+
new_metadata = update_metadata(metadata_path, template_dir.metadata)
|
142
|
+
if options[:noop] && new_metadata.nil?
|
143
|
+
update_manager.add_file(metadata_path, '')
|
144
|
+
elsif PDK::Util::Filesystem.file?(metadata_path)
|
145
|
+
update_manager.modify_file(metadata_path, new_metadata.to_json)
|
146
|
+
else
|
147
|
+
update_manager.add_file(metadata_path, new_metadata.to_json)
|
148
|
+
end
|
149
|
+
|
150
|
+
# new_metadata == nil when creating a new module but with --noop@
|
151
|
+
module_name = new_metadata.nil? ? 'new-module' : new_metadata.data['name']
|
152
|
+
metadata_for_render = new_metadata.nil? ? {} : new_metadata.data
|
153
|
+
|
154
|
+
template_dir.render_new_module(module_name, metadata_for_render) do |relative_file_path, file_content, file_status|
|
155
|
+
absolute_file_path = File.join(module_dir, relative_file_path)
|
156
|
+
case file_status
|
157
|
+
when :unmanage
|
158
|
+
PDK.logger.debug(_("skipping '%{path}'") % { path: absolute_file_path })
|
159
|
+
when :delete
|
160
|
+
update_manager.remove_file(absolute_file_path)
|
161
|
+
when :init
|
162
|
+
if convert? && !PDK::Util::Filesystem.exist?(absolute_file_path)
|
163
|
+
update_manager.add_file(absolute_file_path, file_content)
|
164
|
+
end
|
165
|
+
when :manage
|
166
|
+
if PDK::Util::Filesystem.exist?(absolute_file_path)
|
167
|
+
update_manager.modify_file(absolute_file_path, file_content)
|
168
|
+
else
|
169
|
+
update_manager.add_file(absolute_file_path, file_content)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
rescue ArgumentError => e
|
175
|
+
raise PDK::CLI::ExitWithError, e
|
176
|
+
end
|
177
|
+
|
178
|
+
def update_manager
|
179
|
+
require 'pdk/module/update_manager'
|
180
|
+
|
181
|
+
@update_manager ||= PDK::Module::UpdateManager.new
|
182
|
+
end
|
183
|
+
|
184
|
+
def template_uri
|
185
|
+
require 'pdk/util/template_uri'
|
186
|
+
|
187
|
+
@template_uri ||= PDK::Util::TemplateURI.new(options)
|
188
|
+
end
|
189
|
+
|
190
|
+
def update_metadata(metadata_path, template_metadata)
|
191
|
+
require 'pdk/generate/module'
|
192
|
+
require 'pdk/util/filesystem'
|
193
|
+
require 'pdk/module/metadata'
|
194
|
+
|
195
|
+
if PDK::Util::Filesystem.file?(metadata_path)
|
196
|
+
unless PDK::Util::Filesystem.readable?(metadata_path)
|
197
|
+
raise PDK::CLI::ExitWithError, _('Unable to update module metadata; %{path} exists but it is not readable.') % {
|
198
|
+
path: metadata_path,
|
199
|
+
}
|
200
|
+
end
|
201
|
+
|
202
|
+
begin
|
203
|
+
metadata = PDK::Module::Metadata.from_file(metadata_path)
|
204
|
+
new_values = PDK::Module::Metadata::DEFAULTS.select do |key, _|
|
205
|
+
!metadata.data.key?(key) || metadata.data[key].nil? ||
|
206
|
+
(key == 'requirements' && metadata.data[key].empty?)
|
207
|
+
end
|
208
|
+
metadata.update!(new_values)
|
209
|
+
rescue ArgumentError
|
210
|
+
metadata = PDK::Generate::Module.prepare_metadata(options) unless options[:noop]
|
211
|
+
end
|
212
|
+
elsif PDK::Util::Filesystem.exist?(metadata_path)
|
213
|
+
raise PDK::CLI::ExitWithError, _('Unable to update module metadata; %{path} exists but it is not a file.') % {
|
214
|
+
path: metadata_path,
|
215
|
+
}
|
216
|
+
else
|
217
|
+
return if options[:noop]
|
218
|
+
|
219
|
+
project_dir = File.basename(Dir.pwd)
|
220
|
+
options[:module_name] = project_dir.split('-', 2).compact[-1]
|
221
|
+
options[:prompt] = false
|
222
|
+
options[:'skip-interview'] = true if options[:force]
|
223
|
+
|
224
|
+
metadata = PDK::Generate::Module.prepare_metadata(options)
|
225
|
+
end
|
226
|
+
|
227
|
+
metadata.update!(template_metadata)
|
228
|
+
metadata
|
229
|
+
end
|
230
|
+
|
231
|
+
def summary
|
232
|
+
summary = {}
|
233
|
+
update_manager.changes.each do |category, update_category|
|
234
|
+
if update_category.respond_to?(:keys)
|
235
|
+
updated_files = update_category.keys
|
236
|
+
else
|
237
|
+
begin
|
238
|
+
updated_files = update_category.map { |file| file[:path] }
|
239
|
+
rescue TypeError
|
240
|
+
updated_files = update_category.to_a
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
summary[category] = updated_files
|
245
|
+
end
|
246
|
+
|
247
|
+
summary
|
248
|
+
end
|
249
|
+
|
250
|
+
def print_summary
|
251
|
+
require 'pdk/report'
|
252
|
+
|
253
|
+
footer = false
|
254
|
+
|
255
|
+
summary.keys.each do |category|
|
256
|
+
next if summary[category].empty?
|
257
|
+
|
258
|
+
PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner("Files to be #{category}", 40) })
|
259
|
+
PDK::Report.default_target.puts(summary[category])
|
260
|
+
footer = true
|
261
|
+
end
|
262
|
+
|
263
|
+
PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner('', 40) }) if footer
|
264
|
+
end
|
265
|
+
|
266
|
+
def print_result(banner_text)
|
267
|
+
require 'pdk/report'
|
268
|
+
|
269
|
+
PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner(banner_text, 40) })
|
270
|
+
summary_to_print = summary.map { |k, v| "#{v.length} files #{k}" unless v.empty? }.compact
|
271
|
+
PDK::Report.default_target.puts(_("\n%{summary}\n\n") % { summary: "#{summary_to_print.join(', ')}." })
|
272
|
+
end
|
273
|
+
|
274
|
+
def full_report(path)
|
275
|
+
require 'pdk/report'
|
276
|
+
|
277
|
+
report = ["/* Report generated by PDK at #{Time.now} */"]
|
278
|
+
report.concat(update_manager.changes[:modified].map { |_, diff| "\n\n\n#{diff}" })
|
279
|
+
PDK::Util::Filesystem.write_file(path, report.join)
|
280
|
+
PDK::Report.default_target.puts(_("\nYou can find a report of differences in %{path}.\n\n") % { path: path })
|
281
|
+
end
|
282
|
+
|
283
|
+
def generate_banner(text, width = 80)
|
284
|
+
padding = width - text.length
|
285
|
+
banner = ''
|
286
|
+
padding_char = '-'
|
287
|
+
|
288
|
+
(padding / 2.0).ceil.times { banner << padding_char }
|
289
|
+
banner << text
|
290
|
+
(padding / 2.0).floor.times { banner << padding_char }
|
291
|
+
|
292
|
+
banner
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|