Skip to content

Commit 8d279a0

Browse files
authored
Add Utilities/setup-dev.sh for one-command contributor setup (swiftwasm#726)
Wraps the manual steps already documented in CONTRIBUTING.md: - Verifies required tools (swiftly, swift, jq, npm, make, curl). - If a .swift-version file is present, installs the pinned toolchain via swiftly. - Resolves and installs a matching Wasm SDK from swift-sdk-index (idempotent — skipped if already installed). - Runs `make bootstrap` to install JS deps. - Prints SWIFT_SDK_ID for use with `make unittest`. The script runs under bash via shebang; the export instructions it prints work unchanged in zsh and bash. The repo does not track .swift-version; contributors who want it ignored locally can add it to .git/info/exclude. The original manual instructions are kept in CONTRIBUTING.md as a fallback.
1 parent f3ce20c commit 8d279a0

2 files changed

Lines changed: 143 additions & 8 deletions

File tree

CONTRIBUTING.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,37 @@ Thank you for considering contributing to JavaScriptKit! We welcome contribution
1212
- Relevant error messages or logs
1313

1414
### Setting Up the Development Environment
15-
1. Clone the repository:
16-
```bash
17-
git clone https://github.com/swiftwasm/JavaScriptKit.git
18-
cd JavaScriptKit
19-
```
2015

21-
2. Install **OSS** Swift toolchain via `swiftly`
22-
3. Install Swift SDK for Wasm corresponding to the Swift version:
16+
Clone the repository:
17+
18+
```bash
19+
git clone https://github.com/swiftwasm/JavaScriptKit.git
20+
cd JavaScriptKit
21+
```
22+
23+
#### Quick start (recommended)
24+
25+
If you already have an **OSS** Swift toolchain installed via [`swiftly`](https://www.swift.org/install/macos/swiftly), run:
26+
27+
```bash
28+
./Utilities/setup-dev.sh
29+
```
30+
31+
The script verifies prerequisites, installs a matching Wasm Swift SDK from
32+
[swift-sdk-index](https://github.com/swiftwasm/swift-sdk-index), runs `make bootstrap`,
33+
and prints the `SWIFT_SDK_ID` to use with `make unittest`. Re-running it is
34+
idempotent.
35+
36+
If a `.swift-version` file is present in the repo root, the script will install
37+
that toolchain via `swiftly` automatically. Create one to pin your local dev
38+
toolchain to an indexed release (e.g. `echo 6.3.0 > .swift-version`). The repo
39+
does not track `.swift-version`; if you'd like git to ignore it locally, add it
40+
to `.git/info/exclude`.
41+
42+
#### Manual setup
43+
44+
1. Install an **OSS** Swift toolchain via `swiftly`.
45+
2. Install the Swift SDK for Wasm corresponding to the Swift version:
2346
```bash
2447
(
2548
set -eo pipefail; \
@@ -35,7 +58,7 @@ Thank you for considering contributing to JavaScriptKit! We welcome contribution
3558
jq -r '.["swift-sdks"]["wasm32-unknown-wasip1"]["id"]'
3659
)
3760
```
38-
4. Install dependencies:
61+
3. Install dependencies:
3962
```bash
4063
make bootstrap
4164
```

Utilities/setup-dev.sh

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)