From ad3716c78ad22760cd91b3a2415346681d2dfbc5 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sun, 4 Oct 2026 01:33:47 +0200 Subject: [PATCH] refactor(ci): centralize badge rendering --- scripts/make-badge.sh | 270 +++++++++++++++++++++++++++++++ scripts/render-coverage-badge.sh | 63 +------- scripts/render-test-badge.sh | 45 +----- scripts/render-version-badge.sh | 52 +----- 4 files changed, 288 insertions(+), 142 deletions(-) create mode 100755 scripts/make-badge.sh diff --git a/scripts/make-badge.sh b/scripts/make-badge.sh new file mode 100755 index 0000000..c76b094 --- /dev/null +++ b/scripts/make-badge.sh @@ -0,0 +1,270 @@ +#!/bin/sh +set -eu + +usage() { + cat >&2 <<'EOF' +usage: $0 [options] + +Render one deterministic SVG badge. Defaults: + version --text version --value unknown --color #007ec6 + test --text tests --value passing --color #4c1 + coverage --text coverage --value - --color #9f9f9f --suffix % + +Options: + --text TEXT left-hand label + --value VALUE right-hand value + --color COLOR value background (#RGB or #RRGGBB) + --size PX font size, from 6 through 24 (default: 11) + --height PX badge height, from 14 through 64 (default: 20) + --label-width PX fixed label width instead of automatic sizing + --value-width PX fixed value width instead of automatic sizing + --suffix TEXT text appended to the displayed value + --output PATH output file (default: coverage/.svg) + -h, --help show this help + +Examples: + ./ci-coverage-badges.sh version --value 1.2.3 + ./ci-coverage-badges.sh test --value failing + ./ci-coverage-badges.sh coverage --value 87.4 + ./ci-coverage-badges.sh version --text release --value rc.12 --size 12 --color '#8a2be2' +EOF + exit "${1:-2}" +} + +[ "$#" -ge 1 ] || usage +kind=$1 +shift + +case "$kind" in + version) + text=version + value=unknown + color='#007ec6' + suffix= + output=coverage/version.svg + ;; + test) + text=tests + value=passing + color= + suffix= + output=coverage/test.svg + ;; + coverage) + text=coverage + value=- + color= + suffix=% + output=coverage/coverage.svg + ;; + -h|--help) usage 0 ;; + *) + echo "unsupported badge kind: $kind" >&2 + usage + ;; +esac + +font_size=11 +height=20 +label_width= +value_width= +color_set=false + +while [ "$#" -gt 0 ]; do + option=$1 + case "$option" in + --text|--value|--color|--size|--height|--label-width|--value-width|--suffix|--output) + [ "$#" -ge 2 ] || { + echo "missing value for $option" >&2 + exit 2 + } + option_value=$2 + shift 2 + case "$option" in + --text) text=$option_value ;; + --value) value=$option_value ;; + --color) color=$option_value; color_set=true ;; + --size) font_size=$option_value ;; + --height) height=$option_value ;; + --label-width) label_width=$option_value ;; + --value-width) value_width=$option_value ;; + --suffix) suffix=$option_value ;; + --output) output=$option_value ;; + esac + ;; + -h|--help) usage 0 ;; + *) + echo "unknown option: $option" >&2 + usage + ;; + esac +done + +validate_text() { + field_name=$1 + field_value=$2 + if ! printf '%sX\n' "$field_value" | LC_ALL=C awk ' + BEGIN { valid = 1 } + NR != 1 || $0 !~ /^[ -~]+X$/ || length($0) <= 1 { valid = 0 } + END { exit(valid == 0 ? 1 : 0) } + '; then + echo "$field_name must contain printable ASCII characters: $field_value" >&2 + exit 2 + fi +} + +validate_integer() { + field_name=$1 + field_value=$2 + minimum=$3 + maximum=$4 + case "$field_value" in + ''|*[!0-9]*) + echo "$field_name must be an integer from $minimum through $maximum: $field_value" >&2 + exit 2 + ;; + esac + if [ "$field_value" -lt "$minimum" ] || [ "$field_value" -gt "$maximum" ]; then + echo "$field_name must be an integer from $minimum through $maximum: $field_value" >&2 + exit 2 + fi +} + +validate_text text "$text" +validate_text value "$value" +if [ -n "$suffix" ]; then + validate_text suffix "$suffix" +fi +validate_integer size "$font_size" 6 24 +validate_integer height "$height" 14 64 +if [ -n "$label_width" ]; then + validate_integer label-width "$label_width" 20 1000 +fi +if [ -n "$value_width" ]; then + validate_integer value-width "$value_width" 20 1000 +fi +case "$output" in + ''|*/) echo "output must name an SVG file: $output" >&2; exit 2 ;; +esac + +case "$kind" in + test) + if [ "$color_set" = false ]; then + case "$value" in + passing) color='#4c1' ;; + failing) color='#e05d44' ;; + *) color='#9f9f9f' ;; + esac + fi + ;; + coverage) + if [ "$value" = '-' ]; then + [ "$color_set" = true ] || color='#9f9f9f' + else + if ! awk -v candidate="$value" 'BEGIN { + valid = candidate ~ /^[0-9]+([.][0-9]+)?$/ && candidate >= 0 && candidate <= 100 + exit(valid ? 0 : 1) + }'; then + echo "coverage value must be - or a number from 0 through 100: $value" >&2 + exit 2 + fi + value=$(awk -v candidate="$value" 'BEGIN { printf "%.0f", candidate }') + if [ "$color_set" = false ]; then + if [ "$value" -ge 90 ]; then + color='#4c1' + elif [ "$value" -ge 70 ]; then + color='#dfb317' + else + color='#e05d44' + fi + fi + fi + ;; +esac + +case "$color" in + \#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]|\#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]) ;; + *) echo "color must be #RGB or #RRGGBB: $color" >&2; exit 2 ;; +esac + +display_value=$value$suffix +if [ -z "$label_width" ]; then + label_width=$(printf '%s\\n' "$text" | awk -v size="$font_size" '{ + width = int(length($0) * size * 0.64 + 14) + print (width < 35 ? 35 : width) + }') +fi +if [ -z "$value_width" ]; then + value_width=$(printf '%s\\n' "$display_value" | awk -v size="$font_size" '{ + width = int(length($0) * size * 0.64 + 14) + print (width < 35 ? 35 : width) + }') +fi + +badge_width=$((label_width + value_width)) +label_center=$(awk -v width="$label_width" 'BEGIN { printf "%.1f", width / 2 }') +value_center=$(awk -v left="$label_width" -v width="$value_width" 'BEGIN { + printf "%.1f", left + width / 2 +}') +vertical_center=$(awk -v badge_height="$height" 'BEGIN { printf "%.1f", badge_height / 2 }') + +xml_escape() { + sed \ + -e 's/&/\&/g' \ + -e 's//\>/g' \ + -e 's/"/\"/g' +} + +sed_replacement_escape() { + sed -e 's/[\\&|]/\\&/g' +} + +escaped_text=$(printf '%s' "$text" | xml_escape | sed_replacement_escape) +escaped_value=$(printf '%s' "$display_value" | xml_escape | sed_replacement_escape) +escaped_title=$(printf '%s: %s' "$text" "$display_value" | xml_escape | sed_replacement_escape) + +output_dir=${output%/*} +if [ "$output_dir" != "$output" ]; then + mkdir -p "$output_dir" +fi +temporary="${output}.tmp.$$" +trap 'rm -f "$temporary"' EXIT HUP INT TERM + +sed \ + -e "s/@BADGE_WIDTH@/$badge_width/g" \ + -e "s/@HEIGHT@/$height/g" \ + -e "s/@LABEL_WIDTH@/$label_width/g" \ + -e "s/@VALUE_WIDTH@/$value_width/g" \ + -e "s/@LABEL_CENTER@/$label_center/g" \ + -e "s/@VALUE_CENTER@/$value_center/g" \ + -e "s/@VERTICAL_CENTER@/$vertical_center/g" \ + -e "s/@FONT_SIZE@/$font_size/g" \ + -e "s/@COLOR@/$color/g" \ + -e "s|@TITLE@|$escaped_title|g" \ + -e "s|@TEXT@|$escaped_text|g" \ + -e "s|@VALUE@|$escaped_value|g" \ + >"$temporary" <<'EOF' + + @TITLE@ + + + + + + + + + + + + + + @TEXT@ + @VALUE@ + + +EOF + +mv "$temporary" "$output" +trap - EXIT HUP INT TERM diff --git a/scripts/render-coverage-badge.sh b/scripts/render-coverage-badge.sh index 1927940..3e1ea29 100755 --- a/scripts/render-coverage-badge.sh +++ b/scripts/render-coverage-badge.sh @@ -10,60 +10,9 @@ usage() { percent=$1 output=$2 -if [ "$percent" = "-" ]; then - rounded=- - color='#9f9f9f' -else - if ! awk -v value="$percent" 'BEGIN { - valid = value ~ /^[0-9]+([.][0-9]+)?$/ && value >= 0 && value <= 100 - exit(valid ? 0 : 1) - }'; then - echo "coverage percentage must be - or a number from 0 through 100: $percent" >&2 - exit 2 - fi - - rounded=$(awk -v value="$percent" 'BEGIN { printf "%.0f", value }') - if [ "$rounded" -ge 90 ]; then - color='#4c1' - elif [ "$rounded" -ge 70 ]; then - color='#dfb317' - else - color='#e05d44' - fi -fi - -output_dir=${output%/*} -if [ "$output_dir" != "$output" ]; then - mkdir -p "$output_dir" -fi - -temporary="${output}.tmp.$$" -trap 'rm -f "$temporary"' EXIT HUP INT TERM - -sed \ - -e "s/@PERCENT@/${rounded}/g" \ - -e "s/@COLOR@/${color}/g" \ - >"$temporary" <<'EOF' - - coverage: @PERCENT@% - - - - - - - - - - - - - - coverage - @PERCENT@% - - -EOF - -mv "$temporary" "$output" -trap - EXIT HUP INT TERM +script_dir=$(CDPATH= cd -P "$(dirname "$0")" && pwd) +exec "$script_dir/make-badge.sh" coverage \ + --value "$percent" \ + --label-width 75 \ + --value-width 37 \ + --output "$output" diff --git a/scripts/render-test-badge.sh b/scripts/render-test-badge.sh index 997cc8a..6e8159c 100755 --- a/scripts/render-test-badge.sh +++ b/scripts/render-test-badge.sh @@ -9,48 +9,17 @@ usage() { [ "$#" -eq 2 ] || usage status=$1 output=$2 - case "$status" in - passing) color='#4c1' ;; - failing) color='#e05d44' ;; + passing|failing) ;; *) echo "test badge status must be passing or failing: $status" >&2 exit 2 ;; esac -output_dir=${output%/*} -if [ "$output_dir" != "$output" ]; then - mkdir -p "$output_dir" -fi - -temporary="${output}.tmp.$$" -trap 'rm -f "$temporary"' EXIT HUP INT TERM - -sed \ - -e "s/@STATUS@/${status}/g" \ - -e "s/@COLOR@/${color}/g" \ - >"$temporary" <<'EOF' - - tests: @STATUS@ - - - - - - - - - - - - - - tests - @STATUS@ - - -EOF - -mv "$temporary" "$output" -trap - EXIT HUP INT TERM +script_dir=$(CDPATH= cd -P "$(dirname "$0")" && pwd) +exec "$script_dir/make-badge.sh" test \ + --value "$status" \ + --label-width 45 \ + --value-width 55 \ + --output "$output" diff --git a/scripts/render-version-badge.sh b/scripts/render-version-badge.sh index fcb868e..ce08345 100755 --- a/scripts/render-version-badge.sh +++ b/scripts/render-version-badge.sh @@ -9,7 +9,6 @@ usage() { [ "$#" -eq 2 ] || usage version=$1 output=$2 - case "$version" in ''|*[!0-9A-Za-z.+-]*) echo "package version contains unsupported badge characters: $version" >&2 @@ -17,49 +16,8 @@ case "$version" in ;; esac -value_width=$(awk -v value="$version" 'BEGIN { - width = length(value) * 7 + 10 - print (width < 45 ? 45 : width) -}') -badge_width=$((59 + value_width)) -value_center=$(awk -v width="$value_width" 'BEGIN { - printf "%.1f", 59 + width / 2 -}') - -output_dir=${output%/*} -if [ "$output_dir" != "$output" ]; then - mkdir -p "$output_dir" -fi - -temporary="${output}.tmp.$$" -trap 'rm -f "$temporary"' EXIT HUP INT TERM - -sed \ - -e "s/@VERSION@/${version}/g" \ - -e "s/@BADGE_WIDTH@/${badge_width}/g" \ - -e "s/@VALUE_WIDTH@/${value_width}/g" \ - -e "s/@VALUE_CENTER@/${value_center}/g" \ - >"$temporary" <<'EOF' - - version: @VERSION@ - - - - - - - - - - - - - - version - @VERSION@ - - -EOF - -mv "$temporary" "$output" -trap - EXIT HUP INT TERM +script_dir=$(CDPATH= cd -P "$(dirname "$0")" && pwd) +exec "$script_dir/make-badge.sh" version \ + --value "$version" \ + --label-width 59 \ + --output "$output"