-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.z-pms.py
executable file
·300 lines (241 loc) · 8.68 KB
/
.z-pms.py
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
#!/usr/bin/env python
import atexit
import functools
import sys
import os
import subprocess
from dataclasses import dataclass, field
from enum import StrEnum
from random import choice
from pathlib import Path
from datetime import date
from shlex import quote
from shelve import open as shelve_open
from typing import cast, Any, Annotated, Optional, Literal
# import redis
import typer
from rich import print
from rich.panel import Panel
from rich.text import Text
def as_any(obj: Any) -> Any:
return cast(obj, Any)
@dataclass
class PM:
base_script: Path
name: Optional[str] = None
ld_name: Optional[str] = None
ld_path: Optional[Path] = None
x_init_config: Path = field(init=False)
x_prepare: Path = field(init=False)
x_init: Path = field(init=False)
x_cleanup: Path = field(init=False)
def __post_init__(self):
self.x_init_config = self.base_script.with_suffix(".config.zsh")
self.x_prepare = self.base_script.with_suffix(".pre.zsh")
self.x_init = self.base_script.with_suffix(".init.zsh")
self.x_cleanup = self.base_script.with_suffix(".clean.zsh")
if not self.ld_path and self.name:
self.ld_path = self.base_script.parent / f"load-{self.name}.zsh"
@classmethod
def from_loader(cls, ld_name: str):
return cls.from_name(name_from_loader(ld_name), ld_name=ld_name)
@classmethod
def from_name(cls, pm_name: str, *, ld_name: str = None, ld_path: Path = None):
return cls(
PMS_DIR / f".{pm_name}.zsh", name=pm_name, ld_name=ld_name, ld_path=ld_path
)
def print_if(self):
if self.x_init.is_file():
print(f"X PM Init:\t {self.x_init}")
if self.x_prepare.is_file():
print(f"X Pre Init:\t {self.x_prepare}")
if self.x_cleanup.is_file():
print(f"X Cleanup:\t {self.x_cleanup}")
def print_all(self):
print(f"X PM Init:\t {self.x_init if self.x_init.is_file() else 'N/A'}")
print(f"X Pre Init:\t {self.x_prepare if self.x_prepare.is_file() else 'N/A'}")
print(f"X Cleanup:\t {self.x_cleanup if self.x_cleanup.is_file() else 'N/A'}")
def set_ldpath(self, ld_path: Path):
self.ld_path = ld_path
def exist(self):
if self.ld_path:
return self.ld_path.is_file()
return (
self.base_script.exists()
or self.x_init.is_file()
or self.x_prepare.is_file()
or self.x_cleanup.is_file()
)
def cname(self):
return f"[cyan]{self.name}[/]"
def __eq__(self, other):
if isinstance(other, PM):
if other.exist() and self.exist():
return self.base_script == other.base_script
return False
class PMEnum(StrEnum):
zinit = "zinit"
zimfw = "zimfw"
zplug = "zplug"
zpm = "zpm"
random = "random"
daily = "daily"
def name_from_loader(ld_name: str) -> str:
return ld_name.removeprefix("load-").removesuffix(".zsh")
app = typer.Typer()
KEY_TODAYS_PM = "z-pms-todays-pm"
KEY_LAST_DATE = "z-pms-last-date"
ZPM_ROOT = Path(__file__).parent.resolve()
HOME_DIR = Path(os.path.expanduser("~")).resolve()
PM_LOAD = ZPM_ROOT / ".pm-loader.zsh"
ZSHRC_PATH = ZPM_ROOT / ".zshrc"
PMS_DIR = ZPM_ROOT / Path(".z-pms/").resolve()
DISABLES_DIR = PMS_DIR / "disable"
DISABLES = DISABLES_DIR.read_text("u8").splitlines()
PMS_LOADERS = (*PMS_DIR.glob("load-*.zsh"),)
Path(typer.get_app_dir("z-pms")).mkdir(parents=True, exist_ok=True)
shelve = shelve_open(str(Path(typer.get_app_dir("z-pms")) / "state.shelve"))
@functools.cache
def get_pms():
return {
name_from_loader(path.name): path
for path in PMS_DIR.glob("./load-*.zsh")
if name_from_loader(path.name) not in DISABLES
}
def set_environ():
os.environ["ZPMS_DIR"] = str(PMS_DIR.absolute())
def print_banner():
banner = Panel(
Text.assemble(
("ZshPMs", "cyan"),
(" - ", "bright_black"),
("Use random plugin manager everyday", "blue"),
)
)
print(banner)
def print_env_info():
print("Home path\t", HOME_DIR)
print("ZPM Root path\t", ZPM_ROOT)
print("Zshrc path\t", ZSHRC_PATH)
print("PMs dir:\t", PMS_DIR)
print("Available PMs:\t", " ".join(get_pms()))
def cdata(data: str | Any):
# return f"[cyan]{data}[/]"
return str(data)
@app.command()
def select(
which_pm: Annotated[
PMEnum,
typer.Option(
help="Chose specific pm",
prompt="Which PM you prefer to use",
show_choices=True,
),
],
use_relative: Annotated[
bool, typer.Option(help="Use relative path instead of absolute")
] = True,
force: Annotated[
bool, typer.Option("--force", "-f", help="Force reinit the pm")
] = False,
):
"""
Select new zsh plugin manager
If 'new_pm' is given, use it instead of a randomly chosen plugin manager
"""
pms = get_pms()
prev_pm_name = shelve.get(KEY_TODAYS_PM, None)
last_date: date = shelve.get(KEY_LAST_DATE, date.min)
todays_pm_name: str = (
choice(tuple(pms.keys())) if which_pm == "random" else which_pm
)
if (date.today() - last_date).days >= 1:
shelve[KEY_LAST_DATE] = date.today()
if which_pm == "daily":
todays_pm_name = choice(which_pm)
pm_loader = pms[todays_pm_name]
os.environ["ZPM_ROOT"] = str(ZPM_ROOT)
pm = PM.from_name(todays_pm_name)
shelve[KEY_TODAYS_PM] = todays_pm_name
shelve.sync()
prev_pm = PM.from_name(prev_pm_name)
print("")
if force or pm != prev_pm:
print(f"Todays pm:\t {pm.cname()}")
print(f"Located loader:\t {pm.ld_path}")
pm.print_if()
if prev_pm.exist():
print(f"Previous PM:\t {prev_pm.cname()}")
print(f"Located Loader:\t {cdata(prev_pm.ld_path)}")
prev_pm.print_if()
if prev_pm.x_cleanup and prev_pm.x_cleanup.is_file():
print("Cleaning up previous PM...")
subprocess.call(["/usr/bin/zsh", "-c", prev_pm.x_cleanup.read_text("u8")])
if pm.x_init.is_file():
print("Setting up new PM...")
subprocess.call(["/usr/bin/zsh", "-c", pm.x_init.read_text("u8")])
print(f"Updating {cdata(ZSHRC_PATH)}...")
if not ZSHRC_PATH.is_file():
ZSHRC_PATH.unlink(True)
if use_relative:
LD_PM_REL = PM_LOAD.relative_to(HOME_DIR)
ROOT_REL = ZPM_ROOT.relative_to(HOME_DIR)
PMS_REL = PMS_DIR.relative_to(HOME_DIR)
X_INITC = pm.x_init_config.relative_to(HOME_DIR)
ZSHRC_PATH.write_text(
"\n".join(
(
# f"source ~/.z-headers.zsh\n"
f"export ZPM_ROOT=$HOME/{quote(str(ROOT_REL))}",
f"export ZPMS_DIR=$HOME/{quote(str(PMS_REL))}",
f"export ZPM_CUR_PM={quote(str(todays_pm_name))}",
(
f"source $HOME/{quote(str(X_INITC))}"
if pm.x_init_config.is_file()
else ""
),
(
f"source {pm.base_script.read_text()}\n"
if pm.base_script.is_file()
else ""
f"source $HOME/{LD_PM_REL}\n"
f"source $HOME/{quote(str(ROOT_REL / '.z-footer.zsh'))}\n"
),
)
)
)
else:
ZSHRC_PATH.write_text(
"\n".join(
(
# f"source ~/.z-headers.zsh\n"
f"export ZPM_ROOT={quote(str(ZPM_ROOT))}",
f"export ZPMS_DIR={quote(str(PMS_DIR))}",
f"export ZPM_CUR_PM={quote(str(todays_pm_name))}",
(
f"source {quote(str(pm.x_init_config))}"
if pm.x_init_config.is_file()
else ""
),
(
f"source {pm.base_script.read_text()}\n"
if pm.base_script.is_file()
else ""
f"source {PM_LOAD}\n"
f"source {quote(str(ZPM_ROOT / '.z-footer.zsh'))}\n"
),
)
)
)
PM_LOAD.unlink(True)
PM_LOAD.symlink_to(pm_loader.relative_to(PM_LOAD.parent))
print(f"Link {cdata(PM_LOAD)} -> {cdata(pm_loader)}")
if (force or pm != prev_pm) and pm.x_prepare.is_file():
print("Configure new PM...")
subprocess.call(["/usr/bin/zsh", "-c", pm.x_prepare.read_text("u8")])
print("Done!")
if __name__ == "__main__":
print_banner()
set_environ()
app()
atexit.register(shelve.close)