-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
264 lines (212 loc) · 9.55 KB
/
Copy pathbuild.py
File metadata and controls
264 lines (212 loc) · 9.55 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
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
import os
import sys
import subprocess
import shutil
import concurrent.futures
import time
from threading import Lock
import json
print_lock = Lock()
def safe_print(msg):
with print_lock:
print(msg)
def load_config(src_dir):
config_path = os.path.join(src_dir, "config.json")
if os.path.exists(config_path):
try:
with open(config_path, "r") as f:
return json.load(f)
except:
pass
return {}
def run_command(cmd, cwd=None, name=""):
try:
if "dotnet" in cmd:
cmd.extend(["-p:EnableWindowsTargeting=true"])
subprocess.run(cmd, cwd=cwd, check=True, shell=(sys.platform == "win32"), stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
err_msg = e.stderr.decode() if e.stderr else "Unknown error"
safe_print(f"[ERROR] {name} failed: {err_msg}")
os._exit(1)
def build_frontend(src_dir):
safe_print("[Frontend] Building Svelte App...")
start_time = time.time()
frontend_dir = os.path.join(src_dir, "frontend")
if not os.path.exists(os.path.join(frontend_dir, "node_modules")):
run_command(["pnpm", "install"], cwd=frontend_dir, name="Frontend Install")
run_command(["pnpm", "build"], cwd=frontend_dir, name="Frontend Build")
safe_print(f"[Frontend] Done in {time.time() - start_time:.2f}s")
return os.path.join(frontend_dir, "dist")
def build_windows_component(name, project_path, build_dir, assembly_name):
start_time = time.time()
safe_print(f"[Windows][{name}] Building as {assembly_name}...")
temp_publish = os.path.join(build_dir, f"win_{name}_pub")
if os.path.exists(temp_publish):
shutil.rmtree(temp_publish)
proj_build_bin = os.path.join(build_dir, f"win_{name}_bin")
proj_build_obj = os.path.join(build_dir, f"win_{name}_obj")
cmd = [
"dotnet",
"publish",
project_path,
"-c",
"Release",
"-r",
"win-x64",
"--self-contained",
"false",
"-p:PublishSingleFile=false",
"-p:EnableWindowsTargeting=true",
f"-p:AssemblyName={assembly_name}",
f"-p:BaseOutputPath={proj_build_bin}/",
f"-p:BaseIntermediateOutputPath={proj_build_obj}/",
"-o",
temp_publish,
]
run_command(cmd, name=f"Windows {name}")
safe_print(f"[Windows][{name}] Finished in {time.time() - start_time:.2f}s")
return temp_publish
def cleanup_windows_files(path):
cultures = ["cs", "de", "es", "fr", "it", "ja", "ko", "pl", "pt-BR", "ru", "tr", "zh-Hans", "zh-Hant"]
for culture in cultures:
culture_path = os.path.join(path, culture)
if os.path.isdir(culture_path):
shutil.rmtree(culture_path)
for item in os.listdir(path):
if item.endswith(".pdb") or item.endswith(".xml") or item.endswith(".deps.json"):
file_path = os.path.join(path, item)
if os.path.isfile(file_path):
os.remove(file_path)
wpf_dll = os.path.join(path, "Microsoft.Web.WebView2.Wpf.dll")
if os.path.isfile(wpf_dll):
os.remove(wpf_dll)
runtimes_path = os.path.join(path, "runtimes")
if os.path.isdir(runtimes_path):
shutil.rmtree(runtimes_path)
def windows_track(src_dir, build_dir, dist_output, release_output, project_root, no_pack, binary_name, frontend_dist):
try:
safe_print("[Windows] Starting Parallel Track...")
components = {
"webview": (os.path.join(src_dir, "windows", "WebView", "WebView.csproj"), f"{binary_name}-webview"),
"main": (os.path.join(src_dir, "windows", "Main", "Main.csproj"), binary_name),
"server": (os.path.join(src_dir, "windows", "Server", "Server.csproj"), f"{binary_name}-server"),
}
temp_folders = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
future_to_name = {executor.submit(build_windows_component, name, path, build_dir, asm_name): name for name, (path, asm_name) in components.items()}
for future in concurrent.futures.as_completed(future_to_name):
temp_folders[future_to_name[future]] = future.result()
safe_print("[Windows] Organizing and cleaning distribution...")
final_win_root = os.path.join(dist_output, "windows")
if os.path.exists(final_win_root):
shutil.rmtree(final_win_root)
os.makedirs(final_win_root)
for folder in temp_folders.values():
for item in os.listdir(folder):
src, dst = os.path.join(folder, item), os.path.join(final_win_root, item)
if os.path.isdir(src):
if not os.path.exists(dst):
shutil.copytree(src, dst)
else:
if not os.path.exists(dst):
shutil.copy2(src, dst)
cleanup_windows_files(final_win_root)
safe_print("[Windows] Copying frontend assets...")
dest_frontend = os.path.join(final_win_root, "frontend")
if os.path.exists(dest_frontend):
shutil.rmtree(dest_frontend)
shutil.copytree(frontend_dist, dest_frontend)
config_src = os.path.join(src_dir, "config.json")
if os.path.exists(config_src):
shutil.copy2(config_src, os.path.join(final_win_root, "config.json"))
if not no_pack:
safe_print("[Windows] Creating Zip archive...")
shutil.make_archive(os.path.join(release_output, "windows"), "zip", final_win_root)
safe_print("[Windows] Zip created.")
safe_print("[Windows] Track Completed.")
except Exception as e:
safe_print(f"[Windows] Track failed: {e}")
def build_linux_track(src_dir, build_dir, dist_output, release_output, project_root, no_pack, binary_name, frontend_dist):
try:
safe_print("[Linux] Starting Track...")
linux_pkg_dir = os.path.join(build_dir, "linux_pkg")
if os.path.exists(linux_pkg_dir):
shutil.rmtree(linux_pkg_dir)
os.makedirs(linux_pkg_dir)
linux_src_dir = os.path.join(src_dir, "linux")
for item in os.listdir(linux_src_dir):
s, d = os.path.join(linux_src_dir, item), os.path.join(linux_pkg_dir, item)
shutil.copy2(s, d) if not os.path.isdir(s) else shutil.copytree(s, d)
# Rename launcher to binary_name
run_sh = os.path.join(linux_pkg_dir, "run.sh")
target_bin = os.path.join(linux_pkg_dir, binary_name)
if os.path.exists(run_sh):
os.rename(run_sh, target_bin)
os.chmod(target_bin, 0o755)
final_linux_root = os.path.join(dist_output, "linux")
if os.path.exists(final_linux_root):
shutil.rmtree(final_linux_root)
shutil.copytree(linux_pkg_dir, final_linux_root)
safe_print("[Linux] Copying frontend assets...")
dest_frontend = os.path.join(final_linux_root, "frontend")
if os.path.exists(dest_frontend):
shutil.rmtree(dest_frontend)
shutil.copytree(frontend_dist, dest_frontend)
config_src = os.path.join(src_dir, "config.json")
if os.path.exists(config_src):
shutil.copy2(config_src, os.path.join(final_linux_root, "config.json"))
if not no_pack:
shutil.make_archive(os.path.join(release_output, "linux"), "zip", final_linux_root)
safe_print("[Linux] Track Completed.")
except Exception as e:
safe_print(f"[Linux] Track failed: {e}")
def main():
project_root = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.join(project_root, "src")
dist_output = os.path.join(project_root, "dist")
release_output = os.path.join(project_root, "release")
build_dir = os.path.join(project_root, "build")
config = load_config(src_dir)
binary_name = config.get("binary_name", "browser-as-wallpaper")
no_pack = "--no-pack" in sys.argv
target = None
if "--target" in sys.argv:
try:
idx = sys.argv.index("--target")
if idx + 1 < len(sys.argv):
target = sys.argv[idx + 1]
except ValueError:
pass
# Selective cleanup
if target:
for d in [os.path.join(dist_output, target), os.path.join(release_output, target + ".zip")]:
if os.path.exists(d):
if os.path.isdir(d):
shutil.rmtree(d)
else:
os.remove(d)
else:
for d in [dist_output, release_output, build_dir]:
if os.path.exists(d):
shutil.rmtree(d)
os.makedirs(d)
if not os.path.exists(build_dir):
os.makedirs(build_dir)
if not os.path.exists(dist_output):
os.makedirs(dist_output)
if not os.path.exists(release_output):
os.makedirs(release_output)
safe_print("--- WaifuPaper Build System ---")
total_start = time.time()
# Build Frontend first
frontend_dist = build_frontend(src_dir)
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
if target is None or target == "windows":
futures.append(executor.submit(windows_track, src_dir, build_dir, dist_output, release_output, project_root, no_pack, binary_name, frontend_dist))
if target is None or target == "linux":
futures.append(executor.submit(build_linux_track, src_dir, build_dir, dist_output, release_output, project_root, no_pack, binary_name, frontend_dist))
concurrent.futures.wait(futures)
safe_print(f"\n[System] All builds finished in {time.time() - total_start:.2f}s")
if __name__ == "__main__":
main()