Class: Fastlane::Actions::PromoScreenshotsAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



127
128
129
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 127

def self.authors
  ['Automattic']
end

.available_optionsObject



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
175
176
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 140

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :orig_folder,
                                 env_name: 'PROMOSS_ORIG',
                                 description: 'The directory containing the original screenshots',
                                 optional: false,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :output_folder,
                                 env_name: 'PROMOSS_OUTPUT',
                                 description: 'The path of the folder to save the promo screenshots',
                                 optional: false,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :metadata_folder,
                                 env_name: 'PROMOSS_METADATA_FOLDER',
                                 description: 'The directory containing the translation data',
                                 optional: false,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :config_file,
                                 env_name: 'PROMOSS_CONFIG_FILE',
                                 description: 'The path to the file containing the promo screenshot configuration',
                                 optional: true,
                                 type: String,
                                 default_value: 'screenshots.json'),
    FastlaneCore::ConfigItem.new(key: :force,
                                 env_name: 'PROMOSS_FORCE_CREATION',
                                 description: 'Overwrite existing promo screenshots without asking first?',
                                 optional: true,
                                 type: Boolean,
                                 default_value: false),
    FastlaneCore::ConfigItem.new(key: :skip_font_check,
                                 env_name: 'PROMOSS_SKIP_FONT_CHECK',
                                 description: 'Skip the check verifying that needed fonts are installed and active',
                                 optional: true,
                                 type: Boolean,
                                 default_value: false),
  ]
end

.build_entries(config_entries, languages, output_directory, params) ⇒ Object



182
183
184
185
186
187
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 182

def self.build_entries(config_entries, languages, output_directory, params)
  config_entries
    .flat_map do |entry|
    languages.map do |language|
      new_entry = entry.deep_dup

      # Not every output file will have a screenshot, so handle cases where no
      # screenshot file is defined
      if !entry['screenshot'].nil? && !entry['filename'].nil?
        new_entry['screenshot'] = helper.resolve_path(params[:orig_folder]) + language + entry['screenshot']
        new_entry['filename'] = output_directory + language + entry['filename']
      elsif !entry['screenshot'].nil? && entry['filename'].nil?
        new_entry['screenshot'] = helper.resolve_path(params[:orig_folder]) + language + entry['screenshot']
        new_entry['filename'] = output_directory + language + entry['screenshot']
      elsif entry['screenshot'].nil? && !entry['filename'].nil?
        new_entry['filename'] = output_directory + language + entry['filename']
      else
        puts new_entry
        abort 'Unable to find output file names'
      end

      new_entry['locale'] = language

      # Localize file paths for text
      new_entry['text'].sub!('{locale}', language.dup) unless entry['text'].nil?

      # Map attachments paths to their localized versions
      new_entry['attachments'] = [] if new_entry['attachments'].nil?

      new_entry['attachments'].each do |attachment|
        ## If there are no translated screenshot images (whether it's because they haven't been generated yet,
        ##   or because we aren't using them), just use the translated directories.
        ## And vice-versa.
        ## If there are original screenshots and translations available, use only locales that exist in both.
        # Create a hash of devices, keyed by device name
        # Not every output file will have a screenshot, so handle cases where no
        # screenshot file is defined
        # Localize file paths for text
        # Map attachments paths to their localized versions
        # Automatically create intermediate directories for output
        # Run the GC in the same thread to clean up after RMagick
        # If your method provides a return value, you can describe here what it does
        # Optional:
        attachment['file']&.sub!('{locale}', language.dup)

        ## If there are no translated screenshot images (whether it's because they haven't been generated yet,
        ##   or because we aren't using them), just use the translated directories.
        ## And vice-versa.
        ## If there are original screenshots and translations available, use only locales that exist in both.
        # Create a hash of devices, keyed by device name
        # Not every output file will have a screenshot, so handle cases where no
        # screenshot file is defined
        # Localize file paths for text
        # Map attachments paths to their localized versions
        # Automatically create intermediate directories for output
        # Run the GC in the same thread to clean up after RMagick
        # If your method provides a return value, you can describe here what it does
        # Optional:
        attachment['text']&.sub!('{locale}', language.dup)
      end

      new_entry
    end
  end
    .sort do |x, y|
    x['filename'] <=> y['filename']
  end
end

