class MakeIcon::SocialAction

Public Class Methods

authors() click to toggle source
# File lib/makeicon.rb, line 130
def self.authors
  ["@adrum"]
end
available_options() click to toggle source
# File lib/makeicon.rb, line 134
def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :appicon_image_file,
                            env_name: "APPICON_IMAGE_FILE",
                         description: "Path to a square image file, at least 512x512",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_devices,
                            env_name: "APPICON_DEVICES",
                       default_value: [:phone],
                         description: "Array of device types to generate icons for",
                            optional: true,
                                type: Array),
    FastlaneCore::ConfigItem.new(key: :appicon_path,
                            env_name: "APPICON_PATH",
                       default_value: 'app/res/mipmap/',
                         description: "Path to res subfolder",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_filename,
                            env_name: "APPICON_FILENAME",
                       default_value: 'ic_launcher',
                         description: "The output filename of each image",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_notification_icon_path,
                            env_name: "APPICON_NOTIFICATION_ICON_PATH",
                       default_value: 'app/res/drawable/',
                         description: "Path to res subfolder",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_notification_icon_filename,
                            env_name: "APPICON_NOTIFICATION_ICON_FILENAME",
                       default_value: 'ic_stat_onesignal_default',
                         description: "File name for notification icons",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :appicon_custom_sizes,
                         description: "Hash of custom sizes - {'path/icon.png' => '256x256'}",
                            optional: true,
                                type: Hash)
  ]
end
description() click to toggle source
# File lib/makeicon.rb, line 126
def self.description
  "Generate required icon sizes from a master application icon"
end
get_custom_sizes(image, custom_sizes) click to toggle source
# File lib/makeicon.rb, line 122
def self.get_custom_sizes(image, custom_sizes)
  
end
get_needed_icons(device, needed_icons, is_android = false, custom_sizes = {}) click to toggle source
# File lib/makeicon.rb, line 18
def self.get_needed_icons(device, needed_icons, is_android = false, custom_sizes = {})
  icons = []
  puts device
  needed_icons[device].each do |scale, sizes|
    sizes.each do |size|
      if size.kind_of?(Array)
        size, role, subtype = size
      end

      if is_android
        width, height = size.split('x').map { |v| v.to_f }
      else
        width, height = size.split('x').map { |v| v.to_f * scale.to_i }
      end

      icons << {
        'width' => width,
        'height' => height,
        'size' => size,
        'device' => device.to_s.gsub('_', '-'),
        'scale' => scale,
        'role' => role,
        'subtype' => subtype
      }
    end
  end
  
  # Add custom icon sizes (probably for notifications)
  custom_sizes.each do |path, size|
    path = path.to_s
    width, height = size.split('x').map { |v| v.to_f }

    icons << {
      'width' => width,
      'height' => height,
      'size' => size,
      'basepath' => File.dirname(path),
      'filename' => File.basename(path)
    }
  end
  
  # Sort from the largest to the smallest needed icon
  icons = icons.sort_by {|value| value['width']} .reverse
end
is_supported?(platform) click to toggle source
# File lib/makeicon.rb, line 178
def self.is_supported?(platform)
  [:android].include?(platform)
end
needed_icons() click to toggle source
# File lib/makeicon.rb, line 63
def self.needed_icons
  {
    social: {
      'weixin' => ['28x28','108x108'],
      'weibo' => ['16x16','80x80','120x120'],
      'qq' => ['16x16','512x512'],
    },
  }
end
notification_icons(path, filename) click to toggle source
# File lib/makeicon.rb, line 73
def self.notification_icons(path, filename)
  return {} if !path || !filename
  
  {
    "#{path}-ldpi/#{filename}" => '36x36',
    "#{path}-mdpi/#{filename}" => '24x24',
    "#{path}-hdpi/#{filename}" => '36x36',
    "#{path}-xhdpi/#{filename}" => '48x48',
    "#{path}-xxhdpi/#{filename}" => '72x72',
    "#{path}-xxxhdpi/#{filename}" => '96x96',
  }
end
run(params) click to toggle source
# File lib/makeicon.rb, line 86
def self.run(params)
  fname = params[:appicon_image_file]
  today = Time.new
  require 'mini_magick'
  image = MiniMagick::Image.open(fname)
  fpname = Pathname.new(fname)
  basedir  = Pathname.new(fpname.dirname) + today.strftime("%Y-%m-%d-%H-%M-%S")

  Helper::AppiconHelper.check_input_image_size(image, 512)

  # Convert image to png
  image.format 'png'
  
  # Merge notification icons into customer sizes as they are handled thes same way

  icons = get_needed_icons(:social, self.needed_icons, true)
  icons.each do |icon|
    width = icon['width']
    height = icon['height']

    # Custom icons will have basepath and filename already defined
    if icon.has_key?('basepath') && icon.has_key?('filename')
      basepath = Pathname.new(icon['basepath'])
      filename = icon['filename']  
    else
      basepath = basedir + Pathname.new("#{params[:appicon_path]}-#{icon['scale']}")
      filename = "#{params[:appicon_filename]}_#{width}x#{height}.png"
    end
    FileUtils.mkdir_p(basepath)
    image.resize "#{width}x#{height}"
    image.write basepath + filename
  end

  puts "Successfully stored launcher icons at '#{basedir}'"
end