| 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. |
| 5 | set -euo pipefail |
| 6 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 7 | BUILD_FILE="$ROOT/build/game.js" |
| 8 | SRC_DIR="$ROOT/src" |
| 9 | |
| 10 | mkdir -p "$ROOT/build" |
| 11 | |
| 12 | printf "Build started, output file: %s\n" "$BUILD_FILE" |
| 13 | rm -f "$BUILD_FILE" |
| 14 | |
| 15 | FILES=( |
| 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 | |
| 50 | for file in "${FILES[@]}" |
| 51 | do |
| 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" |
| 61 | done |
| 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. |
| 65 | SERVER="$ROOT/server" |
| 66 | cp -f "$SERVER/api.php" "$SERVER/auth.php" "$SERVER/callback.php" \ |
| 67 | "$SERVER/logout.php" "$SERVER/router.php" "$SERVER/.htaccess" \ |
| 68 | "$ROOT/build/" |
| 69 | mkdir -p "$ROOT/build/lib" |
| 70 | cp -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/" |
| 75 | mkdir -p "$ROOT/build/data" |
| 76 | cp -f "$SERVER/data/.htaccess" "$ROOT/build/data/.htaccess" |
| 77 | # Belt and braces: a leftover local config or db must never enter build/. |
| 78 | rm -f "$ROOT/build/data/config.php" "$ROOT/build/data/config.lock" |
| 79 | rm -f "$ROOT/build/data/"*.sqlite "$ROOT/build/data/"*.sqlite-journal |
| 80 | rm -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. |
| 86 | asset_hash() { |
| 87 | sha256sum "$1" | cut -c1-12 |
| 88 | } |
| 89 | INDEX="$ROOT/build/index.html" |
| 90 | GAME_HASH="$(asset_hash "$ROOT/build/game.js")" |
| 91 | CSS_HASH="$(asset_hash "$ROOT/build/style.css")" |
| 92 | FW_HASH="$(asset_hash "$ROOT/build/framework.js")" |
| 93 | I18N_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. |
| 96 | sed -i -E "s/href=\"style\.css(\?v=[^\"]*)?\"/href=\"style.css?v=${CSS_HASH}\"/" "$INDEX" |
| 97 | sed -i -E "s/src=\"framework\.js(\?v=[^\"]*)?\"/src=\"framework.js?v=${FW_HASH}\"/" "$INDEX" |
| 98 | sed -i -E "s/src=\"game\.js(\?v=[^\"]*)?\"/src=\"game.js?v=${GAME_HASH}\"/" "$INDEX" |
| 99 | printf "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 | |
| 102 | printf "DONE\n" |
projects-timfalken-com meteors