Class: Fastlane::Actions::AndroidAppiconAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/appicon/actions/android_appicon_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject


138
139
140
# File 'lib/fastlane/plugin/appicon/actions/android_appicon_action.rb', line 138

def self.authors
  ["@adrum"]
end

.available_optionsObject


142
143
144
145
146
147
148
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
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fastlane/plugin/appicon/actions/android_appicon_action.rb', line 142

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_icon_types,
                            env_name: "APPICON_ICON_TYPES",
                       default_value: [:launcher],
                         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_custom_sizes,
                         description: "Hash of custom sizes - {'path/icon.png' => '256x256'}",
                       default_value: {},
                            optional: true,
                                type: Hash),
    FastlaneCore::ConfigItem.new(key: :generate_rounded,
                         description: "Generate round icons?",
                       default_value: false,
                                type: Boolean),
    FastlaneCore::ConfigItem.new(key: :minimagick_cli,
                            env_name: "APPICON_MINIMAGICK_CLI",
                         description: "Set MiniMagick CLI (auto picked by default). Values are: graphicsmagick, imagemagick",
                            optional: true,
                                type: String,
                        verify_block: proc do |value|
                                  av = %w(graphicsmagick imagemagick)
                                  UI.user_error!("Unsupported minimagick cli '#{value}', must be: #{av}") unless av.include?(value)
                                end)
  ]
end

.descriptionObject


134
135
136
# File 'lib/fastlane/plugin/appicon/actions/android_appicon_action.rb', line 134

def self.description
  "Generate required icon sizes from a master application icon"
end

.get_custom_sizes(image, custom_sizes) ⇒ Object


130
131
132
# File 'lib/fastlane/plugin/appicon/actions/android_appicon_action.rb', line 130

def self.get_custom_sizes(image, custom_sizes)

end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)

188
189
190
# File 'lib/fastlane/plugin/appicon/actions/android_appicon_action.rb', line 188

def self.is_supported?(platform)
  [:android].include?(platform)
end

.needed_iconsObject


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fastlane/plugin/appicon/actions/android_appicon_action.rb', line 7

def self.needed_icons
  {
    launcher: {
      :ldpi => ['36x36'],
      :mdpi => ['48x48'],
      :hdpi => ['72x72'],
      :xhdpi => ['96x96'],
      :xxhdpi => ['144x144'],
      :xxxhdpi => ['192x192']
    },
    notification: {
      :ldpi => ['18x18'],
      :mdpi => ['24x24'],
      :hdpi => ['36x36'],
      :xhdpi => ['48x48'],
      :xxhdpi => ['72x72'],
      :xxxhdpi => ['96x96'],
    },
    splash_land: {
      'land-ldpi' => ['320x200'],
      'land-mdpi' => ['480x320'],
      'land-hdpi' => ['800x480'],
      'land-xhdpi' => ['1280x720'],
      'land-xxhdpi' => ['1600x960'],
      'land-xxxhdpi' => ['1920x1280']
    },
    splash_port: {
      'port-ldpi' => ['200x320'],
      'port-mdpi' => ['320x480'],
      'port-hdpi' => ['480x800'],
      'port-xhdpi' => ['720x1280'],
      'port-xxhdpi' => ['960x1600'],
      'port-xxxhdpi' => ['1280x1920']
    }
  }
end

.round(img) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fastlane/plugin/appicon/actions/android_appicon_action.rb', line 105

def self.round(img)
  require 'mini_magick'
  img.format 'png'

  width = img[:width]-2
  radius = width/2

  mask = ::MiniMagick::Image.open img.path
  mask.format 'png'

  mask.combine_options do |m|
    m.alpha 'transparent'
    m.background 'none'
    m.fill 'white'
    m.draw 'roundrectangle 1,1,%s,%s,%s,%s' % [width, width, radius, radius]
  end

  masked = img.composite(mask, 'png') do |i|
    i.alpha "set"
    i.compose 'DstIn'
  end

  return masked
end

.run(params) ⇒ Object


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
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
# File 'lib/fastlane/plugin/appicon/actions/android_appicon_action.rb', line 44

def self.run(params)

  Helper::AppiconHelper.set_cli(params[:minimagick_cli])

  fname = params[:appicon_image_file]
  custom_sizes = params[:appicon_custom_sizes]

  icons = Helper::AppiconHelper.get_needed_icons(params[:appicon_icon_types], self.needed_icons, true, custom_sizes)
  icons.each do |icon|
    image = MiniMagick::Image.open(fname)

    Helper::AppiconHelper.check_input_image_size(image, 1024, 1024)

    # 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 = Pathname.new("#{params[:appicon_path]}-#{icon['scale']}")
      filename = "#{params[:appicon_filename]}.png"
    end

    width_height = [icon['width'], icon['height']].map(&:to_i)
    width, height = width_height
    max = width_height.max

    image.format 'png'
    image.resize "#{max}x#{max}"

    unless width == height
      offset =
      if width > height
        "+0+#{(width - height) / 2}"
      elsif height > width
        "+#{(height - width) / 2}+0"
      end

      image.crop "#{icon['size']}#{offset}"
    end

    FileUtils.mkdir_p(basepath)
    image.write basepath + filename

    if basepath.to_s.match("port-")
      default_portrait_path = basepath.to_s.gsub("port-","")
      FileUtils.mkdir_p(default_portrait_path)
      image.write default_portrait_path + '/' + filename
    end

    if params[:generate_rounded]
      rounded_image = MiniMagick::Image.open(fname)
      rounded_image.format 'png'
      rounded_image.resize "#{width}x#{height}"
      rounded_image = round(rounded_image)
      rounded_image.write basepath + filename.gsub('.png', '_round.png')
    end
  end

  UI.success("Successfully stored launcher icons at '#{params[:appicon_path]}'")
end