Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

README badges: downloads, stars and version from the site, not shields.io's GitHub lookups

Mason363db2606409a63
diff --git a/.github/workflows/badges.yml b/.github/workflows/badges.yml
new file mode 100644
index 00000000..afdf9e50
--- /dev/null
+++ b/.github/workflows/badges.yml
@@ -0,0 +1,23 @@
+name: Badges
+
+# The README's downloads, stars and version badges read docs/badges/*.json, which GitHub Pages makes
+# from the repository's own data each time it builds the site (shields.io then never asks GitHub
+# itself). This asks for a build every six hours, so the numbers stay fresh between pushes; Build asks
+# for one after each release.
+
+on:
+ schedule:
+ - cron: "17 */6 * * *"
+ workflow_dispatch:
+
+permissions:
+ pages: write
+
+jobs:
+ pages:
+ runs-on: ubuntu-24.04
+ steps:
+ - name: Build the site again
+ env:
+ GH_TOKEN: ${{ github.token }}
+ run: gh api -X POST "repos/$GITHUB_REPOSITORY/pages/builds"
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 6214682b..0b61d454 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -12,6 +12,7 @@ on:
permissions:
contents: write
+ pages: write
jobs:
build:
@@ -78,3 +79,5 @@ jobs:
mapfile -t files < build/release-files.txt
gh release create "$tag" "${files[@]}" --target "$GITHUB_SHA" \
--title "NumPlay $tag" --notes-file build/release-notes.md
+ # the README's version and downloads badges come from the site (docs/badges): build it again
+ gh api -X POST "repos/$GITHUB_REPOSITORY/pages/builds" > /dev/null || echo "(the site was not built again)"
diff --git a/README.md b/README.md
index 8533eecc..1ca2d8c3 100644
--- a/README.md
+++ b/README.md
@@ -10,9 +10,9 @@
</p>
<p align="center">
- <a href="https://github.com/Mason363/NumPlay/releases"><img src="https://img.shields.io/github/downloads/Mason363/NumPlay/total?style=for-the-badge&label=downloads&color=1E7F74" alt="Total downloads"></a>
- <a href="https://github.com/Mason363/NumPlay/stargazers"><img src="https://img.shields.io/github/stars/Mason363/NumPlay?style=for-the-badge&color=E3B341" alt="GitHub stars"></a>
- <a href="https://github.com/Mason363/NumPlay/releases/latest"><img src="https://img.shields.io/github/v/release/Mason363/NumPlay?style=for-the-badge&label=version&color=2D9F4F" alt="Latest version"></a>
+ <a href="https://github.com/Mason363/NumPlay/releases"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fmason363.github.io%2FNumPlay%2Fdocs%2Fbadges%2Fdownloads.json&style=for-the-badge" alt="Total downloads"></a>
+ <a href="https://github.com/Mason363/NumPlay/stargazers"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fmason363.github.io%2FNumPlay%2Fdocs%2Fbadges%2Fstars.json&style=for-the-badge" alt="GitHub stars"></a>
+ <a href="https://github.com/Mason363/NumPlay/releases/latest"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fmason363.github.io%2FNumPlay%2Fdocs%2Fbadges%2Fversion.json&style=for-the-badge" alt="Latest version"></a>
</p>
<p align="center">
diff --git a/docs/badges/downloads.json b/docs/badges/downloads.json
new file mode 100644
index 00000000..13d98717
--- /dev/null
+++ b/docs/badges/downloads.json
@@ -0,0 +1,15 @@
+---
+layout: null
+---
+{%- comment -%}
+The README's downloads badge (a shields.io endpoint badge): every file of every release, counted when
+GitHub Pages builds the site, as shields.io writes big numbers (8.1k, 22k).
+{%- endcomment -%}
+{%- assign n = 0 -%}
+{%- for r in site.github.releases -%}{%- for a in r.assets -%}{%- assign n = n | plus: a.download_count -%}{%- endfor -%}{%- endfor -%}
+{%- if n >= 1000000 -%}{%- assign t = n | divided_by: 100000 -%}{%- assign tenth = t | modulo: 10 -%}{%- assign msg = t | divided_by: 10 | append: "." | append: tenth | append: "M" -%}
+{%- elsif n >= 10000 -%}{%- assign msg = n | divided_by: 1000 | append: "k" -%}
+{%- elsif n >= 1000 -%}{%- assign t = n | divided_by: 100 -%}{%- assign tenth = t | modulo: 10 -%}{%- assign msg = t | divided_by: 10 | append: "." | append: tenth | append: "k" -%}
+{%- else -%}{%- assign msg = n -%}
+{%- endif -%}
+{"schemaVersion": 1, "label": "downloads", "message": "{{ msg }}", "color": "1E7F74"}
diff --git a/docs/badges/stars.json b/docs/badges/stars.json
new file mode 100644
index 00000000..4f26fc50
--- /dev/null
+++ b/docs/badges/stars.json
@@ -0,0 +1,10 @@
+---
+layout: null
+---
+{%- comment -%}
+The README's stars badge (a shields.io endpoint badge): this repository's stars, read when GitHub Pages
+builds the site.
+{%- endcomment -%}
+{%- assign s = 0 -%}
+{%- for r in site.github.public_repositories -%}{%- if r.name == site.github.repository_name -%}{%- assign s = r.stargazers_count -%}{%- endif -%}{%- endfor -%}
+{"schemaVersion": 1, "label": "stars", "message": "{{ s }}", "color": "E3B341"}
diff --git a/docs/badges/version.json b/docs/badges/version.json
new file mode 100644
index 00000000..4a92cfe5
--- /dev/null
+++ b/docs/badges/version.json
@@ -0,0 +1,8 @@
+---
+layout: null
+---
+{%- comment -%}
+The README's version badge (a shields.io endpoint badge): the latest release, read when GitHub Pages
+builds the site (Build asks for a build after each release).
+{%- endcomment -%}
+{"schemaVersion": 1, "label": "version", "message": "{{ site.github.latest_release.tag_name }}", "color": "2D9F4F"}