-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit
More file actions
executable file
·212 lines (154 loc) · 6.03 KB
/
Copy pathinit
File metadata and controls
executable file
·212 lines (154 loc) · 6.03 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
#!/usr/bin/env python3
import logging
import os
import shutil
import subprocess
import sys
from pathlib import Path
def expandvars(path: str) -> Path:
return Path(os.path.expandvars(path))
def mkdir(p: Path, *args, **kwargs) -> None:
p.mkdir(*args, **kwargs)
logging.info("%s is created", p)
def ln(src: Path, dst: Path, exist_ok: bool = False) -> None:
if not dst.parent.exists():
mkdir(dst.parent, parents=True)
if dst.exists():
if not exist_ok:
raise RuntimeError(f"{dst} is already exist")
if not dst.is_symlink():
raise RuntimeError(f"{dst} is not a symlink")
dst.unlink()
os.symlink(src, dst)
logging.info("Symlink %s is created for %s", dst, src)
def git_clone(repo: str, dst: Path) -> None:
if dst.exists():
raise RuntimeError()
if not dst.parent.exists():
mkdir(dst.parent)
ret = subprocess.run(["git", "clone", repo, str(dst)], capture_output=True)
if ret.returncode != 0:
raise RuntimeError(ret.stderr)
logging.info("Repository %s is cloned to %s", repo, dst)
class InstallItem:
def __init__(self, dst: Path, exist_ok: bool) -> None:
self.dst = dst
self.exist_ok = exist_ok
def check(self) -> None:
if not self.exist_ok and self.dst.exists():
raise RuntimeError(f"{self.dst} already exists")
def exec(self) -> None:
raise NotImplementedError
class Symlink(InstallItem):
def __init__(self, src: str, dst: str, exist_ok: bool = False) -> None:
self.src = Path(__file__).absolute().parent / expandvars(src)
self.dst = expandvars(dst)
super().__init__(self.dst, exist_ok)
def exec(self) -> None:
ln(self.src, self.dst, self.exist_ok)
def check(self) -> None:
if not self.dst.exists():
return
if not self.exist_ok:
raise RuntimeError(f"{self.dst} already exists")
if self.dst.is_symlink():
return
raise RuntimeError(f"{self.dst} is not symlink. Abort")
class SymlinkToConfig(Symlink):
def __init__(self, src: str, exist_ok: bool = False) -> None:
dst = f"$XDG_CONFIG_HOME/{src}"
super().__init__(src, dst, exist_ok)
class Directory(InstallItem):
def __init__(self, dirname: str, exist_ok: bool = False) -> None:
self.dirname = expandvars(dirname)
self.exist_ok = exist_ok
super().__init__(self.dirname, exist_ok)
def exec(self) -> None:
mkdir(self.dirname, parents=True, exist_ok=self.exist_ok)
class GitClone(InstallItem):
def __init__(self, repo: str, dst: str, exist_ok: bool = False) -> None:
self.repo = repo
self.dst = expandvars(dst)
self.already_cloned = False
super().__init__(self.dst, exist_ok)
def exec(self) -> None:
if not self.already_cloned:
git_clone(self.repo, self.dst)
def check(self) -> None:
if not self.dst.exists():
return
if not self.dst.is_dir():
raise RuntimeError(f"{self.dst} is not directory")
ret = subprocess.run(
"git config --get remote.origin.url".split(),
cwd=self.dst,
capture_output=True,
)
if ret.returncode != 0:
raise RuntimeError(f"{self.dst} is already exists but not git repository")
repo = ret.stdout.decode().strip()
if repo != self.repo:
raise RuntimeError(f"{self.dst} contains a different repository")
logging.info("Repository %s is already cloned to %s", self.repo, self.dst)
self.already_cloned = True
def check_system() -> None:
if shutil.which("git") is None:
raise RuntimeError("git not found")
def setup_prerequisite() -> None:
if os.getenv("XDG_CONFIG_HOME") is None:
os.environ["XDG_CONFIG_HOME"] = os.path.expandvars("$HOME/.config")
logging.info(
"XDG_CONFIG_HOME is set as %s", os.path.expandvars("$HOME/.config")
)
else:
logging.info(
"XDG_CONFIG_HOME is already set as %s", os.getenv("XDG_CONFIG_HOME")
)
def process() -> None:
check_system()
setup_prerequisite()
install_items = [
# alacritty
SymlinkToConfig("alacritty/alacritty.toml", exist_ok=True),
# ghostty
SymlinkToConfig("ghostty/config.ghostty", exist_ok=True),
# git
SymlinkToConfig("git/ignore", exist_ok=True),
SymlinkToConfig("git/config", exist_ok=True),
# herdr
SymlinkToConfig("herdr/config.toml", exist_ok=True),
# nvim
SymlinkToConfig("nvim/init.lua", exist_ok=True),
SymlinkToConfig("nvim/lazy-lock.json", exist_ok=True),
SymlinkToConfig("nvim/lua", exist_ok=True),
# tmux
SymlinkToConfig("tmux/tmux.conf", exist_ok=True),
GitClone(
"https://github.com/tmux-plugins/tpm", "$XDG_CONFIG_HOME/tmux/plugins/tpm"
),
# fish
SymlinkToConfig("fish/config.fish", exist_ok=True),
SymlinkToConfig("fish/conf.d/cleanup.fish", exist_ok=True),
SymlinkToConfig("fish/conf.d/colima.fish", exist_ok=True),
SymlinkToConfig("fish/fish_plugins", exist_ok=True),
SymlinkToConfig("fish/functions/brew-upgrade.fish", exist_ok=True),
SymlinkToConfig("fish/functions/fish_prompt.fish", exist_ok=True),
SymlinkToConfig("fish/functions/fish_right_prompt.fish", exist_ok=True),
SymlinkToConfig("fish/functions/fzf-docker.fish", exist_ok=True),
SymlinkToConfig("fish/functions/fzf-git-branch.fish", exist_ok=True),
SymlinkToConfig("fish/functions/fzf-z.fish", exist_ok=True),
]
for install_item in install_items:
install_item.check()
for install_item in install_items:
install_item.exec()
def main() -> None:
logging.basicConfig(level=logging.INFO, format="[%(funcName)s] %(message)s")
try:
process()
except RuntimeError as e:
logging.error("Error occurred: %s", e)
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()