-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage-dotfiles
executable file
·169 lines (132 loc) · 4.13 KB
/
manage-dotfiles
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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import subprocess
import sys
from pathlib import Path
from typing import Iterable
RED = "\033[31m"
YELLOW = "\033[33m"
RESET = "\033[0m"
NEEDS_COMPILATION: dict[str, tuple[str, ...]] = {
# TODO: do i want to actually compile this??
# "migrate-to-obsidian": ("go", "build", "./cmd/migrate-to-obsidian"),
}
MANIFEST = {
".config": {
"zed",
},
".config/kitty": {
"current-theme.conf",
"kitty.conf",
},
".": {
".company.zshrc",
".fzf.zsh",
".gitconfig",
".tmux.conf",
".zprofile",
".zshrc",
},
"bin": {
"convert-things-to-org",
"dev-shell",
"dmd",
"gen-shell",
"make-packing-checklist",
"manage-dotfiles",
"migrate-to-obsidian",
"my-pgrep",
},
}
def log(contents: str, color: str) -> None:
print(
f"{color}{contents}{RESET}",
file=sys.stderr,
flush=True,
)
def error(contents: str) -> None:
log(contents, RED)
def fatal(contents: str) -> None:
error(contents)
raise SystemExit()
def warning(contents: str) -> None:
log(contents, YELLOW)
def info(contents: str) -> None:
log(contents, "")
def iter_manifest() -> Iterable[tuple[Path, Path]]:
dotfiles_dir = Path(__file__).resolve().parent
home = Path.home()
for target_dir, items in MANIFEST.items():
for item in items:
yield (dotfiles_dir / item, home / target_dir / item)
def deploy(dry_run: bool) -> None:
for src_item, dst_item in iter_manifest():
item_name = src_item.name
if (compile_command := NEEDS_COMPILATION.get(item_name)) is not None:
info(f"Compiling {item_name} with: {' '.join(compile_command)}")
try:
subprocess.check_call(compile_command)
except subprocess.CalledProcessError:
warning(f"Failed to compile {item_name}")
dst_item.parent.mkdir(parents=True, exist_ok=True)
if not src_item.exists():
warning(f"Skipping {src_item.relative_to(Path.cwd())} -- doesn't exist.")
continue
if (
dst_item.exists()
and dst_item.is_symlink()
and dst_item.readlink() == src_item
):
continue
if dst_item.exists():
warning(f"Skipping {src_item.relative_to(Path.cwd())} -- already exists.")
continue
if dry_run:
info(f"Would have linked {src_item} to {dst_item}.")
continue
info(f"Linked {src_item} to {dst_item}.")
dst_item.symlink_to(src_item)
def undeploy(dry_run: bool) -> None:
for src_item, dst_item in iter_manifest():
if not dst_item:
# We don't need to log here,
# because this is idempotent.
continue
if not dst_item.is_symlink():
warning(f"Skipping {dst_item} -- not a symlink.")
continue
if dst_item.readlink() != src_item:
warning(f"Skipping {dst_item} -- doesn't refer to {src_item}.")
continue
if dry_run:
info(f"Would have unlinked {src_item} from {dst_item}.")
continue
info(f"Unlinked {src_item} from {dst_item}.")
dst_item.unlink()
def main(args: list[str]) -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"command",
help="Must be on of `deploy` or `undeploy`.",
)
parser.add_argument(
"--dry-run",
nargs="?",
const=True,
default=False,
help=(
"If enabled, this script will not make any changes "
"and instead print out the changes it would have made."
),
)
structured_args = parser.parse_args(args[1:])
command: str = structured_args.command
if command == "deploy":
deploy(structured_args.dry_run)
elif command == "undeploy":
undeploy(structured_args.dry_run)
else:
fatal("Command must be one of `deploy` or `undeploy`,\n" f"not `{command}`.")
if __name__ == "__main__":
main(sys.argv)