interplane.io
Source of interplane.io: a Rust generator that renders the page from the protocol repository's public record, deployed hourly from the Spark.
Brought by drakestapleton
interplane.io / content / hop.install.sh
| 1 | #!/bin/sh |
| 2 | # Installs Hop after checking that the download is the one you expect. |
| 3 | # |
| 4 | # HOP_DOWNLOAD_BASE=<url> ./install.sh <version> <sha256 of the zip> |
| 5 | # HOP_DOWNLOAD_BASE=<url> ./install.sh --release <RELEASE.json file or URL> |
| 6 | # ./install.sh --from-source <git url> <full commit id> |
| 7 | # |
| 8 | # HOP_DOWNLOAD_BASE is the folder the zip is served from (https://, http:// or file://). |
| 9 | # The zip is installed only if its sha256 matches. Take the expected sha256 (or the RELEASE.json) |
| 10 | # from a place you trust: a checksum served next to the zip only proves the download was not damaged. |
| 11 | # --from-source clones the code, checks out exactly that commit, builds it on this Mac and installs it. |
| 12 | # |
| 13 | # Hop needs the macOS version named in the app (26 or later). On an older system nothing is installed. |
| 14 | # |
| 15 | # Options through the environment: |
| 16 | # HOP_INSTALL_DIR where Hop.app goes (default: ~/Applications) |
| 17 | # HOP_NO_OPEN=1 do not open Hop after installing (also lets the install go ahead while a Hop from |
| 18 | # another folder is running; that one is left alone) |
| 19 | # HOP_NO_CLI=1 do not put the hop command in ~/.local/bin (it is also left out when |
| 20 | # HOP_INSTALL_DIR is set: a Hop in a folder of your choosing does not take the name) |
| 21 | set -eu |
| 22 | |
| 23 | say() { printf '%s\n' "$*"; } |
| 24 | die() { printf 'install.sh: %s\n' "$*" >&2; exit 1; } |
| 25 | usage() { sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; } |
| 26 | BUNDLE_ID=com.aienos.hop |
| 27 | sha() { shasum -a 256 "$1" | cut -d' ' -f1; } |
| 28 | is_sha() { printf '%s' "$1" | grep -Eq '^[0-9a-f]{64}$'; } |
| 29 | |
| 30 | [ "$(uname -s)" = "Darwin" ] || die "Hop runs on macOS only" |
| 31 | [ "$(uname -m)" = "arm64" ] || die "Hop needs an Apple silicon Mac" |
| 32 | |
| 33 | DEST_DIR=${HOP_INSTALL_DIR:-$HOME/Applications} |
| 34 | DEST="$DEST_DIR/Hop.app" |
| 35 | WORK=$(mktemp -d "${TMPDIR:-/tmp}/hop-install.XXXXXX") |
| 36 | trap 'rm -rf "$WORK"' EXIT |
| 37 | |
| 38 | # Stops, before anything is downloaded, built or moved, when this Mac's macOS is older than the app needs. |
| 39 | # HOP_TEST_MACOS_VERSION stands in for the detected version (tests only). |
| 40 | need_macos() { # need_macos <minimum version, e.g. 26.0> |
| 41 | MIN_MAJOR=${1%%.*} |
| 42 | HAVE=${HOP_TEST_MACOS_VERSION:-$(sw_vers -productVersion)} |
| 43 | HAVE_MAJOR=${HAVE%%.*} |
| 44 | printf '%s' "$MIN_MAJOR" | grep -Eq '^[0-9]+$' || die "cannot read the macOS version Hop needs ($1); nothing was installed" |
| 45 | printf '%s' "$HAVE_MAJOR" | grep -Eq '^[0-9]+$' || die "cannot read this Mac's macOS version ($HAVE); nothing was installed" |
| 46 | [ "$HAVE_MAJOR" -ge "$MIN_MAJOR" ] || die "this Mac runs macOS $HAVE, and Hop needs macOS $MIN_MAJOR or later; nothing was installed" |
| 47 | } |
| 48 | plist_min() { /usr/libexec/PlistBuddy -c 'Print :LSMinimumSystemVersion' "$1" 2>/dev/null || die "cannot read the macOS version Hop needs from $1; nothing was installed"; } |
| 49 | |
| 50 | fetch() { # fetch <url> <file> |
| 51 | curl --fail --silent --show-error --location --proto '=https,http,file' -o "$2" "$1" || die "could not download $1" |
| 52 | } |
| 53 | |
| 54 | # Puts a checked Hop.app in place. The app that was there is kept as a backup. |
| 55 | install_app() { # install_app <path to Hop.app> |
| 56 | [ -x "$1/Contents/MacOS/Hop" ] || die "that is not a complete Hop.app" |
| 57 | need_macos "$(plist_min "$1/Contents/Info.plist")" # the app's own minimum, checked before the destination is touched |
| 58 | codesign --verify --strict "$1" || die "the app's signature does not match its contents; nothing was installed" |
| 59 | # A running Hop is found by its exact program path (compared as plain text, never as a pattern) and, |
| 60 | # wherever it was started from, by its bundle id. |
| 61 | if ps -axo comm= | grep -Fxq "$DEST/Contents/MacOS/Hop"; then |
| 62 | die "Hop is running from $DEST. Quit it (menu bar icon, Quit Hop) and run this again. Nothing was installed." |
| 63 | fi |
| 64 | ELSEWHERE=$(lsappinfo list 2>/dev/null | grep -A2 -F "bundleID=\"$BUNDLE_ID\"" | sed -n 's/^ *bundle path="\(.*\)"$/\1/p' | head -n 1) |
| 65 | if [ -n "$ELSEWHERE" ]; then |
| 66 | if [ "${HOP_NO_OPEN:-}" = "1" ]; then |
| 67 | say "note: a Hop is running from $ELSEWHERE; it is left alone and the new copy is not opened" |
| 68 | else |
| 69 | die "a Hop is already running from $ELSEWHERE. Quit it (menu bar icon, Quit Hop) and run this again. Nothing was installed." |
| 70 | fi |
| 71 | fi |
| 72 | mkdir -p "$DEST_DIR" |
| 73 | STAGED="$DEST_DIR/.Hop-install-$$.app" |
| 74 | rm -rf "$STAGED" |
| 75 | /usr/bin/ditto "$1" "$STAGED" |
| 76 | BACKUP="" |
| 77 | if [ -e "$DEST" ]; then |
| 78 | mkdir -p "$DEST_DIR/.Hop-backups" |
| 79 | BACKUP="$DEST_DIR/.Hop-backups/Hop-$(date -u +%Y%m%dT%H%M%SZ)-$$.app" |
| 80 | mv "$DEST" "$BACKUP" |
| 81 | fi |
| 82 | if ! mv "$STAGED" "$DEST"; then |
| 83 | [ -z "$BACKUP" ] || mv "$BACKUP" "$DEST" |
| 84 | die "could not put Hop in $DEST_DIR" |
| 85 | fi |
| 86 | say "installed $DEST" |
| 87 | [ -z "$BACKUP" ] || say "the app that was there before: $BACKUP" |
| 88 | # The hop command for a terminal: `Hop link` writes ~/.local/bin/hop and leaves a file or another program's |
| 89 | # link there alone. A release from before that command would open the app on the word, so it is asked only |
| 90 | # when the program carries the command's own words. |
| 91 | if [ "${HOP_NO_CLI:-}" != "1" ] && [ -z "${HOP_INSTALL_DIR:-}" ] && grep -a -q 'hop is already linked' "$DEST/Contents/MacOS/Hop"; then |
| 92 | "$DEST/Contents/MacOS/Hop" link 2>&1 | sed 's/^/install.sh: /' || true |
| 93 | fi |
| 94 | if [ "${HOP_NO_OPEN:-}" = "1" ]; then say "not opened (HOP_NO_OPEN=1)"; else open "$DEST"; fi |
| 95 | } |
| 96 | |
| 97 | from_download() { # from_download <version> <zip sha256> [executable sha256] |
| 98 | VERSION=$1; WANT=$2; WANT_EXE=${3:-} |
| 99 | printf '%s' "$VERSION" | grep -Eq '^[0-9A-Za-z][0-9A-Za-z._-]*$' || die "that does not look like a version: $VERSION" |
| 100 | is_sha "$WANT" || die "the expected sha256 must be 64 lower-case hex characters" |
| 101 | [ -n "${HOP_DOWNLOAD_BASE:-}" ] || die "set HOP_DOWNLOAD_BASE to the folder the zip is served from" |
| 102 | ZIP="$WORK/Hop-$VERSION.zip" |
| 103 | say "downloading ${HOP_DOWNLOAD_BASE%/}/Hop-$VERSION.zip" |
| 104 | fetch "${HOP_DOWNLOAD_BASE%/}/Hop-$VERSION.zip" "$ZIP" |
| 105 | GOT=$(sha "$ZIP") |
| 106 | if [ "$GOT" != "$WANT" ]; then |
| 107 | say "expected sha256 $WANT" >&2 |
| 108 | say "got sha256 $GOT" >&2 |
| 109 | die "the download does not match the expected sha256; nothing was installed" |
| 110 | fi |
| 111 | say "sha256 matches: $GOT" |
| 112 | # Only Hop.app may come out of the zip. |
| 113 | if unzip -Z1 "$ZIP" | grep -Ev '^Hop\.app(/|$)' | grep -q . || unzip -Z1 "$ZIP" | grep -Eq '(^|/)\.\.(/|$)'; then |
| 114 | die "the zip holds something other than Hop.app; nothing was installed" |
| 115 | fi |
| 116 | mkdir "$WORK/unzipped" |
| 117 | /usr/bin/ditto -x -k "$ZIP" "$WORK/unzipped" |
| 118 | if [ -n "$WANT_EXE" ]; then |
| 119 | [ "$(sha "$WORK/unzipped/Hop.app/Contents/MacOS/Hop")" = "$WANT_EXE" ] || die "the program inside the zip does not match RELEASE.json; nothing was installed" |
| 120 | say "program sha256 matches RELEASE.json" |
| 121 | fi |
| 122 | install_app "$WORK/unzipped/Hop.app" |
| 123 | } |
| 124 | |
| 125 | from_release() { # from_release <RELEASE.json file or URL> |
| 126 | J="$WORK/RELEASE.json" |
| 127 | case "$1" in |
| 128 | http://*|https://*|file://*) fetch "$1" "$J" ;; |
| 129 | *) [ -f "$1" ] || die "no such file: $1"; cp "$1" "$J" ;; |
| 130 | esac |
| 131 | field() { plutil -extract "$1" raw -o - "$J" 2>/dev/null || die "RELEASE.json has no $1"; } |
| 132 | V=$(field version); Z=$(field zip_sha256); E=$(field executable_sha256) |
| 133 | need_macos "$(field minimum_macos)" # before the download |
| 134 | is_sha "$E" || die "RELEASE.json: executable_sha256 is not a sha256" |
| 135 | say "release $V, built from commit $(field source_commit)" |
| 136 | from_download "$V" "$Z" "$E" |
| 137 | } |
| 138 | |
| 139 | from_source() { # from_source <git url> <commit> |
| 140 | URL=$1; COMMIT=$2 |
| 141 | printf '%s' "$COMMIT" | grep -Eq '^[0-9a-f]{40}$' || die "give the full 40-character commit id, not a branch, tag or short id" |
| 142 | command -v git >/dev/null 2>&1 && command -v swiftc >/dev/null 2>&1 || die "building needs Apple's Command Line Tools: xcode-select --install" |
| 143 | SRC="$WORK/src" |
| 144 | git clone --quiet --no-checkout "$URL" "$SRC" || die "could not clone $URL" |
| 145 | git -C "$SRC" checkout --quiet --detach "$COMMIT" 2>/dev/null || die "commit $COMMIT is not in $URL; nothing was built" |
| 146 | HEAD=$(git -C "$SRC" rev-parse HEAD) |
| 147 | [ "$HEAD" = "$COMMIT" ] || die "checked out $HEAD, not $COMMIT; nothing was built" |
| 148 | [ -z "$(git -C "$SRC" status --porcelain)" ] || die "the checkout is not clean; nothing was built" |
| 149 | need_macos "$(plist_min "$SRC/Resources/Info.plist")" # before the build |
| 150 | say "building commit $HEAD" |
| 151 | (cd "$SRC" && ./build.sh) || die "the build failed; nothing was installed" |
| 152 | say "program sha256 $(sha "$SRC/build/Hop.app/Contents/MacOS/Hop")" |
| 153 | install_app "$SRC/build/Hop.app" |
| 154 | } |
| 155 | |
| 156 | case "${1:-}" in |
| 157 | --from-source) [ $# -eq 3 ] || usage; from_source "$2" "$3" ;; |
| 158 | --release) [ $# -eq 2 ] || usage; from_release "$2" ;; |
| 159 | -h|--help|"") usage ;; |
| 160 | -*) usage ;; |
| 161 | *) [ $# -eq 2 ] || usage; from_download "$1" "$2" ;; |
| 162 | esac |