Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·1066 lines (992 loc) · 39.4 KB
/
Copy pathinstall
File metadata and controls
executable file
·1066 lines (992 loc) · 39.4 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
#!/usr/bin/env bash
# LocalSetup front-end installer.
# Purpose: keep the stable shell entrypoint while delegating framework work to
# ls/tools/localsetup.py.
set -euo pipefail
CALLER_DIR="$(pwd)"
SOURCE_DIR="."
ATTACH_TARGET_DIR=""
HOME_DIR="${HOME}"
PLATFORMS_CSV=""
PACKS_CSV=""
PRESET=""
SKILLS_CSV=""
SKILL_CLASSES_CSV=""
SKILL_TAGS_CSV=""
EXCLUDE_SKILLS_CSV=""
GLOBAL_PACKS_CSV=""
GLOBAL_PRESET=""
GLOBAL_SKILLS_CSV=""
GLOBAL_SKILL_CLASSES_CSV=""
GLOBAL_SKILL_TAGS_CSV=""
GLOBAL_EXCLUDE_SKILLS_CSV=""
REPO_PACKS_CSV=""
REPO_PRESET=""
REPO_SKILLS_CSV=""
REPO_SKILL_CLASSES_CSV=""
REPO_SKILL_TAGS_CSV=""
REPO_EXCLUDE_SKILLS_CSV=""
MODE="symlink"
YES=false
NON_INTERACTIVE=false
SYNC_ENV=false
LEGACY_INSTALL_DEPS=false
REGISTER_SHELL=true
DEPENDENCY_MODE="prompt-only"
UV_BOOTSTRAP_POLICY="never"
UV_BOOTSTRAP_SOURCE="${LOCALSETUP_UV_BOOTSTRAP_SOURCE:-https://astral.sh/uv/install.sh}"
UV_OFFLINE=false
COLOR_MODE="auto"
GLYPH_MODE="auto"
MIN_PYTHON_VERSION="3.12.0"
MIN_UV_VERSION="0.4.27"
EXPLICIT_SOURCE_DIR=false
EXPLICIT_TARGET_DIR=false
BOOTSTRAPPED_SOURCE=false
BOOTSTRAP_REPO="${LOCALSETUP_BOOTSTRAP_REPO:-https://github.com/CruxExperts/localsetup.git}"
BOOTSTRAP_REF_OVERRIDE="${LOCALSETUP_BOOTSTRAP_REF:-}"
BOOTSTRAP_REF="$BOOTSTRAP_REF_OVERRIDE"
BOOTSTRAP_SOURCE_DIR="${LOCALSETUP_BOOTSTRAP_SOURCE_DIR:-${HOME}/.local/share/localsetup/source}"
BOOTSTRAP_LATEST_REF=""
BOOTSTRAP_RELEASE_STATUS="not checked"
usage() {
cat <<'USAGE_END'
LocalSetup installer
Usage:
curl -sSL https://raw.githubusercontent.com/CruxExperts/localsetup/main/install | bash -s --
curl -sSL https://raw.githubusercontent.com/CruxExperts/localsetup/main/install | bash -s -- --yes
curl -sSL https://raw.githubusercontent.com/CruxExperts/localsetup/main/install | bash -s -- --non-interactive --yes
./install --directory .
./install --directory . --tools codex,kilo
./install --directory /path/to/localsetup --target-directory /path/to/project --tools cursor
./install --directory . --home /tmp/localsetup-home --non-interactive --yes
./install --directory . --platforms codex kilo --non-interactive --yes
Options:
--directory PATH LocalSetup source checkout containing ls/ (default: . when run
from a checkout; otherwise creates or refreshes a managed source checkout).
--target-directory PATH
Directory where adapter links and .localsetup/lock.json are written.
Defaults to --directory for explicit checkouts and to the caller's current
directory for piped bootstrap installs with --tools/--platforms.
Without selector flags, repo-targeted automation uses LocalSetup auto mode.
--home PATH Home directory for the managed shared skill library (default: $HOME)
--yes Legacy accepted flag for interactive installs. For automation, combine
with --non-interactive.
--non-interactive Automation mode. Requires --yes and preserves JSON machine output.
--tools LIST Compatibility alias for --platforms. Comma-separated.
--platforms LIST Platform adapter ids. Explicit values override auto mode.
--preset NAME Selection preset: core, normal, suggested, all, or custom.
--packs LIST Skill packs to install. Comma-separated. Fresh selector-free installs
use the normal global baseline; repo-visible packs stay separate.
--skills LIST Individual skill ids to include. Comma-separated.
--skill-classes LIST Skill taxonomy classes to include. Comma-separated.
--skill-tags LIST Skill taxonomy tags to include. Comma-separated.
--exclude-skills LIST
Individual skill ids to exclude unless required by a selected workflow.
Comma-separated.
--global-preset NAME
Selection preset for the managed package-library baseline.
--global-packs LIST Skill packs for the managed package-library baseline. Comma-separated.
--global-skills LIST Individual skills for the managed package-library baseline. Comma-separated.
--global-skill-classes LIST
Skill taxonomy classes for the managed package-library baseline.
--global-skill-tags LIST
Skill taxonomy tags for the managed package-library baseline.
--global-exclude-skills LIST
Skills excluded from the managed package-library baseline.
--repo-preset NAME Selection preset for repo-visible adapter packages.
--repo-packs LIST Skill packs exposed through repo adapter paths. Comma-separated.
--repo-skills LIST Individual skills exposed through repo adapter paths. Comma-separated.
--repo-skill-classes LIST
Skill taxonomy classes exposed through repo adapter paths.
--repo-skill-tags LIST
Skill taxonomy tags exposed through repo adapter paths.
--repo-exclude-skills LIST
Skills excluded from repo adapter visibility.
--mode MODE symlink or portable. Default: symlink.
--sync-env Sync the uv-managed project environment from pyproject.toml and uv.lock.
--install-deps Deprecated alias for --sync-env.
--install-uv If uv is missing, download the official uv installer to a temp file and run it.
--no-install-uv Never bootstrap uv automatically. This is the default.
--offline Run uv dependency sync in offline/cache-only mode.
--no-register-shell Do not create/update ~/.local/bin/localsetup.
--color MODE Wizard color mode: auto, always, or never. Default: auto.
--no-color Alias for --color never.
--glyphs MODE Wizard glyph mode: auto, ascii, or unicode. Default: auto.
--help, -h Show this help.
Windows note:
LocalSetup supports Windows through WSL2 only. Run this script inside WSL.
USAGE_END
}
die() {
echo "Error: $*" >&2
exit 1
}
csv_to_args() {
local csv="$1"
local -n out_ref="$2"
local item trimmed
IFS=',' read -r -a out_ref <<< "$csv"
for i in "${!out_ref[@]}"; do
item="${out_ref[$i]}"
trimmed="${item#"${item%%[![:space:]]*}"}"
trimmed="${trimmed%"${trimmed##*[![:space:]]}"}"
[[ -n "$trimmed" ]] || die "empty value in comma-separated list: $csv"
out_ref[$i]="$trimmed"
done
}
check_python() {
command -v python3 >/dev/null 2>&1 || die "python3 is required"
python3 - "$MIN_PYTHON_VERSION" <<'PY' || die "python3 >= 3.12 is required"
from __future__ import annotations
import sys
required = tuple(int(part) for part in sys.argv[1].split("."))
current = sys.version_info[:3]
raise SystemExit(0 if current >= required else 1)
PY
}
uv_cmd() {
if [[ -n "${LOCALSETUP_UV_BIN:-}" ]]; then
[[ -x "$LOCALSETUP_UV_BIN" ]] || die "LOCALSETUP_UV_BIN is not executable: $LOCALSETUP_UV_BIN"
printf '%s\n' "$LOCALSETUP_UV_BIN"
return 0
fi
command -v uv 2>/dev/null || return 1
}
uv_version_value() {
local uv_bin output
uv_bin="$(uv_cmd)" || die "uv is required"
output="$("$uv_bin" --version 2>&1)" || die "uv version check failed: $output"
if [[ "$output" =~ ([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
printf '%s.%s.%s\n' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}"
return 0
fi
die "uv version output was not recognized: $output"
}
version_ge() {
local current="$1" required="$2"
python3 - "$current" "$required" <<'PY'
from __future__ import annotations
import sys
current = tuple(int(part) for part in sys.argv[1].split("."))
required = tuple(int(part) for part in sys.argv[2].split("."))
raise SystemExit(0 if current >= required else 1)
PY
}
ensure_uv_min_version() {
local version
version="$(uv_version_value)"
version_ge "$version" "$MIN_UV_VERSION" || die "uv $version is too old; LocalSetup requires uv >= $MIN_UV_VERSION"
}
bootstrap_uv() {
local tmp installer fetcher
tmp="$(mktemp -d)"
installer="$tmp/uv-install.sh"
export LOCALSETUP_UV_BOOTSTRAP_ATTEMPTED=1
export LOCALSETUP_UV_BOOTSTRAP_SOURCE="$UV_BOOTSTRAP_SOURCE"
if command -v curl >/dev/null 2>&1; then
fetcher=(curl -fsSL "$UV_BOOTSTRAP_SOURCE" -o "$installer")
elif command -v wget >/dev/null 2>&1; then
fetcher=(wget -qO "$installer" "$UV_BOOTSTRAP_SOURCE")
else
die "uv is missing and neither curl nor wget is available for explicit --install-uv bootstrap"
fi
echo "Fetching uv installer to $installer from $UV_BOOTSTRAP_SOURCE" >&2
"${fetcher[@]}"
sh "$installer"
export PATH="$HOME/.local/bin:$PATH"
uv_cmd >/dev/null || die "uv bootstrap finished but uv was not found; add $HOME/.local/bin to PATH or set LOCALSETUP_UV_BIN"
}
ensure_uv_for_sync() {
if ! uv_cmd >/dev/null; then
if [[ "$UV_BOOTSTRAP_POLICY" != "auto" ]]; then
die "uv is required for --sync-env. Install uv first, pass --install-uv, or set LOCALSETUP_UV_BIN."
fi
bootstrap_uv
fi
ensure_uv_min_version
}
uv_project_base() {
local uv_bin
uv_bin="$(uv_cmd)" || die "uv is required"
printf '%s\0' "$uv_bin"
if "$UV_OFFLINE"; then
printf '%s\0' "--offline"
fi
printf '%s\0' "--project" "$SOURCE_DIR"
}
localsetup_env_is_corrupt() {
local env_path="$1" python_path cfg_path
[[ -e "$env_path" || -L "$env_path" ]] || return 1
if [[ -L "$env_path" && ! -e "$env_path" ]]; then
return 0
fi
cfg_path="$env_path/pyvenv.cfg"
if [[ -L "$cfg_path" && ! -e "$cfg_path" ]]; then
return 0
fi
if [[ -e "$cfg_path" && ! -r "$cfg_path" ]]; then
return 0
fi
python_path="$env_path/bin/python"
if [[ ! -e "$python_path" && ! -L "$python_path" ]]; then
python_path="$env_path/Scripts/python.exe"
fi
if [[ ! -e "$python_path" && ! -L "$python_path" ]]; then
return 0
fi
if [[ -L "$python_path" && ! -e "$python_path" ]]; then
return 0
fi
[[ -x "$python_path" ]] || return 0
return 1
}
quarantine_root_for_env() {
local env_path="$1"
if [[ "$env_path" == "$SOURCE_DIR/.venv" ]]; then
printf '%s\n' "$SOURCE_DIR/.localsetup/state/dependency-repair"
elif [[ "$env_path" == "$HOME_DIR/.local/share/localsetup/venv" ]]; then
printf '%s\n' "$HOME_DIR/.local/share/localsetup/state/dependency-repair"
else
printf '%s\n' "$(dirname "$env_path")/state/dependency-repair"
fi
}
quarantine_localsetup_env() {
local env_path="$1" owner="$2" reason="$3" uv_error="${4:-}" quarantine_root stamp dest index record_path
[[ -e "$env_path" || -L "$env_path" ]] || return 1
quarantine_root="$(quarantine_root_for_env "$env_path")"
mkdir -p "$quarantine_root"
stamp="$(date -u '+%Y%m%dT%H%M%SZ')"
dest="$quarantine_root/$(basename "$env_path")-$stamp"
index=1
while [[ -e "$dest" || -L "$dest" ]]; do
index=$((index + 1))
dest="$quarantine_root/$(basename "$env_path")-$stamp-$index"
done
mv "$env_path" "$dest" || die "failed to quarantine LocalSetup-owned environment: $env_path"
record_path="$quarantine_root/$(basename "$dest").json"
python3 - "$env_path" "$dest" "$owner" "$reason" "$DEPENDENCY_MODE" "$uv_error" "$record_path" <<'PY'
from __future__ import annotations
from datetime import datetime, timezone
import json
import sys
original, dest, owner, reason, mode, uv_error, record_path = sys.argv[1:]
payload = {
"original_path": original,
"quarantine_path": dest,
"owner": owner,
"reason": reason,
"mode": mode,
"timestamp": datetime.now(timezone.utc).isoformat(),
"uv_error": uv_error or None,
}
with open(record_path, "w", encoding="utf-8") as handle:
json.dump(payload, handle, indent=2, sort_keys=True)
handle.write("\n")
PY
echo "Warning: quarantined LocalSetup-owned corrupt environment: $env_path -> $dest" >&2
}
pre_sync_repair_localsetup_envs() {
local target_root legacy_target_env
if localsetup_env_is_corrupt "$SOURCE_DIR/.venv"; then
quarantine_localsetup_env "$SOURCE_DIR/.venv" "source_venv" "environment failed pre-sync corruption checks"
fi
if localsetup_env_is_corrupt "$HOME_DIR/.local/share/localsetup/venv"; then
quarantine_localsetup_env "$HOME_DIR/.local/share/localsetup/venv" "legacy_global_venv" "environment failed pre-sync corruption checks"
fi
target_root="${ATTACH_TARGET_DIR:-$SOURCE_DIR}"
legacy_target_env="$target_root/.localsetup/venv"
if localsetup_env_is_corrupt "$legacy_target_env"; then
quarantine_localsetup_env "$legacy_target_env" "legacy_target_local_venv" "environment failed pre-sync corruption checks"
fi
}
uv_error_is_environment_corruption() {
local lowered
lowered="$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')"
[[ "$lowered" == *".venv"* || "$lowered" == *"virtual environment"* || "$lowered" == *"pyvenv.cfg"* ]] || return 1
[[ "$lowered" == *"no such file"* || "$lowered" == *"not found"* || "$lowered" == *"permission denied"* || "$lowered" == *"could not"* || "$lowered" == *"failed"* || "$lowered" == *"broken"* || "$lowered" == *"invalid"* || "$lowered" == *"is not a virtual environment"* ]]
}
sync_uv_environment() {
local -a uv_base
local output
pre_sync_repair_localsetup_envs
mapfile -d '' -t uv_base < <(uv_project_base)
if output="$("${uv_base[@]}" sync --locked --no-dev 2>&1)"; then
[[ -z "$output" ]] || printf '%s\n' "$output" >&2
export LOCALSETUP_UV_ALREADY_SYNCED=1
return 0
fi
if uv_error_is_environment_corruption "$output" && [[ -e "$SOURCE_DIR/.venv" || -L "$SOURCE_DIR/.venv" ]]; then
quarantine_localsetup_env "$SOURCE_DIR/.venv" "source_venv" "uv sync reported source environment corruption" "$output"
if output="$("${uv_base[@]}" sync --locked --no-dev 2>&1)"; then
[[ -z "$output" ]] || printf '%s\n' "$output" >&2
export LOCALSETUP_UV_ALREADY_SYNCED=1
return 0
fi
fi
printf '%s\n' "$output" >&2
return 1
}
inspect_managed_bootstrap_source() {
SOURCE_DIR="$(cd "$SOURCE_DIR" && pwd -P)" || die "failed to resolve managed bootstrap source: $SOURCE_DIR"
if ! git -C "$SOURCE_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
die "managed bootstrap source exists but is not a Git checkout: $SOURCE_DIR. Move or remove it, or pass --directory PATH to use an explicit LocalSetup checkout."
fi
if [[ ! -d "$SOURCE_DIR/ls" && ! -f "$SOURCE_DIR/_localsetup/tools/localsetup.py" ]]; then
die "managed bootstrap source exists but is not a LocalSetup checkout: $SOURCE_DIR. Move or remove it, or pass --directory PATH to use an explicit LocalSetup checkout."
fi
if [[ ! -d "$SOURCE_DIR/ls" ]]; then
local git_root origin
git_root="$(git -C "$SOURCE_DIR" rev-parse --show-toplevel)" || die "failed to identify managed bootstrap source root: $SOURCE_DIR"
if [[ "$git_root" != "$SOURCE_DIR" ]]; then
die "managed legacy bootstrap source must be the root of its Git checkout: $SOURCE_DIR. Move or remove it, or pass --directory PATH to use an explicit LocalSetup checkout."
fi
origin="$(git -C "$SOURCE_DIR" remote get-url origin 2>/dev/null || true)"
if ! git -C "$SOURCE_DIR" ls-files --error-unmatch -- '_localsetup/tools/localsetup.py' >/dev/null 2>&1 || [[ "$origin" != "$BOOTSTRAP_REPO" ]]; then
die "managed bootstrap source exists but is not a LocalSetup checkout: $SOURCE_DIR. Move or remove it, or pass --directory PATH to use an explicit LocalSetup checkout."
fi
fi
local status
status="$(git -C "$SOURCE_DIR" status --porcelain --untracked-files=all)" || {
die "cannot inspect managed bootstrap source Git status: $SOURCE_DIR"
}
if [[ -n "$status" ]]; then
die "cannot refresh managed bootstrap source because it has uncommitted or untracked changes: $SOURCE_DIR. Commit, stash, move, or remove it, or pass --directory PATH to use an explicit checkout."
fi
}
is_legacy_managed_bootstrap_source() {
local origin
[[ ! -d "$SOURCE_DIR/ls" && -f "$SOURCE_DIR/_localsetup/tools/localsetup.py" ]] || return 1
git -C "$SOURCE_DIR" ls-files --error-unmatch -- '_localsetup/tools/localsetup.py' >/dev/null 2>&1 || return 1
origin="$(git -C "$SOURCE_DIR" remote get-url origin 2>/dev/null || true)"
[[ "$origin" == "$BOOTSTRAP_REPO" ]]
}
legacy_bootstrap_rollback_root() {
local candidate fallback
if [[ "$SOURCE_DIR" == "$HOME_DIR/.local/share/localsetup/source" ]]; then
candidate="$HOME_DIR/.local/share/localsetup/state/source-migrations"
else
candidate="$(dirname "$SOURCE_DIR")/state/source-migrations"
fi
fallback="$HOME_DIR/.local/share/localsetup/state/source-migrations"
if bootstrap_rollback_path_is_within_source "$candidate"; then
candidate="$fallback"
fi
if bootstrap_rollback_path_is_within_source "$candidate"; then
die "cannot create legacy managed source rollback evidence outside source checkout: $SOURCE_DIR"
fi
printf '%s\n' "$candidate"
}
bootstrap_rollback_path_is_within_source() {
python3 - "$SOURCE_DIR" "$1" <<'PY'
from __future__ import annotations
from pathlib import Path
import sys
source_path = Path(sys.argv[1]).resolve()
candidate_path = Path(sys.argv[2]).resolve()
try:
candidate_path.relative_to(source_path)
except ValueError:
raise SystemExit(1)
raise SystemExit(0)
PY
}
record_legacy_bootstrap_rollback() {
local rollback_root stamp bundle_path manifest_path original_head index
rollback_root="$(legacy_bootstrap_rollback_root)"
mkdir -p "$rollback_root" || die "failed to create legacy source rollback directory: $rollback_root"
original_head="$(git -C "$SOURCE_DIR" rev-parse HEAD)" || die "failed to identify legacy managed source revision: $SOURCE_DIR"
stamp="$(date -u '+%Y%m%dT%H%M%SZ')"
bundle_path="$rollback_root/legacy-source-$stamp.bundle"
index=1
while [[ -e "$bundle_path" || -L "$bundle_path" ]]; do
index=$((index + 1))
bundle_path="$rollback_root/legacy-source-$stamp-$index.bundle"
done
git -C "$SOURCE_DIR" bundle create "$bundle_path" --all "$original_head" || die "failed to preserve legacy managed source rollback bundle: $bundle_path"
manifest_path="${bundle_path%.bundle}.json"
python3 - "$manifest_path" "$SOURCE_DIR" "$original_head" "$BOOTSTRAP_REF" "$bundle_path" <<'PY' || die "failed to record legacy managed source rollback evidence: $manifest_path"
from __future__ import annotations
from datetime import datetime, timezone
import json
import sys
manifest_path, source_path, original_head, target_ref, bundle_path = sys.argv[1:]
payload = {
"schema_version": 1,
"source_path": source_path,
"original_head": original_head,
"target_ref": target_ref,
"created_at": datetime.now(timezone.utc).isoformat(),
"bundle_path": bundle_path,
}
with open(manifest_path, "w", encoding="utf-8") as handle:
json.dump(payload, handle, indent=2, sort_keys=True)
handle.write("\n")
PY
}
refresh_bootstrap_source() {
command -v git >/dev/null 2>&1 || die "git is required to refresh LocalSetup from $BOOTSTRAP_REPO"
inspect_managed_bootstrap_source
if is_legacy_managed_bootstrap_source; then
record_legacy_bootstrap_rollback
fi
git -C "$SOURCE_DIR" fetch --quiet "$BOOTSTRAP_REPO" "$BOOTSTRAP_REF" || {
die "failed to fetch LocalSetup bootstrap source from $BOOTSTRAP_REPO at ref $BOOTSTRAP_REF"
}
git -C "$SOURCE_DIR" checkout --quiet --detach FETCH_HEAD || {
die "failed to check out refreshed LocalSetup bootstrap source from $BOOTSTRAP_REPO at ref $BOOTSTRAP_REF"
}
}
derive_github_releases_url() {
local repo="$1"
local path
case "$repo" in
https://github.com/*|http://github.com/*|git@github.com:*)
path="${repo#https://github.com/}"
path="${path#http://github.com/}"
path="${path#git@github.com:}"
path="${path%.git}"
printf 'https://api.github.com/repos/%s/releases\n' "$path"
;;
*) return 1 ;;
esac
}
latest_release_from_api() {
local api_url="$1"
python3 - "$api_url" <<'PY'
from __future__ import annotations
import json
import sys
import urllib.request
url = sys.argv[1]
request = urllib.request.Request(
url,
headers={"Accept": "application/vnd.github+json", "User-Agent": "localsetup-installer"},
)
with urllib.request.urlopen(request, timeout=10) as response:
releases = json.load(response)
stable = [
release
for release in releases
if isinstance(release, dict)
and release.get("tag_name")
and not release.get("draft")
and not release.get("prerelease")
]
stable.sort(key=lambda release: release.get("published_at") or release.get("created_at") or "", reverse=True)
if not stable:
raise SystemExit("no stable GitHub release found")
print(stable[0]["tag_name"])
PY
}
latest_release_from_git_tags() {
command -v git >/dev/null 2>&1 || return 1
local tags tag version major first_major=""
tags="$(git ls-remote --tags --refs "$BOOTSTRAP_REPO" 'v*' 2>/dev/null \
| awk '{print $2}' \
| sed 's#refs/tags/##' \
| grep -E '^v[0-9]+([.][0-9]+)*$' \
|| true)"
[[ -n "$tags" ]] || return 1
# When API discovery is unavailable, a mixed-major tag set has no safe
# chronological interpretation. Do not silently select an older release
# line just because its major number sorts higher.
for tag in $tags; do
version="${tag#v}"
major="${version%%.*}"
if [[ -n "$first_major" && "$major" != "$first_major" ]]; then
return 1
fi
first_major="$major"
done
printf '%s\n' "$tags" | sort -V | tail -n 1
}
resolve_bootstrap_ref() {
if [[ -n "$BOOTSTRAP_REF_OVERRIDE" ]]; then
BOOTSTRAP_REF="$BOOTSTRAP_REF_OVERRIDE"
BOOTSTRAP_LATEST_REF="$BOOTSTRAP_REF"
BOOTSTRAP_RELEASE_STATUS="override: LOCALSETUP_BOOTSTRAP_REF"
return 0
fi
local api_url resolved
api_url="${LOCALSETUP_BOOTSTRAP_RELEASES_URL:-}"
if [[ -z "$api_url" ]]; then
api_url="$(derive_github_releases_url "$BOOTSTRAP_REPO" || true)"
fi
if [[ -n "$api_url" ]]; then
resolved="$(latest_release_from_api "$api_url" 2>/dev/null || true)"
if [[ -n "$resolved" ]]; then
BOOTSTRAP_REF="$resolved"
BOOTSTRAP_LATEST_REF="$resolved"
BOOTSTRAP_RELEASE_STATUS="latest stable GitHub release"
return 0
fi
fi
resolved="$(latest_release_from_git_tags || true)"
if [[ -n "$resolved" ]]; then
BOOTSTRAP_REF="$resolved"
BOOTSTRAP_LATEST_REF="$resolved"
BOOTSTRAP_RELEASE_STATUS="latest stable tag"
return 0
fi
BOOTSTRAP_REF=""
BOOTSTRAP_LATEST_REF="unknown"
BOOTSTRAP_RELEASE_STATUS="latest release lookup failed"
return 1
}
ensure_bootstrap_source() {
if [[ -f "$SOURCE_DIR/ls/tools/localsetup.py" ]]; then
return
fi
if "$EXPLICIT_SOURCE_DIR"; then
[[ -d "$SOURCE_DIR" ]] || die "directory does not exist: $SOURCE_DIR"
die "missing LocalSetup tool at $SOURCE_DIR/ls/tools/localsetup.py"
fi
SOURCE_DIR="$BOOTSTRAP_SOURCE_DIR"
if ! resolve_bootstrap_ref; then
if [[ -e "$SOURCE_DIR" ]]; then
echo "Warning: failed to discover the latest LocalSetup release; using existing managed source at $SOURCE_DIR." >&2
else
die "failed to discover the latest LocalSetup release and no managed source exists at $SOURCE_DIR. Check network access, set LOCALSETUP_BOOTSTRAP_REF, or pass --directory PATH."
fi
fi
if [[ -e "$SOURCE_DIR" ]]; then
if [[ -n "$BOOTSTRAP_REF" ]]; then
refresh_bootstrap_source
else
inspect_managed_bootstrap_source
fi
[[ -f "$SOURCE_DIR/ls/tools/localsetup.py" ]] || die "refreshed bootstrap source is missing ls/tools/localsetup.py: $SOURCE_DIR"
BOOTSTRAPPED_SOURCE=true
return
fi
command -v git >/dev/null 2>&1 || die "git is required to bootstrap LocalSetup from $BOOTSTRAP_REPO"
[[ -n "$BOOTSTRAP_REF" ]] || die "no bootstrap release ref is available; set LOCALSETUP_BOOTSTRAP_REF or pass --directory PATH"
mkdir -p "$(dirname "$SOURCE_DIR")"
git clone --branch "$BOOTSTRAP_REF" --single-branch "$BOOTSTRAP_REPO" "$SOURCE_DIR" >/dev/null 2>&1 || {
rm -rf "$SOURCE_DIR"
die "failed to clone LocalSetup bootstrap source from $BOOTSTRAP_REPO at ref $BOOTSTRAP_REF"
}
[[ -f "$SOURCE_DIR/ls/tools/localsetup.py" ]] || die "bootstrap clone is missing ls/tools/localsetup.py: $SOURCE_DIR"
BOOTSTRAPPED_SOURCE=true
}
while [[ $# -gt 0 ]]; do
case "$1" in
--directory)
[[ $# -ge 2 ]] || die "missing value for --directory"
SOURCE_DIR="$2"
EXPLICIT_SOURCE_DIR=true
shift 2
;;
--target-directory)
[[ $# -ge 2 ]] || die "missing value for --target-directory"
ATTACH_TARGET_DIR="$2"
EXPLICIT_TARGET_DIR=true
shift 2
;;
--home)
[[ $# -ge 2 ]] || die "missing value for --home"
HOME_DIR="$2"
shift 2
;;
--tools)
[[ $# -ge 2 ]] || die "missing value for --tools"
PLATFORMS_CSV="$2"
shift 2
;;
--platforms)
shift
platform_values=()
while [[ $# -gt 0 && "$1" != --* ]]; do
platform_values+=("$1")
shift
done
[[ ${#platform_values[@]} -gt 0 ]] || die "missing value for --platforms"
PLATFORMS_CSV="$(IFS=,; echo "${platform_values[*]}")"
;;
--packs)
[[ $# -ge 2 ]] || die "missing value for --packs"
PACKS_CSV="$2"
shift 2
;;
--preset)
[[ $# -ge 2 ]] || die "missing value for --preset"
PRESET="$2"
shift 2
;;
--skills)
[[ $# -ge 2 ]] || die "missing value for --skills"
SKILLS_CSV="$2"
shift 2
;;
--skill-classes)
[[ $# -ge 2 ]] || die "missing value for --skill-classes"
SKILL_CLASSES_CSV="$2"
shift 2
;;
--skill-tags)
[[ $# -ge 2 ]] || die "missing value for --skill-tags"
SKILL_TAGS_CSV="$2"
shift 2
;;
--exclude-skills)
[[ $# -ge 2 ]] || die "missing value for --exclude-skills"
EXCLUDE_SKILLS_CSV="$2"
shift 2
;;
--global-packs)
[[ $# -ge 2 ]] || die "missing value for --global-packs"
GLOBAL_PACKS_CSV="$2"
shift 2
;;
--global-preset)
[[ $# -ge 2 ]] || die "missing value for --global-preset"
GLOBAL_PRESET="$2"
shift 2
;;
--global-skills)
[[ $# -ge 2 ]] || die "missing value for --global-skills"
GLOBAL_SKILLS_CSV="$2"
shift 2
;;
--global-skill-classes)
[[ $# -ge 2 ]] || die "missing value for --global-skill-classes"
GLOBAL_SKILL_CLASSES_CSV="$2"
shift 2
;;
--global-skill-tags)
[[ $# -ge 2 ]] || die "missing value for --global-skill-tags"
GLOBAL_SKILL_TAGS_CSV="$2"
shift 2
;;
--global-exclude-skills)
[[ $# -ge 2 ]] || die "missing value for --global-exclude-skills"
GLOBAL_EXCLUDE_SKILLS_CSV="$2"
shift 2
;;
--repo-packs)
[[ $# -ge 2 ]] || die "missing value for --repo-packs"
REPO_PACKS_CSV="$2"
shift 2
;;
--repo-preset)
[[ $# -ge 2 ]] || die "missing value for --repo-preset"
REPO_PRESET="$2"
shift 2
;;
--repo-skills)
[[ $# -ge 2 ]] || die "missing value for --repo-skills"
REPO_SKILLS_CSV="$2"
shift 2
;;
--repo-skill-classes)
[[ $# -ge 2 ]] || die "missing value for --repo-skill-classes"
REPO_SKILL_CLASSES_CSV="$2"
shift 2
;;
--repo-skill-tags)
[[ $# -ge 2 ]] || die "missing value for --repo-skill-tags"
REPO_SKILL_TAGS_CSV="$2"
shift 2
;;
--repo-exclude-skills)
[[ $# -ge 2 ]] || die "missing value for --repo-exclude-skills"
REPO_EXCLUDE_SKILLS_CSV="$2"
shift 2
;;
--mode)
[[ $# -ge 2 ]] || die "missing value for --mode"
MODE="$2"
shift 2
;;
--yes)
YES=true
shift
;;
--non-interactive)
NON_INTERACTIVE=true
shift
;;
--install-deps)
SYNC_ENV=true
LEGACY_INSTALL_DEPS=true
shift
;;
--sync-env)
SYNC_ENV=true
shift
;;
--install-uv)
UV_BOOTSTRAP_POLICY="auto"
shift
;;
--no-install-uv)
UV_BOOTSTRAP_POLICY="never"
shift
;;
--offline)
UV_OFFLINE=true
shift
;;
--no-register-shell)
REGISTER_SHELL=false
shift
;;
--color)
[[ $# -ge 2 ]] || die "missing value for --color"
case "$2" in
auto|always|never) COLOR_MODE="$2" ;;
*) die "invalid --color: $2" ;;
esac
shift 2
;;
--no-color)
COLOR_MODE="never"
shift
;;
--glyphs)
[[ $# -ge 2 ]] || die "missing value for --glyphs"
case "$2" in
auto|ascii|unicode) GLYPH_MODE="$2" ;;
*) die "invalid --glyphs: $2" ;;
esac
shift 2
;;
--upgrade-policy)
die "--upgrade-policy was removed in LocalSetup; managed global installs are always metadata-driven"
;;
--global)
die "--global was removed in LocalSetup; the shared package library is always global-first"
;;
--help|-h)
usage
exit 0
;;
*)
die "unknown option: $1"
;;
esac
done
if "$NON_INTERACTIVE" && [[ "$YES" != true ]]; then
die "automation mode requires --non-interactive --yes"
fi
if [[ "$NON_INTERACTIVE" != true && ! -t 1 && ! -t 2 ]]; then
die "interactive installer requires a terminal. Run from a TTY, or use --non-interactive --yes for automation."
fi
ensure_bootstrap_source
# Managed non-interactive bootstrap must provision the locked CLI runtime before
# doctor or package materialization. A bare system Python need not provide the
# project's mandatory modules. When uv is absent, callers must explicitly select
# --install-uv before the installer downloads and executes the uv bootstrap.
# Lightweight fixture/bootstrap sources without uv metadata retain the
# dependency-free path.
if "$BOOTSTRAPPED_SOURCE" && "$NON_INTERACTIVE" && ! "$SYNC_ENV" \
&& [[ -f "$SOURCE_DIR/pyproject.toml" && -f "$SOURCE_DIR/uv.lock" ]]; then
SYNC_ENV=true
fi
export LOCALSETUP_BOOTSTRAP_LATEST_REF="$BOOTSTRAP_LATEST_REF"
export LOCALSETUP_BOOTSTRAP_RELEASE_STATUS="$BOOTSTRAP_RELEASE_STATUS"
[[ -d "$SOURCE_DIR" ]] || die "directory does not exist: $SOURCE_DIR"
SOURCE_DIR="$(cd "$SOURCE_DIR" && pwd)"
[[ -f "$SOURCE_DIR/ls/tools/localsetup.py" ]] || die "missing LocalSetup tool at $SOURCE_DIR/ls/tools/localsetup.py"
check_python
case "$MODE" in
symlink|portable) ;;
*) die "invalid --mode: $MODE" ;;
esac
if "$SYNC_ENV"; then
ensure_uv_for_sync
sync_uv_environment
DEPENDENCY_MODE="uv-sync"
fi
if "$LEGACY_INSTALL_DEPS"; then
export LOCALSETUP_LEGACY_INSTALL_DEPS=1
fi
if "$UV_OFFLINE"; then
export LOCALSETUP_UV_OFFLINE=1
fi
cli_base=(python3 "$SOURCE_DIR/ls/tools/localsetup.py")
if "$SYNC_ENV"; then
mapfile -d '' -t uv_base < <(uv_project_base)
cli_base=("${uv_base[@]}" run --locked --no-sync --no-dev python "$SOURCE_DIR/ls/tools/localsetup.py")
fi
pack_args=()
if [[ -n "$PACKS_CSV" ]]; then
csv_to_args "$PACKS_CSV" pack_args
fi
skill_args=()
if [[ -n "$SKILLS_CSV" ]]; then
csv_to_args "$SKILLS_CSV" skill_args
fi
skill_class_args=()
if [[ -n "$SKILL_CLASSES_CSV" ]]; then
csv_to_args "$SKILL_CLASSES_CSV" skill_class_args
fi
skill_tag_args=()
if [[ -n "$SKILL_TAGS_CSV" ]]; then
csv_to_args "$SKILL_TAGS_CSV" skill_tag_args
fi
exclude_skill_args=()
if [[ -n "$EXCLUDE_SKILLS_CSV" ]]; then
csv_to_args "$EXCLUDE_SKILLS_CSV" exclude_skill_args
fi
global_pack_args=()
if [[ -n "$GLOBAL_PACKS_CSV" ]]; then
csv_to_args "$GLOBAL_PACKS_CSV" global_pack_args
fi
global_skill_args=()
if [[ -n "$GLOBAL_SKILLS_CSV" ]]; then
csv_to_args "$GLOBAL_SKILLS_CSV" global_skill_args
fi
global_skill_class_args=()
if [[ -n "$GLOBAL_SKILL_CLASSES_CSV" ]]; then
csv_to_args "$GLOBAL_SKILL_CLASSES_CSV" global_skill_class_args
fi
global_skill_tag_args=()
if [[ -n "$GLOBAL_SKILL_TAGS_CSV" ]]; then
csv_to_args "$GLOBAL_SKILL_TAGS_CSV" global_skill_tag_args
fi
global_exclude_skill_args=()
if [[ -n "$GLOBAL_EXCLUDE_SKILLS_CSV" ]]; then
csv_to_args "$GLOBAL_EXCLUDE_SKILLS_CSV" global_exclude_skill_args
fi
repo_pack_args=()
if [[ -n "$REPO_PACKS_CSV" ]]; then
csv_to_args "$REPO_PACKS_CSV" repo_pack_args
fi
repo_skill_args=()
if [[ -n "$REPO_SKILLS_CSV" ]]; then
csv_to_args "$REPO_SKILLS_CSV" repo_skill_args
fi
repo_skill_class_args=()
if [[ -n "$REPO_SKILL_CLASSES_CSV" ]]; then
csv_to_args "$REPO_SKILL_CLASSES_CSV" repo_skill_class_args
fi
repo_skill_tag_args=()
if [[ -n "$REPO_SKILL_TAGS_CSV" ]]; then
csv_to_args "$REPO_SKILL_TAGS_CSV" repo_skill_tag_args
fi
repo_exclude_skill_args=()
if [[ -n "$REPO_EXCLUDE_SKILLS_CSV" ]]; then
csv_to_args "$REPO_EXCLUDE_SKILLS_CSV" repo_exclude_skill_args
fi
platform_args=()
if [[ -n "$PLATFORMS_CSV" ]]; then
csv_to_args "$PLATFORMS_CSV" platform_args
fi
target_args=()
if [[ -n "$ATTACH_TARGET_DIR" ]]; then
if [[ "$NON_INTERACTIVE" == true ]]; then
mkdir -p "$ATTACH_TARGET_DIR"
ATTACH_TARGET_DIR="$(cd "$ATTACH_TARGET_DIR" && pwd)"
fi
target_args=(--target-directory "$ATTACH_TARGET_DIR")
elif "$BOOTSTRAPPED_SOURCE" && ! "$EXPLICIT_TARGET_DIR" && [[ ${#platform_args[@]} -gt 0 ]]; then
ATTACH_TARGET_DIR="$CALLER_DIR"
target_args=(--target-directory "$ATTACH_TARGET_DIR")
fi
if [[ "$NON_INTERACTIVE" != true ]]; then
wizard_target_args=()
if [[ -n "$ATTACH_TARGET_DIR" ]]; then
wizard_target_args=(--target-directory "$ATTACH_TARGET_DIR")
fi
wizard_cmd=("${cli_base[@]}" --home "$HOME_DIR" --source-root "$SOURCE_DIR" "${wizard_target_args[@]}" wizard --caller-directory "$CALLER_DIR" --dependency-mode "$DEPENDENCY_MODE" --mode "$MODE" --color "$COLOR_MODE" --glyphs "$GLYPH_MODE")
[[ -z "$PRESET" ]] || wizard_cmd+=(--preset "$PRESET")
[[ ${#pack_args[@]} -eq 0 ]] || wizard_cmd+=(--packs "${pack_args[@]}")
[[ ${#skill_args[@]} -eq 0 ]] || wizard_cmd+=(--skills "${skill_args[@]}")
[[ ${#skill_class_args[@]} -eq 0 ]] || wizard_cmd+=(--skill-classes "${skill_class_args[@]}")
[[ ${#skill_tag_args[@]} -eq 0 ]] || wizard_cmd+=(--skill-tags "${skill_tag_args[@]}")
[[ ${#exclude_skill_args[@]} -eq 0 ]] || wizard_cmd+=(--exclude-skills "${exclude_skill_args[@]}")
[[ -z "$GLOBAL_PRESET" ]] || wizard_cmd+=(--global-preset "$GLOBAL_PRESET")
[[ ${#global_pack_args[@]} -eq 0 ]] || wizard_cmd+=(--global-packs "${global_pack_args[@]}")
[[ ${#global_skill_args[@]} -eq 0 ]] || wizard_cmd+=(--global-skills "${global_skill_args[@]}")
[[ ${#global_skill_class_args[@]} -eq 0 ]] || wizard_cmd+=(--global-skill-classes "${global_skill_class_args[@]}")
[[ ${#global_skill_tag_args[@]} -eq 0 ]] || wizard_cmd+=(--global-skill-tags "${global_skill_tag_args[@]}")
[[ ${#global_exclude_skill_args[@]} -eq 0 ]] || wizard_cmd+=(--global-exclude-skills "${global_exclude_skill_args[@]}")
[[ -z "$REPO_PRESET" ]] || wizard_cmd+=(--repo-preset "$REPO_PRESET")
[[ ${#repo_pack_args[@]} -eq 0 ]] || wizard_cmd+=(--repo-packs "${repo_pack_args[@]}")
[[ ${#repo_skill_args[@]} -eq 0 ]] || wizard_cmd+=(--repo-skills "${repo_skill_args[@]}")
[[ ${#repo_skill_class_args[@]} -eq 0 ]] || wizard_cmd+=(--repo-skill-classes "${repo_skill_class_args[@]}")
[[ ${#repo_skill_tag_args[@]} -eq 0 ]] || wizard_cmd+=(--repo-skill-tags "${repo_skill_tag_args[@]}")
[[ ${#repo_exclude_skill_args[@]} -eq 0 ]] || wizard_cmd+=(--repo-exclude-skills "${repo_exclude_skill_args[@]}")
if [[ -n "$ATTACH_TARGET_DIR" && "$EXPLICIT_TARGET_DIR" != true ]]; then
wizard_cmd+=(--target-directory-origin inferred)
fi
if [[ ${#platform_args[@]} -gt 0 ]]; then
wizard_cmd+=(--platforms "${platform_args[@]}")
fi
if [[ "$REGISTER_SHELL" != true ]]; then
wizard_cmd+=(--no-register-shell)
fi
exec "${wizard_cmd[@]}"
fi
mkdir -p "$HOME_DIR"
HOME_DIR="$(cd "$HOME_DIR" && pwd)"
doctor_cmd=("${cli_base[@]}" --home "$HOME_DIR" --source-root "$SOURCE_DIR" "${target_args[@]}" doctor --dependency-mode "$DEPENDENCY_MODE")
[[ ${#pack_args[@]} -eq 0 ]] || doctor_cmd+=(--packs "${pack_args[@]}")
[[ -z "$PRESET" ]] || doctor_cmd+=(--preset "$PRESET")
[[ ${#skill_args[@]} -eq 0 ]] || doctor_cmd+=(--skills "${skill_args[@]}")
[[ ${#skill_class_args[@]} -eq 0 ]] || doctor_cmd+=(--skill-classes "${skill_class_args[@]}")
[[ ${#skill_tag_args[@]} -eq 0 ]] || doctor_cmd+=(--skill-tags "${skill_tag_args[@]}")