krz/micro-roguelike

A micro roguelike web game in 1kb and 1mb versions.

clone: git clone https://gitbay.org/krz/micro-roguelike.git

main: tools/check-size.sh · raw

 1#!/bin/sh
 2# Byte-budget gate for the 1MB roguelike.
 3# Usage: tools/check-size.sh [dir]   (dir defaults to the repo's 1mb/)
 4# Prints a per-file ledger, the total, and percent of budget.
 5# Exit 1 if the total exceeds 1,048,576 bytes; loud warning above 95%.
 6set -eu
 7
 8dir=${1:-$(cd "$(dirname "$0")/.." && pwd)/1mb}
 9budget=1048576
10
11if [ ! -d "$dir" ]; then
12  echo "check-size: no such directory: $dir" >&2
13  exit 2
14fi
15
16total=0
17echo "byte ledger for $dir"
18echo "----------------------------------------"
19# Newline-separated iteration so paths with spaces survive.
20oldifs=$IFS
21IFS='
22'
23for f in $(find "$dir" -type f | sort); do
24  bytes=$(wc -c < "$f")
25  bytes=$((bytes + 0))
26  total=$((total + bytes))
27  printf '%10d  %s\n' "$bytes" "${f#"$dir"/}"
28done
29IFS=$oldifs
30
31# Percent to one decimal place using integer math only.
32pct10=$((total * 1000 / budget))
33echo "----------------------------------------"
34printf '%10d  total (%d.%d%% of %d)\n' "$total" $((pct10 / 10)) $((pct10 % 10)) "$budget"
35
36if [ "$total" -gt "$budget" ]; then
37  echo "FAIL: over budget by $((total - budget)) bytes" >&2
38  exit 1
39fi
40if [ "$pct10" -ge 950 ]; then
41  echo ""
42  echo "!!! WARNING: past 95% of the 1 MiB budget ($((budget - total)) bytes left) !!!"
43  echo ""
44fi
45echo "OK: $((budget - total)) bytes remaining"