|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Set up a local development environment for JavaScriptKit. |
| 4 | +# |
| 5 | +# Steps: |
| 6 | +# 1. Verify required tools are available (swiftly, swift, jq, npm, make, curl). |
| 7 | +# 2. If .swift-version is present, ensure that toolchain is installed via swiftly. |
| 8 | +# 3. Resolve a matching Wasm SDK from https://github.com/swiftwasm/swift-sdk-index |
| 9 | +# and install it (idempotent — skipped if already installed). |
| 10 | +# 4. Run `make bootstrap` to install JS dependencies. |
| 11 | +# 5. Print the SWIFT_SDK_ID so it can be exported for `make unittest`. |
| 12 | +# |
| 13 | +# The script runs under bash via the shebang. The final `export` instructions |
| 14 | +# it prints work unchanged in both bash and zsh. |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 19 | +cd "$REPO_ROOT" |
| 20 | + |
| 21 | +INDEX_BASE="https://raw.githubusercontent.com/swiftwasm/swift-sdk-index/refs/heads/main/v1" |
| 22 | + |
| 23 | +if [[ -t 1 ]]; then |
| 24 | + C_BLUE=$'\033[1;34m'; C_YELLOW=$'\033[1;33m'; C_RED=$'\033[1;31m'; C_RESET=$'\033[0m' |
| 25 | +else |
| 26 | + C_BLUE=''; C_YELLOW=''; C_RED=''; C_RESET='' |
| 27 | +fi |
| 28 | + |
| 29 | +log() { printf '%s==>%s %s\n' "$C_BLUE" "$C_RESET" "$*"; } |
| 30 | +warn() { printf '%swarn:%s %s\n' "$C_YELLOW" "$C_RESET" "$*" >&2; } |
| 31 | +fail() { printf '%serror:%s %s\n' "$C_RED" "$C_RESET" "$*" >&2; exit 1; } |
| 32 | + |
| 33 | +require_cmd() { |
| 34 | + command -v "$1" >/dev/null 2>&1 || fail "missing required command: $1${2:+ ($2)}" |
| 35 | +} |
| 36 | + |
| 37 | +log "Checking required tools..." |
| 38 | +require_cmd curl |
| 39 | +require_cmd jq "install via 'brew install jq' or your package manager" |
| 40 | +require_cmd npm "install Node.js from https://nodejs.org" |
| 41 | +require_cmd make |
| 42 | +require_cmd swiftly "install from https://www.swift.org/install/macos/swiftly" |
| 43 | +require_cmd swift "install a Swift toolchain via swiftly" |
| 44 | +require_cmd swiftc |
| 45 | + |
| 46 | +# 1. Honor a .swift-version pin if the repo has one. |
| 47 | +if [[ -f .swift-version ]]; then |
| 48 | + pinned="$(tr -d '[:space:]' < .swift-version)" |
| 49 | + if [[ -n "$pinned" ]]; then |
| 50 | + log "Repo pins Swift $pinned via .swift-version" |
| 51 | + if ! swiftly list 2>/dev/null | grep -qF "$pinned"; then |
| 52 | + log "Installing Swift $pinned via swiftly..." |
| 53 | + swiftly install "$pinned" |
| 54 | + fi |
| 55 | + fi |
| 56 | +fi |
| 57 | + |
| 58 | +SWIFT_VERSION_KEY="$(swiftc --version | head -n1)" |
| 59 | +log "Active Swift: $SWIFT_VERSION_KEY" |
| 60 | + |
| 61 | +# 2. Resolve a matching Wasm SDK. |
| 62 | +log "Resolving Wasm SDK from swift-sdk-index..." |
| 63 | +TAG_BY_VERSION="$(curl -fsSL "$INDEX_BASE/tag-by-version.json")" |
| 64 | +TAG="$(jq -r --arg v "$SWIFT_VERSION_KEY" '.[$v] // [] | .[-1] // empty' <<<"$TAG_BY_VERSION")" |
| 65 | + |
| 66 | +if [[ -z "$TAG" ]]; then |
| 67 | + cat >&2 <<EOF |
| 68 | +${C_RED}error:${C_RESET} no Wasm SDK indexed for '$SWIFT_VERSION_KEY'. |
| 69 | +
|
| 70 | +This usually means swiftly resolved a patch version (e.g. 6.3.1) that the |
| 71 | +swift-sdk-index hasn't published a Wasm SDK for yet. Try one of: |
| 72 | +
|
| 73 | + - Pin to an indexed version. List indexed versions: |
| 74 | + curl -fsSL '$INDEX_BASE/tag-by-version.json' | jq 'keys' |
| 75 | + Then write the version to .swift-version and run 'swiftly install <ver>'. |
| 76 | +
|
| 77 | + - Use an OSS development snapshot from https://www.swift.org/install/ |
| 78 | +
|
| 79 | +See https://github.com/swiftwasm/swift-sdk-index for details. |
| 80 | +EOF |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +log "Resolved tag: $TAG" |
| 85 | +BUILD_JSON="$(curl -fsSL "$INDEX_BASE/builds/$TAG.json")" |
| 86 | +SDK_URL="$(jq -r '."swift-sdks"."wasm32-unknown-wasip1".url' <<<"$BUILD_JSON")" |
| 87 | +SDK_CHECKSUM="$(jq -r '."swift-sdks"."wasm32-unknown-wasip1".checksum' <<<"$BUILD_JSON")" |
| 88 | +SDK_ID="$(jq -r '."swift-sdks"."wasm32-unknown-wasip1".id' <<<"$BUILD_JSON")" |
| 89 | + |
| 90 | +if swift sdk list 2>/dev/null | grep -qx "$SDK_ID"; then |
| 91 | + log "Wasm SDK already installed: $SDK_ID" |
| 92 | +else |
| 93 | + log "Installing Wasm SDK: $SDK_ID" |
| 94 | + swift sdk install "$SDK_URL" --checksum "$SDK_CHECKSUM" |
| 95 | +fi |
| 96 | + |
| 97 | +# 3. JS dependencies. |
| 98 | +log "Installing JS dependencies (make bootstrap)..." |
| 99 | +make bootstrap |
| 100 | + |
| 101 | +cat <<EOF |
| 102 | +
|
| 103 | +${C_BLUE}----${C_RESET} |
| 104 | +Setup complete. |
| 105 | +
|
| 106 | +Run the Wasm unit tests: |
| 107 | + make unittest SWIFT_SDK_ID=$SDK_ID |
| 108 | +
|
| 109 | +To avoid passing SWIFT_SDK_ID every time, add the following to your shell |
| 110 | +profile (~/.zshrc or ~/.bashrc): |
| 111 | + export SWIFT_SDK_ID=$SDK_ID |
| 112 | +EOF |
0 commit comments