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
-
#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_json(configFilePath) ⇒ 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.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 23 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
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 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 202 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
359 360 361 362 363 364 365 366 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 359 def can_resolve_path(path) begin resolve_path(path) return true rescue return false end end |
#composite_image(original, child, x_position, y_position, starting_position = NorthWestGravity) ⇒ Magick::Image
composite_image
303 304 305 306 307 308 309 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 303 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
319 320 321 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 319 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
315 316 317 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 315 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
311 312 313 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 311 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
351 352 353 354 355 356 357 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 351 def create_image(width, height, background = 'transparent') background_color = 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
337 338 339 340 341 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 337 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
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 137 def (entry, canvas) entry['attachments'].each do || if !['file'].nil? canvas = (, canvas, entry) elsif !['text'].nil? canvas = (, canvas, entry['locale']) end end return canvas end |
#draw_background_to_canvas(canvas, entry) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 83 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
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 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 49 def (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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 99 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
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 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 149 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 119 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 176 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
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 233 def draw_text_to_canvas(canvas, text, width, height, x_position, y_position, font_size, stylesheet_path, position = 'center') begin tempTextFile = Tempfile.new() Action.sh('drawText', "html=#{text}", "maxWidth=#{width}", "maxHeight=#{height}", "output=#{tempTextFile.path}", "fontSize=#{font_size}", "stylesheet=#{stylesheet_path}", "alignment=#{position}") text_content = open_image(tempTextFile.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 tempTextFile.close tempTextFile.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
267 268 269 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 267 def mask_image(image, mask, offset_x = 0, offset_y = 0) image.composite(mask, offset_x, offset_y, CopyAlphaCompositeOp) end |
#open_image(path) ⇒ Object
343 344 345 346 347 348 349 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 343 def open_image(path) path = resolve_path(path) Magick::Image.read(path) do self.background_color = 'transparent' end.first end |
#read_json(configFilePath) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 35 def read_json(configFilePath) configFilePath = resolve_path(configFilePath) begin return JSON.parse(open(configFilePath).read) rescue linter = JsonLint::Linter.new linter.check(configFilePath) linter.display_errors UI.user_error!('Invalid JSON configuration. See errors in log.') end end |
#resize_image(original, width, height) ⇒ Magick::Image
resize_image
283 284 285 286 287 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 283 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
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 368 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
385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/promo_screenshots_helper.rb', line 385 def resolve_text_into_path(text, locale) localizedFile = format(text, locale) text = if File.exist?(localizedFile) localizedFile elsif can_resolve_path(localizedFile) resolve_path(localizedFile).realpath.to_s else format(text, 'source') end end |