BlueGit

projects-timfalken-com meteors

Files

meteors / src / build.sh

build.sh

build.sh 3.6 KiB Download Raw
1#!/usr/bin/env bash
2# Concatenate game sources into build/game.js, same pattern as raytracer / frameworkv3.
3# Also copies the PHP API into build/ so a local `php -S` and the FTP deploy
4# serve the same tree. data/config.php and any sqlite are NEVER copied.
5set -euo pipefail
6ROOT="$(cd "$(dirname "$0")/.." && pwd)"
7BUILD_FILE="$ROOT/build/game.js"
8SRC_DIR="$ROOT/src"
9
10mkdir -p "$ROOT/build"
11
12printf "Build started, output file: %s\n" "$BUILD_FILE"
13rm -f "$BUILD_FILE"
14
15FILES=(
16 "$SRC_DIR/rng.js"
17 "$SRC_DIR/i18n.js"
18 "$SRC_DIR/planet.js"
19 "$SRC_DIR/blocks.js"
20 "$SRC_DIR/easing.js"
21 "$SRC_DIR/gameCore.js"
22 "$SRC_DIR/flameFx.js"
23 "$SRC_DIR/miniLayout.js"
24 "$SRC_DIR/miniPredict.js"
25 "$SRC_DIR/online.js"
26 "$SRC_DIR/offline.js"
27 "$SRC_DIR/reward.js"
28 "$SRC_DIR/designer.js"
29 "$SRC_DIR/loginPrompt.js"
30 "$SRC_DIR/avatar.js"
31 "$SRC_DIR/main.js"
32)
33
34{
35 printf '%s\n' "/*"
36 printf '%s\n' " * AUTO-GENERATED FILE — DO NOT EDIT BY HAND."
37 printf '%s\n' " * Built by src/build.sh from the sources in src/."
38 printf '%s\n' " */"
39 printf '%s' "var PLANET_CATALOG = "
40 cat "$ROOT/server/lib/planets.json"
41 printf ';\n'
42 printf '%s' "var BLOCK_REGISTRY = "
43 cat "$ROOT/server/lib/blocks.json"
44 printf ';\n'
45 printf '%s' "var I18N_CATALOG = "
46 cat "$ROOT/server/lib/i18n.json"
47 printf ';\n'
48} >> "$BUILD_FILE"
49
50for file in "${FILES[@]}"
51do
52 if [[ ! -f "$file" ]]
53 then
54 printf "Missing source file: %s\n" "$file" >&2
55 exit 1
56 fi
57 printf "\n/*\n * Start File:\n * %s\n */ \n" "$file" >> "$BUILD_FILE"
58 printf "Building %s\n" "$file"
59 cat "$file" >> "$BUILD_FILE"
60 printf "\n/*\n * End File:\n * %s\n */ \n" "$file" >> "$BUILD_FILE"
61done
62
63# PHP API lives next to the static game so api.php, auth.php and callback.php
64# share a cookie path. Never copy secrets or the database.
65SERVER="$ROOT/server"
66cp -f "$SERVER/api.php" "$SERVER/auth.php" "$SERVER/callback.php" \
67 "$SERVER/logout.php" "$SERVER/router.php" "$SERVER/.htaccess" \
68 "$ROOT/build/"
69mkdir -p "$ROOT/build/lib"
70cp -f "$SERVER/lib/bootstrap.php" "$SERVER/lib/Store.php" \
71 "$SERVER/lib/Planets.php" "$SERVER/lib/planets.json" \
72 "$SERVER/lib/Blocks.php" "$SERVER/lib/blocks.json" \
73 "$SERVER/lib/i18n.json" "$SERVER/lib/I18n.php" \
74 "$ROOT/build/lib/"
75mkdir -p "$ROOT/build/data"
76cp -f "$SERVER/data/.htaccess" "$ROOT/build/data/.htaccess"
77# Belt and braces: a leftover local config or db must never enter build/.
78rm -f "$ROOT/build/data/config.php" "$ROOT/build/data/config.lock"
79rm -f "$ROOT/build/data/"*.sqlite "$ROOT/build/data/"*.sqlite-journal
80rm -rf "$ROOT/build/data/sessions"
81
82# t1256: stamp content-hash query strings on JS/CSS so browsers pick up
83# a new bundle even when they have an old game.js or style.css cached.
84# Locales live inside game.js (I18N_CATALOG), so the game.js hash covers
85# i18n.json as well. Idempotent: an existing ?v=... is replaced.
86asset_hash() {
87 sha256sum "$1" | cut -c1-12
88}
89INDEX="$ROOT/build/index.html"
90GAME_HASH="$(asset_hash "$ROOT/build/game.js")"
91CSS_HASH="$(asset_hash "$ROOT/build/style.css")"
92FW_HASH="$(asset_hash "$ROOT/build/framework.js")"
93I18N_HASH="$(asset_hash "$ROOT/server/lib/i18n.json")"
94# i18n.json is inlined into game.js, so a catalog change already bumps GAME_HASH.
95# I18N_HASH is logged so a deploy can be compared against the catalog file.
96sed -i -E "s/href=\"style\.css(\?v=[^\"]*)?\"/href=\"style.css?v=${CSS_HASH}\"/" "$INDEX"
97sed -i -E "s/src=\"framework\.js(\?v=[^\"]*)?\"/src=\"framework.js?v=${FW_HASH}\"/" "$INDEX"
98sed -i -E "s/src=\"game\.js(\?v=[^\"]*)?\"/src=\"game.js?v=${GAME_HASH}\"/" "$INDEX"
99printf "Asset hashes: game.js=%s style.css=%s framework.js=%s i18n.json=%s\n" \
100 "$GAME_HASH" "$CSS_HASH" "$FW_HASH" "$I18N_HASH"
101
102printf "DONE\n"