Class: Fastlane::Helper::PromoScreenshots
- Inherits:
-
Object
- Object
- Fastlane::Helper::PromoScreenshots
- Defined in:
- lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb
Class Method Summary collapse
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 36 37 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 25 def initialize if $skip_magick = <<~MSG PromoScreenshots feature is currently disabled. Please, install RMagick if you aim to generate the PromoScreenshots. 'bundle install --with screenshots' should do it if your project is configured for PromoScreenshots. Aborting. MSG UI.user_error!() end UI.user_error!('`drawText` not found – install it using `brew install automattic/build-tools/drawText`.') unless self.class.draw_text_available? end |
Class Method Details
.draw_text_available? ⇒ Boolean
39 40 41 42 43 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 39 def self.draw_text_available? return @draw_text_available if defined?(@draw_text_available) @draw_text_available = system('command -v drawText', %i[out err] => File::NULL) end |
Instance Method Details
#apply_operation(image, operation, canvas) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 261 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
431 432 433 434 435 436 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 431 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.
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 99 100 101 102 103 104 105 106 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 67 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
372 373 374 375 376 377 378 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 372 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
388 389 390 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 388 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
384 385 386 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 384 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
380 381 382 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 380 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
420 421 422 423 424 425 426 427 428 429 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 420 def create_image(width, height, background = 'transparent') # The paint method we call below modifies the string in place. # But if the string is frozen, we need to dup it first, otherwise we'll get a frozen string error. working_background = background.frozen? ? background.dup : background working_background.paint.to_hex Image.new(width, height) do |info| info.background_color = working_background end end |
#crop_image(original, x_position, y_position, width, height) ⇒ Magick::Image
crop_image
406 407 408 409 410 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 406 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
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 196 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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 142 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
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 133 134 135 136 137 138 139 140 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 108 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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 158 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
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 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 208 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 178 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
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 235 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
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 292 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 Actions.sh( 'drawText', "html=#{text}", "maxWidth=#{width}", "maxHeight=#{height}", "output=#{temp_text_file.path}", "fontSize=#{font_size}", "stylesheet=#{stylesheet_path}", "alignment=#{position}", log: false ) 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
336 337 338 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 336 def mask_image(image, mask, offset_x = 0, offset_y = 0) image.composite(mask, offset_x, offset_y, CopyAlphaCompositeOp) end |
#open_image(path) ⇒ Object
412 413 414 415 416 417 418 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 412 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
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 45 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
352 353 354 355 356 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 352 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
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 438 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 = <<~MESSAGE Unable to locate #{path}. Did you run the automation to generate the screenshots? MESSAGE UI.user_error!() end |
#resolve_text_into_path(text, locale) ⇒ Object
459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 459 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 UI.important("Could not identify '#{localized_file}' as a file path or the file was not found. Will use its value as a raw string. This may result in undesired annotations.") format(text, 'source') end end |