-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·168 lines (143 loc) · 6.29 KB
/
install.sh
File metadata and controls
executable file
·168 lines (143 loc) · 6.29 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
#!/bin/bash
# Token Optimizer - One-command installer
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/alexgreensh/token-optimizer/main/install.sh | bash
#
# What it does:
# 1. Checks prerequisites (Python 3.8+, git, ~/.claude/)
# 2. Clones (or updates) the repo into ~/.claude/token-optimizer
# 3. Symlinks the skill into ~/.claude/skills/token-optimizer
# 4. Prints success + usage instructions
#
# Idempotent: safe to run multiple times.
#
# Copyright (C) 2026 Alex Greenshpun
# SPDX-License-Identifier: AGPL-3.0-only
set -euo pipefail
REPO_HTTPS="https://github.com/alexgreensh/token-optimizer.git"
REPO_SSH="git@github.com:alexgreensh/token-optimizer.git"
INSTALL_DIR="${HOME}/.claude/token-optimizer"
SKILL_DIR="${HOME}/.claude/skills"
# ── Colors ────────────────────────────────────────────────────
if [ -t 1 ]; then
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
BOLD='\033[1m'
NC='\033[0m'
else
GREEN='' YELLOW='' RED='' BOLD='' NC=''
fi
info() { printf "${GREEN}>${NC} %s\n" "$1"; }
warn() { printf "${YELLOW}!${NC} %s\n" "$1"; }
fail() { printf "${RED}x${NC} %s\n" "$1"; exit 1; }
# ── Prerequisites ─────────────────────────────────────────────
info "Checking prerequisites..."
# Python 3.8+
if ! command -v python3 &>/dev/null; then
fail "python3 not found. Install Python 3.8+ first."
fi
PY_VERSION=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")' 2>/dev/null)
PY_MAJOR=$(echo "$PY_VERSION" | cut -d. -f1)
PY_MINOR=$(echo "$PY_VERSION" | cut -d. -f2)
if [ "$PY_MAJOR" -lt 3 ] 2>/dev/null || { [ "$PY_MAJOR" -eq 3 ] && [ "$PY_MINOR" -lt 8 ]; } 2>/dev/null; then
fail "Python ${PY_VERSION} found, but 3.8+ is required."
fi
info "Python ${PY_VERSION} OK"
# Git
if ! command -v git &>/dev/null; then
fail "git not found. Install git first."
fi
info "git OK"
# Claude Code directory
if [ ! -d "${HOME}/.claude" ]; then
fail "~/.claude/ not found. Install Claude Code first: https://claude.ai/download"
fi
info "~/.claude/ OK"
# ── Plugin Conflict Check ────────────────────────────────────
if [ -d "${HOME}/.claude/plugins/cache" ]; then
if find "${HOME}/.claude/plugins/cache" -name "plugin.json" -exec grep -l '"name"[[:space:]]*:[[:space:]]*"token-optimizer"' {} \; 2>/dev/null | head -1 | grep -q .; then
warn "Token Optimizer is already installed as a Claude Code plugin."
warn "The script installer creates a skill symlink, which would duplicate the plugin."
warn "If you want the script version instead, first uninstall the plugin:"
warn " /plugin uninstall token-optimizer@alexgreensh-token-optimizer"
echo ""
if [ -t 0 ] || [ -e /dev/tty ]; then
printf "Continue anyway? (y/N) "
read -r confirm < /dev/tty
[ "$confirm" = "y" ] || [ "$confirm" = "Y" ] || exit 0
else
warn "Non-interactive mode detected. Skipping (use plugin install instead)."
exit 0
fi
fi
fi
# ── Clone or Update ───────────────────────────────────────────
clone_repo() {
local clone_log="/tmp/token-optimizer-clone-$$.log"
if git clone --depth 1 "$REPO_HTTPS" "$INSTALL_DIR" 2>"$clone_log"; then
rm -f "$clone_log"
return 0
fi
warn "HTTPS clone failed. Details: $(cat "$clone_log" 2>/dev/null)"
info "Trying SSH..."
if git clone --depth 1 "$REPO_SSH" "$INSTALL_DIR" 2>"$clone_log"; then
rm -f "$clone_log"
return 0
fi
warn "SSH clone also failed. Details: $(cat "$clone_log" 2>/dev/null)"
rm -f "$clone_log"
fail "Could not clone repository. Check network connectivity and GitHub access."
}
if [ -d "${INSTALL_DIR}/.git" ]; then
info "Existing install found. Updating..."
git -C "$INSTALL_DIR" pull --ff-only || {
warn "git pull failed. Try: cd ${INSTALL_DIR} && git pull"
warn "Continuing with existing version."
}
elif [ -d "$INSTALL_DIR" ]; then
BACKUP="${INSTALL_DIR}.backup.$(date +%Y%m%d_%H%M%S)"
warn "Non-git install found at ${INSTALL_DIR}"
warn "Backing up to ${BACKUP}"
mv "$INSTALL_DIR" "$BACKUP"
info "Cloning Token Optimizer..."
clone_repo
else
info "Cloning Token Optimizer..."
clone_repo
fi
# ── Symlink Skill ─────────────────────────────────────────────
mkdir -p "$SKILL_DIR"
SKILL_LINK="${SKILL_DIR}/token-optimizer"
if [ -d "$SKILL_LINK" ] && [ ! -L "$SKILL_LINK" ]; then
warn "/token-optimizer skill directory exists (not a symlink). Skipping."
warn "To use the repo version, move it: mv ${SKILL_LINK} ${SKILL_LINK}.local"
elif [ -f "$SKILL_LINK" ] && [ ! -L "$SKILL_LINK" ]; then
warn "Regular file exists at ${SKILL_LINK}. Moving to ${SKILL_LINK}.bak"
mv "$SKILL_LINK" "${SKILL_LINK}.bak"
ln -sf "${INSTALL_DIR}/skills/token-optimizer" "$SKILL_LINK"
info "Linked /token-optimizer skill"
else
ln -sf "${INSTALL_DIR}/skills/token-optimizer" "$SKILL_LINK"
info "Linked /token-optimizer skill"
fi
# ── Make Scripts Executable ───────────────────────────────────
chmod +x "${INSTALL_DIR}/skills/token-optimizer/scripts/measure.py" 2>/dev/null || true
# ── Summary ───────────────────────────────────────────────────
COMMIT=$(git -C "$INSTALL_DIR" rev-parse --short HEAD 2>/dev/null || echo "?")
echo ""
printf "${BOLD}${GREEN}Token Optimizer installed!${NC}\n"
echo ""
echo " Location: ${INSTALL_DIR}"
echo " Commit: ${COMMIT}"
echo " Skill: /token-optimizer"
echo ""
echo " Measure current overhead:"
echo " python3 ${INSTALL_DIR}/skills/token-optimizer/scripts/measure.py report"
echo ""
echo " Start a Claude Code session and run:"
echo " /token-optimizer"
echo ""
echo " Full docs: https://github.com/alexgreensh/token-optimizer"
echo ""