Skip to content

Commit 2df21c2

Browse files
committed
Add installation script
- Created install.sh to automate the download and installation of trigrep. - Added logic to detect OS and architecture for proper binary selection. - Implemented automated version resolution and checksum verification. - Configured default installation path detection for macOS and Linux.
1 parent f4347c2 commit 2df21c2

1 file changed

Lines changed: 166 additions & 0 deletions

File tree

scripts/install.sh

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
TRIGREP_REPO="${TRIGREP_REPO:-PythonicNinja/trigrep}"
5+
TRIGREP_VERSION="${TRIGREP_VERSION:-}"
6+
TRIGREP_INSTALL_DIR="${TRIGREP_INSTALL_DIR:-}"
7+
8+
require_tool() {
9+
if ! command -v "$1" >/dev/null 2>&1; then
10+
echo "error: required tool not found: $1" >&2
11+
exit 1
12+
fi
13+
}
14+
15+
sha256_file() {
16+
local file="$1"
17+
18+
if command -v sha256sum >/dev/null 2>&1; then
19+
sha256sum "$file" | awk '{print $1}'
20+
return
21+
fi
22+
23+
if command -v shasum >/dev/null 2>&1; then
24+
shasum -a 256 "$file" | awk '{print $1}'
25+
return
26+
fi
27+
28+
echo "error: no SHA256 tool found (need sha256sum or shasum)" >&2
29+
exit 1
30+
}
31+
32+
resolve_latest_version() {
33+
local latest_url="https://github.com/${TRIGREP_REPO}/releases/latest"
34+
local final_url
35+
36+
final_url="$(curl -fsSLI -o /dev/null -w '%{url_effective}' "$latest_url")"
37+
basename "$final_url"
38+
}
39+
40+
detect_target() {
41+
local os
42+
local arch
43+
44+
os="$(uname -s)"
45+
arch="$(uname -m)"
46+
47+
case "$os" in
48+
Linux)
49+
case "$arch" in
50+
x86_64)
51+
echo "x86_64-unknown-linux-gnu"
52+
;;
53+
aarch64|arm64)
54+
echo "aarch64-unknown-linux-gnu"
55+
;;
56+
*)
57+
echo "error: unsupported Linux architecture: $arch" >&2
58+
exit 1
59+
;;
60+
esac
61+
;;
62+
Darwin)
63+
case "$arch" in
64+
x86_64)
65+
echo "x86_64-apple-darwin"
66+
;;
67+
arm64|aarch64)
68+
echo "aarch64-apple-darwin"
69+
;;
70+
*)
71+
echo "error: unsupported macOS architecture: $arch" >&2
72+
exit 1
73+
;;
74+
esac
75+
;;
76+
*)
77+
echo "error: unsupported OS: $os (this installer supports macOS and Linux)" >&2
78+
exit 1
79+
;;
80+
esac
81+
}
82+
83+
select_install_dir() {
84+
if [ -n "$TRIGREP_INSTALL_DIR" ]; then
85+
echo "$TRIGREP_INSTALL_DIR"
86+
return
87+
fi
88+
89+
if [ -d "/usr/local/bin" ] && [ -w "/usr/local/bin" ]; then
90+
echo "/usr/local/bin"
91+
return
92+
fi
93+
94+
echo "${HOME}/.local/bin"
95+
}
96+
97+
require_tool curl
98+
require_tool tar
99+
100+
TARGET="$(detect_target)"
101+
VERSION="${TRIGREP_VERSION}"
102+
if [ -z "$VERSION" ]; then
103+
VERSION="$(resolve_latest_version)"
104+
fi
105+
106+
INSTALL_DIR="$(select_install_dir)"
107+
mkdir -p "$INSTALL_DIR"
108+
if [ ! -w "$INSTALL_DIR" ]; then
109+
echo "error: install directory is not writable: $INSTALL_DIR" >&2
110+
echo "set TRIGREP_INSTALL_DIR to a writable directory" >&2
111+
exit 1
112+
fi
113+
114+
ASSET="trigrep-${VERSION}-${TARGET}.tar.gz"
115+
BASE_URL="https://github.com/${TRIGREP_REPO}/releases/download/${VERSION}"
116+
ASSET_URL="${BASE_URL}/${ASSET}"
117+
CHECKSUMS_URL="${BASE_URL}/checksums.txt"
118+
119+
TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/trigrep-install.XXXXXX")"
120+
trap 'rm -rf "$TMP_DIR"' EXIT
121+
122+
ARCHIVE_PATH="${TMP_DIR}/${ASSET}"
123+
CHECKSUMS_PATH="${TMP_DIR}/checksums.txt"
124+
125+
echo "==> Installing trigrep ${VERSION} (${TARGET})"
126+
echo "==> Downloading ${ASSET_URL}"
127+
curl -fsSL --retry 3 -o "$ARCHIVE_PATH" "$ASSET_URL"
128+
129+
echo "==> Downloading ${CHECKSUMS_URL}"
130+
if curl -fsSL --retry 3 -o "$CHECKSUMS_PATH" "$CHECKSUMS_URL"; then
131+
EXPECTED_SHA="$(awk -v name="$ASSET" '$2 == name { print $1 }' "$CHECKSUMS_PATH")"
132+
if [ -z "$EXPECTED_SHA" ]; then
133+
echo "error: checksum for ${ASSET} not found in checksums.txt" >&2
134+
exit 1
135+
fi
136+
137+
ACTUAL_SHA="$(sha256_file "$ARCHIVE_PATH")"
138+
if [ "$EXPECTED_SHA" != "$ACTUAL_SHA" ]; then
139+
echo "error: checksum mismatch for ${ASSET}" >&2
140+
echo "expected: $EXPECTED_SHA" >&2
141+
echo "actual: $ACTUAL_SHA" >&2
142+
exit 1
143+
fi
144+
145+
echo "==> Checksum verified"
146+
else
147+
echo "==> Warning: checksums.txt not found for ${VERSION}; skipping checksum verification" >&2
148+
fi
149+
tar -xzf "$ARCHIVE_PATH" -C "$TMP_DIR"
150+
151+
if [ ! -f "${TMP_DIR}/trigrep" ]; then
152+
echo "error: trigrep binary not found in archive" >&2
153+
exit 1
154+
fi
155+
156+
install -m 0755 "${TMP_DIR}/trigrep" "${INSTALL_DIR}/trigrep"
157+
158+
echo "==> Installed to ${INSTALL_DIR}/trigrep"
159+
"${INSTALL_DIR}/trigrep" --version
160+
161+
case ":$PATH:" in
162+
*":${INSTALL_DIR}:"*) ;;
163+
*)
164+
echo "==> Add ${INSTALL_DIR} to your PATH if needed"
165+
;;
166+
esac

0 commit comments

Comments
 (0)