#!/bin/sh # The Louise installer: the latest release's prebuilt compiler and runtime # archives into ~/.local/bin. # # curl -fsSL https://toboggan.cloud/louise-lang/louise/raw/main/scripts/install.sh | sh # # Knobs (environment): # LOUISE_INSTALL_DIR where louise and liblouise_rt*.a land (~/.local/bin) # LOUISE_VERSION install this tag (0.1.0 or v0.1.0) instead of the latest # LOUISE_TOKEN read the forge with this token (while the repository is # internal; TOBOGGAN_TOKEN and tob's sign-in work too) # LOUISE_FORCE replace an existing install without asking # NO_COLOR no colors, no animations (also: not a tty) # # POSIX sh; needs curl, tar (xz), sha256sum, uname, mktemp. set -eu HOST="${LOUISE_HOST:-toboggan.cloud}" REPO="${LOUISE_REPO:-louise-lang/louise}" DIR="${LOUISE_INSTALL_DIR:-$HOME/.local/bin}" PLATFORM="x86_64-linux" # the only prebuilt so far # ------------------------------------------------------------ output if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then B=$(printf '\033[1m') D=$(printf '\033[2m') G=$(printf '\033[32m') R=$(printf '\033[31m') C=$(printf '\033[36m') N=$(printf '\033[0m') else B='' D='' G='' R='' C='' N='' fi say() { printf '%s\n' "$*"; } step() { printf '%s==>%s %s\n' "$C$B" "$N" "$*"; } ok() { printf ' %s✓%s %s\n' "$G" "$N" "$*"; } die() { printf ' %s✗%s %s\n' "$R$B" "$N" "$*" >&2; exit 1; } # spinner : braille frames on stderr while runs. spin() { [ -t 2 ] || { wait "$1"; return $?; } frames='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏' i=0 while kill -0 "$1" 2>/dev/null; do i=$(( (i + 1) % 10 )) printf '\r %s%s%s %s' "$C" "$(printf '%s' "$frames" | cut -c "$i")" "$N" "$2" >&2 sleep 0.1 2>/dev/null || sleep 1 done printf '\r%40s\r' '' >&2 wait "$1" } # fetch [progress-message]: download with auth when there is # one; a progress bar on a tty, a spinner otherwise. fetch() { url=$1 dest=$2 msg=${3:-} auth= token=$(token) [ -n "$token" ] && auth="Authorization: Bearer $token" if [ -t 2 ]; then # shellcheck disable=SC2086 curl -fsSL ${auth:+-H "$auth"} --connect-timeout 20 \ --speed-time 30 --speed-limit 1000 --progress-bar "$url" -o "$dest" >&2 else # shellcheck disable=SC2086 curl -fsSL ${auth:+-H "$auth"} --connect-timeout 20 \ --speed-time 30 --speed-limit 1000 -sS "$url" -o "$dest" 2>/dev/null & spin $! "${msg:-downloading}" fi } # The token, first match wins: LOUISE_TOKEN, TOBOGGAN_TOKEN, tob's # hosts.toml (tob auth login --hostname toboggan.cloud). token() { if [ -n "${LOUISE_TOKEN:-}" ]; then printf '%s' "$LOUISE_TOKEN"; return; fi if [ -n "${TOBOGGAN_TOKEN:-}" ]; then printf '%s' "$TOBOGGAN_TOKEN"; return; fi conf="${XDG_CONFIG_HOME:-$HOME/.config}/tob/hosts.toml" [ -f "$conf" ] || return 0 awk -F'"' -v host="$HOST" ' /^\[hosts\./ { s = ($0 ~ host) ? 1 : 0 } s && /^token/ { print $2; exit } ' "$conf" } # ------------------------------------------------------------ the steps banner() { [ -t 1 ] || return 0 printf '%s%s' "$B" "$C" cat <<'EOF' ░██ ░█▒ ░██ ░█ ░██ ░█████╗ ░██╗ ░██╗░░░ ░███░░░░ ░█████╗ ░██ ░██║ ░██║░██║ ░██║░░ ░█▒░██╗ ░██║░░░██╗ ░██ ░██║ ░██║░██║ ░██║ ░██ ░██╗ ░██║ ░░ ░██ ░██║ ░██║░██║ ░██║ ░██ ░██╗░██║ ░░ ░██ ░█████╔╝ ░██████╔╝░░ ░██░░░██║░░█████╔╝ ░═╝ ░═════╝ ░═════╝═╝ ░═╝ ░══╝ ░═════╝ EOF printf '%s%s A statically typed language with algebraic effects and actors.\n\n' "$N" "$D" } check_platform() { step "Checking the platform" [ "$(uname -s)" = "Linux" ] && [ "$(uname -m)" = "x86_64" ] || die "prebuilt binaries are x86_64-linux only for now; build from source (README, \"Getting started\")" for tool in curl tar sha256sum; do command -v "$tool" >/dev/null || die "needs $tool" done ok "linux x86_64, curl, tar, sha256sum" } # The latest release's tag, from the /releases/latest redirect (plain # HTTP; the API answers MessagePack, which a shell cannot read). latest_tag() { auth= t=$(token) [ -n "$t" ] && auth="Authorization: Bearer $t" loc=$( # GET, not HEAD: the route is GET-only. -D - prints the headers. # shellcheck disable=SC2086 curl -sS ${auth:+-H "$auth"} --connect-timeout 20 -o /dev/null -D - \ "https://$HOST/$REPO/releases/latest" 2>/dev/null | tr -d '\r' | sed -n 's/^location:.*\/tag\/\([^[:space:]]*\)$/\1/p' | head -1 ) [ -n "$loc" ] || die "no release found (none tagged yet, or the repository asks for a sign-in — LOUISE_TOKEN reads it then); build from source (README)" printf '%s' "$loc" } find_tag() { if [ -n "${LOUISE_VERSION:-}" ]; then case "$LOUISE_VERSION" in v*) tag="$LOUISE_VERSION" ;; *) tag="v$LOUISE_VERSION" ;; esac step "Release $tag (pinned)" else step "Finding the latest release" tag=$(latest_tag) fi ok "$tag" } already() { if command -v louise >/dev/null 2>&1 && [ -z "${LOUISE_FORCE:-}" ]; then have=$(louise --version 2>/dev/null || true) say " ${D}$have already installed;${N} ${B}louise self update${N} upgrades it (or LOUISE_FORCE=1 to reinstall)" exit 0 fi } download() { step "Downloading louise-$tag-$PLATFORM.tar.xz" tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT base="https://$HOST/api/$REPO/releases/tag/$tag/assets" tarball="louise-$tag-$PLATFORM.tar.xz" fetch "$base/$tarball" "$tmp/$tarball" "downloading louise $tag" fetch "$base/$tarball.sha256" "$tmp/$tarball.sha256" "the checksum" ok "$(stat -c%s "$tmp/$tarball") bytes" } verify() { step "Verifying the checksum" (cd "$tmp" && sha256sum -c "$tarball.sha256" >/dev/null) || die "the checksum does not match; not installing (try again — a cut short download is the usual cause)" ok "sha256 matches the release's" } install_it() { step "Installing to $DIR" mkdir -p "$DIR" (cd "$tmp" && tar -xJf "$tarball") src="$tmp/louise-$tag-$PLATFORM" [ -d "$src" ] || src=$(find "$tmp" -maxdepth 1 -mindepth 1 -type d | head -1) [ -n "$src" ] || die "the tarball holds no directory" for f in liblouise_rt.a liblouise_rt-fast.a liblouise_rt-lto.a liblouise_rt-lto.llvm; do [ -f "$src/$f" ] && install -m 644 "$src/$f" "$DIR/$f" done install -m 755 "$src/louise" "$DIR/louise" # the compiler last ok "$("$DIR/louise" --version)" } path_note() { case ":$PATH:" in *":$DIR:"*) ;; *) say "" step "One more thing: $DIR is not on your PATH" say " bash/zsh: echo 'export PATH=\"$DIR:\$PATH\"' >> ~/.bashrc # or ~/.zshrc" say " fish: fish_add_path $DIR" ;; esac } finale() { [ -t 1 ] || return 0 printf '\n %s%sLouise is installed.%s Try it:\n\n' "$B" "$G" "$N" say " mkdir hello && cd hello" say " printf 'import std/io\npub fn main :: -> () / { io.println(\"hello\") }\n' > main.lse" say " louise run main.lse" say "" say " ${D}Later: louise self update upgrades in place. Editors: README, \"Getting started\".${N}" } banner check_platform already find_tag download verify install_it path_note finale