Class: Badge::Runner
- Inherits:
-
Object
- Object
- Badge::Runner
- Defined in:
- lib/badge/runner.rb
Constant Summary collapse
- @@retry_attemps =
0
- @@rsvg_enabled =
true
Instance Method Summary collapse
- #add_badge(custom_badge, dark_badge, icon, alpha_badge, alpha_channel, badge_gravity) ⇒ Object
- #add_shield(icon, result, shield, alpha_channel, shield_gravity, shield_no_resize, shield_scale, shield_geometry) ⇒ Object
- #check_tools! ⇒ Object
- #composite(image, overlay, alpha_channel, gravity, geometry = nil) ⇒ Object
- #load_shield(shield_string) ⇒ Object
- #run(path, options) ⇒ Object
Instance Method Details
#add_badge(custom_badge, dark_badge, icon, alpha_badge, alpha_channel, badge_gravity) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/badge/runner.rb', line 165 def add_badge(custom_badge, dark_badge, icon, alpha_badge, alpha_channel, badge_gravity) UI. "'#{icon.path}'" UI.verbose "Adding badge image ontop of icon".blue 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 = composite(icon, badge, alpha_channel, badge_gravity || "SouthEast") end |
#add_shield(icon, result, shield, alpha_channel, shield_gravity, shield_no_resize, shield_scale, shield_geometry) ⇒ Object
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 |
# File 'lib/badge/runner.rb', line 101 def add_shield(icon, result, shield, alpha_channel, shield_gravity, shield_no_resize, shield_scale, shield_geometry) UI. "'#{icon.path}'" UI.verbose "Adding shields.io image ontop of icon".blue shield_scale = shield_scale ? shield_scale.to_f : 1.0 if @@rsvg_enabled new_path = "#{shield.path}.png" if shield_no_resize `rsvg-convert #{shield.path} -z #{shield_scale} -o #{new_path}` else `rsvg-convert #{shield.path} -w #{(icon.width * shield_scale).to_i} -a -o #{new_path}` end new_shield = MiniMagick::Image.open(new_path) else new_shield = MiniMagick::Image.open(shield.path) if icon.width > new_shield.width && !shield_no_resize new_shield.resize "#{(icon.width * shield_scale).to_i}x#{icon.height}<" else new_shield.resize "#{icon.width}x#{icon.height}>" end end result = composite(result, new_shield, alpha_channel, shield_gravity || "north", shield_geometry) end |
#check_tools! ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/badge/runner.rb', line 140 def check_tools! if !`which rsvg-convert`.include?('rsvg-convert') UI.important("Install RSVG to get better results for shields on top of your icon") UI.important("") UI.important("Install it using (RSVG):") UI.command("brew install librsvg") UI.important("") @@rsvg_enabled = false end return if `which convert`.include?('convert') return if `which gm`.include?('gm') UI.error("You have to install ImageMagick or GraphicsMagick to use `badge`") UI.error("") UI.error("Install it using (ImageMagick):") UI.command("brew install imagemagick") UI.error("") UI.error("Install it using (GraphicsMagick):") UI.command("brew install graphicsmagick") UI.error("") UI.error("If you don't have homebrew, visit http://brew.sh") UI.user_error!("Install ImageMagick and start your lane again!") end |
#composite(image, overlay, alpha_channel, gravity, geometry = nil) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/badge/runner.rb', line 182 def composite(image, , alpha_channel, gravity, geometry = nil) image.composite(, 'png') do |c| c.compose "Over" c.alpha 'On' unless !alpha_channel c.gravity gravity c.geometry geometry if geometry end end |
#load_shield(shield_string) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/badge/runner.rb', line 127 def load_shield(shield_string) url = Badge.shield_base_url + Badge.shield_path + shield_string + (@@rsvg_enabled ? ".svg" : ".png") file_name = shield_string + (@@rsvg_enabled ? ".svg" : ".png") UI.verbose "Trying to load image from shields.io. Timeout: #{Badge.shield_io_timeout}s".blue UI.verbose "URL: #{url}".blue Curl::Easy.download(url, file_name) MiniMagick::Image.open(file_name) unless @@rsvg_enabled File.open(file_name) end |
#run(path, options) ⇒ Object
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 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 |
# File 'lib/badge/runner.rb', line 11 def run(path, ) check_tools! glob = "/**/*.appiconset/*.{png,PNG}" glob = [:glob] if [:glob] app_icons = Dir.glob("#{path}#{glob}") UI.verbose "Verbose active... VERSION: #{Badge::VERSION}".blue UI.verbose "Parameters: #{.values.inspect}".blue if [:custom] && !File.exist?([:custom]) UI.error("Could not find custom badge image") UI.user_error!("Specify a valid custom badge image path!") end alpha_channel = false if [:alpha_channel] alpha_channel = true end if app_icons.count > 0 UI. "Start adding badges...".green shield = nil begin timeout = Badge.shield_io_timeout timeout = [:shield_io_timeout] if [:shield_io_timeout] Timeout.timeout(timeout.to_i) do shield = load_shield([:shield]) if [:shield] end rescue Timeout::Error UI.error "Error loading image from shields.io timeout reached. Use --verbose for more info".red rescue Curl::Err::CurlError => error response = error.io UI.error "Error loading image from shields.io response Error. Use --verbose for more info".red UI.verbose response.status if FastlaneCore::Globals.verbose? rescue MiniMagick::Invalid UI.error "Error validating image from shields.io. Use --verbose for more info".red rescue Exception => error UI.error "Other error occured. Use --verbose for more info".red UI.verbose error if FastlaneCore::Globals.verbose? end if [:shield] && shield == nil if @@retry_attemps >= Badge.shield_io_retries UI.error "Cannot load image from shields.io skipping it...".red else UI. "Waiting for #{timeout.to_i}s and retry to load image from shields.io tries remaining: #{Badge.shield_io_retries - @@retry_attemps}".red sleep timeout.to_i @@retry_attemps += 1 return run(path, ) end 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 ![:no_badge] result = add_badge([:custom], [:dark], icon, [:alpha], alpha_channel, [:badge_gravity]) icon_changed = true end if shield result = add_shield(icon, result, shield, alpha_channel, [:shield_gravity], [:shield_no_resize], [:shield_scale], [:shield_geometry]) icon_changed = true end if icon_changed result.format "png" result.write full_path end end if icon_changed UI. "Badged \\o/!".green else UI. "Did nothing... Enable --verbose for more info.".red end if shield File.delete(shield) if File.exist?(shield) File.delete("#{shield.path}.png") if File.exist?("#{shield.path}.png") end else UI.error "Could not find any app icons...".red end end |