|
| 1 | +<?php |
| 2 | + |
| 3 | +$inputFile = $argv[1]; |
| 4 | +$outputFile = $argv[2]; |
| 5 | +$coverageName = isset($argv[3]) ? $argv[3] : 'coverage'; |
| 6 | + |
| 7 | +if (!file_exists($inputFile)) { |
| 8 | + throw new InvalidArgumentException('Invalid input file provided'); |
| 9 | +} |
| 10 | + |
| 11 | +try { |
| 12 | + $xml = new SimpleXMLElement(file_get_contents($inputFile)); |
| 13 | + |
| 14 | + $metrics = $xml->xpath('//metrics'); |
| 15 | + $totalElements = 0; |
| 16 | + $checkedElements = 0; |
| 17 | + |
| 18 | + foreach ($metrics as $metric) { |
| 19 | + $totalElements += (int) $metric['elements']; |
| 20 | + $checkedElements += (int) $metric['coveredelements']; |
| 21 | + } |
| 22 | + |
| 23 | + $coverage = (int)(($totalElements === 0) ? 0 : ($checkedElements / $totalElements) * 100); |
| 24 | + |
| 25 | + $template = file_get_contents(__DIR__ . '/templates/badge.svg'); |
| 26 | + |
| 27 | + $template = str_replace('{{ total }}', $coverage, $template); |
| 28 | + |
| 29 | + $template = str_replace('{{ coverage }}', $coverageName, $template); |
| 30 | + |
| 31 | + $color = '#a4a61d'; // Default Gray |
| 32 | + if ($coverage < 40) { |
| 33 | + $color = '#e05d44'; // Red |
| 34 | + } elseif($coverage < 60) { |
| 35 | + $color = '#fe7d37'; // Orange |
| 36 | + } elseif($coverage < 75) { |
| 37 | + $color = '#dfb317'; // Yellow |
| 38 | + } elseif($coverage < 90) { |
| 39 | + $color = '#a4a61d'; // Yellow-Green |
| 40 | + } elseif($coverage < 95) { |
| 41 | + $color = '#97CA00'; // Green |
| 42 | + } elseif ($coverage <= 100) { |
| 43 | + $color = '#4c1'; // Bright Green |
| 44 | + } |
| 45 | + |
| 46 | + $template = str_replace('{{ total }}', $coverage, $template); |
| 47 | + $template = str_replace('{{ color }}', $color, $template); |
| 48 | + |
| 49 | + file_put_contents($outputFile, $template); |
| 50 | +} catch (Exception $e) { |
| 51 | + echo $e->getMessage(); |
| 52 | +} |
| 53 | + |
| 54 | + |
0 commit comments