Class: PDK::Module::Convert
- Inherits:
-
Object
- Object
- PDK::Module::Convert
- Defined in:
- lib/pdk/module/convert.rb
Class Method Summary collapse
- .full_report(update_manager) ⇒ Object
- .generate_banner(text, width = 80) ⇒ Object
- .get_summary(update_manager) ⇒ Object
- .invoke(options) ⇒ Object
- .print_result(summary) ⇒ Object
- .print_summary(summary) ⇒ Object
- .update_metadata(metadata_path, template_metadata, options = {}) ⇒ Object
Class Method Details
.full_report(update_manager) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'lib/pdk/module/convert.rb', line 140 def self.full_report(update_manager) File.open('convert_report.txt', 'w') do |f| f.write("/* Convert Report generated by PDK at #{Time.now} */") update_manager.changes[:modified].each do |_, diff| f.write("\n\n\n" + diff) end end PDK::Report.default_target.puts(_("\nYou can find a report of differences in convert_report.txt.\n\n")) end |
.generate_banner(text, width = 80) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/pdk/module/convert.rb', line 150 def self.(text, width = 80) padding = width - text.length = '' padding_char = '-' (padding / 2.0).ceil.times { << padding_char } << text (padding / 2.0).floor.times { << padding_char } end |
.get_summary(update_manager) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/pdk/module/convert.rb', line 105 def self.get_summary(update_manager) summary = {} update_manager.changes.each do |category, update_category| updated_files = if update_category.respond_to?(:keys) update_category.keys else update_category.map { |file| file[:path] } end summary[category] = updated_files end summary end |
.invoke(options) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pdk/module/convert.rb', line 9 def self.invoke() update_manager = PDK::Module::UpdateManager.new template_url = .fetch(:'template-url', PDK::Util.default_template_url) PDK::Module::TemplateDir.new(template_url, nil, false) do |templates| = ('metadata.json', templates., ) if [:noop] && .nil? update_manager.add_file('metadata.json', '') elsif File.file?('metadata.json') update_manager.modify_file('metadata.json', ) else update_manager.add_file('metadata.json', ) end templates.render do |file_path, file_content| if File.exist? file_path update_manager.modify_file(file_path, file_content) else update_manager.add_file(file_path, file_content) end end end unless update_manager.changes? PDK::Report.default_target.puts(_('No changes required.')) return end # Print the summary to the default target of reports summary = get_summary(update_manager) print_summary(summary) # Generates the full convert report full_report(update_manager) unless update_manager.changes[:modified].empty? return if [:noop] unless [:force] 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.', ) continue = PDK::CLI::Util.prompt_for_yes(_('Do you want to continue and make these changes to your module?')) return unless continue end # Mark these files for removal after generating the report as these # changes are not something that the user needs to review. if update_manager.changed?('Gemfile') update_manager.remove_file('Gemfile.lock') update_manager.remove_file(File.join('.bundle', 'config')) end update_manager.sync_changes! PDK::Util::Bundler.ensure_bundle! if update_manager.changed?('Gemfile') print_result(summary) end |
.print_result(summary) ⇒ Object
134 135 136 137 138 |
# File 'lib/pdk/module/convert.rb', line 134 def self.print_result(summary) PDK::Report.default_target.puts(_("\n%{banner}") % { banner: ('Convert completed', 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 |
.print_summary(summary) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/pdk/module/convert.rb', line 120 def self.print_summary(summary) = false summary.keys.each do |category| next if summary[category].empty? PDK::Report.default_target.puts(_("\n%{banner}") % { banner: ("Files to be #{category}", 40) }) PDK::Report.default_target.puts(summary[category]) = true end PDK::Report.default_target.puts(_("\n%{banner}") % { banner: ('', 40) }) if end |
.update_metadata(metadata_path, template_metadata, options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pdk/module/convert.rb', line 71 def self.(, , = {}) if File.file?() if File.readable?() begin = PDK::Module::Metadata.from_file() new_values = PDK::Module::Metadata::DEFAULTS.reject { |key, _| .data.key?(key) } .update!(new_values) rescue ArgumentError = PDK::Generate::Module.() unless [:noop] # rubocop:disable Metrics/BlockNesting end else raise PDK::CLI::ExitWithError, _('Unable to convert module metadata; %{path} exists but it is not readable.') % { path: , } end elsif File.exist?() raise PDK::CLI::ExitWithError, _('Unable to convert module metadata; %{path} exists but it is not a file.') % { path: , } else return nil if [:noop] project_dir = File.basename(Dir.pwd) [:module_name] = project_dir.split('-', 2).compact[-1] [:prompt] = false [:'skip-interview'] = true if [:force] = PDK::Generate::Module.() end .update!() .to_json end |