#!/usr/bin/env bash # Idempotent build script for the steamworks.js vote and favorites patch. Safe to re-run: every step # checks whether it already happened before doing anything. # # Usage: ./build.sh [path-to-projects-dir] # Defaults to ~/projects/private if no argument is given (matches this project's usual layout). # STEAMWORKSJS_BUILD=build makes a release build instead of the default debug one. set -euo pipefail PROJECTS_DIR="${1:-$HOME/projects/private}" PATCHES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" STEAMWORKS_RS="$PROJECTS_DIR/steamworks-rs" STEAMWORKSJS="$PROJECTS_DIR/steamworksjs" PINNED_REV="fbb79635b06b4feea8261e5ca3e8ea3ef42facf9" PINNED_JS_REV="80c5fd7c4958ba90cf87403c3f967b8a6d748473" echo "==> Rust toolchain" if ! command -v cargo >/dev/null 2>&1; then curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y fi # shellcheck disable=SC1091 [ -f "$HOME/.cargo/env" ] && source "$HOME/.cargo/env" cargo --version echo "==> Cloning steamworks-rs (skipped if already present)" if [ ! -d "$STEAMWORKS_RS" ]; then git clone https://github.com/Noxime/steamworks-rs "$STEAMWORKS_RS" fi git -C "$STEAMWORKS_RS" checkout --quiet "$PINNED_REV" echo "==> Cloning steamworks.js (skipped if already present)" if [ ! -d "$STEAMWORKSJS" ]; then git clone https://github.com/ceifa/steamworks.js "$STEAMWORKSJS" fi # The files this script manages go back to upstream first, so a clone that is patched already can move to the pinned # commit. A clone that does not know the commit yet fetches it. for managed in src/api/workshop.rs client.d.ts Cargo.toml Cargo.lock; do git -C "$STEAMWORKSJS" ls-files --error-unmatch "$managed" >/dev/null 2>&1 \ && git -C "$STEAMWORKSJS" checkout --quiet -- "$managed" done git -C "$STEAMWORKSJS" cat-file -e "$PINNED_JS_REV^{commit}" 2>/dev/null || git -C "$STEAMWORKSJS" fetch --quiet origin git -C "$STEAMWORKSJS" checkout --quiet "$PINNED_JS_REV" # A clone patched before favorites were added has only the vote half, so anything short of the # full patch is reset to upstream first and patched again. apply_patch() { local repo="$1" file="$2" marker="$3" patch="$4" if grep -q "$marker" "$repo/$file"; then echo " already applied" return fi git -C "$repo" checkout -- "$file" git -C "$repo" apply "$patch" } echo "==> Applying steamworks-rs patch (skipped if already applied)" apply_patch "$STEAMWORKS_RS" src/ugc.rs add_item_to_favorites "$PATCHES_DIR/steamworks-rs-ugc.patch" echo "==> Applying steamworks.js patch (skipped if already applied)" apply_patch "$STEAMWORKSJS" src/api/workshop.rs add_favorite "$PATCHES_DIR/steamworks-js-workshop.patch" echo "==> Pointing steamworks.js's Cargo.toml at the local patched steamworks-rs (skipped if already done)" if grep -qF 'Noxime/steamworks-rs' "$STEAMWORKSJS/Cargo.toml" && grep -q '\[patch\.' "$STEAMWORKSJS/Cargo.toml"; then echo " already present" else { printf '\n[patch."https://github.com/Noxime/steamworks-rs/"]\n' printf 'steamworks = { path = "../steamworks-rs" }\n' } >> "$STEAMWORKSJS/Cargo.toml" fi echo "==> Building steamworks.js (this runs cargo + napi, may take a minute)" cd "$STEAMWORKSJS" npm install npm run "${STEAMWORKSJS_BUILD:-build:debug}" echo "" echo "==> Done. Built artifact:" ls -la "$STEAMWORKSJS/dist/linux64/steamworksjs.linux-x64-gnu.node" echo "" echo "Next: wire it into we_manager with:" echo " cd /path/to/we_manager" echo " npm pkg set dependencies.steamworks.js=\"file:$STEAMWORKSJS\"" echo " npm install && npm run build"