Gleam-authored Zed extension (Gleam → Wasm guest) for Gleam LSP + highlighting
1.6 kB
61 lines
1#!/usr/bin/env bash
2# Custom linker for wasm32-wasip2: ignore rustc's object files and emit the
3# Gleam-built component (written by build.rs to target/gleam-extension.wasm).
4#
5# Zed Install Dev Extension runs `cargo build --target wasm32-wasip2` and then
6# copies this crate's .wasm to extension.wasm — so the "link" product must be
7# the real guest.
8set -euo pipefail
9
10ROOT="$(cd "$(dirname "$0")/.." && pwd)"
11SRC="${GLEAM_EXTENSION_WASM:-$ROOT/target/gleam-extension.wasm}"
12
13if [[ ! -f "$SRC" ]]; then
14 echo "gleam-wasm-linker: missing $SRC" >&2
15 echo " build.rs should have run \`gleam export zed-extension\` first." >&2
16 echo " Set GLEAM to a wasm-capable gleam binary if needed." >&2
17 exit 1
18fi
19
20# Expand rustc response files (@path) into a flat arg list, then find -o OUT.
21OUT=""
22args=()
23for arg in "$@"; do
24 if [[ "$arg" == @* ]]; then
25 resp="${arg#@}"
26 if [[ -f "$resp" ]]; then
27 # shellcheck disable=SC2207
28 mapfile -t lines <"$resp"
29 args+=("${lines[@]}")
30 fi
31 else
32 args+=("$arg")
33 fi
34done
35
36i=0
37while [[ $i -lt ${#args[@]} ]]; do
38 case "${args[$i]}" in
39 -o)
40 i=$((i + 1))
41 OUT="${args[$i]:-}"
42 ;;
43 -o*)
44 OUT="${args[$i]#-o}"
45 ;;
46 --output=*)
47 OUT="${args[$i]#--output=}"
48 ;;
49 esac
50 i=$((i + 1))
51done
52
53if [[ -z "$OUT" ]]; then
54 echo "gleam-wasm-linker: could not find -o output path in linker args" >&2
55 printf ' %q\n' "$@" >&2
56 exit 1
57fi
58
59mkdir -p "$(dirname "$OUT")"
60cp -f "$SRC" "$OUT"
61echo "gleam-wasm-linker: installed Gleam component → $OUT" >&2