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, shield_parameters, shield_base_url = nil) ⇒ Object
- #run(path, options) ⇒ Object
Instance Method Details
#add_badge(custom_badge, dark_badge, icon, alpha_badge, alpha_channel, badge_gravity) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/badge/runner.rb', line 188 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
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/badge/runner.rb', line 111 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" begin # Use argv-form system() instead of backticks so paths and scale # values can never be interpreted by the shell (avoids e.g. the # "Multiple SVG files are only allowed for PDF and (E)PS output" # error caused by unquoted shield paths, #54). if shield_no_resize system('rsvg-convert', '-z', shield_scale.to_s, '-o', new_path, '--', shield.path) else system('rsvg-convert', '-w', (icon.width * shield_scale).to_i.to_s, '-a', '-o', new_path, '--', shield.path) end rescue Exception => error UI.error "Other error occured. Use --verbose for more info".red UI.verbose error if FastlaneCore::Globals.verbose? 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
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/badge/runner.rb', line 163 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
205 206 207 208 209 210 211 212 |
# File 'lib/badge/runner.rb', line 205 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, shield_parameters, shield_base_url = nil) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/badge/runner.rb', line 149 def load_shield(shield_string, shield_parameters, shield_base_url = nil) svg_base = shield_base_url || Badge.shield_svg_base_url png_base = shield_base_url || Badge.shield_base_url url = (@@rsvg_enabled ? svg_base : png_base) + Badge.shield_path + shield_string + (@@rsvg_enabled ? ".svg" : ".png") if shield_parameters url = url + "?" + shield_parameters end UI.verbose "Trying to load image from shields.io. Timeout: #{Badge.shield_io_timeout}s".blue UI.verbose "URL: #{url}".blue MiniMagick::Image.open(url) end |
#run(path, options) ⇒ Object
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 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 104 105 106 107 108 109 |
# File 'lib/badge/runner.rb', line 10 def run(path, ) check_tools! UI.verbose "Verbose active... VERSION: #{Badge::VERSION}".blue UI.verbose "Parameters: #{.values.inspect}".blue # If custom glob is provided, use legacy behavior for backward compatibility if [:glob] UI.verbose "Using custom glob pattern (legacy mode)".blue glob = [:glob] app_icons = Dir.glob("#{path}#{glob}") else # Use new IconCatalog approach to intelligently detect icon formats UI.verbose "Using smart icon detection with Contents.json parsing".blue catalogs = IconCatalog.find_catalogs(path, [:glob]) app_icons = catalogs.flat_map(&:badgeable_icons) end UI.verbose "Found #{app_icons.count} icon file(s) to badge".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], [:shield_parameters], [:shield_base_url]) if [:shield] end rescue Timeout::Error UI.error "Error loading image from shields.io timeout reached. Use --verbose for more info".red rescue OpenURI::HTTPError => 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 [:grayscale] result.colorspace 'gray' icon_changed = true end 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 # Preserve the original file format (webp, jpg, png, ...) so we # don't silently write PNG bytes into a .webp filename. Falls back # to png if the input has no recognizable extension. ext = icon_path.extname.downcase.delete('.') result.format(ext.empty? ? 'png' : ext) result.write full_path end end if icon_changed UI. "Badged \\o/!".green else UI. "Did nothing... Enable --verbose for more info.".red end else UI.error "Could not find any app icons...".red end end |