Skip to content
Myr

install-script.ts

This file

Path

scripts/lib/release/install-script.ts

113 lines · 4.2 KiB · TypeScript

Last changed by

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

History of this file

Take it

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

line 1why, one step back
1/**

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

lines 2–7why, one step back
2 * The install script the platform serves at `<origin>/install.sh`, as the
3 * release writes it for one version and one origin:4 * `curl -fsSL <origin>/install.sh | sh`. POSIX `sh`, so it runs on Linux and5 * macOS as they come. It downloads the binary for the machine from the platform, checks it6 * against the release's `SHA256SUMS` from the same place, and installs nothing7 * unless the two agree.

Merge branch 'build/715-release'oscabrielbefore adoption918a2c1

lines 8–113why, one step back
8 */
9import type { Brand } from "@repo/brand";1011/** `value` as one single-quoted shell word. */12const quoted = (value: string) => `'${value.replaceAll("'", `'\\''`)}'`;1314/**15 * The script for `version`, whose files are at `baseUrl`: the stage's16 * public origin and `/releases/<version>`, where the platform serves17 * `<slug>-<target>` and `SHA256SUMS`. `machines` are the targets a release18 * builds, without Bun's `bun-` prefix (`linux-x64`). It installs the19 * binary as `<dir>/<cli>` and links `<dir>/git-credential-<slug>` to it (the20 * one binary is git's credential helper when started by that name); `<dir>`21 * is `$<SLUG>_INSTALL_DIR`, or `~/.local/bin`.22 */23export const installScriptFor = (input: {24 readonly brand: typeof Brand;25 readonly version: string;26 readonly baseUrl: string;27 readonly machines: readonly string[];28}): string => {29 const { brand, machines } = input;30 const installDir = `${brand.slug.toUpperCase()}_INSTALL_DIR`;3132 return [33 "#!/bin/sh",34 `# Installs ${brand.cli} ${input.version}, the ${brand.name} command line, from`,35 `# ${input.baseUrl}.`,36 "#",37 "# It downloads the binary for this machine, checks it against the",38 "# release's SHA256SUMS, and installs it as <dir>/" +39 `${brand.cli}, with <dir>/${brand.gitCredentialHelper}`,40 `# beside it (git's credential helper). <dir> is $${installDir},`,41 "# or ~/.local/bin. Nothing is installed unless the checksum matches.",42 "set -eu",43 "",44 `base=${quoted(input.baseUrl)}`,45 `version=${quoted(input.version)}`,46 `slug=${quoted(brand.slug)}`,47 `name=${quoted(brand.cli)}`,48 `helper=${quoted(brand.gitCredentialHelper)}`,49 `dir="\${${installDir}:-$HOME/.local/bin}"`,50 "",51 "fail() {",52 ' echo "$name install: $*" >&2',53 " exit 1",54 "}",55 "",56 'case "$(uname -s)" in',57 " Linux) os=linux ;;",58 " Darwin) os=darwin ;;",59 ' *) os="$(uname -s)" ;;',60 "esac",61 "",62 'case "$(uname -m)" in',63 " x86_64 | amd64) arch=x64 ;;",64 " arm64 | aarch64) arch=arm64 ;;",65 ' *) arch="$(uname -m)" ;;',66 "esac",67 "",68 'case "$os-$arch" in',69 ` ${machines.join(" | ")}) asset="$slug-$os-$arch" ;;`,70 ` *) fail "no build for $os-$arch; $version is built for ${machines.join(" and ")}" ;;`,71 "esac",72 "",73 "if command -v curl >/dev/null 2>&1; then",74 ' fetch() { curl -fsSL "$1" -o "$2"; }',75 "elif command -v wget >/dev/null 2>&1; then",76 ' fetch() { wget -q -O "$2" "$1"; }',77 "else",78 ' fail "needs curl or wget to download $version"',79 "fi",80 "",81 "if command -v sha256sum >/dev/null 2>&1; then",82 " digest() { sha256sum \"$1\" | cut -d ' ' -f 1; }",83 "elif command -v shasum >/dev/null 2>&1; then",84 " digest() { shasum -a 256 \"$1\" | cut -d ' ' -f 1; }",85 "else",86 ' fail "needs sha256sum or shasum to check the download"',87 "fi",88 "",89 'tmp="$(mktemp -d)"',90 "trap 'rm -rf \"$tmp\"' EXIT",91 "trap 'exit 1' HUP INT TERM",92 "",93 'fetch "$base/$asset" "$tmp/$asset" || fail "could not download $base/$asset"',94 'fetch "$base/SHA256SUMS" "$tmp/SHA256SUMS" || fail "could not download $base/SHA256SUMS"',95 "",96 'expected="$(awk -v asset="$asset" \'$2 == asset { print $1 }\' "$tmp/SHA256SUMS")"',97 '[ -n "$expected" ] || fail "SHA256SUMS lists no $asset; nothing was installed"',98 'actual="$(digest "$tmp/$asset")"',99 '[ "$actual" = "$expected" ] || fail "$asset does not match SHA256SUMS (expected $expected, got $actual); nothing was installed"',100 "",101 'mkdir -p "$dir"',102 'chmod 755 "$tmp/$asset"',103 'mv -f "$tmp/$asset" "$dir/$name"',104 'ln -sf "$name" "$dir/$helper"',105 "",106 'echo "installed $name $version as $dir/$name, with $helper beside it"',107 'case ":$PATH:" in',108 ' *":$dir:"*) ;;',109 ' *) echo "add $dir to your PATH to run $name" ;;',110 "esac",111 "",112 ].join("\n");113};