Class: Fastlane::Helper::PromoScreenshots
- Inherits:
-
Object
- Object
- Fastlane::Helper::PromoScreenshots
- Defined in:
- lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb
Instance Method Summary collapse
- #apply_operation(image, operation, canvas) ⇒ Object
- #can_resolve_path(path) ⇒ Object
-
#check_fonts_installed!(config:) ⇒ Object
Checks that all required fonts are installed - Visits the JSON config to find all the stylesheets referenced in it - For each stylesheet, extract the first font of each ‘font-family` attribute found - Finally, for each of those fonts, check that they exist and are activated.
-
#composite_image(original, child, x_position, y_position, starting_position = NorthWestGravity) ⇒ Magick::Image
composite_image.
- #composite_image_center(original, child, x_position, y_position) ⇒ Object
- #composite_image_left(original, child, x_position, y_position) ⇒ Object
- #composite_image_top(original, child, x_position, y_position) ⇒ Object
- #create_image(width, height, background = 'transparent') ⇒ Object
-
#crop_image(original, x_position, y_position, width, height) ⇒ Magick::Image
crop_image.
- #draw_attachments_to_canvas(entry, canvas) ⇒ Object
- #draw_background_to_canvas(canvas, entry) ⇒ Object
- #draw_caption_to_canvas(entry, canvas, device, stylesheet_path = '') ⇒ Object
- #draw_device_frame_to_canvas(device, canvas) ⇒ Object
- #draw_file_attachment_to_canvas(attachment, canvas, entry) ⇒ Object
- #draw_screenshot_to_canvas(entry, canvas, device) ⇒ Object
- #draw_text_attachment_to_canvas(attachment, canvas, locale) ⇒ Object
- #draw_text_to_canvas(canvas, text, width, height, x_position, y_position, font_size, stylesheet_path, position = 'center') ⇒ Object
-
#initialize ⇒ PromoScreenshots
constructor
A new instance of PromoScreenshots.
-
#mask_image(image, mask, offset_x = 0, offset_y = 0) ⇒ Magick::Image
mask_image.
- #open_image(path) ⇒ Object
- #read_config(config_file_path) ⇒ Object
-
#resize_image(original, width, height) ⇒ Magick::Image
resize_image.
- #resolve_path(path) ⇒ Object
- #resolve_text_into_path(text, locale) ⇒ Object
Constructor Details
#initialize ⇒ PromoScreenshots
Returns a new instance of PromoScreenshots.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 25 def initialize if $skip_magick = "PromoScreenshots feature is currently disabled.\n" << "Please, install RMagick if you aim to generate the PromoScreenshots.\n" << "'bundle install --with screenshots' should do it if your project is configured for PromoScreenshots.\n" << 'Aborting.' UI.user_error!() end UI.user_error!('`drawText` not found – install it using `brew install automattic/build-tools/drawText`.') unless system('command -v drawText') end |
Instance Method Details
#apply_operation(image, operation, canvas) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 253 def apply_operation(image, operation, canvas) case operation['type'] when 'crop' x_pos = operation['at'][0] y_pos = operation['at'][1] width = operation['to'][0] height = operation['to'][1] crop_image(image, x_pos, y_pos, width, height) when 'resize' width = operation['to'][0] height = operation['to'][1] resize_image(image, width, height) when 'composite' x_pos = operation['at'][0] y_pos = operation['at'][1] if operation.member?('offset') x_pos += operation['offset'][0] y_pos += operation['offset'][1] end composite_image(canvas, image, x_pos, y_pos) end end |
#can_resolve_path(path) ⇒ Object
410 411 412 413 414 415 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 410 def can_resolve_path(path) resolve_path(path) true rescue StandardError false end |
#check_fonts_installed!(config:) ⇒ Object
Checks that all required fonts are installed
- Visits the JSON config to find all the stylesheets referenced in it
- For each stylesheet, extract the first font of each `font-family` attribute found
- Finally, for each of those fonts, check that they exist and are activated.
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 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 59 def check_fonts_installed!(config:) # Find all stylesheets in the config all_stylesheets = ([config['stylesheet']] + config['entries'].flat_map do |entry| entry['attachments']&.map { |att| att['stylesheet'] } end).compact.uniq # Parse the first font in each `font-family` attribute found in all found CSS files. # Only the first in each `font-family` font list matters, as others are fallbacks we don't want to use anyway. font_families = all_stylesheets.flat_map do |s| stylesheet_path = resolve_path(s) File.readlines(stylesheet_path).flat_map do |line| attr = line.match(/font-family: (.*);/)&.captures&.first attr.split(',').first.strip.gsub(/'(.*)'/, '\1').gsub(/"(.*)"/, '\1') unless attr.nil? end end.compact.uniq # Verify that all fonts exists and are active—using a small swift script as there's no nice CLI for that swift_script = <<~SWIFT import AppKit var exitCode: Int32 = 0 for fontName in CommandLine.arguments.dropFirst() { if NSFont(name: fontName, size: NSFont.systemFontSize) != nil { print(" ✅ Font \\"\\(fontName)\\" found and active") } else { print(" ❌ Font \\"\\(fontName)\\" not found, it is either not installed or disabled. Please install it using FontBook first.") exitCode = 1 } } exit(exitCode) SWIFT Tempfile.create(['fonts-check-', '.swift']) do |f| f.write(swift_script) f.close oe, s = Open3.capture2e('/usr/bin/env', 'xcrun', 'swift', f.path, *font_families) UI.command_output(oe) UI.user_error!('Some fonts required by your stylesheets are missing. Please install them and try again.') unless s.success? end end |
#composite_image(original, child, x_position, y_position, starting_position = NorthWestGravity) ⇒ Magick::Image
composite_image
354 355 356 357 358 359 360 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 354 def composite_image(original, child, x_position, y_position, starting_position = NorthWestGravity) UI.user_error!('You must pass an image object as the first argument to `composite_image`.') unless original.is_a?(Magick::Image) UI.user_error!('You must pass an image object as the second argument to `composite_image`.') unless child.is_a?(Magick::Image) original.composite(child, starting_position, x_position, y_position, Magick::OverCompositeOp) end |
#composite_image_center(original, child, x_position, y_position) ⇒ Object
370 371 372 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 370 def composite_image_center(original, child, x_position, y_position) composite_image(original, child, x_position, y_position, CenterGravity) end |
#composite_image_left(original, child, x_position, y_position) ⇒ Object
366 367 368 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 366 def composite_image_left(original, child, x_position, y_position) composite_image(original, child, x_position, y_position, WestGravity) end |
#composite_image_top(original, child, x_position, y_position) ⇒ Object
362 363 364 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 362 def composite_image_top(original, child, x_position, y_position) composite_image(original, child, x_position, y_position, NorthGravity) end |
#create_image(width, height, background = 'transparent') ⇒ Object
402 403 404 405 406 407 408 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 402 def create_image(width, height, background = 'transparent') background.paint.to_hex Image.new(width, height) do self.background_color = background end end |
#crop_image(original, x_position, y_position, width, height) ⇒ Magick::Image
crop_image
388 389 390 391 392 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 388 def crop_image(original, x_position, y_position, width, height) UI.user_error!('You must pass an image object to `crop_image`.') unless original.is_a?(Magick::Image) original.crop(x_position, y_position, width, height) end |
#draw_attachments_to_canvas(entry, canvas) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 188 def (entry, canvas) entry['attachments'].each do || if !['file'].nil? canvas = (, canvas, entry) elsif !['text'].nil? canvas = (, canvas, entry['locale']) end end canvas end |
#draw_background_to_canvas(canvas, entry) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 134 def draw_background_to_canvas(canvas, entry) unless entry['background'].nil? # If we're passed an image path, let's open it and paint it to the canvas if can_resolve_path(entry['background']) background_image = open_image(entry['background']) return composite_image(canvas, background_image, 0, 0) else # Otherwise, let's assume this is a colour code background_image = create_image(canvas.columns, canvas.rows, entry['background']) canvas = composite_image(canvas, background_image, 0, 0) end end canvas end |
#draw_caption_to_canvas(entry, canvas, device, stylesheet_path = '') ⇒ Object
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 131 132 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 100 def draw_caption_to_canvas(entry, canvas, device, stylesheet_path = '') # If no caption is provided, it's ok to skip the body of this method return canvas if entry['text'].nil? text = entry['text'] text_size = device['text_size'] font_size = device['font_size'] locale = entry['locale'] text = resolve_text_into_path(text, locale) stylesheet_path = resolve_path(stylesheet_path) if can_resolve_path(stylesheet_path) width = text_size[0] height = text_size[1] x_position = 0 y_position = 0 unless device['text_offset'].nil? x_position = device['text_offset'][0] y_position = device['text_offset'][1] end draw_text_to_canvas(canvas, text, width, height, x_position, y_position, font_size, stylesheet_path) end |
#draw_device_frame_to_canvas(device, canvas) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 150 def draw_device_frame_to_canvas(device, canvas) # Apply the device frame to the canvas, but only if one is provided return canvas if device['device_frame_size'].nil? w = device['device_frame_size'][0] h = device['device_frame_size'][1] x = 0 y = 0 unless device['device_frame_size'].nil? x = device['device_frame_offset'][0] y = device['device_frame_offset'][1] end device_frame = open_image(device['device_frame']) device_frame = resize_image(device_frame, w, h) composite_image(canvas, device_frame, x, y) end |
#draw_file_attachment_to_canvas(attachment, canvas, entry) ⇒ Object
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 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 200 def (, canvas, entry) file = resolve_path(['file']) image = open_image(file) if .member?('operations') ['operations'].each do |operation| image = apply_operation(image, operation, canvas) end end size = ['size'] x_pos = ['position'][0] y_pos = ['position'][1] unless ['offset'].nil? x_pos += ['offset'][0] y_pos += ['offset'][1] end image = resize_image(image, size[0], size[1]) canvas = composite_image(canvas, image, x_pos, y_pos) end |
#draw_screenshot_to_canvas(entry, canvas, device) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 170 def draw_screenshot_to_canvas(entry, canvas, device) # Don't require a screenshot to be present – we can just skip this function if one doesn't exist. return canvas if entry['screenshot'].nil? device_mask = device['screenshot_mask'] screenshot_size = device['screenshot_size'] screenshot_offset = device['screenshot_offset'] screenshot = entry['screenshot'] screenshot = open_image(screenshot) screenshot = mask_image(screenshot, open_image(device_mask)) unless device_mask.nil? screenshot = resize_image(screenshot, screenshot_size[0], screenshot_size[1]) composite_image(canvas, screenshot, screenshot_offset[0], screenshot_offset[1]) end |
#draw_text_attachment_to_canvas(attachment, canvas, locale) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 227 def (, canvas, locale) text = resolve_text_into_path(['text'], locale) font_size = ['font-size'] ||= 12 width = ['size'][0] height = ['size'][1] x_position = ['position'][0] ||= 0 y_position = ['position'][1] ||= 0 stylesheet_path = ['stylesheet'] stylesheet_path = resolve_path(stylesheet_path) if can_resolve_path(stylesheet_path) alignment = ['alignment'] ||= 'center' draw_text_to_canvas(canvas, text, width, height, x_position, y_position, font_size, stylesheet_path, alignment) end |
#draw_text_to_canvas(canvas, text, width, height, x_position, y_position, font_size, stylesheet_path, position = 'center') ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 284 def draw_text_to_canvas(canvas, text, width, height, x_position, y_position, font_size, stylesheet_path, position = 'center') begin temp_text_file = Tempfile.new Action.sh('drawText', "html=#{text}", "maxWidth=#{width}", "maxHeight=#{height}", "output=#{temp_text_file.path}", "fontSize=#{font_size}", "stylesheet=#{stylesheet_path}", "alignment=#{position}") text_content = open_image(temp_text_file.path).trim text_frame = create_image(width, height) text_frame = case position when 'left' then composite_image_left(text_frame, text_content, 0, 0) when 'center' then composite_image_center(text_frame, text_content, 0, 0) when 'top' then composite_image_top(text_frame, text_content, 0, 0) end ensure temp_text_file.close temp_text_file.unlink end composite_image(canvas, text_frame, x_position, y_position) end |
#mask_image(image, mask, offset_x = 0, offset_y = 0) ⇒ Magick::Image
mask_image
318 319 320 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 318 def mask_image(image, mask, offset_x = 0, offset_y = 0) image.composite(mask, offset_x, offset_y, CopyAlphaCompositeOp) end |
#open_image(path) ⇒ Object
394 395 396 397 398 399 400 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 394 def open_image(path) path = resolve_path(path) Magick::Image.read(path) do |image| image.background_color = 'transparent' end.first end |
#read_config(config_file_path) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 37 def read_config(config_file_path) config_file_path = resolve_path(config_file_path) begin # NOTE: While JSON is a subset of YAML and thus YAML.load_file would technically cover both cases at once, in practice # `JSON.parse` is more lenient with JSON files than `YAML.load_file` is — especially, it accepts `// comments` in the # JSON file, despite this not being allowed in the spec — hence why we still try with `JSON.parse` for `.json` files. File.extname(config_file_path) == '.json' ? JSON.parse(File.read(config_file_path)) : YAML.load_file(config_file_path) rescue StandardError => e UI.error(e) UI.user_error!('Invalid JSON/YAML configuration. Please lint your config file to check for syntax errors.') end end |
#resize_image(original, width, height) ⇒ Magick::Image
resize_image
334 335 336 337 338 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 334 def resize_image(original, width, height) UI.user_error!('You must pass an image object to `resize_image`.') unless original.is_a?(Magick::Image) original.adaptive_resize(width, height) end |
#resolve_path(path) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 417 def resolve_path(path) UI.crash!('Path not provided – you must provide one to continue') if path.nil? [ Pathname.new(path), # Absolute Path Pathname.new(FastlaneCore::FastlaneFolder.fastfile_path).dirname + path, # Path Relative to the fastfile Fastlane::Helper::FilesystemHelper.plugin_root + path, # Path Relative to the plugin Fastlane::Helper::FilesystemHelper.plugin_root + 'spec/test-data/' + path, # Path Relative to the test data ] .each do |resolved_path| return resolved_path if !resolved_path.nil? && resolved_path.exist? end = "Unable to locate #{path}" UI.crash!() end |
#resolve_text_into_path(text, locale) ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 434 def resolve_text_into_path(text, locale) localized_file = format(text, locale) if File.exist?(localized_file) localized_file elsif can_resolve_path(localized_file) resolve_path(localized_file).realpath.to_s else format(text, 'source') end end |