build.sh 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env bash
  2. # Idempotent build script for the steamworks.js vote and favorites patch. Safe to re-run: every step
  3. # checks whether it already happened before doing anything.
  4. #
  5. # Usage: ./build.sh [path-to-projects-dir]
  6. # Defaults to ~/projects/private if no argument is given (matches this project's usual layout).
  7. # STEAMWORKSJS_BUILD=build makes a release build instead of the default debug one.
  8. set -euo pipefail
  9. PROJECTS_DIR="${1:-$HOME/projects/private}"
  10. PATCHES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  11. STEAMWORKS_RS="$PROJECTS_DIR/steamworks-rs"
  12. STEAMWORKSJS="$PROJECTS_DIR/steamworksjs"
  13. PINNED_REV="fbb79635b06b4feea8261e5ca3e8ea3ef42facf9"
  14. PINNED_JS_REV="80c5fd7c4958ba90cf87403c3f967b8a6d748473"
  15. echo "==> Rust toolchain"
  16. if ! command -v cargo >/dev/null 2>&1; then
  17. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
  18. fi
  19. # shellcheck disable=SC1091
  20. [ -f "$HOME/.cargo/env" ] && source "$HOME/.cargo/env"
  21. cargo --version
  22. echo "==> Cloning steamworks-rs (skipped if already present)"
  23. if [ ! -d "$STEAMWORKS_RS" ]; then
  24. git clone https://github.com/Noxime/steamworks-rs "$STEAMWORKS_RS"
  25. fi
  26. git -C "$STEAMWORKS_RS" checkout --quiet "$PINNED_REV"
  27. echo "==> Cloning steamworks.js (skipped if already present)"
  28. if [ ! -d "$STEAMWORKSJS" ]; then
  29. git clone https://github.com/ceifa/steamworks.js "$STEAMWORKSJS"
  30. fi
  31. # The files this script manages go back to upstream first, so a clone that is patched already can move to the pinned
  32. # commit. A clone that does not know the commit yet fetches it.
  33. for managed in src/api/workshop.rs client.d.ts Cargo.toml Cargo.lock; do
  34. git -C "$STEAMWORKSJS" ls-files --error-unmatch "$managed" >/dev/null 2>&1 \
  35. && git -C "$STEAMWORKSJS" checkout --quiet -- "$managed"
  36. done
  37. git -C "$STEAMWORKSJS" cat-file -e "$PINNED_JS_REV^{commit}" 2>/dev/null || git -C "$STEAMWORKSJS" fetch --quiet origin
  38. git -C "$STEAMWORKSJS" checkout --quiet "$PINNED_JS_REV"
  39. # A clone patched before favorites were added has only the vote half, so anything short of the
  40. # full patch is reset to upstream first and patched again.
  41. apply_patch() {
  42. local repo="$1" file="$2" marker="$3" patch="$4"
  43. if grep -q "$marker" "$repo/$file"; then
  44. echo " already applied"
  45. return
  46. fi
  47. git -C "$repo" checkout -- "$file"
  48. git -C "$repo" apply "$patch"
  49. }
  50. echo "==> Applying steamworks-rs patch (skipped if already applied)"
  51. apply_patch "$STEAMWORKS_RS" src/ugc.rs add_item_to_favorites "$PATCHES_DIR/steamworks-rs-ugc.patch"
  52. echo "==> Applying steamworks.js patch (skipped if already applied)"
  53. apply_patch "$STEAMWORKSJS" src/api/workshop.rs add_favorite "$PATCHES_DIR/steamworks-js-workshop.patch"
  54. echo "==> Pointing steamworks.js's Cargo.toml at the local patched steamworks-rs (skipped if already done)"
  55. if grep -qF 'Noxime/steamworks-rs' "$STEAMWORKSJS/Cargo.toml" && grep -q '\[patch\.' "$STEAMWORKSJS/Cargo.toml"; then
  56. echo " already present"
  57. else
  58. {
  59. printf '\n[patch."https://github.com/Noxime/steamworks-rs/"]\n'
  60. printf 'steamworks = { path = "../steamworks-rs" }\n'
  61. } >> "$STEAMWORKSJS/Cargo.toml"
  62. fi
  63. echo "==> Building steamworks.js (this runs cargo + napi, may take a minute)"
  64. cd "$STEAMWORKSJS"
  65. npm install
  66. npm run "${STEAMWORKSJS_BUILD:-build:debug}"
  67. echo ""
  68. echo "==> Done. Built artifact:"
  69. ls -la "$STEAMWORKSJS/dist/linux64/steamworksjs.linux-x64-gnu.node"
  70. echo ""
  71. echo "Next: wire it into we_manager with:"
  72. echo " cd /path/to/we_manager"
  73. echo " npm pkg set dependencies.steamworks.js=\"file:$STEAMWORKSJS\""
  74. echo " npm install && npm run build"