#!/bin/sh # Installs the newest release of toolchain for the user running it, on # Linux or macOS: # # curl -fsSL https://solidplanssoftware.com/install.sh | sh # # It reads the manifest the downloads host publishes, takes the # executable built for this machine, checks it against the sha256 the # manifest records, and puts it where `toolchain` itself keeps an # installed application, so `toolchain update` finds it afterwards: # # /hyperfoundry/toolchain/apps/toolchain/toolchain// the release # /hyperfoundry/toolchain/apps/toolchain/toolchain/current the version in use # /hyperfoundry/toolchain/bin/toolchain its launcher # # is $XDG_DATA_HOME, or ~/.local/share. Where `bin` is not on PATH # it is added in ~/.profile, in ~/.bash_profile where that exists, and in # ~/.zshenv where the login shell is zsh. Nothing needs root. # # Everything runs from `main` on the last line, so a download cut short # runs nothing. set -eu DOWNLOADS="https://downloads.solidplanssoftware.com" MANIFEST="public/toolchain/toolchain/latest.json" NAMESPACE="toolchain" APPLICATION="toolchain" work="" staging="" fail() { printf 'install: %s\n' "$1" >&2 exit 1 } cleanup() { [ -z "$work" ] || rm -rf "$work" [ -z "$staging" ] || rm -rf "$staging" } # This machine as a release names it, `-`. platform() { system=$(uname -s | tr '[:upper:]' '[:lower:]') machine=$(uname -m | tr '[:upper:]' '[:lower:]') case $system in darwin) system="macos" ;; esac case $machine in amd64 | x86_64) machine="x64" ;; arm64 | aarch64) machine="arm64" ;; esac printf '%s-%s' "$system" "$machine" } # fetch fetch() { if command -v curl >/dev/null 2>&1; then curl -fsSL -o "$2" "$1" elif command -v wget >/dev/null 2>&1; then wget -q -O "$2" "$1" else fail "curl or wget is needed to download, and neither was found." fi } # The sha256 of a file, in lowercase hexadecimal. sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f 1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d ' ' -f 1 else fail "sha256sum or shasum is needed to check the download, and neither was found." fi } # field : the string a flat JSON object holds under a name. field() { printf '%s' "$2" | sed -n 's/.*"'"$1"'"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' } main() { trap cleanup EXIT work=$(mktemp -d) fetch "$DOWNLOADS/$MANIFEST" "$work/latest.json" || fail "the manifest at $DOWNLOADS/$MANIFEST could not be read." manifest=$(tr -d '\n\r' <"$work/latest.json") # Every key that opens an object, less `platforms` itself, is a platform. supported=$( printf '%s' "$manifest" | tr '{' '\n' | sed -n 's/.*"\([^"]*\)"[[:space:]]*:[[:space:]]*$/\1/p' | grep -v '^platforms$' | tr '\n' ' ' | sed 's/ $//' ) machine=$(platform) entry="" case $machine in *[!a-z0-9_-]*) ;; *) entry=$( printf '%s' "$manifest" | sed -n 's/.*"'"$machine"'"[[:space:]]*:[[:space:]]*{\([^}]*\)}.*/\1/p' ) ;; esac [ -n "$entry" ] || fail "toolchain is not built for $machine. It is built for: ${supported:-nothing yet}." version=$(field version "$manifest") path=$(field path "$entry") expected=$(field sha256 "$entry" | tr '[:upper:]' '[:lower:]') case $version in "" | .* | *[!0-9A-Za-z.+-]*) fail "the manifest names no usable version." ;; esac case $path in public/*) ;; *) fail "the manifest names no download for $machine." ;; esac case $path in *..* | */) fail "the manifest names no download for $machine." ;; esac [ ${#expected} -eq 64 ] || fail "the manifest records no sha256 for $machine." name=${path##*/} printf 'Downloading toolchain %s for %s\n' "$version" "$machine" fetch "$DOWNLOADS/$path" "$work/$name" || fail "$DOWNLOADS/$path could not be downloaded." actual=$(sha256 "$work/$name") [ "$actual" = "$expected" ] || fail "the download is not the file the manifest records (sha256 $actual, expected $expected). Nothing was installed." root="${XDG_DATA_HOME:-$HOME/.local/share}/hyperfoundry/toolchain" home="$root/apps/$NAMESPACE/$APPLICATION" target="$home/$version" mkdir -p "$home" "$root/bin" # Unpacked beside where it will live and moved into place whole, so a # run cut short leaves nothing that reads as an installed release. staging="$home/$version.$$.partial" rm -rf "$staging" mkdir "$staging" executable="" case $name in *.zip) command -v unzip >/dev/null 2>&1 || fail "unzip is needed to unpack the release, and was not found." unzip -q "$work/$name" -d "$staging" || fail "$name could not be unpacked." stem=${name%.zip} for suffix in "" .exe .bin; do if [ -f "$staging/$stem$suffix" ]; then executable="$stem$suffix" break fi done [ -n "$executable" ] || fail "$name holds no executable named for it, so there is nothing to run." ;; *) cp "$work/$name" "$staging/$name" executable="$name" ;; esac chmod +x "$staging/$executable" if [ -e "$target" ]; then rm -rf "$target" || fail "$target could not be replaced. Close toolchain and run this again." fi mv "$staging" "$target" staging="" run_as=${executable%.exe} run_as=${run_as%.bin} launcher="$root/bin/$run_as" quoted=$(printf '%s' "$target/$executable" | sed "s/'/'\\\\''/g") printf "#!/bin/sh\nexec '%s' \"\$@\"\n" "$quoted" >"$launcher" chmod 755 "$launcher" printf '%s\n' "$version" >"$home/current" for other in "$home"/*/; do [ -d "$other" ] || continue [ "${other%/}" != "$target" ] || continue rm -rf "$other" 2>/dev/null || printf 'An earlier version could not be removed: %s\n' "${other%/}" done "$launcher" --version >/dev/null 2>&1 || fail "toolchain $version was installed at $target and did not start." printf 'Installed toolchain %s: %s\n' "$version" "$launcher" case ":$PATH:" in *":$root/bin:"*) printf 'Run it as: %s\n' "$run_as" ;; *) on_path "$root/bin" ;; esac } # on_path : name a directory on PATH in the profiles a login reads, # once each. ~/.profile is every POSIX shell's; bash reads ~/.bash_profile # in its place where that exists, and zsh reads neither. on_path() { directory=$1 quoted=$(printf '%s' "$directory" | sed 's/[\\"$`]/\\&/g') line="export PATH=\"$quoted:\$PATH\"" set -- "$HOME/.profile" [ ! -e "$HOME/.bash_profile" ] || set -- "$@" "$HOME/.bash_profile" case ${SHELL:-} in */zsh) set -- "$@" "${ZDOTDIR:-$HOME}/.zshenv" ;; esac for profile; do if [ -f "$profile" ] && grep -qF "$line" "$profile"; then continue fi if { printf '\n# Added by the toolchain install script.\n%s\n' "$line" >>"$profile" } 2>/dev/null; then printf 'Added %s to your PATH in %s\n' "$directory" "$profile" else printf '%s could not be written.\n' "$profile" fi done printf 'It is on your PATH in a new login. For this terminal, run:\n %s\n' "$line" } main "$@"