forked from FRBLanApps/PiliSuper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
1448 lines (1215 loc) · 51 KB
/
build.py
File metadata and controls
1448 lines (1215 loc) · 51 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Flutter 多平台构建脚本
移植自 lib/scripts/build.ps1
用法:
python build.py android
python build.py android --pkg-id com.myfork.app --app-name my_fork --sign
python build.py linux --linux-targets tar.gz deb rpm arch appimage
python build.py all --pkg-id com.myfork.app --output dist/
python build.py android -- --obfuscate --split-debug-info=./debug
"""
from __future__ import annotations
import argparse
from dataclasses import dataclass
import io
import json
import logging
import os
import platform as platform_module
import re
import shutil
import subprocess
import sys
import textwrap
import time
from pathlib import Path
from typing import Sequence
from urllib.request import urlopen
try:
from rich.console import Console
from rich.logging import RichHandler
from rich.traceback import install as install_rich_traceback
except ImportError:
Console = None
RichHandler = None
install_rich_traceback = None
# Windows stdout UTF-8(▶ 等字符在 cp1252 下无法输出)
if sys.platform == "win32":
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace")
@dataclass
class VersionInfo:
version_name: str
version_code: int
version_full: str
def configure_logging() -> tuple[logging.Logger, Console | None]:
logger = logging.getLogger("pilisuper.build")
logger.handlers.clear()
logger.setLevel(logging.INFO)
logger.propagate = False
if RichHandler is not None and Console is not None:
console: Console | None = Console(stderr=True, soft_wrap=True)
if install_rich_traceback is not None:
install_rich_traceback(show_locals=False)
handler: logging.Handler = RichHandler(
console=console,
rich_tracebacks=True,
markup=True,
show_time=False,
show_level=False,
show_path=False,
)
else:
console = None
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(handler)
return logger, console
LOGGER, RICH_CONSOLE = configure_logging()
SKIP_RENAME_DIR_NAMES = {".git", ".dart_tool", "build", "Pods", "ephemeral"}
def _format_log_message(label: str, message: str, style: str) -> str:
if RICH_CONSOLE is not None:
return f"[{style}]{label:<5}[/] {message}"
return f"{label:<5} {message}"
def log_info(message: str) -> None:
LOGGER.info(_format_log_message("INFO", message, "cyan"))
def log_success(message: str) -> None:
LOGGER.info(_format_log_message("OK", message, "green"))
def log_warning(message: str) -> None:
LOGGER.warning(_format_log_message("WARN", message, "yellow"))
def log_error(message: str) -> None:
LOGGER.error(_format_log_message("ERROR", message, "red"))
def log_step(message: str) -> None:
if RICH_CONSOLE is not None:
RICH_CONSOLE.rule(f"[bold cyan]{message}[/bold cyan]")
return
LOGGER.info("")
LOGGER.info(_format_log_message("STEP", message, "cyan"))
# ══════════════════════════════════════════════════════════════════
# Shell 工具
# ══════════════════════════════════════════════════════════════════
IS_WINDOWS = platform_module.system() == "Windows"
def _resolve_command(command: Sequence[str]) -> list[str]:
"""
Windows 上 flutter / dart 等工具是 .bat 文件,
subprocess shell=False 找不到,用 shutil.which 解析完整路径。
"""
command_parts = [str(part) for part in command]
if not IS_WINDOWS:
return command_parts
resolved = shutil.which(command_parts[0])
if resolved:
command_parts = [resolved] + command_parts[1:]
return command_parts
def run_command(
command: Sequence[str],
cwd: str | Path | None = None,
check: bool = True,
capture_output: bool = False,
env: dict[str, str] | None = None,
) -> subprocess.CompletedProcess[str]:
if isinstance(command, str):
raise TypeError(
"run_command() expects a sequence of arguments. "
"Use run_shell_command() for string commands."
)
merged_env = {**os.environ, **(env or {})}
command_parts = list(command)
command_parts = _resolve_command(command_parts)
if not capture_output:
log_info("$ " + " ".join(command_parts))
return subprocess.run(
command_parts,
cwd=cwd,
check=check,
capture_output=capture_output,
text=True,
env=merged_env,
)
def run_shell_command(
command: str,
cwd: str | Path | None = None,
check: bool = True,
capture_output: bool = False,
env: dict[str, str] | None = None,
) -> subprocess.CompletedProcess[str]:
merged_env = {**os.environ, **(env or {})}
if not capture_output:
log_info("$ " + command)
return subprocess.run(
command,
shell=True,
cwd=cwd,
check=check,
capture_output=capture_output,
text=True,
env=merged_env,
)
def get_git_output(args: Sequence[str]) -> str:
try:
return run_command(["git", *args], capture_output=True).stdout.strip()
except subprocess.CalledProcessError:
return ""
def require_command(tool_name: str, hint: str = "") -> None:
if not shutil.which(tool_name):
log_error(f"找不到 {tool_name}。{hint}")
sys.exit(1)
def download_file(url: str, destination: Path) -> None:
log_info(f"下载: {url}")
with urlopen(url, timeout=60) as response, destination.open("wb") as file_obj:
shutil.copyfileobj(response, file_obj)
# ══════════════════════════════════════════════════════════════════
# pubspec 读写
# ══════════════════════════════════════════════════════════════════
def read_pubspec_text() -> str:
return Path("pubspec.yaml").read_text(encoding="utf-8")
def get_pubspec_name() -> str:
match = re.search(r"^name:\s+(\S+)", read_pubspec_text(), re.MULTILINE)
return match.group(1) if match else "app"
def get_pubspec_version_base() -> str:
match = re.search(r"^version:\s*([\d.]+)", read_pubspec_text(), re.MULTILINE)
return match.group(1) if match else "0.0.0"
# ══════════════════════════════════════════════════════════════════
# Prebuild(移植 build.ps1)
# ══════════════════════════════════════════════════════════════════
def run_prebuild(platform_name: str) -> VersionInfo:
log_step("Prebuild(移植 build.ps1)")
lines = read_pubspec_text().splitlines()
version_name = None
version_line_index = None
for index, line in enumerate(lines):
match = re.match(r"^\s*version:\s*([\d.]+)", line)
if match:
version_name = match.group(1)
version_line_index = index
break
if version_name is None:
log_error("pubspec.yaml 中未找到 version 字段")
sys.exit(1)
version_code_str = get_git_output(["rev-list", "--count", "HEAD"])
version_code = int(version_code_str) if version_code_str.isdigit() else 0
commit_hash = get_git_output(["rev-parse", "HEAD"]) or "unknown"
display_version = version_name
if platform_name == "android":
display_version = f"{version_name}-{commit_hash[:9]}"
version_full = f"{display_version}+{version_code}"
lines[version_line_index] = f"version: {version_full}"
Path("pubspec.yaml").write_text("\n".join(lines) + "\n", encoding="utf-8")
log_success(f"pubspec.yaml version → {version_full}")
Path("pili_release.json").write_text(
json.dumps(
{
"pili.name": display_version,
"pili.code": version_code,
"pili.hash": commit_hash,
"pili.time": int(time.time()),
},
separators=(",", ":"),
),
encoding="utf-8",
)
log_success("pili_release.json 已生成")
return VersionInfo(
version_name=display_version,
version_code=version_code,
version_full=version_full,
)
# ══════════════════════════════════════════════════════════════════
# 包名替换
# ① sed patch — pubspec name 字段 + Dart import 路径
# ② rename CLI — Android applicationId / iOS Bundle ID / 显示名
# ══════════════════════════════════════════════════════════════════
def replace_text_in_files(old: str, new: str, globs: list[str]) -> None:
import glob as _glob
for pattern in globs:
for file_name in _glob.glob(pattern, recursive=True):
file_path = Path(file_name)
if not file_path.is_file() or any(
part in SKIP_RENAME_DIR_NAMES for part in file_path.parts
):
continue
try:
content = file_path.read_text(encoding="utf-8")
if old in content:
file_path.write_text(content.replace(old, new), encoding="utf-8")
log_info(f" sed: {file_name}")
except Exception as exc:
log_warning(f" 跳过 {file_name}: {exc}")
def rename_with_search_replace(args: argparse.Namespace) -> None:
"""
sed 负责:
• pubspec.yaml name: 字段
• Dart 文件中 package:old_name/ → package:new_name/
• 原生代码中 com/old/pkg 目录路径形式
• 仓库 URL github.com/old/repo → github.com/new/repo
rename CLI 负责:
• Android applicationId(build.gradle / AndroidManifest)
• iOS/macOS Bundle Identifier(plist / pbxproj)
• 显示名(android:label / CFBundleDisplayName)
"""
orig_name = args.original_app_name
new_name = args.app_name
orig_pkg = args.original_pkg_id
new_pkg = args.pkg_id
dart_globs = ["**/*.dart", "pubspec.yaml"]
native_globs = [
"android/**/*.kt",
"android/**/*.java",
"android/**/*.xml",
"android/**/*.gradle",
"android/**/*.gradle.kts",
"android/**/*.properties",
"ios/**/*.plist",
"ios/**/*.pbxproj",
"macos/**/*.plist",
"macos/**/*.pbxproj",
"linux/**/*.cmake",
"linux/**/*.cc",
"windows/runner/*.rc",
"windows/runner/*.h",
"windows/runner/*.cpp",
"windows/CMakeLists.txt",
"assets/linux/com.example.piliplus.desktop",
"assets/linux/DEBIAN/*",
]
if new_name and orig_name and new_name != orig_name:
log_step(f"sed patch: pubspec name {orig_name} → {new_name}")
# Dart import 路径
replace_text_in_files(
f"package:{orig_name}/", f"package:{new_name}/", dart_globs
)
# pubspec.yaml name: 字段
content = read_pubspec_text()
content = re.sub(
r"^(name:\s+)" + re.escape(orig_name),
r"\g<1>" + new_name,
content,
flags=re.MULTILINE,
)
Path("pubspec.yaml").write_text(content, encoding="utf-8")
log_success("pubspec.yaml name 字段已更新")
if new_pkg and orig_pkg and new_pkg != orig_pkg:
old_path = orig_pkg.replace(".", "/")
new_path = new_pkg.replace(".", "/")
if old_path != new_path:
log_step(f"sed patch: 包路径 {old_path} → {new_path}")
replace_text_in_files(old_path, new_path, native_globs)
# ── 仓库 URL 替换 ──
if args.repo and args.original_repo and args.repo != args.original_repo:
log_step(f"sed patch: 仓库 URL {args.original_repo} → {args.repo}")
repo_globs = [
"assets/linux/DEBIAN/*",
"lib/**/*.dart",
"windows/packaging/exe/make_config.yaml",
]
replace_text_in_files(args.original_repo, args.repo, repo_globs)
def rename_with_cli(args: argparse.Namespace) -> None:
if not args.pkg_id and not args.app_name:
return
log_step("rename CLI(Bundle ID / 显示名)")
require_command("rename", "请先运行:dart pub global activate rename")
if args.pkg_id:
log_info(f"setBundleId → {args.pkg_id}")
run_command(["rename", "setBundleId", "--value", args.pkg_id])
if args.app_name:
log_info(f"setAppName → {args.app_name}")
run_command(["rename", "setAppName", "--value", args.app_name])
def apply_project_rename(args: argparse.Namespace) -> None:
if args.skip_rename:
return
rename_with_search_replace(args)
rename_with_cli(args)
# ══════════════════════════════════════════════════════════════════
# Git Tools
# ══════════════════════════════════════════════════════════════════
def stash_worktree_changes(cwd: str | None) -> bool:
result = run_command(
["git", "stash", "push", "--include-untracked", "-m", "build.py-temp"],
cwd=cwd,
check=False,
capture_output=True,
)
stash_output = (result.stdout or "") + (result.stderr or "")
if result.returncode != 0:
log_warning("git stash 失败,继续尝试应用补丁")
return False
return "No local changes to save" not in stash_output
def restore_stashed_changes(cwd: str | None, has_stash: bool) -> None:
if not has_stash:
return
result = run_command(
["git", "stash", "pop"],
cwd=cwd,
check=False,
capture_output=True,
)
if result.returncode != 0:
log_warning("git stash pop 失败,请手动检查 Flutter SDK 仓库状态")
def abort_git_sequence(operation: str, cwd: str | None) -> None:
run_command(
["git", operation, "--abort"],
cwd=cwd,
check=False,
capture_output=True,
)
def apply_git_revert(
commit_hash: str,
cwd: str | None = None,
finished_message: str = "已回滚",
bad_message: str = "回滚失败",
) -> None:
has_stash = stash_worktree_changes(cwd)
try:
run_command(["git", "config", "user.name", "ci"], cwd=cwd)
run_command(["git", "config", "user.email", "ci@example.com"], cwd=cwd)
run_command(["git", "revert", commit_hash, "--no-edit"], cwd=cwd)
except subprocess.CalledProcessError:
abort_git_sequence("revert", cwd)
log_warning(bad_message)
else:
run_command(["git", "reset", "--soft", "HEAD~1"], cwd=cwd)
log_success(finished_message)
finally:
restore_stashed_changes(cwd, has_stash)
def apply_git_cherry_pick(
commit_hash: str,
cwd: str | None = None,
finished_message: str = "已应用 cherry-pick",
bad_message: str = "应用 cherry-pick 失败",
) -> None:
has_stash = stash_worktree_changes(cwd)
try:
run_command(["git", "config", "user.name", "ci"], cwd=cwd)
run_command(["git", "config", "user.email", "ci@example.com"], cwd=cwd)
run_command(["git", "cherry-pick", commit_hash, "--no-edit"], cwd=cwd)
except subprocess.CalledProcessError:
abort_git_sequence("cherry-pick", cwd)
log_warning(bad_message)
else:
run_command(["git", "reset", "--soft", "HEAD~1"], cwd=cwd)
log_success(finished_message)
finally:
restore_stashed_changes(cwd, has_stash)
def apply_git_patch(
patch_file: str | Path,
cwd: str | None = None,
notfound_message: str = "patch 不存在,跳过",
finished_message: str = "已应用 patch",
bad_message: str = "应用 patch 失败",
) -> None:
patch_file = Path(patch_file).resolve()
if not patch_file.exists():
log_warning(notfound_message)
return
try:
run_command(["git", "apply", str(patch_file), "--ignore-whitespace"], cwd=cwd)
log_success(finished_message)
except subprocess.CalledProcessError:
log_warning(bad_message)
# ══════════════════════════════════════════════════════════════════
# Flutter patches & pub get
# ══════════════════════════════════════════════════════════════════
def find_flutter_root() -> str | None:
flutter_binary = shutil.which("flutter")
return str(Path(flutter_binary).parent.parent) if flutter_binary else None
def apply_flutter_patches(root: str, platform_name: str = "") -> None:
log_step("应用 Flutter patches")
# Android 专属:revert overscroll indicator(https://github.com/flutter/flutter/issues/182281)
if platform_name == "android":
revert_hash = "362b1de29974ffc1ed6faa826e1df870d7bec75f"
log_step(f"revert overscroll indicator commit ({revert_hash[:9]}…)")
apply_git_revert(
revert_hash,
cwd=root,
finished_message="overscroll indicator revert 完成",
bad_message="overscroll indicator revert 失败(已忽略)",
)
if platform_name in ["linux", "macos", "windows"]:
checkout_hash = "56956c33ef102ac0b5fc46b62bd2dd9f50a86616"
log_step(
f"checkout Add RawTooltip.ignorePointer commit ({checkout_hash[:9]}…)"
)
apply_git_cherry_pick(
checkout_hash,
cwd=root,
finished_message="Add RawTooltip.ignorePointer 应用完成",
bad_message="Add RawTooltip.ignorePointer 应用失败(已忽略)",
)
for name in [
"bottom_sheet.patch",
"modal_barrier.patch",
"mouse_cursor.patch",
]:
patch_path = Path("lib/scripts") / name
apply_git_patch(
patch_path,
cwd=root,
notfound_message=f"patch 不存在,跳过: {name}",
finished_message=f"Patch OK: {name}",
bad_message=f"Patch 应用失败(已忽略): {name}",
)
def run_common_setup(args: argparse.Namespace) -> str | None:
flutter_root_dir = find_flutter_root()
if args.apply_patches and flutter_root_dir:
apply_flutter_patches(flutter_root_dir, platform_name=args.platform)
if not args.no_pub_get:
log_step("flutter pub get")
run_command(["flutter", "pub", "get"])
return flutter_root_dir
# ══════════════════════════════════════════════════════════════════
# 构建参数组装
# ══════════════════════════════════════════════════════════════════
def build_dart_define_args(args: argparse.Namespace) -> list[str]:
dart_define_args: list[str] = []
if args.dart_define_from_file and Path(args.dart_define_from_file).exists():
dart_define_args += ["--dart-define-from-file", args.dart_define_from_file]
for dart_define in args.dart_define or []:
dart_define_args += ["--dart-define", dart_define]
return dart_define_args
def ensure_output_dir(args: argparse.Namespace) -> Path:
output_dir = Path(args.output)
output_dir.mkdir(parents=True, exist_ok=True)
return output_dir
# ══════════════════════════════════════════════════════════════════
# Android 签名
# ══════════════════════════════════════════════════════════════════
def configure_android_signing(args: argparse.Namespace) -> bool:
if not args.sign:
return False
log_step("配置 Android 签名")
key_jks = Path("android/app/key.jks")
key_prop = Path("android/key.properties")
if args.keystore_base64:
import base64
key_jks.write_bytes(base64.b64decode(args.keystore_base64))
log_success("keystore 已从 base64 写入")
elif args.keystore_file:
shutil.copy2(args.keystore_file, key_jks)
log_success(f"keystore 已复制: {args.keystore_file}")
elif key_jks.exists():
log_success("使用已有 android/app/key.jks")
else:
log_warning("--sign 已设置,但无 keystore,将用 debug 签名")
return False
if not args.key_alias:
log_error("签名需要 --key-alias")
sys.exit(1)
if not args.key_password:
log_error("签名需要 --key-password")
sys.exit(1)
key_prop.write_text(
f"storeFile=key.jks\nstorePassword={args.store_password or args.key_password}\n"
f"keyAlias={args.key_alias}\nkeyPassword={args.key_password}\n",
encoding="utf-8",
)
log_success("android/key.properties 已写入")
return True
def cleanup_android_signing_files() -> None:
for file_path in [Path("android/app/key.jks"), Path("android/key.properties")]:
if file_path.exists():
file_path.unlink()
log_info(f"已清理: {file_path}")
# ══════════════════════════════════════════════════════════════════
# Android
# ══════════════════════════════════════════════════════════════════
def build_android(args: argparse.Namespace, version_info: VersionInfo) -> None:
log_step("构建 Android")
signed = configure_android_signing(args)
try:
run_command(
[
"flutter",
"build",
"apk",
"--release",
"--pub",
*([] if args.no_split else ["--split-per-abi"]),
*build_dart_define_args(args),
*(args.extra_build_args or []),
]
)
finally:
if signed and args.clean_keys:
cleanup_android_signing_files()
log_step("重命名 APK")
apk_dir = Path("build/app/outputs/flutter-apk")
output_dir = ensure_output_dir(args)
apks = list(apk_dir.glob("app-*-release.apk")) or list(
apk_dir.glob("app-release.apk")
)
for apk in apks:
match = re.search(r"app-(.+)-release\.apk", apk.name)
abi = match.group(1) if match else "universal"
destination = (
output_dir
/ f"{args.output_prefix}_android_{version_info.version_full}_{abi}.apk"
)
shutil.copy2(apk, destination)
log_success(f"输出: {destination}")
# ══════════════════════════════════════════════════════════════════
# iOS
# ══════════════════════════════════════════════════════════════════
def build_ios(args: argparse.Namespace, version_info: VersionInfo) -> None:
if platform_module.system() != "Darwin":
log_error("iOS 构建只能在 macOS")
sys.exit(1)
log_step("构建 iOS")
run_command(
[
"flutter",
"build",
"ios",
"--release",
"--no-codesign",
*build_dart_define_args(args),
*(args.extra_build_args or []),
]
)
log_step("打包 IPA")
ipa_name = f"{args.output_prefix}_ios_{version_info.version_full}.ipa"
payload = Path("Payload")
if payload.is_symlink() or payload.exists():
payload.unlink() if payload.is_symlink() else shutil.rmtree(payload)
payload.symlink_to("build/ios/iphoneos")
run_shell_command(
'find Payload/Runner.app/Frameworks -type d -name "*.framework" '
"-exec codesign --force --sign - --preserve-metadata=identifier,entitlements {} \\;",
check=False,
)
run_shell_command(f"zip -r9 {ipa_name} Payload/Runner.app")
output_path = ensure_output_dir(args) / ipa_name
shutil.move(ipa_name, output_path)
log_success(f"输出: {output_path}")
# ══════════════════════════════════════════════════════════════════
# macOS
# ══════════════════════════════════════════════════════════════════
def build_macos(args: argparse.Namespace, version_info: VersionInfo) -> None:
if platform_module.system() != "Darwin":
log_error("macOS 构建只能在 macOS")
sys.exit(1)
log_step("构建 macOS")
run_command(
[
"flutter",
"build",
"macos",
"--release",
*build_dart_define_args(args),
*(args.extra_build_args or []),
]
)
app = next(Path("build/macos/Build/Products/Release").glob("*.app"), None)
if not app:
log_error("未找到 .app")
sys.exit(1)
output_dir = ensure_output_dir(args)
if shutil.which("create-dmg"):
run_shell_command(f'create-dmg "{app}" || true', check=False)
dmgs = list(Path(".").glob("*.dmg"))
if dmgs:
destination = output_dir / f"{args.output_prefix}_macos_{version_info.version_full}.dmg"
shutil.move(str(dmgs[0]), destination)
log_success(f"输出: {destination}")
return
log_warning("create-dmg 未安装,fallback zip。npm i -g create-dmg")
destination = output_dir / f"{args.output_prefix}_macos_{version_info.version_full}.zip"
run_shell_command(f'zip -r9 "{destination}" "{app}"')
log_success(f"输出(zip): {destination}")
# ══════════════════════════════════════════════════════════════════
# Windows
# ══════════════════════════════════════════════════════════════════
def build_windows(args: argparse.Namespace, version_info: VersionInfo) -> None:
log_step("构建 Windows")
output_dir = ensure_output_dir(args)
prefix = args.output_prefix
version = version_info.version_full
if args.installer and shutil.which("fastforge"):
dart_define_arg = (
f"dart-define-from-file={args.dart_define_from_file}"
if args.dart_define_from_file and Path(args.dart_define_from_file).exists()
else ""
)
run_command(
[
"fastforge",
"package",
"--platform",
"windows",
"--targets",
"exe",
*(["--flutter-build-args", dart_define_arg] if dart_define_arg else []),
*(args.extra_build_args or []),
]
)
for exe in Path("dist").rglob("*.exe"):
destination = output_dir / f"{prefix}_windows_{version}_x64_setup.exe"
shutil.copy2(exe, destination)
log_success(f"输出 (installer): {destination}")
else:
if args.installer:
log_warning("fastforge 未找到,跳过 installer")
run_command(
[
"flutter",
"build",
"windows",
"--release",
*build_dart_define_args(args),
*(args.extra_build_args or []),
]
)
bundle = Path("build/windows/x64/runner/Release")
if bundle.exists():
base = output_dir / f"{prefix}_windows_{version}_x64_portable"
shutil.make_archive(str(base), "zip", bundle)
log_success(f"输出 (portable): {base}.zip")
# ══════════════════════════════════════════════════════════════════
# Linux 各打包格式
# ══════════════════════════════════════════════════════════════════
def resolve_package_name(args: argparse.Namespace) -> str:
return args.app_name or get_pubspec_name()
def package_tar_gz(prefix: str, version: str, arch: str, bundle: Path, output_dir: Path) -> None:
output_file = output_dir / f"{prefix}_linux_{version}_{arch}.tar.gz"
run_shell_command(f"tar -zcf {output_file} -C {bundle} .")
log_success(f"输出: {output_file}")
def package_tar_zst(
prefix: str,
version: str,
arch: str,
bundle: Path,
output_dir: Path,
) -> None:
"""标准 tar.zst,与 pacman 兼容的压缩格式(注意:不含 PKGINFO,不可 pacman -U)"""
require_command("zstd", "sudo pacman -S zstd / sudo apt install zstd")
output_file = output_dir / f"{prefix}_linux_{version}_{arch}.tar.zst"
run_shell_command(
f"tar --use-compress-program='zstd -19 -T0' -cf {output_file} -C {bundle} ."
)
log_success(f"输出: {output_file}")
def package_arch_package(
prefix: str,
version: str,
arch: str,
bundle: Path,
output_dir: Path,
args: argparse.Namespace,
) -> None:
"""
生成正规 Arch Linux .pkg.tar.zst
含 .PKGINFO / .MTREE,可直接 pacman -U 安装。
原理:生成 PKGBUILD + 预构建 bundle source tarball,
用 makepkg 打包(不重新编译 Flutter)。
"""
log_step("打包 Arch Linux .pkg.tar.zst(makepkg)")
require_command("makepkg", "请在 Arch Linux 系统上运行")
app_name = resolve_package_name(args)
# pacman 版本号不能含 + -,转成 _
pkg_ver = re.sub(r"[+\-]", "_", version)
pkg_arch = "x86_64" if arch == "x64" else arch
work = Path(f"/tmp/{prefix}_arch_build")
if work.exists():
shutil.rmtree(work)
work.mkdir(parents=True)
# ── 把 bundle 打成 source tarball(makepkg 需要 source 文件)──
src_name = f"{app_name}-{pkg_ver}"
src_dir = work / src_name
src_dir.mkdir()
shutil.copytree(bundle, src_dir / "bundle", dirs_exist_ok=True)
desktop = (
next(Path("assets/linux").glob("*.desktop"), None)
if Path("assets/linux").exists()
else None
)
logo = Path("assets/images/logo/logo.png")
if desktop:
shutil.copy2(desktop, src_dir / desktop.name)
if logo.exists():
shutil.copy2(logo, src_dir / f"{app_name}.png")
src_tar = work / f"{src_name}.tar.gz"
run_shell_command(f"tar -zcf {src_tar} -C {work} {src_name}")
sha256 = run_shell_command(
f"sha256sum {src_tar}", capture_output=True
).stdout.split()[0]
desktop_name = desktop.name if desktop else f"com.example.{app_name}.desktop"
# ── 写 PKGBUILD ──
(work / "PKGBUILD").write_text(
textwrap.dedent(f"""\
# Maintainer: auto-generated by build.py
pkgname={app_name}
pkgver={pkg_ver}
pkgrel=1
pkgdesc="Flutter App"
arch=('{pkg_arch}')
url="https://github.com"
license=('GPL-3.0-or-later')
depends=('gtk3' 'libayatana-appindicator' 'mpv')
source=("{src_name}.tar.gz")
sha256sums=('{sha256}')
package() {{
cd "$srcdir/{src_name}"
# 主体文件
install -dm755 "$pkgdir/opt/{app_name}"
cp -rdp --no-preserve=ownership bundle/. "$pkgdir/opt/{app_name}/"
chmod 755 "$pkgdir/opt/{app_name}/{app_name}"
# 启动脚本(处理 LD_LIBRARY_PATH,Arch 需要)
install -dm755 "$pkgdir/usr/bin"
cat > "$pkgdir/usr/bin/{app_name}" << 'LAUNCHER'
#!/bin/bash
LD_LIBRARY_PATH="/opt/{app_name}/lib:$LD_LIBRARY_PATH" exec "/opt/{app_name}/{app_name}" "$@"
LAUNCHER
chmod 755 "$pkgdir/usr/bin/{app_name}"
# 桌面文件
install -dm755 "$pkgdir/usr/share/applications"
[[ -f {desktop_name} ]] && install -Dm644 {desktop_name} \\
"$pkgdir/usr/share/applications/{desktop_name}"
# 图标
install -dm755 "$pkgdir/usr/share/icons/hicolor/512x512/apps"
[[ -f {app_name}.png ]] && install -Dm644 {app_name}.png \\
"$pkgdir/usr/share/icons/hicolor/512x512/apps/{app_name}.png"
}}
"""),
encoding="utf-8",
)
# ── 运行 makepkg ──
run_shell_command(
"makepkg -f --noconfirm",
cwd=work,
env={
**os.environ,
"PKGDEST": str(output_dir.resolve()),
"PACKAGER": "build.py <local>",
"SRCDEST": str(work),
},
)
shutil.rmtree(work, ignore_errors=True)
for package_file in output_dir.glob(f"{app_name}-{pkg_ver}*.pkg.tar.zst"):
log_success(f"输出: {package_file}")
def map_deb_architecture(arch: str) -> str:
return {
"x64": "amd64",
"amd64": "amd64",
"arm64": "arm64",
"aarch64": "arm64",
"armv7": "armhf",
"armhf": "armhf",
}.get(arch, arch)
def package_deb(
prefix: str,
version: str,
arch: str,
bundle: Path,
output_dir: Path,
args: argparse.Namespace,
) -> None:
log_step("打包 deb")
require_command("dpkg-deb")
app_name = resolve_package_name(args)
deb_arch = map_deb_architecture(arch)
root = Path(f"/tmp/{prefix}_deb")
if root.exists():
shutil.rmtree(root)
for d in [
"opt/app",
"usr/share/applications",
"usr/share/icons/hicolor/512x512/apps",
]:
(root / d).mkdir(parents=True)
shutil.copytree(bundle, root / "opt/app", dirs_exist_ok=True)
ctrl_src = Path("assets/linux/DEBIAN")
(root / "DEBIAN").mkdir(exist_ok=True)
if ctrl_src.exists():
shutil.copytree(ctrl_src, root / "DEBIAN", dirs_exist_ok=True)
ctrl = root / "DEBIAN/control"
if ctrl.exists():
txt = ctrl.read_text()
txt = txt.replace("version_need_change", version)
txt = re.sub(r"^Architecture:\s+\S+", f"Architecture: {deb_arch}", txt, flags=re.MULTILINE)
size_kb = (
sum(
f.stat().st_size
for f in (root / "opt/app").rglob("*")
if f.is_file()
)
// 1024