iPuzzle/assets/images/puzzles/sync_json.sh

76 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# Regenerates assets/puzzles.json and assets/thumbnails.json from the
# images in assets/images/puzzles/.
#
# Conventions:
# Full image: assets/images/puzzles/<name>.png
# Thumbnail: assets/images/puzzles/thumb_<name>.png
# Puzzle key: puzzle_<name> (hyphens replaced with underscores)
# Thumb key: thumb_<name> (hyphens replaced with underscores)
# Label: title-cased from <name>, underscores/hyphens → spaces
#
# Usage: cd <repo-root> && bash assets/images/puzzles/sync_json.sh
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"
PUZZLE_DIR="assets/images/puzzles"
PUZZLES_JSON="assets/puzzles.json"
THUMBS_JSON="assets/thumbnails.json"
puzzles="["
thumbs="["
first=true
for img in "$PUZZLE_DIR"/*.png; do
base=$(basename "$img" .png)
# Skip thumbnails, scripts
[[ "$base" == thumb_* ]] && continue
[[ "$base" == *.sh ]] && continue
# Derive keys (replace hyphens with underscores for keys)
safe="${base//-/_}"
puzzle_key="puzzle_${safe}"
thumb_key="thumb_${safe}"
thumb_file="$PUZZLE_DIR/thumb_${base}.png"
# Skip if no matching thumbnail
if [ ! -f "$thumb_file" ]; then
echo "WARNING: No thumbnail for $base — skipping"
continue
fi
# Build label: replace underscores/hyphens with spaces, then title-case
label=$(echo "$base" | sed 's/[_-]/ /g' | sed 's/\b\(.\)/\u\1/g')
sep=""
$first || sep=","
first=false
puzzles+="${sep}
{
\"key\": \"${puzzle_key}\",
\"path\": \"${PUZZLE_DIR}/${base}.png\",
\"label\": \"${label}\"
}"
thumbs+="${sep}
{
\"key\": \"${thumb_key}\",
\"path\": \"${PUZZLE_DIR}/thumb_${base}.png\",
\"puzzleKey\": \"${puzzle_key}\"
}"
done
puzzles+="
]"
thumbs+="
]"
echo "$puzzles" > "$PUZZLES_JSON"
echo "$thumbs" > "$THUMBS_JSON"
echo "Updated $PUZZLES_JSON ($(grep -c '"key"' "$PUZZLES_JSON") entries)"
echo "Updated $THUMBS_JSON ($(grep -c '"key"' "$THUMBS_JSON") entries)"