Repository navigation
122 lines (105 loc) · 8.83 KB
/
Copy pathupdate-badges.yml
File metadata and controls
122 lines (105 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
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
110
111
112
113
114
115
116
117
118
119
120
121
122
name: Update README Badges
on:
schedule:
- cron: '0 0 * * *' # Midnight UTC
workflow_dispatch:
# the release build publishes the release, so the pre-release button is never a day behind
workflow_run:
workflows: ["Build Release APKs"]
types: [completed]
permissions:
contents: write
jobs:
update-badges:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Fetch GitHub stats
id: stats
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
REPO="LeanBitLab/LeanType"
# Latest version
VERSION=$(gh api repos/$REPO/releases/latest --jq '.tag_name' | sed 's/^v//' 2>/dev/null || echo "N/A")
echo "version=$VERSION" >> $GITHUB_OUTPUT
# Latest pre-release, shown on the Get Pre-release button. Only while it is newer than the latest
# stable release, otherwise the button has no version. The version comes from the APK name
# (LeanType_4.3.3-...) and the beta number from the tag (beta-433-3).
PRE_VERSION=""
PRE=$(gh api "repos/$REPO/releases?per_page=30" --jq '[.[] | select(.prerelease and (.draft | not))] | first // empty' 2>/dev/null || echo "")
if [ -n "$PRE" ]; then
PRE_TAG=$(printf '%s' "$PRE" | jq -r '.tag_name')
PRE_DATE=$(printf '%s' "$PRE" | jq -r '.published_at')
STABLE_DATE=$(gh api repos/$REPO/releases/latest --jq '.published_at' 2>/dev/null || echo "")
if [ -z "$STABLE_DATE" ] || [[ "$PRE_DATE" > "$STABLE_DATE" ]]; then
BASE=$(printf '%s' "$PRE" | jq -r '[.assets[].name | capture("LeanType_(?<v>[0-9]+(\\.[0-9]+)*)-") | .v] | first // empty')
if [ -n "$BASE" ]; then PRE_VERSION="v${BASE}-beta${PRE_TAG##*-}"; else PRE_VERSION="$PRE_TAG"; fi
fi
fi
echo "pre_version=$PRE_VERSION" >> $GITHUB_OUTPUT
# Total downloads
DOWNLOADS=$(gh api repos/$REPO/releases --paginate --jq '.[] | .assets[]?.download_count' 2>/dev/null | jq -s 'add // 0')
echo "downloads=$DOWNLOADS" >> $GITHUB_OUTPUT
# Stars
STARS=$(gh api repos/$REPO --jq '.stargazers_count' 2>/dev/null || echo "0")
echo "stars=$STARS" >> $GITHUB_OUTPUT
- name: Generate badge SVGs
env:
VERSION: ${{ steps.stats.outputs.version }}
PRE_VERSION: ${{ steps.stats.outputs.pre_version }}
DOWNLOADS: ${{ steps.stats.outputs.downloads }}
STARS: ${{ steps.stats.outputs.stars }}
run: |
mkdir -p docs/badges
# Format numbers with commas
DOWNLOADS_FMT=$(printf "%'d" "$DOWNLOADS" 2>/dev/null || echo "$DOWNLOADS")
STARS_FMT=$(printf "%'d" "$STARS" 2>/dev/null || echo "$STARS")
# Download version badge
cat > docs/badges/download.svg << EOF
<svg xmlns="http://www.w3.org/2000/svg" width="94" height="28" viewBox="0 0 94 28"><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="94" height="28" rx="4" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="50" height="28" fill="#555"/><rect x="50" width="44" height="28" fill="#7C4DFF"/><rect width="94" height="28" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11"><text x="25" y="19" fill="#010101" fill-opacity=".3">Version</text><text x="25" y="18">Version</text><text x="72" y="19" fill="#010101" fill-opacity=".3">v${VERSION}</text><text x="72" y="18">v${VERSION}</text></g></svg>
EOF
# Get Pre-release button: the same button as before, with the current pre-release version under the title
if [ -n "$PRE_VERSION" ]; then
BUTTON_TEXT='<text x="60" y="30" font-family="Arial, Helvetica, sans-serif" font-size="17" font-weight="600" fill="#ffffff">Get Pre-release</text><text x="60" y="48" font-family="Arial, Helvetica, sans-serif" font-size="13" fill="#b3b3b3">'"$PRE_VERSION"'</text>'
else
BUTTON_TEXT='<text x="60" y="37" font-family="Arial, Helvetica, sans-serif" font-size="18" font-weight="600" fill="#ffffff">Get Pre-release</text>'
fi
cat > docs/badges/get-pre-release.svg << EOF
<svg xmlns="http://www.w3.org/2000/svg" width="240" height="60" viewBox="0 0 240 60"><rect x="1" y="1" width="238" height="58" rx="10" fill="#000000" stroke="#ffffff" stroke-width="2"/><g transform="translate(20,14) scale(1.3333)"><path fill="#ffffff" d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></g>${BUTTON_TEXT}</svg>
EOF
# Downloads count badge, in the flat-square style and green of the other README badges. Generated here
# because shields.io only sums the newest 100 releases, so its total dropped with every new release.
# Widths follow shields: about 7px per digit or letter at Verdana 11px, 4px for a dot, 10px padding.
# Rounded like shields (1.5k, 116k, 1.2M).
DOWNLOADS_SHORT=$(awk -v n="$DOWNLOADS" 'BEGIN {
if (n >= 1000000) { v = n / 1000000; printf (v < 10 ? "%.1fM" : "%.0fM"), v }
else if (n >= 1000) { v = n / 1000; printf (v < 10 ? "%.1fk" : "%.0fk"), v }
else printf "%d", n }' | sed 's/\.0\([kM]\)/\1/')
LABEL_W=71
VALUE_W=$(( $(printf '%s' "$DOWNLOADS_SHORT" | tr -d '.' | wc -c) * 7 + $(printf '%s' "$DOWNLOADS_SHORT" | tr -cd '.' | wc -c) * 4 + 10 ))
TOTAL_W=$(( LABEL_W + VALUE_W ))
LABEL_X=$(( LABEL_W * 10 / 2 ))
VALUE_X=$(( (LABEL_W * 2 + VALUE_W) * 10 / 2 ))
LABEL_TL=$(( (LABEL_W - 10) * 10 ))
VALUE_TL=$(( (VALUE_W - 10) * 10 ))
cat > docs/badges/downloads.svg << EOF
<svg xmlns="http://www.w3.org/2000/svg" width="${TOTAL_W}" height="20" role="img" aria-label="Downloads: ${DOWNLOADS_SHORT}"><title>Downloads: ${DOWNLOADS_SHORT}</title><g shape-rendering="crispEdges"><rect width="${LABEL_W}" height="20" fill="#555"/><rect x="${LABEL_W}" width="${VALUE_W}" height="20" fill="#059669"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text transform="scale(.1)" x="${LABEL_X}" y="140" textLength="${LABEL_TL}">Downloads</text><text transform="scale(.1)" x="${VALUE_X}" y="140" textLength="${VALUE_TL}">${DOWNLOADS_SHORT}</text></g></svg>
EOF
# Stars badge
cat > docs/badges/stars.svg << EOF
<svg xmlns="http://www.w3.org/2000/svg" width="72" height="28" viewBox="0 0 72 28"><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="72" height="28" rx="4" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="38" height="28" fill="#555"/><rect x="38" width="34" height="28" fill="#7C4DFF"/><rect width="72" height="28" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11"><text x="19" y="19" fill="#010101" fill-opacity=".3">Stars</text><text x="19" y="18">Stars</text><text x="55" y="19" fill="#010101" fill-opacity=".3">${STARS_FMT}</text><text x="55" y="18">${STARS_FMT}</text></g></svg>
EOF
echo "Generated: v$VERSION | $DOWNLOADS_FMT downloads | $STARS_FMT stars"
- name: Update README badge URLs
run: |
# Replace shields.io URLs with local badge paths
sed -i 's|https://img.shields.io/github/v/release/LeanBitLab/LeanType?label=Download\&style=for-the-badge\&color=7C4DFF|docs/badges/download.svg|g' README.md
sed -i 's|https://img.shields.io/github/stars/LeanBitLab/LeanType?style=for-the-badge\&color=7C4DFF|docs/badges/stars.svg|g' README.md
- name: Commit changes
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add docs/badges/ README.md
git diff --staged --quiet || git commit -m "chore: update README badges [skip ci]"
git push