-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·3140 lines (2787 loc) · 104 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·3140 lines (2787 loc) · 104 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -euo pipefail
export PYTHONDONTWRITEBYTECODE=1
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
BOLD=$'\033[1m'
DIM=$'\033[2m'
CYAN=$'\033[0;36m'
GREEN=$'\033[0;32m'
YELLOW=$'\033[0;33m'
RED=$'\033[0;31m'
BLUE=$'\033[0;34m'
MAGENTA=$'\033[0;35m'
RESET=$'\033[0m'
else
BOLD='' DIM='' CYAN='' GREEN='' YELLOW='' RED='' BLUE='' MAGENTA='' RESET=''
fi
info() {
printf "${CYAN}ℹ${RESET} %s\n" "$1"
}
success() {
printf "${GREEN}✓${RESET} %s\n" "$1"
}
error() {
printf "${RED}✗${RESET} %s\n" "$1"
}
warn() {
printf "${YELLOW}!${RESET} %s\n" "$1"
}
step() {
printf " ${CYAN}•${RESET} %s\n" "$1"
}
print_flow_title() {
local command="macroscope install"
if [ "$INSTALL_MODE" = "update" ]; then
command="macroscope update"
fi
printf "\n${BOLD}%s${RESET}\n" "$command"
}
flow_section() {
local index="$1"
local title="$2"
local description="$3"
local header="── [${index}/2] ${title} "
local fill=$((60 - ${#header}))
local rule=""
[ "$fill" -ge 2 ] || fill=2
while [ "${#rule}" -lt "$fill" ]; do
rule="${rule}─"
done
printf "\n${CYAN}${BOLD}%s%s${RESET}\n" "$header" "$rule"
printf "${DIM} %s.${RESET}\n" "$description"
}
render_download_progress_frame() {
local percent="$1"
local label="$2"
local width=20
local filled_count=0
local empty_count=0
local filled=""
local empty=""
local index=0
[ "$percent" -ge 0 ] || percent=0
[ "$percent" -le 100 ] || percent=100
filled_count=$((percent * width / 100))
empty_count=$((width - filled_count))
while [ "$index" -lt "$filled_count" ]; do
filled="${filled}█"
index=$((index + 1))
done
index=0
while [ "$index" -lt "$empty_count" ]; do
empty="${empty}░"
index=$((index + 1))
done
printf "\r\033[2K ${CYAN}%s${RESET}${DIM}%s${RESET} ${BOLD}%3d%%${RESET} %s" \
"$filled" "$empty" "$percent" "$label" >&2
}
# render_download_progress LABEL
# Translates curl's live --progress-bar percentages into the installer's compact
# visual language. Curl remains the source of truth: this renderer never
# advances or completes the bar without receiving a percentage from curl.
render_download_progress() {
local label="$1"
local frame=""
local before_percent=""
local percent_token=""
local percent=""
local progress_visible=1
render_download_progress_frame 0 "$label"
while IFS= read -r -d $'\r' frame || [ -n "$frame" ]; do
frame="${frame//$'\n'/}"
case "$frame" in
*curl:\ *)
printf "\r\033[2Kcurl: %s\n" "${frame#*curl: }" >&2
progress_visible=0
;;
*%*)
before_percent="${frame%\%*}"
percent_token="${before_percent##* }"
percent="${percent_token%%.*}"
case "$percent" in
""|*[!0-9]*) continue ;;
esac
render_download_progress_frame "$percent" "$label"
progress_visible=1
;;
esac
frame=""
done
if [ "$progress_visible" -eq 1 ]; then
printf "\n" >&2
fi
}
# download_with_progress URL DESTINATION LABEL
# Uses curl's native meter outside a terminal. In a terminal, a FIFO lets the
# renderer consume curl's real-time percentages without hiding curl failures or
# changing curl's exit status.
download_with_progress() {
local url="$1"
local destination="$2"
local label="$3"
local progress_fifo=""
local renderer_pid=""
local curl_status=0
if [ ! -t 2 ]; then
curl -fL --proto '=https' --proto-redir '=https' --progress-bar "$url" -o "$destination"
return
fi
progress_fifo="${TMP_DIR}/curl-progress-$$"
if ! mkfifo "$progress_fifo"; then
curl -fL --proto '=https' --proto-redir '=https' --progress-bar "$url" -o "$destination"
return
fi
render_download_progress "$label" < "$progress_fifo" &
renderer_pid=$!
if curl -fL --proto '=https' --proto-redir '=https' --progress-bar "$url" -o "$destination" 2> "$progress_fifo"; then
curl_status=0
else
curl_status=$?
fi
wait "$renderer_pid" || true
rm -f "$progress_fifo"
return "$curl_status"
}
INSTALLED_BINARY=""
INSTALL_VERSION=""
INSTALLED_VERSION=""
TMP_DIR=""
CHECKOUT_DIR=""
PLUGIN_VERSION=""
INSTALL_DIR=""
CONFIG_SEEDED=0
CODEX_SHIM_INSTALLED=0
CODEX_PLUGIN_HOST_WARNING=""
CODEX_LOCAL_PLUGIN_VERSION="local"
CODEX_BUNDLED_BINARY=""
CODEX_SHIM_PATH=""
DRY_RUN=0
ASSUME_YES=0
TOOLS_SPEC=""
SELECTED_TOOLS=""
SKIP_PATH=0
SHELL_CONFIG_OVERRIDE=""
WIZARD_MODE="default"
INSTALL_MODE=""
OUTPUT_FORMAT="text"
RESUME_COMMAND=0
STATE_FILE=""
STATE_LOADED=0
STATE_CONFIGURED=0
STATE_TOOLS=""
STATE_PATH_FILE=""
STATE_PATH_POLICY=""
SAVED_AUTO_UPDATE=0
PATH_ACTION="skip"
PATH_TARGET=""
PATH_POLICY="auto"
APPLY_STARTED=0
APPLY_COMPLETE=0
ROLLBACK_LOG=""
SAVED_TTY_STATE=""
# Integrity verification of downloaded release artifacts against the SHA-256
# GitHub reports for each release asset.
# REQUIRE_CHECKSUM=1 fails closed when GitHub reports no SHA-256 for an asset.
# Default (0) applies a loud, transitional grace in that case; a reported
# SHA-256 that does NOT match is always fatal regardless of this setting.
REQUIRE_CHECKSUM="${MACROSCOPE_REQUIRE_CHECKSUM:-0}"
RELEASE_METADATA=""
RELEASE_METADATA_STATE=""
usage() {
cat <<'EOF'
Usage: install.sh [version] [options]
Options:
--dry-run Print the complete plan without changing files
--tools claude,codex,cursor,opencode|all|none
Select host integrations
--no-path Never edit shell configuration (remembered for updates)
--shell-config PATH Edit exactly this shell configuration file
--wizard Launch setup after installation
--no-wizard Do not launch setup
--yes Apply the displayed plan without confirmation
--format text|json Select completion output format
--mode initial|update Set install lifecycle (normally auto-detected)
-h, --help Show this help
EOF
}
parse_options() {
while [ "$#" -gt 0 ]; do
case "$1" in
--dry-run) DRY_RUN=1 ;;
--yes|-y) ASSUME_YES=1 ;;
--resume-command) RESUME_COMMAND=1 ;;
--no-path) SKIP_PATH=1 ;;
--wizard) WIZARD_MODE="yes" ;;
--no-wizard) WIZARD_MODE="no" ;;
--tools|--host-permissions|--shell-config|--format|--mode)
if [ "$#" -lt 2 ]; then
error "$1 requires a value"
exit 2
fi
case "$1" in
--tools) TOOLS_SPEC="$2" ;;
# Accepted as a no-op so older CLI binaries can still hand off to
# this installer during their mandatory update.
--host-permissions) ;;
--shell-config) SHELL_CONFIG_OVERRIDE="$2" ;;
--format) OUTPUT_FORMAT="$2" ;;
--mode) INSTALL_MODE="$2" ;;
esac
shift
;;
--tools=*|--host-permissions=*|--shell-config=*|--format=*|--mode=*)
case "$1" in
--tools=*) TOOLS_SPEC="${1#*=}" ;;
--host-permissions=*) ;;
--shell-config=*) SHELL_CONFIG_OVERRIDE="${1#*=}" ;;
--format=*) OUTPUT_FORMAT="${1#*=}" ;;
--mode=*) INSTALL_MODE="${1#*=}" ;;
esac
;;
-h|--help) usage; exit 0 ;;
--)
shift
if [ "$#" -gt 1 ] || { [ "$#" -eq 1 ] && [ -n "$INSTALL_VERSION" ]; }; then
error "Unexpected argument: ${2:-$1}"
exit 2
fi
if [ "$#" -eq 1 ]; then
INSTALL_VERSION="$1"
fi
break
;;
-*) error "Unknown option: $1"; usage >&2; exit 2 ;;
*)
if [ -n "$INSTALL_VERSION" ]; then
error "Unexpected argument: $1"
exit 2
fi
INSTALL_VERSION="$1"
;;
esac
shift
done
case "$OUTPUT_FORMAT" in text|json) ;; *) error "--format must be text or json"; exit 2 ;; esac
case "$INSTALL_MODE" in ""|initial|update) ;; *) error "--mode must be initial or update"; exit 2 ;; esac
if [ -n "$SHELL_CONFIG_OVERRIDE" ] && [ "$SKIP_PATH" -eq 1 ]; then
error "--shell-config and --no-path cannot be used together"
exit 2
fi
}
state_file_path() {
if [ -n "${XDG_STATE_HOME:-}" ]; then
printf '%s/macroscope/install.json' "$XDG_STATE_HOME"
else
printf '%s/.local/state/macroscope/install.json' "$HOME"
fi
}
load_install_state() {
STATE_FILE="$(state_file_path)"
[ -f "$STATE_FILE" ] || return 0
local values=""
values="$(python3 - "$STATE_FILE" <<'PY'
import json, sys
try:
with open(sys.argv[1], encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data, dict):
raise TypeError("state must be an object")
tools = data.get("tools", [])
path_file = data.get("pathFile")
path_policy = data.get("pathPolicy")
configured = (
"tools" in data
and "pathPolicy" in data
and all(tool in ("claude", "codex", "cursor", "opencode") for tool in tools)
and path_policy in ("auto", "managed", "skip")
)
if not isinstance(tools, list) or not all(isinstance(tool, str) for tool in tools):
raise TypeError("tools must be a string array")
if path_file is not None and not isinstance(path_file, str):
raise TypeError("pathFile must be a string or null")
if path_policy is None:
# Preserve the behavior of legacy state. Only the new installer can
# record an explicit, durable --no-path choice.
path_policy = "managed" if path_file else "auto"
if path_policy not in ("auto", "managed", "skip"):
raise TypeError("pathPolicy must be auto, managed, or skip")
except Exception:
print("")
print("")
print("")
print("invalid")
print("incomplete")
raise SystemExit
print(",".join(tools))
print(path_file or "")
print(path_policy)
print("valid")
print("configured" if configured else "incomplete")
PY
)"
STATE_TOOLS="$(printf '%s\n' "$values" | sed -n '1p')"
STATE_PATH_FILE="$(printf '%s\n' "$values" | sed -n '2p')"
STATE_PATH_POLICY="$(printf '%s\n' "$values" | sed -n '3p')"
[ "$(printf '%s\n' "$values" | sed -n '4p')" = "valid" ] && STATE_LOADED=1
[ "$(printf '%s\n' "$values" | sed -n '5p')" = "configured" ] && STATE_CONFIGURED=1
return 0
}
detect_installed_tools() {
local detected=""
[ -d "$(get_claude_config_dir)/plugins/cache/macroscope-local" ] && detected="claude"
[ -d "$(get_codex_home)/plugins/cache/local-user-plugins/macroscope" ] || [ -d "$HOME/plugins/macroscope" ] && detected="${detected:+$detected,}codex"
[ -d "$HOME/.cursor/plugins/local/macroscope" ] && detected="${detected:+$detected,}cursor"
if [ -f "$(get_opencode_config_dir)/plugins/macroscope.js" ]; then
detected="${detected:+$detected,}opencode"
fi
printf '%s' "$detected"
}
normalize_tools() {
local value="${1// /}"
value="$(printf '%s' "$value" | tr '[:upper:]' '[:lower:]')"
case "$value" in
all|'') printf 'claude,codex,cursor,opencode'; return ;;
none) printf ''; return ;;
esac
python3 - "$value" <<'PY'
import sys
allowed = ["claude", "codex", "cursor", "opencode"]
requested = [x for x in sys.argv[1].split(",") if x]
unknown = sorted(set(requested) - set(allowed))
if unknown:
print("invalid:" + ",".join(unknown))
else:
print(",".join(x for x in allowed if x in requested))
PY
}
tool_selected() {
case ",$SELECTED_TOOLS," in *",$1,"*) return 0 ;; *) return 1 ;; esac
}
tool_plan_path() {
case "$1" in
claude) printf '%s/plugins/cache/macroscope-local/ and plugin registration in %s/settings.json' "$(get_claude_config_dir)" "$(get_claude_config_dir)" ;;
codex) printf '%s/plugins/macroscope, %s/plugins/cache/, and %s/config.toml' "$HOME" "$(get_codex_home)" "$(get_codex_home)" ;;
cursor) printf '%s/.cursor/plugins/local/macroscope/' "$HOME" ;;
opencode) printf '%s/' "$(get_opencode_config_dir)" ;;
esac
}
tool_install_plan_path() {
case "$1" in
claude) printf '%s/plugins/cache/macroscope-local/ and %s/settings.json' "$(get_claude_config_dir)" "$(get_claude_config_dir)" ;;
*) tool_plan_path "$1" ;;
esac
}
selected_tools_plan_label() {
local tools=()
local tool=""
local index=0
local last=0
for tool in claude codex cursor opencode; do
tool_selected "$tool" && tools+=("$tool")
done
last=$((${#tools[@]} - 1))
for index in "${!tools[@]}"; do
if [ "$index" -eq 0 ]; then
printf '%s' "${tools[$index]}"
elif [ "$index" -eq "$last" ]; then
printf ' and %s' "${tools[$index]}"
else
printf ', %s' "${tools[$index]}"
fi
done
}
tool_installed() {
case "$1" in
claude) [ -d "$(get_claude_config_dir)/plugins/cache/macroscope-local" ] ;;
codex) [ -d "$HOME/plugins/macroscope" ] || find "$(get_codex_home)/plugins/cache" -type d -path '*/macroscope/local' -print -quit 2>/dev/null | grep -q . ;;
cursor) [ -d "$HOME/.cursor/plugins/local/macroscope" ] ;;
opencode) [ -f "$(get_opencode_config_dir)/plugins/macroscope.js" ] ;;
*) return 1 ;;
esac
}
has_interactive_tty() {
[ "${MACROSCOPE_TEST_NONINTERACTIVE:-0}" != "1" ] || return 1
# /dev/tty can exist and appear readable/writable even when this process has
# no controlling terminal. In that case reads fail and an empty answer would
# otherwise be interpreted as the default "yes" confirmation.
( test -t 0 < /dev/tty ) 2>/dev/null
}
TUI_RESULT=""
TUI_KEY=""
tui_start() {
SAVED_TTY_STATE="$(stty -g < /dev/tty 2>/dev/null)" || return 1
stty -echo -icanon min 1 time 0 < /dev/tty
printf '\033[?25l' > /dev/tty
}
tui_stop() {
printf '\033[?25h' > /dev/tty
if [ -n "$SAVED_TTY_STATE" ]; then
stty "$SAVED_TTY_STATE" < /dev/tty 2>/dev/null || true
SAVED_TTY_STATE=""
fi
}
tui_read_key() {
local key=""
local prefix=""
local direction=""
TUI_KEY=""
IFS= read -r -n 1 key < /dev/tty || return 1
if [ "$key" = $'\033' ]; then
stty min 0 time 1 < /dev/tty
prefix="$(dd bs=1 count=1 < /dev/tty 2>/dev/null)"
case "$prefix" in
'['|'O')
direction="$(dd bs=1 count=1 < /dev/tty 2>/dev/null)"
case "$direction" in
A) TUI_KEY="up" ;;
B) TUI_KEY="down" ;;
esac
;;
esac
stty min 1 time 0 < /dev/tty
return 0
fi
case "$key" in
'') TUI_KEY="enter" ;;
' ') TUI_KEY="space" ;;
*) TUI_KEY="$key" ;;
esac
}
# prompt_menu renders a single-select vertical menu and sets TUI_RESULT to the
# zero-based index of the selected entry.
prompt_menu() {
local question="$1"; shift
local labels=("$@")
local count="${#labels[@]}"
local selected=0
local first_render=1
local index=0
local label_color=""
tui_start
printf '\n%s%s?%s %s%s%s\n' "$GREEN" "$BOLD" "$RESET" "$BOLD" "$question" "$RESET" > /dev/tty
printf ' %sUp/down or j/k to move; enter to choose.%s\n' "$DIM" "$RESET" > /dev/tty
while true; do
if [ "$first_render" -eq 0 ]; then
printf '\033[%dA' "$count" > /dev/tty
fi
first_render=0
index=0
while [ "$index" -lt "$count" ]; do
printf '\r\033[2K' > /dev/tty
case "${labels[$index]}" in
Yes) label_color="$GREEN" ;;
"Show exact changes") label_color="$YELLOW" ;;
*) label_color="$DIM" ;;
esac
if [ "$index" -eq "$selected" ]; then
printf '%s%s>%s %s%s%s%s\n' "$CYAN" "$BOLD" "$RESET" "$label_color" "$BOLD" "${labels[$index]}" "$RESET" > /dev/tty
else
printf ' %s%s%s\n' "$label_color" "${labels[$index]}" "$RESET" > /dev/tty
fi
index=$((index + 1))
done
if ! tui_read_key; then
tui_stop
return 1
fi
case "$TUI_KEY" in
up|k|K) selected=$(((selected + count - 1) % count)) ;;
down|j|J) selected=$(((selected + 1) % count)) ;;
enter) break ;;
esac
done
tui_stop
TUI_RESULT="$selected"
}
prompt_tools() {
local default_tools="$1"
local tools=(claude codex cursor opencode)
local labels=("Claude Code" "Codex" "Cursor" "OpenCode")
local checked=(0 0 0 0)
local selected=0
local first_render=1
local index=0
local result=""
local mark=""
for index in 0 1 2 3; do
case ",$default_tools," in
*",${tools[$index]},"*) checked[$index]=1 ;;
esac
done
tui_start
printf '\n%s%s?%s %sCoding agents%s\n' "$GREEN" "$BOLD" "$RESET" "$BOLD" "$RESET" > /dev/tty
printf ' %sUp/down or j/k to move; space to toggle; enter to continue.%s\n' "$DIM" "$RESET" > /dev/tty
while true; do
if [ "$first_render" -eq 0 ]; then
printf '\033[4A' > /dev/tty
fi
first_render=0
for index in 0 1 2 3; do
printf '\r\033[2K' > /dev/tty
mark=' '
[ "${checked[$index]}" -eq 0 ] || mark='x'
if [ "$index" -eq "$selected" ]; then
if [ "${checked[$index]}" -eq 1 ]; then
printf '%s%s>%s %s%s[%s] %s%s\n' "$CYAN" "$BOLD" "$RESET" "$GREEN" "$BOLD" "$mark" "${labels[$index]}" "$RESET" > /dev/tty
else
printf '%s%s>%s %s[%s] %s%s\n' "$CYAN" "$BOLD" "$RESET" "$BOLD" "$mark" "${labels[$index]}" "$RESET" > /dev/tty
fi
elif [ "${checked[$index]}" -eq 1 ]; then
printf ' %s[%s] %s%s\n' "$GREEN" "$mark" "${labels[$index]}" "$RESET" > /dev/tty
else
printf ' %s[%s] %s%s\n' "$DIM" "$mark" "${labels[$index]}" "$RESET" > /dev/tty
fi
done
if ! tui_read_key; then
tui_stop
return 1
fi
case "$TUI_KEY" in
up|k|K) selected=$(((selected + 3) % 4)) ;;
down|j|J) selected=$(((selected + 1) % 4)) ;;
space) checked[$selected]=$((1 - checked[$selected])) ;;
enter) break ;;
esac
done
tui_stop
for index in 0 1 2 3; do
if [ "${checked[$index]}" -eq 1 ]; then
result="${result:+$result,}${tools[$index]}"
fi
done
TUI_RESULT="${result:-none}"
}
select_tools() {
local default_tools=""
local prompt_default=""
local normalized=""
if [ -n "$TOOLS_SPEC" ]; then
normalized="$(normalize_tools "$TOOLS_SPEC")"
elif [ "$INSTALL_MODE" = "update" ]; then
local detected_tools=""
detected_tools="$(detect_installed_tools)"
if [ "$STATE_LOADED" -eq 1 ]; then
default_tools="$STATE_TOOLS"
if [ -n "$default_tools" ] && [ -n "$detected_tools" ]; then
default_tools="$(normalize_tools "${default_tools}${default_tools:+,}${detected_tools}")"
fi
else
default_tools="$detected_tools"
fi
prompt_default="$default_tools"
if has_interactive_tty && [ "$ASSUME_YES" -eq 0 ] && [ "$DRY_RUN" -eq 0 ] && [ "$SAVED_AUTO_UPDATE" -eq 0 ]; then
if [ -z "$prompt_default" ]; then
printf '\n%s!%s %sNo Macroscope host integrations are currently selected.%s\n' "$YELLOW" "$RESET" "$BOLD" "$RESET" > /dev/tty
printf '%s Select integrations now so a CLI-only install is not preserved accidentally.%s\n' "$DIM" "$RESET" > /dev/tty
prompt_default="claude,codex,cursor,opencode"
else
printf '\n%s•%s Current integrations are selected below.\n' "$CYAN" "$RESET" > /dev/tty
fi
prompt_tools "$prompt_default"
TOOLS_SPEC="$TUI_RESULT"
fi
normalized="$(normalize_tools "${TOOLS_SPEC:-${prompt_default:-none}}")"
else
default_tools="claude,codex,cursor,opencode"
if has_interactive_tty && [ "$ASSUME_YES" -eq 0 ] && [ "$DRY_RUN" -eq 0 ]; then
prompt_tools "$default_tools"
TOOLS_SPEC="$TUI_RESULT"
fi
normalized="$(normalize_tools "${TOOLS_SPEC:-$default_tools}")"
fi
case "$normalized" in invalid:*) error "Unknown tool(s): ${normalized#invalid:}"; exit 2 ;; esac
SELECTED_TOOLS="$normalized"
}
active_path_contains_install_dir() {
case ":${PATH:-}:" in *":$HOME/.local/bin:"*) return 0 ;; *) return 1 ;; esac
}
login_shell_name() {
local shell_path="${SHELL:-}"
if [ -z "$shell_path" ] && command -v dscl >/dev/null 2>&1 && [ -n "${USER:-}" ]; then
shell_path="$(dscl . -read "/Users/$USER" UserShell 2>/dev/null | awk '{print $2}')"
fi
if [ -z "$shell_path" ] && command -v getent >/dev/null 2>&1 && [ -n "${USER:-}" ]; then
shell_path="$(getent passwd "$USER" 2>/dev/null | awk -F: '{print $7}')"
fi
basename "${shell_path:-sh}"
}
resolve_path_action() {
PATH_ACTION="skip"
PATH_TARGET=""
PATH_POLICY="auto"
if [ "$SKIP_PATH" -eq 1 ]; then
PATH_POLICY="skip"
return 0
fi
if [ -n "$SHELL_CONFIG_OVERRIDE" ]; then
PATH_POLICY="managed"
PATH_TARGET="$SHELL_CONFIG_OVERRIDE"
case "$PATH_TARGET" in /*) ;; *) PATH_TARGET="$PWD/$PATH_TARGET" ;; esac
PATH_ACTION="modify"
return
fi
if [ "$INSTALL_MODE" = "update" ] && [ "$STATE_LOADED" -eq 1 ]; then
case "$STATE_PATH_POLICY" in
skip)
PATH_POLICY="skip"
return
;;
managed) PATH_POLICY="managed" ;;
auto) PATH_POLICY="auto" ;;
esac
fi
active_path_contains_install_dir && return
if [ "$INSTALL_MODE" = "update" ] && [ "$STATE_LOADED" -eq 1 ] && [ "$STATE_PATH_POLICY" = "managed" ] && [ -n "$STATE_PATH_FILE" ]; then
PATH_TARGET="$STATE_PATH_FILE"
PATH_ACTION="modify"
return
fi
PATH_POLICY="managed"
case "$(login_shell_name)" in
zsh)
if [ -f "$HOME/.zprofile" ]; then PATH_TARGET="$HOME/.zprofile"
elif [ -f "$HOME/.zshrc" ]; then PATH_TARGET="$HOME/.zshrc"
else PATH_TARGET="$HOME/.zprofile"; fi
;;
bash)
if [ -f "$HOME/.bash_profile" ]; then PATH_TARGET="$HOME/.bash_profile"
elif [ -f "$HOME/.bashrc" ]; then PATH_TARGET="$HOME/.bashrc"
else PATH_TARGET="$HOME/.bash_profile"; fi
;;
fish) PATH_TARGET="$HOME/.config/fish/config.fish" ;;
*) PATH_TARGET="$HOME/.profile" ;;
esac
PATH_ACTION="modify"
}
resolve_lifecycle() {
if [ -z "$INSTALL_MODE" ]; then
if [ -x "$HOME/.local/bin/macroscope" ] || [ "$STATE_LOADED" -eq 1 ]; then INSTALL_MODE="update"; else INSTALL_MODE="initial"; fi
fi
if [ "$WIZARD_MODE" = "default" ]; then
if [ "$INSTALL_MODE" = "initial" ]; then WIZARD_MODE="yes"; else WIZARD_MODE="no"; fi
fi
}
# Explicitly approved unattended updates that resume the original command reuse
# saved integration and PATH configuration without rendering the update TUI.
# Human invocations, dry runs, overrides, and incomplete legacy state keep the
# existing plan and confirmation flow.
resolve_saved_auto_update() {
SAVED_AUTO_UPDATE=0
[ "$INSTALL_MODE" = "update" ] || return 0
[ "$RESUME_COMMAND" -eq 1 ] || return 0
[ "$ASSUME_YES" -eq 1 ] || return 0
[ "$STATE_CONFIGURED" -eq 1 ] || return 0
[ -z "$TOOLS_SPEC" ] || return 0
[ "$SKIP_PATH" -eq 0 ] || return 0
[ -z "$SHELL_CONFIG_OVERRIDE" ] || return 0
[ "$DRY_RUN" -eq 0 ] || return 0
SAVED_AUTO_UPDATE=1
}
shell_config_line() {
local install_bin="$HOME/.local/bin"
local target_shell=""
case "$PATH_TARGET" in
*/config.fish) target_shell="fish" ;;
*.zshrc|*.zprofile|*.bashrc|*.bash_profile|*/.profile) target_shell="posix" ;;
*) target_shell="$(login_shell_name)" ;;
esac
if [ "$target_shell" = "fish" ]; then
printf 'set -Ux fish_user_paths %s $fish_user_paths' "$install_bin"
else
printf 'export PATH="%s:$PATH"' "$install_bin"
fi
}
print_plan() {
local index=1
local verb="Install"
local plan_label="installation"
[ "$INSTALL_MODE" = "update" ] && verb="Replace"
[ "$INSTALL_MODE" = "update" ] && plan_label="update"
printf '\n%sMacroscope %s will:%s\n' "$BOLD" "$plan_label" "$RESET"
printf '%d. %s %s/.local/bin/macroscope\n' "$index" "$verb" "$HOME"; index=$((index + 1))
if [ "$PATH_ACTION" = "modify" ]; then
printf '%d. Add %s/.local/bin to PATH in %s\n' "$index" "$HOME" "$PATH_TARGET"
elif active_path_contains_install_dir; then
printf '%d. Keep PATH unchanged (%s/.local/bin is already active)\n' "$index" "$HOME"
elif [ "$PATH_POLICY" = "skip" ]; then
if [ "$SKIP_PATH" -eq 1 ]; then
printf '%d. Keep shell configuration unchanged (remember --no-path for future updates)\n' "$index"
else
printf '%d. Keep shell configuration unchanged (remembered --no-path; use --shell-config PATH to manage it)\n' "$index"
fi
else
printf '%d. Keep shell configuration unchanged\n' "$index"
fi
index=$((index + 1))
local tool=""
if [ -n "$SELECTED_TOOLS" ]; then
local plugin_noun="plugins"
[ "${SELECTED_TOOLS#*,}" = "$SELECTED_TOOLS" ] && plugin_noun="plugin"
printf '%d. Install or update the following %s for %s\n' "$index" "$plugin_noun" "$(selected_tools_plan_label)"
for tool in claude codex cursor opencode; do
tool_selected "$tool" && printf ' (%s)\n' "$(tool_install_plan_path "$tool")"
done
index=$((index + 1))
if tool_selected codex && codex_shim_will_install; then
printf '%d. Install or update the managed Codex CLI wrapper at %s/.local/bin/codex\n' "$index" "$HOME"
index=$((index + 1))
fi
fi
for tool in claude codex cursor opencode; do
if ! tool_selected "$tool" && [ "$INSTALL_MODE" = "update" ] && tool_installed "$tool"; then
printf '%d. Remove Macroscope-owned %s integration state at %s (deselected)\n' "$index" "$tool" "$(tool_plan_path "$tool")"
index=$((index + 1))
fi
done
if [ "$INSTALL_MODE" = "update" ]; then
printf '%d. Clean legacy Macroscope MCP artifacts after the update is staged\n' "$index"; index=$((index + 1))
fi
if { [ -n "${MACROSCOPE_LOCAL_BACK_REPO:-}" ] || [ -n "${MACROSCOPE_LOCAL_BINARY_SOURCE:-}" ]; } && [ ! -f "$HOME/.macroscope/config.yaml" ]; then
printf '%d. Seed local-build configuration at %s/.macroscope/config.yaml\n' "$index" "$HOME"; index=$((index + 1))
fi
if [ "$WIZARD_MODE" = "yes" ]; then
printf '%d. Launch the setup wizard\n' "$index"
fi
}
print_change_details() {
local binary_action="Install"
local tool=""
local claude_config=""
local codex_home=""
local codex_marketplace=""
local codex_plugin_key=""
local opencode_config=""
local quoted_codex_binary=""
local default_env="${MACROSCOPE_DEFAULT_ENV:-prod}"
[ "$INSTALL_MODE" = "update" ] && binary_action="Replace"
claude_config="$(get_claude_config_dir)"
codex_home="$(get_codex_home)"
codex_marketplace="$(get_codex_marketplace_name)"
codex_plugin_key="macroscope@$codex_marketplace"
opencode_config="$(get_opencode_config_dir)"
case "$default_env" in prod|nonprod|local) ;; *) default_env="prod" ;; esac
if tool_selected codex && codex_shim_will_install; then
quoted_codex_binary="$(python3 - "$CODEX_BUNDLED_BINARY" <<'PY'
import shlex, sys
print(shlex.quote(sys.argv[1]))
PY
)"
fi
_ccd_file() { printf '\n %s%s%s%s\n' "$BOLD" "$CYAN" "$1" "$RESET"; }
_ccd_key() { printf ' %s:\n' "$1"; }
_ccd_add() { printf ' %s+ %s%s\n' "$GREEN" "$1" "$RESET"; }
_ccd_remove() { printf ' %s- %s%s\n' "$RED" "$1" "$RESET"; }
{
printf '\n%sExact changes%s\n' "$BOLD" "$RESET"
printf ' %sExisting settings are preserved; the snippets below are merged, refreshed, or removed.%s\n' "$DIM" "$RESET"
_ccd_file "$HOME/.local/bin/macroscope"
if [ -n "${MACROSCOPE_LOCAL_BACK_REPO:-}" ]; then
_ccd_add "$binary_action with a binary built from $MACROSCOPE_LOCAL_BACK_REPO"
elif [ -n "${MACROSCOPE_LOCAL_BINARY_SOURCE:-}" ]; then
_ccd_add "$binary_action from $MACROSCOPE_LOCAL_BINARY_SOURCE"
else
_ccd_add "$binary_action the $INSTALL_VERSION release for $OS-$ARCH"
fi
if [ "$PATH_ACTION" = "modify" ]; then
_ccd_file "$PATH_TARGET"
_ccd_add '# Added by Macroscope installer'
_ccd_add "$(shell_config_line)"
fi
for tool in claude codex cursor opencode; do
if tool_selected "$tool"; then
case "$tool" in
claude)
_ccd_file "$claude_config/settings.json"
_ccd_key 'extraKnownMarketplaces.macroscope-local'
_ccd_add '{'
_ccd_add ' "source": {'
_ccd_add ' "source": "directory",'
_ccd_add " \"path\": \"$claude_config/plugins/marketplaces/macroscope-local\""
_ccd_add ' }'
_ccd_add '}'
_ccd_key 'enabledPlugins'
_ccd_add '"macroscope@macroscope-local": true'
_ccd_file "$claude_config/plugins/marketplaces/macroscope-local/"
_ccd_add 'Macroscope marketplace and plugin bundle'
_ccd_file "$claude_config/plugins/cache/macroscope-local/"
_ccd_add 'Macroscope plugin cache'
;;
codex)
_ccd_file "$HOME/.agents/plugins/marketplace.json"
_ccd_key 'plugins[]'
_ccd_add '{'
_ccd_add ' "name": "macroscope",'
_ccd_add ' "source": {"source": "local", "path": "./plugins/macroscope"},'
_ccd_add ' "policy": {'
_ccd_add ' "installation": "INSTALLED_BY_DEFAULT",'
_ccd_add ' "authentication": "ON_USE"'
_ccd_add ' },'
_ccd_add ' "category": "Development"'
_ccd_add '}'
_ccd_file "$codex_home/config.toml"
_ccd_add '[features]'
_ccd_add 'plugins = true'
_ccd_add "[plugins.\"$codex_plugin_key\"]"
_ccd_add 'enabled = true'
_ccd_file "$HOME/plugins/macroscope/"
_ccd_add 'Macroscope plugin source'
_ccd_file "$codex_home/plugins/cache/$codex_marketplace/macroscope/$CODEX_LOCAL_PLUGIN_VERSION/"
_ccd_add 'Macroscope plugin cache'
if [ -n "$quoted_codex_binary" ]; then
_ccd_file "$HOME/.local/bin/codex"
_ccd_add '#!/bin/bash'
_ccd_add 'set -euo pipefail'
_ccd_add '# Macroscope-managed Codex shim'
_ccd_add "exec $quoted_codex_binary \"\$@\""
fi
;;
cursor)
_ccd_file "$HOME/.cursor/plugins/local/macroscope/"
_ccd_add 'Macroscope plugin bundle'
;;
opencode)
_ccd_file "$opencode_config"
_ccd_add 'plugins/macroscope.js'
_ccd_add 'commands/macroscope-codereview.md'
_ccd_add 'commands/macroscope-autoloop.md'
_ccd_add 'skills/codereview/'
_ccd_add 'skills/autoloop/'
;;
esac
elif [ "$INSTALL_MODE" = "update" ] && tool_installed "$tool"; then
case "$tool" in
claude)
_ccd_file "$claude_config/settings.json"
_ccd_key 'extraKnownMarketplaces'
_ccd_remove '"macroscope-local"'
_ccd_key 'enabledPlugins'
_ccd_remove '"macroscope@macroscope-local"'
_ccd_file "$claude_config/plugins/"
_ccd_remove 'marketplaces/macroscope-local/'
_ccd_remove 'cache/macroscope-local/'
;;
codex)
_ccd_file "$HOME/.agents/plugins/marketplace.json"
_ccd_key 'plugins[]'
_ccd_remove 'entry with "name": "macroscope"'
_ccd_file "$codex_home/config.toml"
_ccd_remove "[plugins.\"$codex_plugin_key\"]"
_ccd_file "$HOME/plugins/"
_ccd_remove 'macroscope/'
_ccd_file "$codex_home/plugins/cache/"
_ccd_remove "$codex_marketplace/macroscope/$CODEX_LOCAL_PLUGIN_VERSION/"
;;
cursor)
_ccd_file "$HOME/.cursor/plugins/local/"
_ccd_remove 'macroscope/'
;;
opencode)
_ccd_file "$opencode_config"
_ccd_remove 'plugins/macroscope.js'
_ccd_remove 'commands/macroscope-codereview.md'
_ccd_remove 'commands/macroscope-autoloop.md'
_ccd_remove 'skills/codereview/'
_ccd_remove 'skills/autoloop/'
;;
esac
fi
done
if [ "$INSTALL_MODE" = "update" ]; then
_ccd_file 'Legacy MCP integration (when present)'
_ccd_remove "$HOME/.local/bin/macroscope-mcp"
_ccd_key "$(get_claude_state_file): mcpServers / projects.*.mcpServers"
_ccd_remove '"macroscope-codereview"'
_ccd_key "$HOME/.cursor/mcp.json: mcpServers"
_ccd_remove '"macroscope-codereview"'
_ccd_key "$codex_home/config.toml"
_ccd_remove '[mcp_servers.macroscope-codereview]'
fi
if { [ -n "${MACROSCOPE_LOCAL_BACK_REPO:-}" ] || [ -n "${MACROSCOPE_LOCAL_BINARY_SOURCE:-}" ]; } && [ ! -f "$HOME/.macroscope/config.yaml" ]; then
_ccd_file "$HOME/.macroscope/config.yaml"
_ccd_add "env: $default_env"
_ccd_add 'envs: {}'
fi
if [ "$WIZARD_MODE" = "yes" ]; then
_ccd_file 'After installation'
_ccd_add 'Launch the setup wizard after verification'
fi
} > /dev/tty
unset -f _ccd_file _ccd_key _ccd_add _ccd_remove
}
confirm_plan() {
[ "$DRY_RUN" -eq 0 ] || return 0
[ "$SAVED_AUTO_UPDATE" -eq 0 ] || return 0
[ "$ASSUME_YES" -eq 0 ] || return 0
if ! has_interactive_tty; then
error "A terminal is required for confirmation. Re-run with --yes after reviewing --dry-run."
return 3
fi
local prompt='Proceed?'
[ "$INSTALL_MODE" = "update" ] && prompt='Update and continue?'
[ "$RESUME_COMMAND" -eq 1 ] && prompt='Update and run the review?'
while true; do
if ! prompt_menu "$prompt" "Yes" "Show exact changes" "No"; then