pdk 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1329 -1321
  3. data/LICENSE +201 -201
  4. data/README.md +163 -163
  5. data/exe/pdk +10 -10
  6. data/lib/pdk/analytics/client/google_analytics.rb +143 -143
  7. data/lib/pdk/analytics/client/noop.rb +25 -25
  8. data/lib/pdk/analytics/util.rb +19 -19
  9. data/lib/pdk/analytics.rb +30 -30
  10. data/lib/pdk/answer_file.rb +12 -12
  11. data/lib/pdk/bolt.rb +19 -19
  12. data/lib/pdk/cli/build.rb +82 -82
  13. data/lib/pdk/cli/bundle.rb +48 -48
  14. data/lib/pdk/cli/config/get.rb +26 -26
  15. data/lib/pdk/cli/config.rb +22 -22
  16. data/lib/pdk/cli/console.rb +148 -148
  17. data/lib/pdk/cli/convert.rb +52 -52
  18. data/lib/pdk/cli/env.rb +52 -52
  19. data/lib/pdk/cli/errors.rb +25 -25
  20. data/lib/pdk/cli/exec/command.rb +293 -293
  21. data/lib/pdk/cli/exec/interactive_command.rb +114 -114
  22. data/lib/pdk/cli/exec.rb +84 -84
  23. data/lib/pdk/cli/exec_group.rb +104 -104
  24. data/lib/pdk/cli/get/config.rb +24 -24
  25. data/lib/pdk/cli/get.rb +20 -20
  26. data/lib/pdk/cli/module/build.rb +12 -12
  27. data/lib/pdk/cli/module/generate.rb +47 -47
  28. data/lib/pdk/cli/module.rb +14 -14
  29. data/lib/pdk/cli/new/class.rb +32 -32
  30. data/lib/pdk/cli/new/defined_type.rb +32 -32
  31. data/lib/pdk/cli/new/fact.rb +29 -29
  32. data/lib/pdk/cli/new/function.rb +29 -29
  33. data/lib/pdk/cli/new/module.rb +53 -53
  34. data/lib/pdk/cli/new/provider.rb +29 -29
  35. data/lib/pdk/cli/new/task.rb +34 -34
  36. data/lib/pdk/cli/new/test.rb +52 -52
  37. data/lib/pdk/cli/new/transport.rb +27 -27
  38. data/lib/pdk/cli/new.rb +21 -21
  39. data/lib/pdk/cli/release/prep.rb +39 -39
  40. data/lib/pdk/cli/release/publish.rb +50 -50
  41. data/lib/pdk/cli/release.rb +194 -194
  42. data/lib/pdk/cli/remove/config.rb +80 -80
  43. data/lib/pdk/cli/remove.rb +20 -20
  44. data/lib/pdk/cli/set/config.rb +119 -119
  45. data/lib/pdk/cli/set.rb +20 -20
  46. data/lib/pdk/cli/test/unit.rb +90 -90
  47. data/lib/pdk/cli/test.rb +11 -11
  48. data/lib/pdk/cli/update.rb +64 -64
  49. data/lib/pdk/cli/util/command_redirector.rb +27 -27
  50. data/lib/pdk/cli/util/interview.rb +72 -72
  51. data/lib/pdk/cli/util/option_normalizer.rb +55 -55
  52. data/lib/pdk/cli/util/option_validator.rb +68 -68
  53. data/lib/pdk/cli/util/spinner.rb +13 -13
  54. data/lib/pdk/cli/util/update_manager_printer.rb +82 -82
  55. data/lib/pdk/cli/util.rb +305 -305
  56. data/lib/pdk/cli/validate.rb +116 -116
  57. data/lib/pdk/cli.rb +175 -175
  58. data/lib/pdk/config/analytics_schema.json +26 -26
  59. data/lib/pdk/config/errors.rb +5 -5
  60. data/lib/pdk/config/ini_file.rb +183 -183
  61. data/lib/pdk/config/ini_file_setting.rb +39 -39
  62. data/lib/pdk/config/json.rb +34 -34
  63. data/lib/pdk/config/json_schema_namespace.rb +142 -142
  64. data/lib/pdk/config/json_schema_setting.rb +53 -53
  65. data/lib/pdk/config/json_with_schema.rb +49 -49
  66. data/lib/pdk/config/namespace.rb +354 -354
  67. data/lib/pdk/config/setting.rb +135 -135
  68. data/lib/pdk/config/validator.rb +31 -31
  69. data/lib/pdk/config/yaml.rb +46 -46
  70. data/lib/pdk/config/yaml_with_schema.rb +59 -59
  71. data/lib/pdk/config.rb +390 -390
  72. data/lib/pdk/context/control_repo.rb +60 -60
  73. data/lib/pdk/context/module.rb +28 -28
  74. data/lib/pdk/context/none.rb +22 -22
  75. data/lib/pdk/context.rb +99 -99
  76. data/lib/pdk/control_repo.rb +90 -90
  77. data/lib/pdk/generate/defined_type.rb +43 -43
  78. data/lib/pdk/generate/fact.rb +25 -25
  79. data/lib/pdk/generate/function.rb +48 -48
  80. data/lib/pdk/generate/module.rb +352 -352
  81. data/lib/pdk/generate/provider.rb +28 -28
  82. data/lib/pdk/generate/puppet_class.rb +43 -43
  83. data/lib/pdk/generate/puppet_object.rb +232 -232
  84. data/lib/pdk/generate/task.rb +68 -68
  85. data/lib/pdk/generate/transport.rb +33 -33
  86. data/lib/pdk/generate.rb +24 -24
  87. data/lib/pdk/i18n.rb +4 -4
  88. data/lib/pdk/logger.rb +45 -45
  89. data/lib/pdk/module/build.rb +322 -322
  90. data/lib/pdk/module/convert.rb +296 -296
  91. data/lib/pdk/module/metadata.rb +202 -202
  92. data/lib/pdk/module/release.rb +260 -260
  93. data/lib/pdk/module/update.rb +131 -131
  94. data/lib/pdk/module/update_manager.rb +227 -227
  95. data/lib/pdk/module.rb +30 -30
  96. data/lib/pdk/report/event.rb +370 -370
  97. data/lib/pdk/report.rb +121 -121
  98. data/lib/pdk/template/fetcher/git.rb +85 -85
  99. data/lib/pdk/template/fetcher/local.rb +28 -28
  100. data/lib/pdk/template/fetcher.rb +98 -98
  101. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -116
  102. data/lib/pdk/template/renderer/v1/renderer.rb +132 -132
  103. data/lib/pdk/template/renderer/v1/template_file.rb +102 -102
  104. data/lib/pdk/template/renderer/v1.rb +25 -25
  105. data/lib/pdk/template/renderer.rb +96 -96
  106. data/lib/pdk/template/template_dir.rb +67 -67
  107. data/lib/pdk/template.rb +59 -59
  108. data/lib/pdk/tests/unit.rb +252 -252
  109. data/lib/pdk/util/bundler.rb +259 -259
  110. data/lib/pdk/util/changelog_generator.rb +137 -137
  111. data/lib/pdk/util/env.rb +47 -47
  112. data/lib/pdk/util/filesystem.rb +138 -138
  113. data/lib/pdk/util/git.rb +179 -179
  114. data/lib/pdk/util/json_finder.rb +85 -85
  115. data/lib/pdk/util/puppet_strings.rb +125 -125
  116. data/lib/pdk/util/puppet_version.rb +266 -266
  117. data/lib/pdk/util/ruby_version.rb +179 -179
  118. data/lib/pdk/util/template_uri.rb +295 -295
  119. data/lib/pdk/util/vendored_file.rb +93 -93
  120. data/lib/pdk/util/version.rb +43 -43
  121. data/lib/pdk/util/windows/api_types.rb +82 -82
  122. data/lib/pdk/util/windows/file.rb +36 -36
  123. data/lib/pdk/util/windows/process.rb +79 -79
  124. data/lib/pdk/util/windows/string.rb +16 -16
  125. data/lib/pdk/util/windows.rb +15 -15
  126. data/lib/pdk/util.rb +278 -277
  127. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -23
  128. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -98
  129. data/lib/pdk/validate/external_command_validator.rb +208 -208
  130. data/lib/pdk/validate/internal_ruby_validator.rb +100 -100
  131. data/lib/pdk/validate/invokable_validator.rb +228 -228
  132. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -86
  133. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -78
  134. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -20
  135. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -133
  136. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -66
  137. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -137
  138. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -21
  139. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -80
  140. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -19
  141. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -88
  142. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -50
  143. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -20
  144. data/lib/pdk/validate/validator.rb +118 -118
  145. data/lib/pdk/validate/validator_group.rb +104 -104
  146. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -95
  147. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -19
  148. data/lib/pdk/validate.rb +94 -94
  149. data/lib/pdk/version.rb +4 -4
  150. data/lib/pdk.rb +76 -76
  151. data/locales/config.yaml +21 -21
  152. data/locales/pdk.pot +2094 -2094
  153. metadata +5 -6
@@ -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