Class: Badge::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/badge/runner.rb

Instance Method Summary collapse

Instance Method Details

#add_badge(custom_badge, dark_badge, icon, alpha_badge, alpha_channel) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/badge/runner.rb', line 93

def add_badge(custom_badge, dark_badge, icon, alpha_badge, alpha_channel)
  Helper.log.info "'#{icon.path}'"
  Helper.log.info "Adding badge image ontop of icon".blue unless not $verbose
  if custom_badge && File.exist?(custom_badge) # check if custom image is provided
    badge = MiniMagick::Image.open(custom_badge)
  else
    if alpha_badge
      badge = MiniMagick::Image.open(dark_badge ? Badge.alpha_dark_badge : Badge.alpha_light_badge)
    else
      badge = MiniMagick::Image.open(dark_badge ? Badge.beta_dark_badge : Badge.beta_light_badge)
    end
  end

  badge.resize "#{icon.width}x#{icon.height}"
  result = icon.composite(badge, 'png') do |c|
    c.compose "Over"
    c.alpha 'On' unless !alpha_channel
  end
end

#add_shield(icon, result, shield, alpha_channel) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/badge/runner.rb', line 66

def add_shield(icon, result, shield, alpha_channel)
  Helper.log.info "'#{icon.path}'"
  Helper.log.info "Adding shield.io image ontop of icon".blue unless not $verbose

  current_shield = MiniMagick::Image.open(shield.path)
  current_shield.resize "#{icon.width}x#{icon.height}>"
  result = result.composite(current_shield, 'png') do |c|
    c.compose "Over"
    c.alpha 'On' unless !alpha_channel
    c.gravity "north"
  end
end

#load_shield(shield_string) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/badge/runner.rb', line 79

def load_shield(shield_string)
  url = Badge.shield_base_url + Badge.shield_path + shield_string + ".png"
  file_name = shield_string + ".png"

  Helper.log.info "Trying to load image from shield.io. Timeout: #{Badge.shield_io_timeout}s".blue unless not $verbose
  Helper.log.info "URL: #{url}".blue unless not $verbose

  shield = Tempfile.new(file_name).tap do |file|
    file.binmode
    file.write(open(url).read)
    file.close
  end
end

#run(path, options) ⇒ Object



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/badge/runner.rb', line 8

def run(path, options)
  glob = "/**/*.appiconset/*.{png,PNG}"
  glob = options[:glob] unless not options[:glob]

  app_icons = Dir.glob("#{path}#{glob}")
  Helper.log.info "Verbose active...".blue unless not $verbose
  Helper.log.info "Parameters: #{options.inspect}".blue unless not $verbose

  alpha_channel = false
  if options[:alpha_channel]
    alpha_channel = true
  end

  if app_icons.count > 0
    Helper.log.info "Start adding badges...".green

    shield = nil
    begin
      timeout = Badge.shield_io_timeout
      timeout = options[:shield_io_timeout] unless not options[:shield_io_timeout]
      Timeout.timeout(timeout.to_i) do
        shield = load_shield(options[:shield]) unless not options[:shield]
      end
    rescue Timeout::Error
      Helper.log.error "Error loading image from shield.io timeout reached. Skipping Shield. Use --verbose for more info".red
    end

    icon_changed = false
    app_icons.each do |full_path|
      icon_path = Pathname.new(full_path)
      icon = MiniMagick::Image.new(full_path)

      result = MiniMagick::Image.new(full_path)
      
      if !options[:no_badge]
        result = add_badge(options[:custom], options[:dark], icon, options[:alpha], alpha_channel)
        icon_changed = true
      end
      if shield
        result = add_shield(icon, result, shield, alpha_channel)
        icon_changed = true
      end
      
      if icon_changed
        result.format "png"
        result.write full_path 
      end
    end
    if icon_changed
      Helper.log.info "Badged \\o/!".green
    else
      Helper.log.info "Did nothing... Enable --verbose for more info.".red
    end
  else
    Helper.log.error "Could not find any app icons...".red
  end
end