|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import sys |
| 5 | +import xml.etree.ElementTree as ET |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def pick_color(percent: int) -> str: |
| 10 | + if percent >= 90: |
| 11 | + return "#4c1" # brightgreen |
| 12 | + if percent >= 80: |
| 13 | + return "#97CA00" # green |
| 14 | + if percent >= 70: |
| 15 | + return "#a4a61d" # yellowgreen |
| 16 | + if percent >= 60: |
| 17 | + return "#dfb317" # yellow |
| 18 | + if percent >= 50: |
| 19 | + return "#fe7d37" # orange |
| 20 | + return "#e05d44" # red |
| 21 | + |
| 22 | + |
| 23 | +def text_width(text: str) -> int: |
| 24 | + # Approximate width for 11px sans-serif shields-style text. |
| 25 | + return max(20, len(text) * 7 + 10) |
| 26 | + |
| 27 | + |
| 28 | +def make_badge(label: str, value: str, color: str) -> str: |
| 29 | + left = text_width(label) |
| 30 | + right = text_width(value) |
| 31 | + total = left + right |
| 32 | + left_center = left / 2 |
| 33 | + right_center = left + right / 2 |
| 34 | + |
| 35 | + return f"""<svg xmlns="http://www.w3.org/2000/svg" width="{total}" height="20" role="img" aria-label="{label}: {value}"> |
| 36 | + <title>{label}: {value}</title> |
| 37 | + <linearGradient id="s" x2="0" y2="100%"> |
| 38 | + <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> |
| 39 | + <stop offset="1" stop-opacity=".1"/> |
| 40 | + </linearGradient> |
| 41 | + <mask id="m"> |
| 42 | + <rect width="{total}" height="20" rx="3" fill="#fff"/> |
| 43 | + </mask> |
| 44 | + <g mask="url(#m)"> |
| 45 | + <rect width="{left}" height="20" fill="#555"/> |
| 46 | + <rect x="{left}" width="{right}" height="20" fill="{color}"/> |
| 47 | + <rect width="{total}" height="20" fill="url(#s)"/> |
| 48 | + </g> |
| 49 | + <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" text-rendering="geometricPrecision" font-size="11"> |
| 50 | + <text x="{left_center}" y="15" fill="#010101" fill-opacity=".3">{label}</text> |
| 51 | + <text x="{left_center}" y="14">{label}</text> |
| 52 | + <text x="{right_center}" y="15" fill="#010101" fill-opacity=".3">{value}</text> |
| 53 | + <text x="{right_center}" y="14">{value}</text> |
| 54 | + </g> |
| 55 | +</svg> |
| 56 | +""" |
| 57 | + |
| 58 | + |
| 59 | +def main() -> int: |
| 60 | + if len(sys.argv) != 3: |
| 61 | + print("Usage: generate_coverage_badge.py <coverage.xml> <output.svg>") |
| 62 | + return 2 |
| 63 | + |
| 64 | + coverage_xml = Path(sys.argv[1]) |
| 65 | + output_svg = Path(sys.argv[2]) |
| 66 | + |
| 67 | + tree = ET.parse(coverage_xml) |
| 68 | + root = tree.getroot() |
| 69 | + line_rate = root.attrib.get("line-rate") |
| 70 | + if line_rate is None: |
| 71 | + raise ValueError("coverage.xml missing line-rate attribute") |
| 72 | + |
| 73 | + percent = round(float(line_rate) * 100) |
| 74 | + badge = make_badge("coverage", f"{percent}%", pick_color(percent)) |
| 75 | + output_svg.write_text(badge, encoding="utf-8") |
| 76 | + print(f"Wrote badge: {output_svg} ({percent}%)") |
| 77 | + return 0 |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + raise SystemExit(main()) |
0 commit comments