Class: PDK::Module::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/module/convert.rb

Class Method Summary collapse

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.generate_banner(text, width = 80)
  padding = width - text.length
  banner = ''
  padding_char = '-'

  (padding / 2.0).ceil.times { banner << padding_char }
  banner << text
  (padding / 2.0).floor.times { banner << padding_char }

  banner
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(options)
  update_manager = PDK::Module::UpdateManager.new
  template_url = options.fetch(:'template-url', PDK::Util.default_template_url)

  PDK::Module::TemplateDir.new(template_url, nil, false) do |templates|
     = ('metadata.json', templates., options)

    if options[: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 options[:noop]

  unless options[: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


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: generate_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


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)
  footer = false

  summary.keys.each do |category|
    next if summary[category].empty?

    PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner("Files to be #{category}", 40) })
    PDK::Report.default_target.puts(summary[category])
    footer = true
  end

  PDK::Report.default_target.puts(_("\n%{banner}") % { banner: generate_banner('', 40) }) if footer
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.(, , options = {})
  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.(options) unless options[: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 options[:noop]

    project_dir = File.basename(Dir.pwd)
    options[:module_name] = project_dir.split('-', 2).compact[-1]
    options[:prompt] = false
    options[:'skip-interview'] = true if options[:force]

     = PDK::Generate::Module.(options)
  end

  .update!()
  .to_json
end