-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
213 lines (188 loc) · 6.23 KB
/
fabfile.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
import locale
import platform
from pathlib import Path
from typing import Literal
from fabric import task
from fabric.util import get_local_user
from InquirerPy import inquirer
from InquirerPy.base.control import Choice
from InquirerPy.separator import Separator
from rich import print
VERSION = "0.26"
PM: Literal["brew", "scoop"] = "scoop" if platform.system() == "Windows" else "brew"
if Path.cwd() != Path.home():
raise Exception("Please `cd ~`.")
@task(default=True)
def hello(c):
"""Hello"""
print(f"Hello ~ {get_local_user()}")
print(f"Version: {VERSION}")
uv_tools = "~\\AppData\\Roaming\\uv\\tools\\" if platform.system() == "Windows" else "~/.local/share/uv/tools/"
print(f"Interpreter: {uv_tools}fabric")
print("fab task -h 可以查看 task\n")
c.run("fab -l", echo=False)
@task
def profile(c):
"""配置"""
match platform.system():
case "Darwin":
c.run("open ~/.zshrc")
case "Windows":
c.run("notepad $PROFILE")
@task(aliases=["clean"])
def cleanup(c):
"""清理"""
match PM:
case "brew":
hint("clean Homebrew")
c.run(f"{PM} cleanup")
c.run(f"{PM} doctor", warn=True)
case "scoop":
hint("clean Scoop")
c.run(f"{PM} cleanup --all")
c.run(f"{PM} checkup")
# hint("clean uv")
# c.run("uv cache clean")
@task
def install(c):
"""安装"""
roles = inquirer.checkbox(
gettext("install"),
[
Separator(),
Choice("android", "Android"),
Choice("ios", "iOS / macOS"),
Choice("java", "Java"),
Choice("js", "JavaScript & TypeScript"),
Choice("python", "Python"),
Separator("-- Database ---"),
Choice("mysql", "MySQL"),
Choice("redis", "Redis"),
Separator("-- Others -----"),
"fastlane",
Choice("fonts", f"{gettext("fonts")}: Cascadia Code NF, JetBrainsMono Nerd Font"),
"zoxide",
Separator()
],
transformer=lambda result: ", ".join(result) if len(result) > 0 else "",
instruction="(Space for select)"
).execute()
if not roles:
return
# if "zh_CN" in locale.getlocale():
# hint("configure RubyGems")
# c.run("gem sources --add https://mirrors.aliyun.com/rubygems/ --remove https://rubygems.org/")
if "zoxide" in roles:
hint("install zoxide fzf")
c.run(f"{PM} install zoxide fzf")
if "android" in roles:
hint("install ktlint")
c.run(f"{PM} install ktlint")
if "ios" in roles:
hint("install CocoaPods, SwiftFormat, SwiftLint")
c.run(f"{PM} install cocoapods swiftformat swiftlint")
if "java" in roles:
hint("install OpenJDK")
if PM == "scoop":
c.run(f"{PM} add bucket java")
c.run(f"{PM} install openjdk")
if "js" in roles:
hint("install Bun")
c.run(f"{PM} install bun")
if "python" in roles:
hint("install Ruff")
c.run("uv tool install ruff")
# 数据库
if "mysql" in roles:
hint("install MySQL")
c.run(f"{PM} install mysql")
if "redis" in roles:
hint("install Redis")
c.run(f"{PM} install redis")
# 其他
if "fastlane" in roles:
hint("install fastlane")
c.run(f"{PM} install fastlane")
if "fonts" in roles:
hint(f"install {gettext("fonts")}: Cascadia Code NF, JetBrainsMono Nerd Font")
match PM:
case "brew":
c.run(f"{PM} install font-cascadia-code-nf font-jetbrains-mono-nerd-font")
case "scoop":
c.run(f"{PM} bucket add nerd-fonts")
c.run(f"{PM} install CascadiaCode-NF JetBrainsMono-NF")
cleanup(c)
@task
def remove(c):
"""删除"""
# if not c.config.sudo.password:
# c.run("fab remove --prompt-for-sudo-password", echo=False)
# return
result = inquirer.select(gettext("remove"), ["python", Choice("", gettext("cancel"))]).execute()
if result == "python":
hint("remove Python")
c.run(f"{PM} uninstall python")
@task(aliases=["up"], help={"config": "更新 .fabric.yaml, .zshrc 配置文件"})
def upgrade(c, config=False):
"""升级"""
hint(f"upgrade 自己 当前版本 {VERSION} 变化在下次执行时生效")
remote = "https://raw.githubusercontent.com/nyssance/Free/main/"
download(c, f"{remote}fabfile.py")
if config:
hint("configure .fabric.yaml")
download(c, f"{remote}fabric.yaml", ".fabric.yaml")
if platform.system() != "Windows":
hint("configure .zshrc")
download(c, f"{remote}zshrc", ".zshrc")
c.run("zsh -lc 'source .zshrc'")
match PM:
case "brew":
hint("upgrade Homebrew")
c.run(f"{PM} update")
c.run(f"{PM} upgrade")
case "scoop":
hint("upgrade Scoop")
c.run(f"{PM} update --all")
hint("upgrade uv")
c.run("uv self update")
c.run("uv tool upgrade --all")
cleanup(c)
if platform.system() == "Windows":
c.run("winget upgrade")
hint(f"upgrade {gettext("completed")}")
@task(aliases=["format"])
def format_code(c):
"""格式化代码"""
c.run("isort fabfile.py")
def download(c, url: str, name: str | None = None):
command = f"{url} > {name}" if name else f"-O {url}"
c.run(f"curl -fsSL {command}")
def gettext(message: str) -> str:
chinese = "zh_CN" in locale.getlocale() or "Chinese (Simplified)_China" in locale.getlocale()
return ZH_CN[message] if chinese else message.capitalize()
def hint(value: str):
operation, message = value.split(" ", 1)
match operation:
case "clean":
color = "yellow"
case "configure":
color = "cyan"
case "install":
color = "green"
case "remove":
color = "red"
case "upgrade":
color = "blue"
case _:
color = "white"
print(f"[on {color}]{gettext(operation)}", message)
ZH_CN = {
"clean": "清理",
"configure": "配置",
"install": "安装",
"remove": "删除",
"upgrade": "升级",
"cancel": "取消",
"completed": "完成",
"fonts": "字体"
}