12#3# The Cloudflare Pages build. Its image carries Node and not Gleam, and the4# compiler is a single static binary, so it is fetched rather than installed:5# pinned to a version, because a toolchain that floats is a build that breaks on6# a day nobody touched the repository.7#8# It does not run `lustre/dev build`, which is the obvious thing to do and does9# not work here. The app targets JavaScript, but lustre_dev_tools is itself an10# Erlang program — it carries a whole HTTP server in mist and glisten so it can11# run a dev server — so building with it needs `escript`, which is a runtime this12# app never uses and which the image does not have either. Pulling an Erlang13# release down to run a tool that shells out to esbuild is the wrong trade when14# esbuild is a Node package and Node is already installed.15#16# So the four things `lustre/dev build` does are done directly: compile to17# JavaScript, bundle, take the shell from assets/index.html, copy the assets in.18set -euo pipefail1920GLEAM_VERSION="1.18.1"21ESBUILD_VERSION="0.28.1"2223# A compiler that is already here is the one to use — this script is the build,24# so it has to run on the machine it was written on as well as on the one that25# deploys it. Only a machine without Gleam fetches one.26if command -v gleam >/dev/null 2>&1; then27 echo "using $(gleam --version)"28else29 case "$(uname -s)" in30 Linux) OS="unknown-linux-musl" ;;31 Darwin) OS="apple-darwin" ;;32 *) echo "no Gleam build for $(uname -s)" >&2; exit 1 ;;33 esac34 case "$(uname -m)" in35 x86_64 | amd64) ARCH="x86_64" ;;36 aarch64 | arm64) ARCH="aarch64" ;;37 *) echo "no Gleam build for $(uname -m)" >&2; exit 1 ;;38 esac3940 TARBALL="gleam-v${GLEAM_VERSION}-${ARCH}-${OS}.tar.gz"41 URL="https://github.com/gleam-lang/gleam/releases/download/v${GLEAM_VERSION}/${TARBALL}"4243 echo "fetching Gleam ${GLEAM_VERSION} for ${ARCH}-${OS}"44 mkdir -p .bin45 WORK="$(mktemp -d)"46 curl -fsSL "$URL" -o "${WORK}/${TARBALL}"47 curl -fsSL "${URL}.sha256" -o "${WORK}/${TARBALL}.sha256"48 # The published digest already names the file it belongs to, so the check runs49 # from the directory that file is in. A download nobody verified is a download50 # anybody can replace.51 if command -v sha256sum >/dev/null 2>&1; then52 ( cd "$WORK" && sha256sum -c "${TARBALL}.sha256" )53 else54 ( cd "$WORK" && shasum -a 256 -c "${TARBALL}.sha256" )55 fi56 tar xzf "${WORK}/${TARBALL}" -C .bin57 rm -rf "$WORK"58 export PATH="$PWD/.bin:$PATH"59fi6061gleam test62gleam build --target javascript63node tools/bundle.mjs