-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·174 lines (148 loc) · 5.07 KB
/
setup.py
File metadata and controls
executable file
·174 lines (148 loc) · 5.07 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
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = ["yaspin"]
# ///
# generated by ai
import os
import subprocess
from contextlib import contextmanager
from pathlib import Path
from yaspin import yaspin
DOTFILES = Path(__file__).parent.resolve()
HOME = Path.home()
BLUE = "\033[34m"
GREEN = "\033[32m"
RED = "\033[31m"
RESET = "\033[0m"
errors = []
@contextmanager
def section(title):
print(f"{BLUE}* {title}{RESET}")
yield
print()
def ok(msg):
print(f"{GREEN} - {msg}{RESET}")
def fail(label, detail):
errors.append((label, detail))
print(f"{RED} - {label} ✗{RESET}")
def run(label, *cmd, stream=False):
try:
if stream:
with yaspin(text=label) as sp:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
assert proc.stdout
proc.stdout.read()
proc.wait()
rc = proc.returncode
else:
result = subprocess.run(cmd, capture_output=True)
rc = result.returncode
except FileNotFoundError:
fail(label, f"command not found: {cmd[0]}")
return
if rc == 0:
ok(label)
else:
fail(label, f"run manually to debug: {' '.join(cmd)}")
def symlink(src, target):
src, target = Path(src), Path(target)
if target.is_symlink():
if Path(os.readlink(target)) == src:
ok(str(target))
return
target.unlink()
elif target.exists():
fail(str(target), f"exists and is not a symlink — remove it to fix: rm -rf {target}")
return
target.parent.mkdir(parents=True, exist_ok=True)
target.symlink_to(src)
ok(str(target))
# homebrew
with section("homebrew"):
run("brew bundle", "brew", "bundle", f"--file={DOTFILES}/Brewfile", stream=True)
# symlinks
def remove_stale_config_symlinks():
config_dir = HOME / ".config"
if not config_dir.exists():
return
for entry in config_dir.iterdir():
if not entry.is_symlink():
continue
target = Path(os.readlink(entry))
if not target.is_absolute():
target = entry.parent / target
try:
target.relative_to(DOTFILES)
except ValueError:
continue
if not target.exists():
entry.unlink()
ok(f"removed stale symlink: {entry}")
with section("symlinks"):
(HOME / ".config").mkdir(exist_ok=True)
remove_stale_config_symlinks()
for name in os.listdir(DOTFILES / "config"):
symlink(DOTFILES / "config" / name, HOME / ".config" / name)
for src, target in {
DOTFILES / "zshrc": HOME / ".zshrc",
DOTFILES / "hushlogin": HOME / ".hushlogin",
DOTFILES / "claude/CLAUDE.md": HOME / ".claude/CLAUDE.md",
}.items():
symlink(src, target)
# nvim providers
with section("nvim providers"):
venv_base = HOME / ".local/share/venv"
venv_base.mkdir(parents=True, exist_ok=True)
venv_python = venv_base / "neovim-python"
r1 = subprocess.run(["uv", "venv", "--clear", str(venv_python)], capture_output=True)
r2 = subprocess.run(["uv", "pip", "install", "--python", str(venv_python / "bin/python"), "pynvim"], capture_output=True)
if r1.returncode == 0 and r2.returncode == 0:
ok("python (pynvim)")
else:
fail("python provider", "uv venv or pynvim install failed")
r = subprocess.run(["rv", "ruby", "find"], capture_output=True, text=True)
if r.returncode == 0:
ruby_bin = Path(r.stdout.strip()).parent
run("ruby (neovim gem)", str(ruby_bin / "gem"), "install", "neovim")
else:
fail("ruby provider", "rv ruby find failed (is a ruby installed via rv?)")
# nvim lsp
with section("nvim lsp"):
run("rubocop", "rv", "tool", "install", "rubocop")
run("ruby-lsp", "rv", "tool", "install", "ruby-lsp")
run("ruff", "uv", "tool", "install", "ruff")
run("node", "volta", "install", "node")
for pkg in [
"bash-language-server",
"vscode-langservers-extracted",
"prettier",
"sql-formatter",
"svelte-language-server",
"@tailwindcss/language-server",
"@vtsls/language-server",
"yaml-language-server",
]:
run(pkg, "volta", "install", pkg)
# drift check
with section("homebrew drift"):
r = subprocess.run(
["brew", "bundle", "cleanup", f"--file={DOTFILES}/Brewfile"],
capture_output=True, text=True,
)
unlisted = r.stdout.strip()
if not unlisted:
ok("no unlisted packages")
else:
indented = "\n".join(f" {line}" for line in unlisted.splitlines())
fail("homebrew drift", f"packages not in Brewfile (run 'brew bundle cleanup --file={DOTFILES}/Brewfile --force' to remove):\n{indented}")
# report
if not errors:
print(f"{BLUE}* done{RESET}")
else:
print(f"{BLUE}* {len(errors)} issue(s):{RESET}")
for label, detail in errors:
print(f"\n{BLUE}{label}:{RESET}")
for line in detail.splitlines():
print(f"{RED} {line}{RESET}")
raise SystemExit(1)