lib/pdk/module/convert.rb in pdk-1.13.0 vs lib/pdk/module/convert.rb in pdk-1.14.0
- old
+ new
@@ -1,10 +1,5 @@
-require 'pdk/generate/module'
-require 'pdk/module/update_manager'
-require 'pdk/util'
-require 'pdk/report'
-
module PDK
module Module
class Convert
def self.invoke(options)
new(options).run
@@ -22,21 +17,30 @@
def run
stage_changes!
unless update_manager.changes?
- PDK::Report.default_target.puts(_('No changes required.'))
+ if adding_tests?
+ add_tests!
+ else
+ require 'pdk/report'
+
+ PDK::Report.default_target.puts(_('No changes required.'))
+ end
+
return
end
print_summary
full_report('convert_report.txt') unless update_manager.changes[:modified].empty?
return if noop?
unless force?
+ require 'pdk/cli/util'
+
PDK.logger.info _(
'Module conversion is a potentially destructive action. ' \
'Ensure that you have committed your module to a version control ' \
'system or have a backup, and review the changes above before continuing.',
)
@@ -50,12 +54,17 @@
update_manager.unlink_file(File.join('.bundle', 'config'))
end
update_manager.sync_changes!
- PDK::Util::Bundler.ensure_bundle! if needs_bundle_update?
+ if needs_bundle_update?
+ require 'pdk/util/bundler'
+ PDK::Util::Bundler.ensure_bundle!
+ end
+ add_tests! if adding_tests?
+
print_result 'Convert completed'
end
def noop?
options[:noop]
@@ -63,15 +72,49 @@
def force?
options[:force]
end
+ def add_tests?
+ options[:'add-tests']
+ end
+
+ def adding_tests?
+ add_tests? && missing_tests?
+ end
+
+ def missing_tests?
+ test_generators.any? { |gen| gen.can_run? }
+ end
+
+ def test_generators
+ return @test_generators unless @test_generators.nil?
+ require 'pdk/util/puppet_strings'
+
+ test_gens = PDK::Util::PuppetStrings.all_objects.map do |generator, objects|
+ (objects || []).map do |obj|
+ generator.new(Dir.pwd, obj['name'], spec_only: true)
+ end
+ end
+
+ @test_generators = test_gens.flatten
+ end
+
+ def add_tests!
+ test_generators.each do |gen|
+ gen.run if gen.can_run?
+ end
+ end
+
def needs_bundle_update?
update_manager.changed?('Gemfile')
end
def stage_changes!
+ require 'pdk/module/templatedir'
+ require 'pdk/util/filesystem'
+
metadata_path = 'metadata.json'
PDK::Module::TemplateDir.new(template_uri, nil, true) do |templates|
new_metadata = update_metadata(metadata_path, templates.metadata)
templates.module_metadata = new_metadata.data unless new_metadata.nil?
@@ -106,18 +149,26 @@
rescue ArgumentError => e
raise PDK::CLI::ExitWithError, e
end
def update_manager
+ require 'pdk/module/update_manager'
+
@update_manager ||= PDK::Module::UpdateManager.new
end
def template_uri
+ require 'pdk/util/template_uri'
+
@template_uri ||= PDK::Util::TemplateURI.new(options)
end
def update_metadata(metadata_path, template_metadata)
+ require 'pdk/generate/module'
+ require 'pdk/util/filesystem'
+ require 'pdk/module/metadata'
+
if PDK::Util::Filesystem.file?(metadata_path)
unless PDK::Util::Filesystem.readable?(metadata_path)
raise PDK::CLI::ExitWithError, _('Unable to update module metadata; %{path} exists but it is not readable.') % {
path: metadata_path,
}
@@ -170,10 +221,12 @@
summary
end
def print_summary
+ require 'pdk/report'
+
footer = false
summary.keys.each do |category|
next if summary[category].empty?
@@ -184,15 +237,19 @@
PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner('', 40) }) if footer
end
def print_result(banner_text)
+ require 'pdk/report'
+
PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner(banner_text, 40) })
summary_to_print = summary.map { |k, v| "#{v.length} files #{k}" unless v.empty? }.compact
PDK::Report.default_target.puts(_("\n%{summary}\n\n") % { summary: "#{summary_to_print.join(', ')}." })
end
def full_report(path)
+ require 'pdk/report'
+
File.open(path, 'w') do |f|
f.write("/* Report generated by PDK at #{Time.now} */")
update_manager.changes[:modified].each do |_, diff|
f.write("\n\n\n" + diff)
end