|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Install the `tw` binary from GitHub Releases (no Rust/Cargo required). |
| 3 | +# Usage: |
| 4 | +# curl -fsSL https://raw.githubusercontent.com/OWNER/REPO/BRANCH/scripts/install-tw.sh | bash |
| 5 | +# Optional environment: |
| 6 | +# TASKWAL_REPO=owner/repo (default: enesify/taskwal) |
| 7 | +# TASKWAL_VERSION=v0.1.0 (default: latest GitHub release) |
| 8 | +# TASKWAL_PREFIX=/usr/local (install to $PREFIX/bin; default: ~/.local) |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +TASKWAL_REPO="${TASKWAL_REPO:-enesify/taskwal}" |
| 13 | +TASKWAL_VERSION="${TASKWAL_VERSION:-}" |
| 14 | +TASKWAL_PREFIX="${TASKWAL_PREFIX:-}" |
| 15 | + |
| 16 | +die() { |
| 17 | + echo "install-tw.sh: $*" >&2 |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +detect_target_triple() { |
| 22 | + local os arch |
| 23 | + os="$(uname -s)" |
| 24 | + arch="$(uname -m)" |
| 25 | + case "$os" in |
| 26 | + Linux) |
| 27 | + case "$arch" in |
| 28 | + x86_64) echo "x86_64-unknown-linux-gnu" ;; |
| 29 | + aarch64 | arm64) die "no prebuilt binary for Linux $arch yet; build from source with Rust" ;; |
| 30 | + *) die "unsupported Linux architecture: $arch" ;; |
| 31 | + esac |
| 32 | + ;; |
| 33 | + Darwin) |
| 34 | + case "$arch" in |
| 35 | + x86_64) echo "x86_64-apple-darwin" ;; |
| 36 | + arm64) echo "aarch64-apple-darwin" ;; |
| 37 | + *) die "unsupported macOS architecture: $arch" ;; |
| 38 | + esac |
| 39 | + ;; |
| 40 | + *) |
| 41 | + die "unsupported OS: $os (install the Windows zip from Releases, or build from source)" |
| 42 | + ;; |
| 43 | + esac |
| 44 | +} |
| 45 | + |
| 46 | +require_cmd() { |
| 47 | + command -v "$1" >/dev/null 2>&1 || die "required command not found: $1" |
| 48 | +} |
| 49 | + |
| 50 | +download_to() { |
| 51 | + local url="$1" out="$2" |
| 52 | + if command -v curl >/dev/null 2>&1; then |
| 53 | + curl -fsSL -o "$out" "$url" |
| 54 | + elif command -v wget >/dev/null 2>&1; then |
| 55 | + wget -q -O "$out" "$url" |
| 56 | + else |
| 57 | + die "need curl or wget to download" |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +fetch_latest_tag() { |
| 62 | + local api_base="$1" json |
| 63 | + json="$(curl -fsSL "${api_base}/releases/latest")" |
| 64 | + if command -v python3 >/dev/null 2>&1; then |
| 65 | + printf '%s' "$json" | python3 -c "import sys,json; print(json.load(sys.stdin)['tag_name'])" |
| 66 | + return |
| 67 | + fi |
| 68 | + require_cmd grep |
| 69 | + require_cmd sed |
| 70 | + printf '%s' "$json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1 |
| 71 | +} |
| 72 | + |
| 73 | +main() { |
| 74 | + require_cmd uname |
| 75 | + local triple |
| 76 | + triple="$(detect_target_triple)" |
| 77 | + |
| 78 | + local api_base="https://api.github.com/repos/${TASKWAL_REPO}" |
| 79 | + local tag asset_name tmpdir install_dir |
| 80 | + |
| 81 | + if [[ -n "$TASKWAL_VERSION" ]]; then |
| 82 | + tag="$TASKWAL_VERSION" |
| 83 | + else |
| 84 | + tag="$(fetch_latest_tag "$api_base")" |
| 85 | + [[ -n "$tag" ]] || die "could not determine latest release tag" |
| 86 | + fi |
| 87 | + |
| 88 | + asset_name="taskwal-${tag}-${triple}.tar.gz" |
| 89 | + |
| 90 | + if [[ -n "$TASKWAL_PREFIX" ]]; then |
| 91 | + install_dir="${TASKWAL_PREFIX%/}/bin" |
| 92 | + else |
| 93 | + install_dir="${HOME}/.local/bin" |
| 94 | + fi |
| 95 | + |
| 96 | + tmpdir="$(mktemp -d "${TMPDIR:-/tmp}/taskwal-install.XXXXXX")" |
| 97 | + trap 'rm -rf "$tmpdir"' EXIT |
| 98 | + |
| 99 | + local sums_url="https://github.com/${TASKWAL_REPO}/releases/download/${tag}/SHA256SUMS" |
| 100 | + local asset_url="https://github.com/${TASKWAL_REPO}/releases/download/${tag}/${asset_name}" |
| 101 | + |
| 102 | + echo "Downloading ${asset_name} ..." |
| 103 | + download_to "$sums_url" "${tmpdir}/SHA256SUMS" |
| 104 | + download_to "$asset_url" "${tmpdir}/${asset_name}" |
| 105 | + |
| 106 | + ( |
| 107 | + cd "$tmpdir" |
| 108 | + grep -F "${asset_name}" SHA256SUMS | sha256sum -c |
| 109 | + tar -xzf "${asset_name}" |
| 110 | + local inner="taskwal-${tag}-${triple}/tw" |
| 111 | + [[ -f "$inner" ]] || die "expected ${inner} inside archive" |
| 112 | + chmod +x "$inner" |
| 113 | + mkdir -p "$install_dir" |
| 114 | + mv "$inner" "${install_dir}/tw" |
| 115 | + ) |
| 116 | + |
| 117 | + echo "Installed tw to ${install_dir}/tw" |
| 118 | + case ":${PATH}:" in |
| 119 | + *":${install_dir}:"*) ;; |
| 120 | + *) |
| 121 | + echo "Add ${install_dir} to your PATH, e.g.:" |
| 122 | + echo " export PATH=\"${install_dir}:\$PATH\"" |
| 123 | + ;; |
| 124 | + esac |
| 125 | +} |
| 126 | + |
| 127 | +main "$@" |
0 commit comments