-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·243 lines (228 loc) · 11 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·243 lines (228 loc) · 11 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
#!/usr/bin/env bash
set -euo pipefail
SOURCE=$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
CODEX_DIR=${CODEX_HOME:-$HOME/.codex}
BIN_DIR=${CORALLINE_BIN_DIR:-$HOME/.local/bin}
MODE=install
SHELL_HOOK=preserve
MIN_CODEX_VERSION=0.145.0
DEFAULT_NATIVE_FIELDS='model-with-reasoning run-state context-remaining five-hour-limit weekly-limit used-tokens git-branch branch-changes fast-mode task-progress'
canonical_path() {
python3 - "$1" <<'PY'
from pathlib import Path
import sys
print(Path(sys.argv[1]).resolve())
PY
}
usage() {
cat <<'EOF'
usage: install.sh [--codex-home PATH] [--bin-dir PATH] [--update|--uninstall]
[--shell-hook auto|bash|zsh|none]
Installs from this checked-out source tree. No remote script execution is used.
EOF
}
while (($#)); do
case $1 in
--codex-home) shift; CODEX_DIR=${1:?missing path} ;;
--bin-dir) shift; BIN_DIR=${1:?missing path} ;;
--update) MODE=update ;;
--uninstall) MODE=uninstall ;;
--shell-hook) shift; SHELL_HOOK=${1:?missing shell hook mode} ;;
-h | --help) usage; exit 0 ;;
*) printf 'install: unknown option: %s\n' "$1" >&2; exit 2 ;;
esac
shift
done
case $SHELL_HOOK in preserve | auto | bash | zsh | none) ;; *) printf 'install: invalid shell hook mode: %s\n' "$SHELL_HOOK" >&2; exit 2 ;; esac
INSTALL_DIR=$CODEX_DIR/coralline-codex
CONFIG=$CODEX_DIR/coralline-codex.conf
THEME_DIR=$CODEX_DIR/themes
BACKUP_ROOT=$CODEX_DIR/coralline-codex-backups
BIN=$BIN_DIR/coralline-codex
EXPECTED_BIN=$(canonical_path "$INSTALL_DIR/bin/coralline-codex")
WINDOWS_SHELL=0
case $(uname -s) in MINGW* | MSYS* | CYGWIN*) WINDOWS_SHELL=1 ;; esac
MANAGED_SHIM_MARKER='# coralline-codex managed Git Bash shim'
bin_is_managed() {
if [ -L "$BIN" ]; then
[ "$(canonical_path "$BIN")" = "$EXPECTED_BIN" ]
elif ((WINDOWS_SHELL)) && [ -f "$BIN" ]; then
grep -Fqx "$MANAGED_SHIM_MARKER" "$BIN"
else
return 1
fi
}
install_command() {
if ((WINDOWS_SHELL)); then
{
printf '#!/usr/bin/env bash\n%s\nexec ' "$MANAGED_SHIM_MARKER"
printf '%q ' "$INSTALL_DIR/bin/coralline-codex"
printf '"$@"\n'
} > "$BIN"
chmod 755 "$BIN"
else
ln -sfn -- "$INSTALL_DIR/bin/coralline-codex" "$BIN"
fi
}
previous_version=
[ -f "$INSTALL_DIR/VERSION" ] && IFS= read -r previous_version < "$INSTALL_DIR/VERSION"
stamp=$(date +%Y%m%d-%H%M%S)
BACKUP_DIR=$BACKUP_ROOT/$stamp
counter=1
while [ -e "$BACKUP_DIR" ]; do
BACKUP_DIR=$BACKUP_ROOT/$stamp.$counter
counter=$((counter + 1))
done
if [ "$MODE" = uninstall ]; then
[ -d "$INSTALL_DIR" ] || { printf 'coralline-codex is not installed in %s\n' "$CODEX_DIR"; exit 0; }
mkdir -p "$BACKUP_DIR"
if [ -x "$INSTALL_DIR/lib/shell_integration.py" ]; then
python3 "$INSTALL_DIR/lib/shell_integration.py" uninstall \
--codex-home "$CODEX_DIR" --backup-dir "$BACKUP_ROOT/shell" || true
fi
if [ -f "$CONFIG" ]; then mv -- "$CONFIG" "$BACKUP_DIR/coralline-codex.conf"; fi
mkdir -p "$BACKUP_DIR/themes"
while IFS=$'\t' read -r theme_name _; do
[[ $theme_name == \#* || -z $theme_name ]] && continue
theme=$THEME_DIR/coralline-$theme_name.tmTheme
[ -e "$theme" ] && mv -- "$theme" "$BACKUP_DIR/themes/"
done < "$INSTALL_DIR/themes/palettes.tsv"
if bin_is_managed; then rm -f -- "$BIN"; fi
mv -- "$INSTALL_DIR" "$BACKUP_DIR/install"
printf 'Uninstalled Coralline Codex. Recoverable backup: %s\n' "$BACKUP_DIR"
printf 'Codex config.toml was not changed.\n'
exit 0
fi
for dependency in bash python3 codex; do
command -v "$dependency" >/dev/null 2>&1 || { printf 'install: required command not found: %s\n' "$dependency" >&2; exit 1; }
done
codex_version=$(codex --version 2>/dev/null) ||
{ printf 'install: failed to run Codex\n' >&2; exit 1; }
if [[ $codex_version =~ ([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
codex_major=${BASH_REMATCH[1]}; codex_minor=${BASH_REMATCH[2]}
else
printf 'install: could not parse Codex version: %s\n' "$codex_version" >&2
exit 1
fi
if ((codex_major == 0 && codex_minor < 145)); then
printf 'install: Codex %s or newer is required (found %s)\n' "$MIN_CODEX_VERSION" "${BASH_REMATCH[0]}" >&2
exit 1
fi
mkdir -p "$CODEX_DIR" "$BIN_DIR" "$THEME_DIR"
if { [ -e "$BIN" ] || [ -L "$BIN" ]; } && ! bin_is_managed; then
printf 'install: refusing to overwrite unrelated path: %s\n' "$BIN" >&2
exit 1
fi
if [ ! -d "$INSTALL_DIR" ]; then
while IFS=$'\t' read -r theme_name _; do
[[ $theme_name == \#* || -z $theme_name ]] && continue
if [ -e "$THEME_DIR/coralline-$theme_name.tmTheme" ]; then
printf 'install: refusing to overwrite unrelated theme: %s\n' "$THEME_DIR/coralline-$theme_name.tmTheme" >&2
exit 1
fi
done < "$SOURCE/themes/palettes.tsv"
fi
stage=$(mktemp -d "$CODEX_DIR/.coralline-codex-stage.XXXXXX")
cleanup() { if [ -n "$stage" ] && [ -d "$stage" ]; then rm -rf -- "$stage"; fi; }
trap cleanup EXIT
mkdir -p "$stage/bin" "$stage/lib" "$stage/themes" "$stage/tools" "$stage/test" "$stage/docs" "$stage/assets"
for file in VERSION CHANGELOG.md CONTRIBUTING.md LICENSE NOTICE.md README.md README.zh-TW.md SECURITY.md install.sh install.ps1 configure.sh configure.ps1; do
[ -f "$SOURCE/$file" ] && cp -p -- "$SOURCE/$file" "$stage/"
done
cp -p -- "$SOURCE/bin/coralline-codex" "$stage/bin/"
cp -p -- "$SOURCE/bin/coralline-codex.ps1" "$stage/bin/"
cp -p -- "$SOURCE/lib/config.py" "$SOURCE/lib/render.sh" "$SOURCE/lib/shell_integration.py" "$SOURCE/lib/usage.py" "$stage/lib/"
cp -p -- "$SOURCE/themes/palettes.tsv" "$stage/themes/"
cp -p -- "$SOURCE/tools/generate_themes.py" "$stage/tools/"
cp -p -- "$SOURCE/tools/render_assets.py" "$stage/tools/"
cp -p -- "$SOURCE/assets/"*.svg "$stage/assets/"
cp -p -- "$SOURCE/test/verify-install.sh" "$stage/test/"
if [ -d "$SOURCE/docs" ]; then
cp -p -- "$SOURCE/docs/"* "$stage/docs/"
fi
chmod 755 "$stage/bin/coralline-codex" "$stage/lib/render.sh" "$stage/lib/config.py" "$stage/lib/shell_integration.py" "$stage/lib/usage.py" "$stage/tools/generate_themes.py" "$stage/tools/render_assets.py" "$stage/install.sh" "$stage/configure.sh" "$stage/test/verify-install.sh"
python3 "$stage/tools/generate_themes.py" --palettes "$stage/themes/palettes.tsv" --output "$stage/themes/generated"
if [ -d "$INSTALL_DIR" ]; then
mkdir -p "$BACKUP_DIR"
mv -- "$INSTALL_DIR" "$BACKUP_DIR/install"
fi
mv -- "$stage" "$INSTALL_DIR"
stage=
mkdir -p "$THEME_DIR"
for theme in "$INSTALL_DIR/themes/generated/"*.tmTheme; do
target=$THEME_DIR/${theme##*/}
if [ -e "$target" ]; then mkdir -p "$BACKUP_DIR/themes"; cp -p -- "$target" "$BACKUP_DIR/themes/"; fi
cp -p -- "$theme" "$target"
done
if [ ! -f "$CONFIG" ]; then
python3 "$INSTALL_DIR/lib/config.py" merge --config "$CONFIG" --backup-dir "$BACKUP_ROOT" \
CC_THEME=claude-coral CC_STYLE=powerline CC_ASCII=auto CC_NODE=off CC_PYTHON=off \
CC_RUNTIME_PROBE=off 'CC_SEGMENTS=limits burn tokens context dir git stash project node python model reasoning profile elapsed clock' \
CC_NATIVE_STATUS=on "CC_NATIVE_FIELDS=$DEFAULT_NATIVE_FIELDS" \
CC_AGENTS=on CC_AGENT_ROWS=3 CC_USAGE_REFRESH=60 CC_USAGE_STALE_AFTER=180 >/dev/null
elif grep -Fxq "CC_SEGMENTS='dir project git node python model profile elapsed clock'" "$CONFIG"; then
python3 "$INSTALL_DIR/lib/config.py" merge --config "$CONFIG" --backup-dir "$BACKUP_ROOT" \
'CC_SEGMENTS=limits burn tokens context dir git stash project node python model reasoning profile elapsed clock' \
CC_USAGE_REFRESH=60 CC_USAGE_STALE_AFTER=180 >/dev/null
elif grep -Fxq "CC_SEGMENTS='limits tokens dir git project node python model profile elapsed clock'" "$CONFIG"; then
python3 "$INSTALL_DIR/lib/config.py" merge --config "$CONFIG" --backup-dir "$BACKUP_ROOT" \
'CC_SEGMENTS=limits burn tokens context dir git stash project node python model reasoning profile elapsed clock' >/dev/null
elif grep -Fxq "CC_SEGMENTS='limits burn tokens dir git project node python model profile elapsed clock'" "$CONFIG"; then
python3 "$INSTALL_DIR/lib/config.py" merge --config "$CONFIG" --backup-dir "$BACKUP_ROOT" \
'CC_SEGMENTS=limits burn tokens context dir git stash project node python model reasoning profile elapsed clock' >/dev/null
fi
declare -a feature_defaults=()
grep -q '^CC_NATIVE_FIELDS=' "$CONFIG" || feature_defaults+=("CC_NATIVE_FIELDS=$DEFAULT_NATIVE_FIELDS")
grep -q '^CC_AGENTS=' "$CONFIG" || feature_defaults+=(CC_AGENTS=on)
grep -q '^CC_AGENT_ROWS=' "$CONFIG" || feature_defaults+=(CC_AGENT_ROWS=3)
if ((${#feature_defaults[@]})); then
python3 "$INSTALL_DIR/lib/config.py" merge --config "$CONFIG" --backup-dir "$BACKUP_ROOT" "${feature_defaults[@]}" >/dev/null
fi
install_command
if [ "$SHELL_HOOK" = none ]; then
python3 "$INSTALL_DIR/lib/shell_integration.py" uninstall \
--codex-home "$CODEX_DIR" --backup-dir "$BACKUP_ROOT/shell"
elif [ "$SHELL_HOOK" != preserve ]; then
real_codex=$(type -P codex || true)
[ -n "$real_codex" ] || { printf 'install: could not resolve the real Codex binary for shell integration\n' >&2; exit 1; }
shell_rc_args=()
case $SHELL_HOOK in
bash) shell_rc_args=(--rc "$HOME/.bashrc") ;;
zsh) shell_rc_args=(--rc "$HOME/.zshrc") ;;
auto)
case ${SHELL##*/} in
bash) shell_rc_args=(--rc "$HOME/.bashrc") ;;
zsh) shell_rc_args=(--rc "$HOME/.zshrc") ;;
esac ;;
esac
python3 "$INSTALL_DIR/lib/shell_integration.py" install \
--codex-home "$CODEX_DIR" --backup-dir "$BACKUP_ROOT/shell" \
--shell "$SHELL_HOOK" "${shell_rc_args[@]}" --wrapper "$BIN" --codex-bin "$real_codex"
elif [ -f "$CODEX_DIR/coralline-codex-shell.json" ]; then
managed_shell=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1], encoding="utf-8")).get("shell", ""))' \
"$CODEX_DIR/coralline-codex-shell.json")
managed_rc=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1], encoding="utf-8")).get("rc", ""))' \
"$CODEX_DIR/coralline-codex-shell.json")
real_codex=$(type -P codex || true)
if [[ $managed_shell == bash || $managed_shell == zsh ]] && [ -n "$managed_rc" ] && [ -n "$real_codex" ]; then
python3 "$INSTALL_DIR/lib/shell_integration.py" install \
--codex-home "$CODEX_DIR" --backup-dir "$BACKUP_ROOT/shell" \
--shell "$managed_shell" --rc "$managed_rc" --wrapper "$BIN" --codex-bin "$real_codex"
fi
fi
printf 'Coralline Codex %s installed.\n' "$(< "$INSTALL_DIR/VERSION")"
printf ' command: %s\n runtime: %s\n config: %s\n' "$BIN" "$INSTALL_DIR" "$CONFIG"
if [ -d "$BACKUP_DIR" ]; then printf ' backup: %s\n' "$BACKUP_DIR"; else printf ' backup: none (no existing files were replaced)\n'; fi
printf ' Codex config.toml: unchanged (native settings are scoped CLI overrides)\n'
current_version=$(< "$INSTALL_DIR/VERSION")
if [ -n "$previous_version" ] && [ "$previous_version" != "$current_version" ]; then
printf '\nUpdated %s -> %s. New in this release:\n' "$previous_version" "$current_version"
awk -v wanted="$current_version" '
index($0, "## " wanted " ") == 1 { show=1; next }
show && /^## / { exit }
show && /^- / { print " " $0 }
' "$INSTALL_DIR/CHANGELOG.md"
fi
if ! command -v tmux >/dev/null 2>&1; then
printf ' note: tmux is absent; native inline mode works, but --full-companion is unavailable.\n'
fi