.check_path(path) ⇒ Object



115
116
117
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 115

def self.check_path(path)
  helper.can_resolve_path(path) ? '' : '🚫'
end

.confirm_directory_overwrite(path, description) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 88

def self.confirm_directory_overwrite(path, description)
  if File.exist?(path)
    if UI.confirm("Do you want to overwrite #{description}?")
      FileUtils.rm_rf(path)
      Dir.mkdir(path)
    else
      UI.user_error!("Exiting to avoid overwriting #{description}.")
    end
  else
    Dir.mkdir(path)
  end
end

.descriptionObject



123
124
125
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 123

def self.description
  'Generate promo screenshots'
end

.detailsObject



135
136
137
138
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 135

def self.details
  # Optional:
  'Creates promo screenshots starting from standard ones'
end

.helperObject



119
120
121
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 119

def self.helper
  Fastlane::Helper::PromoScreenshots.new
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 178

def self.is_supported?(platform)
  true
end

.return_valueObject



131
132
133
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 131

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 9

def self.run(params)
  UI.message 'Creating Promo Screenshots'
  UI.message "#{check_path(params[:orig_folder])} Original Screenshot Source: #{params[:orig_folder]}"
  UI.message "#{check_path(params[:metadata_folder])} Translation source: #{params[:metadata_folder]}"

  config = helper.read_config(params[:config_file])

  helper.check_fonts_installed!(config: config) unless params[:skip_font_check]

  translation_directories = subdirectories_for_path(params[:metadata_folder])
  image_directories = subdirectories_for_path(params[:orig_folder])

  if helper.can_resolve_path(params[:output_folder])
    UI.message "#{check_path(params[:output_folder])} Output Folder: #{params[:output_folder]}"
  else
    UI.message "✅ Created Output Folder: #{params[:output_folder]}"
    FileUtils.mkdir_p(params[:output_folder])
  end

  output_directory = helper.resolve_path(params[:output_folder])

  ## If there are no translated screenshot images (whether it's because they haven't been generated yet,
  ##   or because we aren't using them), just use the translated directories.
  languages = if image_directories == []
                translation_directories
              ## And vice-versa.
              elsif translation_directories == []
                image_directories
              ## If there are original screenshots and translations available, use only locales that exist in both.
              else
                image_directories & translation_directories
              end

  UI.message("💙 Creating Promo Screenshots for: #{languages.join(', ')}")

  confirm_directory_overwrite(params[:output_folder], 'the existing promo screenshots') unless params[:force]

  # Create a hash of devices, keyed by device name
  devices = config['devices']
  devices = devices.map { |device| device['name'] }.zip(devices).to_h

  stylesheet_path = config['stylesheet']

  entries = build_entries(config['entries'], languages, output_directory, params)

  bar = ProgressBar.new(entries.count, :bar, :counter, :eta, :rate)

  Parallel.map(entries, finish: lambda { |_item, _i, _result|
    bar.increment!
  }) do |entry|
    device = devices[entry['device']]

    UI.message("Unable to find device #{entry['device']}.") if device.nil?

    width = device['canvas_size'][0]
    height = device['canvas_size'][1]

    canvas = helper.create_image(width, height)
    canvas = helper.draw_background_to_canvas(canvas, entry)

    canvas = helper.draw_device_frame_to_canvas(device, canvas)
    canvas = helper.draw_caption_to_canvas(entry, canvas, device, stylesheet_path)
    canvas = helper.draw_screenshot_to_canvas(entry, canvas, device)
    canvas = helper.draw_attachments_to_canvas(entry, canvas)

    # Automatically create intermediate directories for output
    output_filename = entry['filename']
    dirname = File.dirname(output_filename)

    FileUtils.mkdir_p(dirname) unless File.directory?(dirname)

    canvas.write(output_filename)
    canvas.destroy!

    # Run the GC in the same thread to clean up after RMagick
    GC.start
  end
end

.subdirectories_for_path(path) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/promo_screenshots_action.rb', line 101

def self.subdirectories_for_path(path)
  subdirectories = []

  return [] unless helper.can_resolve_path(path)

  resolved_path = helper.resolve_path(path)

  Dir.chdir(resolved_path) do
    subdirectories = Dir['*'].select { |o| File.directory?(o) }.sort
  end

  subdirectories
end