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.

Site: deployed by the Pages workflow, with the badges' numbers made there

Mason36338140d03e419
diff --git a/.github/workflows/badges.yml b/.github/workflows/badges.yml
deleted file mode 100644
index a16ead69..00000000
--- a/.github/workflows/badges.yml
+++ /dev/null
@@ -1,23 +0,0 @@
-name: Badges
-
-# The README's downloads 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 0b61d454..89966b26 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -12,7 +12,7 @@ on:
permissions:
contents: write
- pages: write
+ actions: write
jobs:
build:
@@ -79,5 +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)"
+ # the README's version and downloads badges come from the site (docs/badges): deploy it again
+ gh workflow run pages.yml --ref main || echo "(the site was not deployed again)"
diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml
new file mode 100644
index 00000000..2dba35f1
--- /dev/null
+++ b/.github/workflows/pages.yml
@@ -0,0 +1,53 @@
+name: Pages
+
+# Builds the site (mason363.github.io/NumPlay: the README and docs, with Jekyll, as GitHub Pages
+# did on its own) and deploys it, with the README's downloads, stars and version badges made from
+# GitHub's numbers (tools/badges.py). On each push to main, every six hours so the numbers stay
+# fresh, and after each release (Build starts it).
+
+on:
+ push:
+ branches: [main]
+ schedule:
+ - cron: "17 */6 * * *"
+ workflow_dispatch:
+
+permissions:
+ contents: read
+ pages: write
+ id-token: write
+
+concurrency:
+ group: pages
+ cancel-in-progress: false
+
+jobs:
+ build:
+ runs-on: ubuntu-24.04
+ steps:
+ - uses: actions/checkout@v4
+ - name: Badges
+ env:
+ GH_TOKEN: ${{ github.token }}
+ run: |
+ stars=$(gh api "repos/$GITHUB_REPOSITORY" --jq .stargazers_count)
+ downloads=$(gh api --paginate "repos/$GITHUB_REPOSITORY/releases?per_page=100" --jq '.[].assets[].download_count' | awk '{s += $1} END {print s + 0}')
+ version=$(gh api "repos/$GITHUB_REPOSITORY/releases/latest" --jq .tag_name)
+ python3 tools/badges.py --downloads "$downloads" --stars "$stars" --version "$version" --out docs/badges
+ cat docs/badges/*.json
+ - uses: actions/configure-pages@v5
+ - uses: actions/jekyll-build-pages@v1
+ with:
+ source: ./
+ destination: ./_site
+ - uses: actions/upload-pages-artifact@v3
+
+ deploy:
+ needs: build
+ runs-on: ubuntu-24.04
+ environment:
+ name: github-pages
+ url: ${{ steps.deployment.outputs.page_url }}
+ steps:
+ - id: deployment
+ uses: actions/deploy-pages@v4
diff --git a/.gitignore b/.gitignore
index a643c30b..8f0288ff 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,5 @@ __pycache__/
.DS_Store
games/tetris/*/target/
games/tetris/tetris/src/data/image.nppm
+# made by the Pages workflow (tools/badges.py)
+docs/badges/
diff --git a/README.md b/README.md
index 5b7dc9a8..1ca2d8c3 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@
<p align="center">
<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/github/stars/Mason363/NumPlay?style=for-the-badge&color=E3B341" alt="GitHub stars"></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>
diff --git a/docs/badges/downloads.json b/docs/badges/downloads.json
deleted file mode 100644
index 13d98717..00000000
--- a/docs/badges/downloads.json
+++ /dev/null
@@ -1,15 +0,0 @@
----
-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/version.json b/docs/badges/version.json
deleted file mode 100644
index 4a92cfe5..00000000
--- a/docs/badges/version.json
+++ /dev/null
@@ -1,8 +0,0 @@
----
-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"}
diff --git a/tools/badges.py b/tools/badges.py
new file mode 100644
index 00000000..10aca44a
--- /dev/null
+++ b/tools/badges.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+"""The README's downloads, stars and version badges, as shields.io endpoint files.
+
+shields.io's own GitHub badges ask GitHub through a pool of tokens that runs dry ("invalid"), so the
+site carries the numbers instead: the Pages workflow (.github/workflows/pages.yml) runs this with
+GitHub's numbers before it builds the site, and the badges read docs/badges/*.json from there.
+
+Usage: badges.py --downloads N --stars N --version v1.11.0 --out DIR
+"""
+import argparse
+import json
+import os
+
+COLORS = {"downloads": "1E7F74", "stars": "E3B341", "version": "2D9F4F"}
+
+
+def short(n):
+ """As shields.io writes big numbers: 999, 8.1k, 22k, 1.2M."""
+ if n >= 1000000:
+ return f"{n // 100000 / 10:g}M"
+ if n >= 10000:
+ return f"{n // 1000}k"
+ if n >= 1000:
+ return f"{n // 100 / 10:g}k"
+ return str(n)
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--downloads", type=int, required=True)
+ ap.add_argument("--stars", type=int, required=True)
+ ap.add_argument("--version", required=True)
+ ap.add_argument("--out", required=True)
+ a = ap.parse_args()
+ os.makedirs(a.out, exist_ok=True)
+ for label, message in (("downloads", short(a.downloads)), ("stars", str(a.stars)), ("version", a.version)):
+ with open(os.path.join(a.out, f"{label}.json"), "w") as f:
+ json.dump({"schemaVersion": 1, "label": label, "message": message, "color": COLORS[label]}, f)
+ f.write("\n")
+
+
+if __name__ == "__main__":
+ main()