Class: PDK::Module::Convert
- Inherits:
-
Object
- Object
- PDK::Module::Convert
- Defined in:
- lib/pdk/module/convert.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#module_dir ⇒ Object
readonly
Returns the value of attribute module_dir.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #add_tests! ⇒ Object
- #add_tests? ⇒ Boolean
- #adding_tests? ⇒ Boolean
- #available_test_generators ⇒ Object
- #convert? ⇒ Boolean
- #force? ⇒ Boolean
- #full_report(path) ⇒ Object
- #generate_banner(text, width = 80) ⇒ Object
-
#initialize(module_dir, options = {}) ⇒ Convert
constructor
A new instance of Convert.
- #missing_tests? ⇒ Boolean
- #noop? ⇒ Boolean
- #print_result(banner_text) ⇒ Object
- #print_summary ⇒ Object
- #run ⇒ Object
- #stage_changes!(context = PDK.context) ⇒ Object
- #stage_tests!(manager) ⇒ Object
- #summary ⇒ Object
- #template_uri ⇒ Object
- #test_generators(context = PDK.context) ⇒ Object
- #update_manager ⇒ Object
- #update_metadata(metadata_path, template_metadata) ⇒ Object
Constructor Details
#initialize(module_dir, options = {}) ⇒ Convert
Returns a new instance of Convert.
14 15 16 17 |
# File 'lib/pdk/module/convert.rb', line 14 def initialize(module_dir, = {}) @module_dir = module_dir @options = end |
Instance Attribute Details
#module_dir ⇒ Object (readonly)
Returns the value of attribute module_dir.
10 11 12 |
# File 'lib/pdk/module/convert.rb', line 10 def module_dir @module_dir end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/pdk/module/convert.rb', line 12 def @options end |
Class Method Details
.invoke(module_dir, options) ⇒ Object
6 7 8 |
# File 'lib/pdk/module/convert.rb', line 6 def self.invoke(module_dir, ) new(module_dir, ).run end |
Instance Method Details
#add_tests! ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/pdk/module/convert.rb', line 121 def add_tests! update_manager.clear! stage_tests!(update_manager) if update_manager.changes? update_manager.sync_changes! print_summary else PDK::Report.default_target.puts('No test changes required.') end end |
#add_tests? ⇒ Boolean
78 79 80 |
# File 'lib/pdk/module/convert.rb', line 78 def add_tests? [:'add-tests'] end |
#adding_tests? ⇒ Boolean
82 83 84 |
# File 'lib/pdk/module/convert.rb', line 82 def adding_tests? add_tests? && missing_tests? end |
#available_test_generators ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pdk/module/convert.rb', line 90 def available_test_generators # Only select generators which can run and have no pre-existing files test_generators.select do |gen| if gen.can_run? gen.template_files.none? { |_, dst_path| PDK::Util::Filesystem.exist?(File.join(gen.context.root_path, dst_path)) } else false end end end |
#convert? ⇒ Boolean
19 20 21 |
# File 'lib/pdk/module/convert.rb', line 19 def convert? instance_of?(PDK::Module::Convert) end |
#force? ⇒ Boolean
74 75 76 |
# File 'lib/pdk/module/convert.rb', line 74 def force? [:force] end |
#full_report(path) ⇒ Object
272 273 274 275 276 277 278 279 |
# File 'lib/pdk/module/convert.rb', line 272 def full_report(path) require 'pdk/report' report = ["/* Report generated by PDK at #{Time.now} */"] report.concat(update_manager.changes[:modified].map { |_, diff| "\n\n\n#{diff}" }) PDK::Util::Filesystem.write_file(path, report.join) PDK::Report.default_target.puts("\nYou can find a report of differences in %{path}.\n\n" % { path: path }) end |
#generate_banner(text, width = 80) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/pdk/module/convert.rb', line 281 def (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 |
#missing_tests? ⇒ Boolean
86 87 88 |
# File 'lib/pdk/module/convert.rb', line 86 def missing_tests? !available_test_generators.empty? end |
#noop? ⇒ Boolean
70 71 72 |
# File 'lib/pdk/module/convert.rb', line 70 def noop? [:noop] end |
#print_result(banner_text) ⇒ Object
264 265 266 267 268 269 270 |
# File 'lib/pdk/module/convert.rb', line 264 def print_result() require 'pdk/report' PDK::Report.default_target.puts("\n%{banner}" % { banner: (, 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 ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/pdk/module/convert.rb', line 248 def print_summary require 'pdk/report' = 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 |
#run ⇒ Object
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 |
# File 'lib/pdk/module/convert.rb', line 23 def run stage_changes! unless update_manager.changes? if adding_tests? add_tests! print_result 'Convert completed' 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.' continue = PDK::CLI::Util.prompt_for_yes('Do you want to continue and make these changes to your module?') return unless continue end # Remove these files straight away as these changes are not something # that the user needs to review. update_manager.unlink_file('Gemfile.lock') update_manager.unlink_file(File.join('.bundle', 'config')) update_manager.sync_changes! require 'pdk/util/bundler' PDK::Util::Bundler.ensure_bundle! add_tests! if adding_tests? print_result 'Convert completed' end |
#stage_changes!(context = PDK.context) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/pdk/module/convert.rb', line 133 def stage_changes!(context = PDK.context) require 'pdk/util/filesystem' = File.join(module_dir, 'metadata.json') PDK::Template.with(template_uri, context) do |template_dir| = (, template_dir.) if [:noop] && .nil? update_manager.add_file(, '') elsif PDK::Util::Filesystem.file?() update_manager.modify_file(, .to_json) else update_manager.add_file(, .to_json) end # new_metadata == nil when creating a new module but with --noop@ module_name = .nil? ? 'new-module' : .data['name'] = .nil? ? {} : .data template_dir.render_new_module(module_name, ) do |relative_file_path, file_content, file_status| absolute_file_path = File.join(module_dir, relative_file_path) case file_status when :unmanage PDK.logger.debug("skipping '%{path}'" % { path: absolute_file_path }) when :delete update_manager.remove_file(absolute_file_path) when :init if convert? && !PDK::Util::Filesystem.exist?(absolute_file_path) update_manager.add_file(absolute_file_path, file_content) end when :manage if PDK::Util::Filesystem.exist?(absolute_file_path) update_manager.modify_file(absolute_file_path, file_content) else update_manager.add_file(absolute_file_path, file_content) end end end end rescue ArgumentError => e raise PDK::CLI::ExitWithError, e end |
#stage_tests!(manager) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/pdk/module/convert.rb', line 114 def stage_tests!(manager) available_test_generators.each do |gen| gen.stage_changes(manager) end manager end |
#summary ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/pdk/module/convert.rb', line 229 def summary summary = {} update_manager.changes.each do |category, update_category| if update_category.respond_to?(:keys) updated_files = update_category.keys else begin updated_files = update_category.map { |file| file[:path] } rescue TypeError updated_files = update_category.to_a end end summary[category] = updated_files end summary end |
#template_uri ⇒ Object
182 183 184 185 186 |
# File 'lib/pdk/module/convert.rb', line 182 def template_uri require 'pdk/util/template_uri' @template_uri ||= PDK::Util::TemplateURI.new() end |
#test_generators(context = PDK.context) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pdk/module/convert.rb', line 101 def test_generators(context = PDK.context) 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(context, obj['name'], spec_only: true) end end @test_generators = test_gens.flatten end |
#update_manager ⇒ Object
176 177 178 179 180 |
# File 'lib/pdk/module/convert.rb', line 176 def update_manager require 'pdk/module/update_manager' @update_manager ||= PDK::Module::UpdateManager.new end |
#update_metadata(metadata_path, template_metadata) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/pdk/module/convert.rb', line 188 def (, ) require 'pdk/generate/module' require 'pdk/util/filesystem' require 'pdk/module/metadata' if PDK::Util::Filesystem.file?() unless PDK::Util::Filesystem.readable?() raise PDK::CLI::ExitWithError, 'Unable to update module metadata; %{path} exists but it is not readable.' % { path: , } end begin = PDK::Module::Metadata.from_file() new_values = PDK::Module::Metadata::DEFAULTS.select do |key, _| !.data.key?(key) || .data[key].nil? || (key == 'requirements' && .data[key].empty?) end .update!(new_values) rescue ArgumentError = PDK::Generate::Module.() unless [:noop] end elsif PDK::Util::Filesystem.exist?() raise PDK::CLI::ExitWithError, 'Unable to update module metadata; %{path} exists but it is not a file.' % { path: , } else return 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!() end |