Class: IconBanner::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/icon_banner/process.rb

Direct Known Subclasses

AppIconSet, IcLauncher

Constant Summary collapse

BASE_ICON_PATH =
'__TO OVERRIDE__'
PLATFORM =
'__TO OVERRIDE__'
PLATFORM_CODE =
'__TO OVERRIDE__'
BACKUP_EXTENSION =
'.bak'

Instance Method Summary collapse

Instance Method Details

#backup_path(path) ⇒ Object



117
118
119
# File 'lib/icon_banner/process.rb', line 117

def backup_path(path)
  UI.error '`backup_path` should not be run on base class'
end

#create_backup(icon_path) ⇒ Object



94
95
96
97
98
# File 'lib/icon_banner/process.rb', line 94

def create_backup(icon_path)
  backup_path = backup_path(icon_path)
  FileUtils.mkdir_p(File.dirname(backup_path))
  FileUtils.cp(icon_path, backup_path)
end

#find_base_color(path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/icon_banner/process.rb', line 73

def find_base_color(path)
  color = MiniMagick::Tool::Convert.new do |convert|
    convert << path
    convert.colors 3
    convert.background 'white'
    convert.alpha 'remove'
    convert << '-unique-colors'
    convert.format '%[pixel:u]'
    convert << 'xc:transparent'
    convert << 'info:-'
  end
  color[/rgba?\([^)]*\)/]
end

#generate(path, options) ⇒ Object



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
43
44
45
46
47
48
49
# File 'lib/icon_banner/process.rb', line 12

def generate(path, options)
  IconBanner.validate_libs!
  return unless validate_platform(options)

  UI.message "Generating #{self.class::PLATFORM} banners..."

  restore(path) # restore in case it was already run before

  app_icons = get_app_icons(path)

  label = options[:label]
  font = options[:font] || IconBanner.font_path

  if app_icons.count > 0
    app_icons.each do |icon_path|
      UI.verbose "Processing #{icon_path}"
      create_backup icon_path if options[:backup]

      color = options[:color]
      begin
        color = find_base_color(icon_path)
        UI.verbose "Color: #{color}"
      end unless color

      banner_file = Tempfile.new %w[banner .png]
      generate_banner banner_file.path, label, color, font
      process_icon icon_path, banner_file.path
      banner_file.close

      UI.verbose "Completed processing #{File.basename icon_path}"
      UI.verbose ''
    end

    UI.message "Completed #{self.class::PLATFORM} generation."
  else
    UI.message "No #{self.class::PLATFORM} icons found."
  end
end

#generate_banner(path, label, color, font) ⇒ Object



113
114
115
# File 'lib/icon_banner/process.rb', line 113

def generate_banner(path, label, color, font)
  UI.error '`generate_banner` should not be run on base class'
end

#get_app_icons(path) ⇒ Object



68
69
70
71
# File 'lib/icon_banner/process.rb', line 68

def get_app_icons(path)
  app_icons = Dir.glob("#{path}#{self.class::BASE_ICON_PATH}")
  app_icons.reject { |icon| should_ignore_icon(icon) }
end

#process_icon(icon_path, banner_path) ⇒ Object



87
88
89
90
91
92
# File 'lib/icon_banner/process.rb', line 87

def process_icon(icon_path, banner_path)
  icon = MiniMagick::Image.open(icon_path)
  banner = MiniMagick::Image.open(banner_path)
  banner.resize "#{icon.width}x#{icon.height}"
  icon.composite(banner).write(icon_path)
end

#restore(path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/icon_banner/process.rb', line 51

def restore(path)
  return unless validate_platform({})

  UI.message "Restoring #{self.class::PLATFORM} icons..."

  app_icons = get_app_icons(path)

  if app_icons.count > 0
    app_icons.each do |icon_path|
      UI.verbose "Restoring #{icon_path}"
      restore_backup icon_path
    end
  end

  UI.message "Completed #{self.class::PLATFORM} restore."
end

#restore_backup(icon_path) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/icon_banner/process.rb', line 100

def restore_backup(icon_path)
  restore_path = backup_path(icon_path)
  if File.exist?(restore_path)
    FileUtils.cp(restore_path, icon_path)
    File.delete restore_path
  end
end

#should_ignore_icon(icon) ⇒ Object



121
122
123
# File 'lib/icon_banner/process.rb', line 121

def should_ignore_icon(icon)
  UI.error '`should_ignore_icon` should not be run on base class'
end

#validate_platform(options) ⇒ Object



108
109
110
111
# File 'lib/icon_banner/process.rb', line 108

def validate_platform(options)
  platform = ENV['FASTLANE_PLATFORM_NAME'] || options[:platform]
  platform.nil? || platform[/#{self.class::PLATFORM_CODE}/i] || platform == 'all'
end