|
| 1 | +#!/bin/sh |
| 2 | +# Installs the CodeWork CLI: one binary, no Node required. |
| 3 | +# |
| 4 | +# curl -fsSL https://codework.sh/install | sh |
| 5 | +# curl -fsSL https://codework.sh/install | sh -s -- --dev |
| 6 | +# curl -fsSL https://codework.sh/install | sh -s -- --version 0.3.0 |
| 7 | +# |
| 8 | +# CODEWORK_INSTALL_DIR picks where the binary goes (default ~/.codework/bin), and |
| 9 | +# CODEWORK_RELEASES_URL where releases are downloaded from. |
| 10 | +set -eu |
| 11 | + |
| 12 | +releases="${CODEWORK_RELEASES_URL:-https://github.com/codeworksh/codework/releases}" |
| 13 | +dir="${CODEWORK_INSTALL_DIR:-$HOME/.codework/bin}" |
| 14 | +npm="install it with npm instead: pnpm add -g @codeworksh/cli" |
| 15 | + |
| 16 | +fail() { |
| 17 | + printf 'codework install: %s\n' "$1" >&2 |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +usage() { |
| 22 | + cat <<EOF |
| 23 | +Usage: install [--dev | --version <version>] [--no-modify-path] |
| 24 | +
|
| 25 | + --dev the newest development build instead of the latest release |
| 26 | + --version <version> a specific release, such as 0.3.0 |
| 27 | + --no-modify-path leave your shell's startup file alone |
| 28 | +EOF |
| 29 | +} |
| 30 | + |
| 31 | +release="latest/download" |
| 32 | +modify_path=1 |
| 33 | +while [ $# -gt 0 ]; do |
| 34 | + case "$1" in |
| 35 | + --dev) release="download/devel" ;; |
| 36 | + --version) |
| 37 | + [ $# -ge 2 ] || fail "--version needs a version, such as 0.3.0" |
| 38 | + release="download/cli-v${2#v}" |
| 39 | + shift |
| 40 | + ;; |
| 41 | + --no-modify-path) modify_path=0 ;; |
| 42 | + -h | --help) |
| 43 | + usage |
| 44 | + exit 0 |
| 45 | + ;; |
| 46 | + *) fail "unknown option $1 (see --help)" ;; |
| 47 | + esac |
| 48 | + shift |
| 49 | +done |
| 50 | + |
| 51 | +case "$(uname -s)" in |
| 52 | + Darwin) os=darwin ;; |
| 53 | + Linux) os=linux ;; |
| 54 | + *) fail "$(uname -s) has no CodeWork binary; $npm" ;; |
| 55 | +esac |
| 56 | +case "$(uname -m)" in |
| 57 | + arm64 | aarch64) arch=arm64 ;; |
| 58 | + x86_64 | amd64) arch=x64 ;; |
| 59 | + *) fail "$(uname -m) has no CodeWork binary; $npm" ;; |
| 60 | +esac |
| 61 | +if [ "$os-$arch" = darwin-x64 ]; then |
| 62 | + # A shell under Rosetta reports x86_64 on an Apple silicon Mac. |
| 63 | + if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then |
| 64 | + arch=arm64 |
| 65 | + else |
| 66 | + fail "Intel Macs have no CodeWork binary; $npm" |
| 67 | + fi |
| 68 | +fi |
| 69 | +if [ "$os" = linux ] && ldd --version 2>&1 | grep -qi musl; then |
| 70 | + fail "the Linux binary needs glibc, and this system uses musl; $npm" |
| 71 | +fi |
| 72 | + |
| 73 | +download() { |
| 74 | + if command -v curl > /dev/null 2>&1; then |
| 75 | + curl -fsSL "$1" -o "$2" |
| 76 | + elif command -v wget > /dev/null 2>&1; then |
| 77 | + wget -qO "$2" "$1" |
| 78 | + else |
| 79 | + fail "downloading needs curl or wget" |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +sha256() { |
| 84 | + if command -v sha256sum > /dev/null 2>&1; then |
| 85 | + sha256sum "$1" | cut -d' ' -f1 |
| 86 | + else |
| 87 | + shasum -a 256 "$1" | cut -d' ' -f1 |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +file="cli-$os-$arch.tar.gz" |
| 92 | +tmp="$(mktemp -d)" |
| 93 | +trap 'rm -rf "$tmp"' EXIT |
| 94 | + |
| 95 | +printf 'Downloading %s\n' "$releases/$release/$file" |
| 96 | +download "$releases/$release/$file" "$tmp/$file" || fail "could not download $releases/$release/$file" |
| 97 | +download "$releases/$release/SHA256SUMS" "$tmp/SHA256SUMS" || fail "could not download the release's SHA256SUMS" |
| 98 | +expected="$(grep " $file\$" "$tmp/SHA256SUMS" | cut -d' ' -f1)" |
| 99 | +[ -n "$expected" ] || fail "SHA256SUMS does not list $file" |
| 100 | +[ "$(sha256 "$tmp/$file")" = "$expected" ] || fail "$file does not match its SHA256SUMS entry; nothing was installed" |
| 101 | + |
| 102 | +tar -xzf "$tmp/$file" -C "$tmp" |
| 103 | +mkdir -p "$dir" |
| 104 | +chmod 755 "$tmp/codework" |
| 105 | +mv "$tmp/codework" "$dir/codework" |
| 106 | +printf 'Installed %s\n' "$dir/codework" |
| 107 | + |
| 108 | +case ":$PATH:" in |
| 109 | + *":$dir:"*) ;; |
| 110 | + *) |
| 111 | + case "$(basename "${SHELL:-}")" in |
| 112 | + zsh) profile="$HOME/.zshrc" line="export PATH=\"$dir:\$PATH\"" ;; |
| 113 | + bash) profile="$HOME/.bashrc" line="export PATH=\"$dir:\$PATH\"" ;; |
| 114 | + fish) profile="$HOME/.config/fish/conf.d/codework.fish" line="fish_add_path \"$dir\"" ;; |
| 115 | + *) profile="" line="export PATH=\"$dir:\$PATH\"" ;; |
| 116 | + esac |
| 117 | + if [ "$modify_path" = 1 ] && [ -n "$profile" ]; then |
| 118 | + if ! grep -qsF "$line" "$profile"; then |
| 119 | + mkdir -p "$(dirname "$profile")" |
| 120 | + printf '\n# CodeWork\n%s\n' "$line" >> "$profile" |
| 121 | + printf 'Added %s to PATH in %s; open a new shell to use it.\n' "$dir" "$profile" |
| 122 | + fi |
| 123 | + else |
| 124 | + printf 'Add %s to your PATH:\n %s\n' "$dir" "$line" |
| 125 | + fi |
| 126 | + ;; |
| 127 | +esac |
| 128 | + |
| 129 | +"$dir/codework" --version |
0 commit comments