Class: PDK::Module::Convert

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

Direct Known Subclasses

Update

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Convert

Returns a new instance of Convert.



15
16
17
# File 'lib/pdk/module/convert.rb', line 15

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/pdk/module/convert.rb', line 13

def options
  @options
end

Class Method Details

.invoke(options) ⇒ Object



9
10
11
# File 'lib/pdk/module/convert.rb', line 9

def self.invoke(options)
  new(options).run
end

Instance Method Details

#force?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/pdk/module/convert.rb', line 60

def force?
  options[:force]
end

#full_report(path) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/pdk/module/convert.rb', line 171

def full_report(path)
  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
  end
  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



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/pdk/module/convert.rb', line 181

def 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

#needs_bundle_update?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/pdk/module/convert.rb', line 64

def needs_bundle_update?
  update_manager.changed?('Gemfile')
end

#noop?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/pdk/module/convert.rb', line 56

def noop?
  options[:noop]
end


165
166
167
168
169
# File 'lib/pdk/module/convert.rb', line 165

def print_result(banner_text)
  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


151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pdk/module/convert.rb', line 151

def print_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

#runObject



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
# File 'lib/pdk/module/convert.rb', line 19

def run
  stage_changes!

  unless update_manager.changes?
    PDK::Report.default_target.puts(_('No changes required.'))
    return
  end

  print_summary

  full_report('convert_report.txt') 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

  # Remove these files straight away as these changes are not something that the user needs to review.
  if needs_bundle_update?
    update_manager.unlink_file('Gemfile.lock')
    update_manager.unlink_file(File.join('.bundle', 'config'))
  end

  update_manager.sync_changes!

  PDK::Util::Bundler.ensure_bundle! if needs_bundle_update?

  print_result 'Convert completed'
end

#stage_changes!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pdk/module/convert.rb', line 68

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

    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
end

#summaryObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pdk/module/convert.rb', line 132

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_urlObject



94
95
96
# File 'lib/pdk/module/convert.rb', line 94

def template_url
  @template_url ||= options.fetch(:'template-url', PDK::Util.default_template_url)
end

#update_managerObject



90
91
92
# File 'lib/pdk/module/convert.rb', line 90

def update_manager
  @update_manager ||= PDK::Module::UpdateManager.new
end

#update_metadata(metadata_path, template_metadata) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pdk/module/convert.rb', line 98

def (, )
  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 update module metadata; %{path} exists but it is not readable.') % {
        path: ,
      }
    end
  elsif File.exist?()
    raise PDK::CLI::ExitWithError, _('Unable to update 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