|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +$cloverPath = $argv[1] ?? 'clover.xml'; |
| 6 | +$outputPath = $argv[2] ?? 'coverage.svg'; |
| 7 | + |
| 8 | +if (!is_file($cloverPath)) { |
| 9 | + fwrite(\STDERR, "Clover report not found: {$cloverPath}\n"); |
| 10 | + exit(1); |
| 11 | +} |
| 12 | + |
| 13 | +libxml_use_internal_errors(true); |
| 14 | +$xml = simplexml_load_file($cloverPath); |
| 15 | +if ($xml === false) { |
| 16 | + fwrite(\STDERR, "Failed to parse clover XML: {$cloverPath}\n"); |
| 17 | + foreach (libxml_get_errors() as $error) { |
| 18 | + fwrite(\STDERR, trim($error->message) . "\n"); |
| 19 | + } |
| 20 | + exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +$metrics = $xml->project->metrics; |
| 24 | +if ($metrics === null) { |
| 25 | + fwrite(\STDERR, "Clover XML is missing /coverage/project/metrics\n"); |
| 26 | + exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +$statements = (int) ($metrics['statements'] ?? 0); |
| 30 | +$coveredStatements = (int) ($metrics['coveredstatements'] ?? 0); |
| 31 | + |
| 32 | +$percent = 0; |
| 33 | +if ($statements > 0) { |
| 34 | + $percent = (int) round(($coveredStatements / $statements) * 100); |
| 35 | +} |
| 36 | + |
| 37 | +$percent = max(0, min(100, $percent)); |
| 38 | + |
| 39 | +// Match the typical shields.io color thresholds. |
| 40 | +$color = '#e05d44'; // red |
| 41 | +if ($percent >= 90) { |
| 42 | + $color = '#4c1'; // green |
| 43 | +} elseif ($percent >= 50) { |
| 44 | + $color = '#dfb317'; // yellow |
| 45 | +} |
| 46 | + |
| 47 | +$percentText = $percent . ' %'; |
| 48 | + |
| 49 | +$svg = <<<SVG |
| 50 | +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="108" height="20" role="img"> |
| 51 | + <linearGradient id="s" x2="0" y2="100%"> |
| 52 | + <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> |
| 53 | + <stop offset="1" stop-opacity=".1"/> |
| 54 | + </linearGradient> |
| 55 | + <clipPath id="r"> |
| 56 | + <rect width="108" height="20" rx="3" fill="#fff"/> |
| 57 | + </clipPath> |
| 58 | + <g clip-path="url(#r)"> |
| 59 | + <rect width="63" height="20" fill="#555"/> |
| 60 | + <rect x="63" width="45" height="20" fill="{$color}"/> |
| 61 | + <rect width="108" height="20" fill="url(#s)"/> |
| 62 | + </g> |
| 63 | + <g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"> |
| 64 | + <text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="530">coverage</text> |
| 65 | + <text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="530">coverage</text> |
| 66 | + <text aria-hidden="true" x="850" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="350">{$percentText}</text> |
| 67 | + <text x="850" y="140" transform="scale(.1)" fill="#fff" textLength="350">{$percentText}</text> |
| 68 | + </g> |
| 69 | +</svg> |
| 70 | +SVG; |
| 71 | + |
| 72 | +if (file_put_contents($outputPath, $svg) === false) { |
| 73 | + fwrite(\STDERR, "Failed to write badge: {$outputPath}\n"); |
| 74 | + exit(1); |
| 75 | +} |
0 commit comments