Class: Fastlane::Actions::FramerAction
- Inherits:
-
Action
- Object
- Action
- Fastlane::Actions::FramerAction
- Defined in:
- lib/fastlane/plugin/framer/actions/framer_action.rb
Documentation collapse
- .authors ⇒ Object
- .available_options ⇒ Object
- .description ⇒ Object
- .is_supported?(platform) ⇒ Boolean
Class Method Summary collapse
-
.combine(screenshot_file, template, colors, text, output_file, list_files, index) ⇒ Object
Magic is HERE.
- .create_dir_if_not_exists(path) ⇒ Object
- .find_colors(source_dir, screenshot_file, colors_dir) ⇒ Object
- .find_output(source_folder, screenshot_file, output_folder, output_suffix) ⇒ Object
- .find_template(templates, screenshot_file, platform) ⇒ Object
- .find_text(source_dir, screenshot_file) ⇒ Object
- .load_templates(template_folder) ⇒ Object
- .run(params) ⇒ Object
Class Method Details
.authors ⇒ Object
465 466 467 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 465 def self. ["DrAL3X", "AzureRodrigo"] end |
.available_options ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 469 def self. [ FastlaneCore::ConfigItem.new(key: :source_folder, env_name: "FL_FRAMER_SOURCE_FOLDER", description: "Folder that contains screenshots to frame", is_string: true, default_value: "./fastlane/framer/screens", verify_block: proc do |value| UI.user_error!("Couldn't find folder at path '#{value}'") unless File.exist?(value) end), FastlaneCore::ConfigItem.new(key: :template_folder, env_name: "FL_FRAMER_TEMPLATE_FOLDER", description: "Folder that contains frames", is_string: true, default_value: "./fastlane/framer/templates", verify_block: proc do |value| UI.user_error!("Couldn't find folder at path '#{value}'") unless File.exist?(value) end), FastlaneCore::ConfigItem.new(key: :output_folder, env_name: "FL_FRAMER_OUTPUT_FOLDER", description: "Folder that will contains framed screenshots", is_string: true, default_value: "./fastlane/screenshots", verify_block: proc do |value| UI.user_error!("Couldn't find folder at path '#{value}'") unless File.exist?(value) end), FastlaneCore::ConfigItem.new(key: :output_suffix, env_name: "FL_FRAMER_OUTPUT_FILE_SUFFIX", description: "Suffix added to each framed screenshot in the output folder", is_string: true, default_value: "-framed") ] end |
.combine(screenshot_file, template, colors, text, output_file, list_files, index) ⇒ Object
Magic is HERE
244 245 246 247 248 249 250 251 252 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 283 284 285 286 287 288 289 290 291 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 244 def self.combine(screenshot_file, template, colors, text, output_file, list_files, index) # Get list lenght list_lenght = list_files.length() # Var images screenshot_img_back = nil screenshot_img = nil screenshot_img_next = nil # Prepare base image result_img = MiniMagick::Image.open("#{Framer::ROOT}/assets/background.png") result_img.resize "#{template.width}x#{template.height}!" # `!` says it should ignore the ratio # Apply background color, if any unless colors.background.nil? result_img. do |c| c.define "png:color-type=2" c.fill "#{colors.background}" c.draw "rectangle 0,0,#{template.width},#{template.height}" end end # Get template image template_img = MiniMagick::Image.open(template.file) # Get back screenshot unless template.imagePreviousOffset.nil? if list_lenght >= index -1 screenshot_img_back = MiniMagick::Image.open(list_files[index -1]).auto_orient screenshot_img_back.resize "#{template.imagePreviousWidth}x" unless template.imagePreviousRotation.nil? screenshot_img_back. do |cmd| cmd.background "rgba(255,255,255,0.0)" # transparent cmd.rotate(template.imagePreviousRotation.to_f) end end else UI.error "Unable to find back screenshot index #{index-1} in #{list_lenght}" end end # Get screenshot image screenshot_img = MiniMagick::Image.open(screenshot_file).auto_orient # Resize screenshot to fit template screenshot_img.resize "#{template.imageWidth}x" # rotate screenshot unless template.imageRotation.nil? screenshot_img. do |cmd| cmd.background "rgba(255,255,255,0.0)" # transparent cmd.rotate(template.imageRotation.to_f) end end # Get next screenshot unless template.imageNextOffset.nil? if list_lenght >= index + 1 image_path = list_files[index+1] list_files.each_with_index do |_file, _index| if _index === index print("file: ", _file, " index ", _index, " my index ", index) end end # print("aqui jovem: ", image_path, " list_lenght ", list_lenght, " index ", index) screenshot_img_next = MiniMagick::Image.open(list_files[index]).auto_orient screenshot_img_next.resize "#{template.imageNextWidth}x" unless template.imageNextRotation.nil? screenshot_img_next. do |cmd| cmd.background "rgba(255,255,255,0.0)" # transparent cmd.rotate(template.imageNextRotation.to_f) end end else UI.error "Unable to find next screenshot index #{index+1} in #{list_lenght}" end end # Put screenshot over template if template.imageBelow # Screenshot back unless screenshot_img_back.nil? result_img = result_img.composite(screenshot_img_back) do |c| c.compose "Over" c.geometry template.imagePreviousOffset.to_s end end # Screenshot first result_img = result_img.composite(screenshot_img) do |c| c.compose "Over" c.geometry template.imageOffset.to_s end # Screenshot next unless screenshot_img_next.nil? result_img = result_img.composite(screenshot_img_next) do |c| c.compose "Over" c.geometry template.imageNextOffset.to_s end end # Template second result_img = result_img.composite(template_img) do |c| c.compose "Over" end else # Template first result_img = result_img.composite(template_img) do |c| c.compose "Over" end # Screenshot back unless screenshot_img_back.nil? result_img = result_img.composite(screenshot_img_back) do |c| c.compose "Over" c.geometry template.imagePreviousOffset.to_s end end # Screenshot first result_img = result_img.composite(screenshot_img) do |c| c.compose "Over" c.geometry template.imageOffset.to_s end # Screenshot next unless screenshot_img_next.nil? result_img = result_img.composite(screenshot_img_next) do |c| c.compose "Over" c.geometry template.imageNextOffset.to_s end end end # Apply text, if any unless text.nil? # Clean text string before using it text.gsub! '\n', "\n" text.gsub!(/(?<!\\)(')/) { |s| "\\#{s}" } # escape unescaped apostrophes with a backslash # Create image with text text_img = MiniMagick::Image.open("#{Framer::ROOT}/assets/background.png") text_img.resize "2732x2732!" # Max space available. `!` says it should ignore the ratio text_font = template.textFont.nil? ? "Helvetica" : template.textFont text_img. do |c| c.font text_font c.pointsize template.textSize.to_s c.gravity "NorthWest" c.draw "text 0,0 '#{text}'" c.fill colors.text.to_s end text_img.trim # remove white space UI.verbose "text requires an area of #{text_img.width}x#{text_img.height}" # Scale down to fit space (if needed) available_width = (template.textWidth || template_img.width) - template.textPadding * 2 available_height = template.textHeight ratio = available_width.to_f / text_img.width.to_f if ratio < 1 UI.important "Scaling down text to fit in space (ratio: #{ratio.round(3)})" text_img.resize "#{available_width}x" end UI.verbose "text area is now #{text_img.width}x#{text_img.height}" # Put text image over template offset_x = ((available_width - text_img.width) / 2.0 + template.textPadding).round + template.textOffsetX offset_y = ((available_height - text_img.height) / 2.0).round + template.textOffsetY UI.verbose "text final offset x: #{offset_x} y: #{offset_y}" result_img = result_img.composite(text_img) do |c| c.compose "Over" c.geometry "+#{offset_x}+#{offset_y}" end text_img.destroy! end # Save result result_img.format "png" result_img.write output_file # Cleanup temp files result_img.destroy! screenshot_img.destroy! template_img.destroy! end |
.create_dir_if_not_exists(path) ⇒ Object
448 449 450 451 452 453 454 455 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 448 def self.create_dir_if_not_exists(path) recursive = path.split('/') directory = '' recursive.each do |sub_directory| directory += sub_directory + '/' Dir.mkdir(directory) unless (File.directory? directory) end end |
.description ⇒ Object
461 462 463 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 461 def self.description "Create images combining app screenshots to templates to make a nice \'screenshot\' to upload in App Store and Google Play" end |
.find_colors(source_dir, screenshot_file, colors_dir) ⇒ Object
180 181 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 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 180 def self.find_colors(source_dir, screenshot_file, colors_dir) # Default values colors = Colors.new colors.text = "#000000" colors.background = nil # Read values from file directory = File.dirname(screenshot_file) colors_path = File.join(colors_dir, "colors.json") while directory.start_with?(source_dir) && !File.exist?(colors_path) do directory = File.dirname(directory) colors_path = File.join(directory, "colors.json") end if File.exist?(colors_path) config = JSON.parse(File.read(colors_path)) # Read default values default = Colors.new default.text = config['default']['text'] default.background = config['default']['background'] colors.merge(default) # Read and apply override, if any override = config.select { |k, v| File.basename(screenshot_file).upcase.include? k.upcase }.values.map { |value| c = Colors.new c.text = value['text'] c.background = value['background'] c } unless override.empty? colors.merge(override.first) end end return colors end |
.find_output(source_folder, screenshot_file, output_folder, output_suffix) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 220 def self.find_output(source_folder, screenshot_file, output_folder, output_suffix) # Prepare file name if output_suffix.empty? file = File.basename(screenshot_file) else filename = File.basename(screenshot_file, ".*") extention = File.extname(screenshot_file) file = filename + output_suffix + extention end sub_path = File.dirname(screenshot_file).sub(source_folder, "") # Prepare file path file_path = File.join(File.join(output_folder, sub_path), file) # Ensure output dir exist folder = File.dirname(file_path) self.create_dir_if_not_exists(folder) return file_path end |
.find_template(templates, screenshot_file, platform) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 146 def self.find_template(templates, screenshot_file, platform) if [:ios, :mac].include? platform # Read device name from file filename = File.basename(screenshot_file) device = filename.slice(0, filename.rindex('-')) elsif :android == platform # Read device name from path folder = File.basename(File.dirname(screenshot_file)) device = folder.slice(0, folder.rindex('S')) else UI.error "Unsupported platform" end # Search template that matches that size return templates.find { |template| template.name == device } end |
.find_text(source_dir, screenshot_file) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 163 def self.find_text(source_dir, screenshot_file) directory = File.dirname(screenshot_file) strings_path = File.join(directory, "text.json") while directory.start_with?(source_dir) && !File.exist?(strings_path) do directory = File.dirname(directory) strings_path = File.join(directory, "text.json") end return nil unless File.exist?(strings_path) text = JSON.parse(File.read(strings_path)) result = text.find { |k, v| File.basename(screenshot_file).upcase.include? k.upcase } return result.last if result end |
.is_supported?(platform) ⇒ Boolean
503 504 505 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 503 def self.is_supported?(platform) true end |
.load_templates(template_folder) ⇒ Object
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 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 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 79 def self.load_templates(template_folder) json_file_path = "#{template_folder}/config.json" UI.user_error!("Missing Config.json file in template folder") unless File.exist?(json_file_path) # Read JSON configuration json_file = File.read(json_file_path) json_config = JSON.parse(json_file) config_default = json_config['default'] # Detect available templates templates = [] Dir.glob("#{template_folder}/**/*.png") do |file| name = File.basename(file, ".png") UI. "Loading template #{name}" template = Template.new template.file = file template.name = name # Read template image size img = MiniMagick::Image.open(file) template.width = img.width template.height = img.height img.destroy! # Get template config config_custom = json_config[name] if config_custom.nil? UI.error "Missing configuration for template #{name}" next end # set image template.imageOffset = (config_custom['image'] && config_custom['image']['offset']) || (config_default['image'] && config_default['image']['offset']) template.imageWidth = (config_custom['image'] && config_custom['image']['width']) || (config_default['image'] && config_default['image']['width']) template.imageBelow = (config_custom['image'] && config_custom['image']['add_below']) || (config_default['image'] && config_default['image']['add_below']) || false template.imageRotation = (config_custom['image'] && config_custom['image']['rotation']) || (config_default['image'] && config_default['image']['rotation']) # set image back template.imagePreviousOffset = (config_custom['image']['previous'] && config_custom['image']['previous']['offset']) || (config_default['image']['previous'] && config_default['image']['previous']['offset']) template.imagePreviousWidth = (config_custom['image']['previous'] && config_custom['image']['previous']['width']) || (config_default['image']['previous'] && config_default['image']['previous']['width']) template.imagePreviousRotation = (config_custom['image']['previous'] && config_custom['image']['previous']['rotation']) || (config_default['image']['previous'] && config_default['image']['previous']['rotation']) # set image next template.imageNextOffset = (config_custom['image']['next'] && config_custom['image']['next']['offset']) || (config_default['image']['next'] && config_default['image']['next']['offset']) template.imageNextWidth = (config_custom['image']['next'] && config_custom['image']['next']['width']) || (config_default['image']['next'] && config_default['image']['next']['width']) template.imageNextRotation = (config_custom['image']['next'] && config_custom['image']['next']['rotation']) || (config_default['image']['next'] && config_default['image']['next']['rotation']) # set font template.textFont = (config_custom['text'] && config_custom['text']['font']) || (config_default['text'] && config_default['text']['font']) template.textSize = (config_custom['text'] && config_custom['text']['size']) || (config_default['text'] && config_default['text']['size']) template.textWidth = (config_custom['text'] && config_custom['text']['width']) || (config_default['text'] && config_default['text']['width']) template.textHeight = (config_custom['text'] && config_custom['text']['height']) || (config_default['text'] && config_default['text']['height']) template.textPadding = (config_custom['text'] && config_custom['text']['padding']) || (config_default['text'] && config_default['text']['padding']) || 0 template.textOffsetX = (config_custom['text'] && config_custom['text']['offset_x']) || (config_default['text'] && config_default['text']['offset_x']) || 0 template.textOffsetY = (config_custom['text'] && config_custom['text']['offset_y']) || (config_default['text'] && config_default['text']['offset_y']) || 0 templates << template end return templates end |
.run(params) ⇒ Object
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 |
# File 'lib/fastlane/plugin/framer/actions/framer_action.rb', line 36 def self.run(params) source_folder = params[:source_folder] output_folder = params[:output_folder] template_folder = params[:template_folder] list_files = Dir.glob("#{source_folder}/**/*.png").sort templates = [] platform = Actions.lane_context[Actions::SharedValues::PLATFORM_NAME] # Read config UI.success "Fetching templates from #{template_folder}" templates = self.load_templates(template_folder) # Process each screen UI.success "Processing screenshots from #{source_folder}" list_files.each_with_index do |file, index| UI. "Processing #{file} index #{index}" template = self.find_template(templates, file, platform) if template.nil? UI.error "Unable to find template for screenshot #{file}" next end UI.verbose "Using template: #{template.name} (#{template.width}x#{template.height})" text = self.find_text(source_folder, file) UI.verbose "Using text: #{text}" colors = self.find_colors(source_folder, file, template_folder) UI.verbose "Using colors: #{colors}" output = self.find_output(source_folder, file, output_folder, params[:output_suffix]) UI.verbose "Saving to: #{output}" # # Do the magic self.combine(file, template, colors, text, output, list_files, index) UI.verbose "Framed screenshot #{output}" end # Done UI.success "All screenshots are now framed!" end |