forked from yvgude/lean-ctx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·295 lines (261 loc) · 9.38 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·295 lines (261 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/sh
# install.sh — Install lean-ctx (download pre-built binary or build from source)
#
# Usage:
# ./install.sh # download pre-built binary (no Rust needed)
# ./install.sh --download # download pre-built binary (no Rust needed)
# ./install.sh --build-only # build only, don't install
# ./install.sh --uninstall # fully remove lean-ctx (processes, configs, autostart, data, binary)
#
# One-liner (no Rust required):
# curl -fsSL https://leanctx.com/install.sh | sh
# curl -fsSL https://leanctx.com/install.sh | bash
#
# Uninstall one-liner:
# curl -fsSL https://leanctx.com/install.sh | sh -s -- --uninstall
set -eu
REPO="yvgude/lean-ctx"
INSTALL_DIR="${LEAN_CTX_INSTALL_DIR:-$HOME/.local/bin}"
# Resolve the script's directory when invoked as a file. When piped via
# `curl ... | sh`, $0 is "sh" (or similar) — the [ -f "$0" ] guard then
# falls back to pwd, which is what the bottom-of-file dispatcher expects:
# RUST_DIR check fails outside the repo, so we route to install_download.
SCRIPT_DIR="$(
src="$0"
if [ -n "$src" ] && [ -f "$src" ]; then
cd "$(dirname "$src")" 2>/dev/null && pwd
else
pwd
fi
)"
RUST_DIR="$SCRIPT_DIR/rust"
echo "lean-ctx installer"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
finish() {
case ":$PATH:" in
*":$INSTALL_DIR:"*) ;;
*)
echo ""
echo "Warning: $INSTALL_DIR is not in your PATH."
shell_name="$(basename "${SHELL:-bash}" 2>/dev/null || echo bash)"
rc="$HOME/.bashrc"
case "$shell_name" in
zsh) rc="$HOME/.zshrc" ;;
fish) rc="$HOME/.config/fish/config.fish" ;;
esac
if [ "$shell_name" = "fish" ]; then
echo " fish_add_path $INSTALL_DIR"
else
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> $rc && source $rc"
# macOS (and any bash login shell) reads ~/.bash_profile, not ~/.bashrc — so a PATH
# line in ~/.bashrc never loads in Terminal.app/IDE login shells. 'lean-ctx setup'
# fixes this automatically; this is the manual one-liner if you skip setup.
if [ "$shell_name" = "bash" ] && [ "$(uname -s)" = "Darwin" ]; then
echo " # then make login shells load ~/.bashrc (macOS bash):"
echo " grep -qs '.bashrc' \"\$HOME/.bash_profile\" 2>/dev/null || printf '\\n[ -f ~/.bashrc ] && . ~/.bashrc\\n' >> \"\$HOME/.bash_profile\""
fi
fi
;;
esac
echo ""
echo "Done! Verify with: lean-ctx --version"
echo ""
echo "Next step: Run 'lean-ctx setup' to configure your IDE integration."
echo " This sets up MCP tools, shell hooks, and editor rules."
}
detect_target() {
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
case "$arch" in
x86_64) arch="x86_64" ;;
arm64|aarch64) arch="aarch64" ;;
*)
echo "Error: unsupported architecture '$arch'"
echo "Build from source instead: ./install.sh"
exit 1 ;;
esac
case "$os" in
linux)
libc="musl"
if command -v ldd >/dev/null 2>&1; then
glibc_ver="$(ldd --version 2>&1 | head -1 | grep -oE '[0-9]+\.[0-9]+$' || true)"
if [ -n "$glibc_ver" ]; then
major="${glibc_ver%%.*}"
minor="${glibc_ver##*.}"
if [ "$major" -gt 2 ] || { [ "$major" -eq 2 ] && [ "$minor" -ge 35 ]; }; then
libc="gnu"
fi
fi
fi
echo "${arch}-unknown-linux-${libc}"
;;
darwin) echo "${arch}-apple-darwin" ;;
*)
echo "Error: unsupported OS '$os'"
echo "Windows: download from https://github.com/${REPO}/releases/latest"
exit 1 ;;
esac
}
verify_checksum() {
file="$1"
expected="$2"
if command -v sha256sum >/dev/null 2>&1; then
actual="$(sha256sum "$file" | cut -d' ' -f1)"
elif command -v shasum >/dev/null 2>&1; then
actual="$(shasum -a 256 "$file" | cut -d' ' -f1)"
else
echo "Warning: no sha256sum/shasum found, skipping checksum verification"
return 0
fi
if [ "$actual" != "$expected" ]; then
echo "Error: checksum mismatch!"
echo " Expected: $expected"
echo " Got: $actual"
exit 1
fi
echo " Checksum verified ✓"
}
install_download() {
target="$(detect_target)"
echo "Mode: download pre-built binary"
echo "Platform: $target"
echo ""
echo "Fetching latest release..."
latest="$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| grep '"tag_name"' | head -1 | cut -d'"' -f4)"
if [ -z "$latest" ]; then
echo "Error: could not determine latest release."
exit 1
fi
echo "Latest: $latest"
asset_url="https://github.com/${REPO}/releases/download/${latest}/lean-ctx-${target}.tar.gz"
sums_url="https://github.com/${REPO}/releases/download/${latest}/SHA256SUMS"
tmpdir="$(mktemp -d)"
tmp_bin=""
trap 'rm -rf "${tmpdir:-}"; [ -n "${tmp_bin:-}" ] && rm -f "${tmp_bin:-}" 2>/dev/null || true' EXIT
echo "Downloading binary..."
if ! curl -fsSL "$asset_url" -o "$tmpdir/lean-ctx.tar.gz"; then
echo "Error: download failed. Check: https://github.com/${REPO}/releases"
exit 1
fi
echo "Downloading checksums..."
if curl -fsSL "$sums_url" -o "$tmpdir/SHA256SUMS" 2>/dev/null; then
expected="$(grep "lean-ctx-${target}.tar.gz" "$tmpdir/SHA256SUMS" | cut -d' ' -f1)"
if [ -n "$expected" ]; then
verify_checksum "$tmpdir/lean-ctx.tar.gz" "$expected"
fi
else
echo " Warning: checksums not available, skipping verification"
fi
tar -xzf "$tmpdir/lean-ctx.tar.gz" -C "$tmpdir"
mkdir -p "$INSTALL_DIR"
tmp_bin="$INSTALL_DIR/.lean-ctx.new.$$"
install -m755 "$tmpdir/lean-ctx" "$tmp_bin"
if [ "$(uname -s)" = "Darwin" ]; then
xattr -cr "$tmp_bin" 2>/dev/null || true
codesign --force --sign - "$tmp_bin" 2>/dev/null || true
fi
mv -f "$tmp_bin" "$INSTALL_DIR/lean-ctx"
tmp_bin=""
echo " Installed: $INSTALL_DIR/lean-ctx"
finish
}
install_from_source() {
if ! command -v cargo >/dev/null 2>&1; then
echo "Error: cargo not found. Install Rust: https://rustup.rs"
echo "Or download a pre-built binary: $0 --download"
exit 1
fi
build_only="${1:-}"
echo "Mode: build from source"
echo ""
echo "Building lean-ctx (release)..."
if [ -d "$RUST_DIR" ]; then
(cd "$RUST_DIR" && cargo build --release)
binary="$RUST_DIR/target/release/lean-ctx"
else
cargo install lean-ctx
echo ""
echo "Installed via cargo install."
return
fi
if [ ! -x "$binary" ]; then
echo "Error: build failed — binary not found"
exit 1
fi
echo "Built: $binary"
if [ "$build_only" = "--build-only" ]; then
echo "Done (build only)."
return
fi
mkdir -p "$INSTALL_DIR"
tmp_link="$INSTALL_DIR/.lean-ctx.link.$$"
ln -sf "$binary" "$tmp_link"
mv -f "$tmp_link" "$INSTALL_DIR/lean-ctx"
echo " Linked: $INSTALL_DIR/lean-ctx -> $binary"
finish
}
uninstall() {
echo "Mode: uninstall"
echo ""
# The binary's own `uninstall` does the thorough cleanup — stops every process, then
# removes hooks, MCP configs, rules, autostart (LaunchAgent/systemd), data, and the
# binary itself. Prefer it; forward any extra flags (e.g. --keep-config, --dry-run).
if command -v lean-ctx >/dev/null 2>&1; then
lean-ctx uninstall "$@" || true
else
echo "lean-ctx not on PATH — removing known artifacts directly."
if [ "$(uname -s)" = "Darwin" ]; then
for label in com.leanctx.proxy com.leanctx.daemon; do
plist="$HOME/Library/LaunchAgents/$label.plist"
[ -f "$plist" ] && launchctl unload "$plist" 2>/dev/null || true
rm -f "$plist" 2>/dev/null || true
done
else
for svc in lean-ctx-proxy lean-ctx-daemon; do
systemctl --user disable --now "$svc" 2>/dev/null || true
rm -f "$HOME/.config/systemd/user/$svc.service" 2>/dev/null || true
done
systemctl --user daemon-reload 2>/dev/null || true
fi
rm -rf "$HOME/.lean-ctx" "$HOME/.config/lean-ctx" 2>/dev/null || true
echo " Removed autostart + data dir."
echo " (Reinstall the binary and run 'lean-ctx uninstall' for full editor-config cleanup.)"
fi
# Belt-and-suspenders: ensure the binary + PATH symlinks install.sh created are gone,
# even if the self-delete failed or the binary was never on PATH.
for b in "$INSTALL_DIR/lean-ctx" "/usr/local/bin/lean-ctx"; do
if [ -e "$b" ] || [ -L "$b" ]; then
rm -f "$b" 2>/dev/null && echo " Removed $b" || true
fi
done
echo ""
echo "lean-ctx uninstalled. Restart your shell to drop stale aliases."
echo "Verify with: command -v lean-ctx # should print nothing"
}
case "${1:-}" in
--download) install_download ;;
--build-only) install_from_source --build-only ;;
--uninstall) shift; uninstall "$@" ;;
--help|-h)
echo "Usage: $0 [--download|--build-only|--uninstall|--help]"
echo ""
echo " (no args) Download pre-built binary (builds from source if run inside the lean-ctx repo)"
echo " --download Download pre-built binary (no Rust needed)"
echo " --build-only Build only, don't install"
echo " --uninstall Fully remove lean-ctx (processes, configs, autostart, data, binary)"
echo ""
echo "Uninstall one-liner:"
echo " curl -fsSL https://leanctx.com/install.sh | sh -s -- --uninstall"
echo ""
echo "Environment:"
echo " LEAN_CTX_INSTALL_DIR Custom install directory (default: ~/.local/bin)"
;;
*)
if [ -d "$RUST_DIR" ]; then
install_from_source
else
install_download
fi
;;
esac