forked from chen08209/FlClash
-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathsetup.dart
More file actions
1040 lines (944 loc) · 32.5 KB
/
Copy pathsetup.dart
File metadata and controls
1040 lines (944 loc) · 32.5 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
// ignore_for_file: avoid_print
import 'dart:convert';
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:path/path.dart';
import 'package:crypto/crypto.dart';
enum Target {
windows,
linux,
android,
macos,
}
extension TargetExt on Target {
String get os {
if (this == Target.macos) {
return "darwin";
}
return name;
}
bool get same {
if (this == Target.android) {
return true;
}
if (Platform.isWindows && this == Target.windows) {
return true;
}
if (Platform.isLinux && this == Target.linux) {
return true;
}
if (Platform.isMacOS && this == Target.macos) {
return true;
}
return false;
}
String get dynamicLibExtensionName {
final String extensionName;
switch (this) {
case Target.android || Target.linux:
extensionName = ".so";
break;
case Target.windows:
extensionName = ".dll";
break;
case Target.macos:
extensionName = ".dylib";
break;
}
return extensionName;
}
String get executableExtensionName {
final String extensionName;
switch (this) {
case Target.windows:
extensionName = ".exe";
break;
default:
extensionName = "";
break;
}
return extensionName;
}
}
enum Mode { core, lib }
enum Arch { amd64, arm64, arm }
class BuildItem {
Target target;
Arch? arch;
String? archName;
BuildItem({
required this.target,
this.arch,
this.archName,
});
@override
String toString() =>
'BuildLibItem{target: $target, arch: $arch, archName: $archName}';
}
class Build {
static List<BuildItem> get buildItems => [
BuildItem(
target: Target.macos,
arch: Arch.arm64,
),
BuildItem(
target: Target.macos,
arch: Arch.amd64,
),
BuildItem(
target: Target.linux,
arch: Arch.arm64,
),
BuildItem(
target: Target.linux,
arch: Arch.amd64,
),
BuildItem(
target: Target.windows,
arch: Arch.amd64,
),
BuildItem(
target: Target.windows,
arch: Arch.arm64,
),
BuildItem(
target: Target.android,
arch: Arch.arm,
archName: 'armeabi-v7a',
),
BuildItem(
target: Target.android,
arch: Arch.arm64,
archName: 'arm64-v8a',
),
BuildItem(
target: Target.android,
arch: Arch.amd64,
archName: 'x86_64',
),
];
static String get appName => "FlClashX";
static String get coreName => "FlClashCore";
static String get libName => "libclash";
static String get outDir => join(current, libName);
static String get _coreDir => join(current, "core");
static String get _servicesDir => join(current, "services", "helper");
static String get distPath => join(current, "dist");
// Full release version for the User-Agent, taken from the CI tag
// (GITHUB_REF_NAME, e.g. "v0.4.1-pre.18"), baked in via --dart-define=APP_VERSION.
// Only a version tag counts — a branch name (e.g. "dev") is ignored, so local
// and branch builds fall back to the pubspec version at runtime.
static String get appVersion {
final ref = Platform.environment["GITHUB_REF_NAME"]?.trim() ?? "";
return RegExp(r'^v\d').hasMatch(ref) ? ref : "";
}
static String _getCc(BuildItem buildItem) {
final environment = Platform.environment;
if (buildItem.target == Target.android) {
final ndk = environment["ANDROID_NDK"];
assert(ndk != null);
final prebuiltDir =
Directory(join(ndk!, "toolchains", "llvm", "prebuilt"));
final prebuiltDirList = prebuiltDir.listSync();
final map = {
"armeabi-v7a": "armv7a-linux-androideabi21-clang",
"arm64-v8a": "aarch64-linux-android21-clang",
"x86": "i686-linux-android21-clang",
"x86_64": "x86_64-linux-android21-clang"
};
return join(
prebuiltDirList.first.path,
"bin",
map[buildItem.archName],
);
}
return "gcc";
}
static String tagsFor(Target target) =>
target == Target.android ? "with_gvisor,cmfa" : "with_gvisor";
static Future<void> exec(
List<String> executable, {
String? name,
Map<String, String>? environment,
String? workingDirectory,
bool runInShell = true,
}) async {
if (name != null) print("run $name");
final process = await Process.start(
executable[0],
executable.sublist(1),
environment: environment,
workingDirectory: workingDirectory,
runInShell: runInShell,
);
process.stdout.listen((data) {
print(utf8.decode(data, allowMalformed: true));
});
process.stderr.listen((data) {
print(utf8.decode(data, allowMalformed: true));
});
final exitCode = await process.exitCode;
if (exitCode != 0 && name != null) throw "$name error";
}
static Future<String> calcSha256(String filePath) async {
final file = File(filePath);
if (!await file.exists()) {
throw "File not exists";
}
final stream = file.openRead();
return sha256.convert(await stream.reduce((a, b) => a + b)).toString();
}
/// Reads mihomo version from [core/go.mod] (single source of truth).
static Future<String> extractCoreVersion() async {
final goMod = File(join("core", "go.mod"));
if (!await goMod.exists()) {
throw "core/go.mod file not found";
}
final content = await goMod.readAsString();
final match = RegExp(r'github\.com/metacubex/mihomo\s+(v[\d.]+)').firstMatch(content);
if (match == null) {
throw "Could not extract mihomo version from core/go.mod";
}
return match.group(1)!;
}
/// Writes [lib/core_version.dart] so Flutter can show the same version without dart-define.
static Future<void> syncCoreVersionDartFile() async {
final v = await extractCoreVersion();
final out = File(join(current, "lib", "core_version.dart"));
await out.writeAsString(
"// GENERATED by setup.dart from core/constant/version.go — do not edit by hand\n"
"// ignore_for_file: constant_identifier_names\n"
"\n"
"/// Embedded mihomo version (see core/constant/version.go).\n"
"const String kCoreVersionFromSource = '$v';\n",
);
}
static Future<List<String>> buildCore({
required Mode mode,
required Target target,
required String coreVersion,
Arch? arch,
}) async {
final isLib = mode == Mode.lib;
final items = buildItems.where(
(element) =>
element.target == target &&
(arch == null ? true : element.arch == arch),
).toList();
final List<String> corePaths = [];
final targetOutFilePath = join(outDir, target.name);
final targetOutFile = File(targetOutFilePath);
if (await targetOutFile.exists()) {
await targetOutFile.delete(recursive: true);
await Directory(targetOutFilePath).create(recursive: true);
}
for (final item in items) {
final outFilePath = join(targetOutFilePath, item.archName);
final file = File(outFilePath);
if (file.existsSync()) {
file.deleteSync(recursive: true);
}
final fileName = isLib
? "$libName${item.target.dynamicLibExtensionName}"
: "$coreName${item.target.executableExtensionName}";
final realOutPath = join(outFilePath, fileName);
corePaths.add(realOutPath);
final Map<String, String> env = {};
env["GOOS"] = item.target.os;
if (item.arch != null) {
env["GOARCH"] = item.arch!.name;
}
if (isLib) {
env["CGO_ENABLED"] = "1";
env["CC"] = _getCc(item);
env["CFLAGS"] = "-O3 -Werror";
} else {
env["CGO_ENABLED"] = "0";
}
final execLines = [
"go",
"build",
"-ldflags=-w -s -X github.com/metacubex/mihomo/constant.Version=$coreVersion",
"-tags=${tagsFor(target)}",
if (isLib) "-buildmode=c-shared",
"-o",
realOutPath,
];
await exec(
execLines,
name: "build core",
environment: env,
workingDirectory: _coreDir,
);
if (isLib && item.archName != null) {
await adjustLibOut(
targetOutFilePath: targetOutFilePath,
outFilePath: outFilePath,
archName: item.archName!,
);
}
}
return corePaths;
}
static Future<void> adjustLibOut({
required String targetOutFilePath,
required String outFilePath,
required String archName,
}) async {
final includesPath = join(targetOutFilePath, "includes");
final realOutPath = join(includesPath, archName);
await Directory(realOutPath).create(recursive: true);
final targetOutFiles = Directory(outFilePath).listSync();
final coreFiles = Directory(_coreDir).listSync();
for (final file in [...targetOutFiles, ...coreFiles]) {
if (!file.path.endsWith('.h')) {
continue;
}
final targetFilePath = join(realOutPath, basename(file.path));
final realFile = File(file.path);
await realFile.copy(targetFilePath);
if (coreFiles.contains(file)) {
continue;
}
await realFile.delete();
}
}
static buildHelper(Target target, String token, {Arch? arch}) async {
final List<String> buildArgs = [
"cargo",
"build",
"--release",
"--features",
"windows-service",
];
// Add target for cross-compilation
if (arch == Arch.arm64 && target == Target.windows) {
buildArgs.addAll(["--target", "aarch64-pc-windows-msvc"]);
}
await exec(
buildArgs,
environment: {
"TOKEN": token,
},
name: "build helper",
workingDirectory: _servicesDir,
);
// Determine output path based on architecture
final String releasePath;
if (arch == Arch.arm64 && target == Target.windows) {
releasePath = join(_servicesDir, "target", "aarch64-pc-windows-msvc", "release");
} else {
releasePath = join(_servicesDir, "target", "release");
}
final outPath = join(
releasePath,
"helper${target.executableExtensionName}",
);
final targetPath = join(
outDir,
target.name,
"FlClashHelperService${target.executableExtensionName}",
);
await File(outPath).copy(targetPath);
}
static List<String> getExecutable(String command) => command.split(" ");
static String readVersion() {
final pubspec = File(join(current, "pubspec.yaml")).readAsStringSync();
final match = RegExp(r'version:\s*(.+)').firstMatch(pubspec);
return match?.group(1)?.split('+').first ?? "0.0.0";
}
static copyFile(String sourceFilePath, String destinationFilePath) {
final sourceFile = File(sourceFilePath);
if (!sourceFile.existsSync()) {
throw "SourceFilePath not exists";
}
final destinationFile = File(destinationFilePath);
final destinationDirectory = destinationFile.parent;
if (!destinationDirectory.existsSync()) {
destinationDirectory.createSync(recursive: true);
}
try {
sourceFile.copySync(destinationFilePath);
print("File copied successfully!");
} catch (e) {
print("Failed to copy file: $e");
}
}
}
class BuildCommand extends Command {
Target target;
BuildCommand({
required this.target,
}) {
if (target == Target.android || target == Target.linux) {
argParser.addOption(
"arch",
valueHelp: arches.map((e) => e.name).join(','),
help: 'The $name build desc',
);
} else {
argParser.addOption(
"arch",
help: 'The $name build archName',
);
}
argParser.addOption(
"out",
valueHelp: [
if (target.same) "app",
"core",
].join(','),
help: 'The $name build arch',
);
argParser.addOption(
"env",
valueHelp: [
"pre",
"stable",
].join(','),
help: 'The $name build env',
);
if (target == Target.windows) {
argParser.addFlag(
"msix",
help: "Build MSIX package for Microsoft Store",
defaultsTo: false,
);
}
}
@override
String get description => "build $name application";
@override
String get name => target.name;
List<Arch> get arches => Build.buildItems
.where((element) => element.target == target && element.arch != null)
.map((e) => e.arch!)
.toList();
_getLinuxDependencies(Arch arch) async {
await Build.exec(
Build.getExecutable("sudo apt update -y"),
);
await Build.exec(
Build.getExecutable("sudo apt install -y ninja-build libgtk-3-dev"),
);
await Build.exec(
Build.getExecutable("sudo apt install -y libayatana-appindicator3-dev"),
);
await Build.exec(
Build.getExecutable("sudo apt-get install -y libkeybinder-3.0-dev"),
);
await Build.exec(
Build.getExecutable("sudo apt install -y locate"),
);
if (arch == Arch.amd64) {
await Build.exec(
Build.getExecutable("sudo apt install -y rpm patchelf"),
);
await Build.exec(
Build.getExecutable("sudo apt install -y libfuse2"),
);
final downloadName = arch == Arch.amd64 ? "x86_64" : "aarch64";
await Build.exec(
Build.getExecutable(
"wget -O appimagetool https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-$downloadName.AppImage",
),
);
await Build.exec(
Build.getExecutable(
"chmod +x appimagetool",
),
);
await Build.exec(
Build.getExecutable(
"sudo mv appimagetool /usr/local/bin/",
),
);
}
}
_getMacosDependencies() async {
await Build.exec(
Build.getExecutable("npm install -g create-dmg"),
);
}
_buildMacosApp({
required Arch arch,
required String env,
required String coreVersion,
}) async {
await Build.exec(
name: "flutter build macos",
[
"flutter",
"build",
"macos",
"--release",
"--dart-define=APP_ENV=$env",
"--dart-define=CORE_VERSION=$coreVersion",
"--dart-define=APP_VERSION=${Build.appVersion}",
],
);
final pubspecFile = File(join(current, "pubspec.yaml"));
final pubspecContent = pubspecFile.readAsStringSync();
final versionMatch = RegExp(r'version:\s*(.+)').firstMatch(pubspecContent);
final version = versionMatch?.group(1)?.split('+').first ?? "0.0.0";
final appName = Build.appName;
final appPath = join(current, "build", "macos", "Build", "Products",
"Release", "$appName.app");
final distDir = Directory(Build.distPath);
if (!distDir.existsSync()) {
distDir.createSync(recursive: true);
}
print("Creating DMG with create-dmg...");
await Build.exec(
name: "create-dmg",
[
"create-dmg",
"--overwrite",
"--dmg-title",
appName,
appPath,
Build.distPath,
],
);
final createdDmgName = "$appName $version.dmg";
final createdDmgPath = join(Build.distPath, createdDmgName);
final targetDmgName = "$appName-macos-${arch.name}.dmg";
final targetDmgPath = join(Build.distPath, targetDmgName);
final createdDmg = File(createdDmgPath);
if (createdDmg.existsSync()) {
final targetDmg = File(targetDmgPath);
if (targetDmg.existsSync()) {
targetDmg.deleteSync();
}
createdDmg.renameSync(targetDmgPath);
print("✅ DMG created: $targetDmgPath");
} else {
throw "DMG file not created: $createdDmgPath";
}
}
_buildWindowsApp({
required Arch arch,
required String env,
required String coreVersion,
required String token,
bool msix = false,
}) async {
await Build.exec(
name: "flutter build windows",
[
"flutter", "build", "windows", "--release",
"--dart-define=APP_ENV=$env",
"--dart-define=CORE_SHA256=$token",
"--dart-define=CORE_VERSION=$coreVersion",
"--dart-define=APP_VERSION=${Build.appVersion}",
],
);
final winArch = arch == Arch.arm64 ? "arm64" : "x64";
final buildDir = join(current, "build", "windows", winArch, "runner", "Release");
final version = Build.readVersion();
final distDir = Directory(Build.distPath);
if (!distDir.existsSync()) distDir.createSync(recursive: true);
final archName = arch.name;
final zipName = "${Build.appName}-windows-$archName.zip";
final zipPath = join(Build.distPath, zipName);
await Build.exec(
name: "create zip",
["powershell", "Compress-Archive", "-Path", "$buildDir\\*", "-DestinationPath", zipPath, "-Force"],
);
print("✅ ZIP created: $zipPath");
final issTemplate = File(join(current, "windows", "packaging", "exe", "inno_setup.iss"));
if (issTemplate.existsSync()) {
final issContent = issTemplate.readAsStringSync()
.replaceAll("{{APP_ID}}", "728B3532-C74B-4870-9068-BE70FE12A3E6")
.replaceAll("{{APP_VERSION}}", version)
.replaceAll("{{DISPLAY_NAME}}", Build.appName)
.replaceAll("{{PUBLISHER_NAME}}", "pluralplay")
.replaceAll("{{PUBLISHER_URL}}", "https://github.com/pluralplay/FlClashX")
.replaceAll("{{INSTALL_DIR_NAME}}", "{autopf}\\${Build.appName}")
.replaceAll("{{OUTPUT_BASE_FILENAME}}", "${Build.appName}-windows-$archName-setup")
.replaceAll("{{SETUP_ICON_FILE}}", join(current, "windows", "runner", "resources", "app_icon.ico"))
.replaceAll("{{PRIVILEGES_REQUIRED}}", "admin")
.replaceAll("{{ARCH}}", archName == "amd64" ? "x64compatible" : "arm64")
.replaceAll("{{SOURCE_DIR}}", buildDir)
.replaceAll("{{EXECUTABLE_NAME}}", "${Build.appName}.exe");
var processed = issContent;
final locales = [
{"lang": "ru"},
{"lang": "en"},
];
final langLines = <String>[];
for (final locale in locales) {
final lang = locale["lang"]!;
if (lang == "en") langLines.add('Name: "english"; MessagesFile: "compiler:Default.isl"');
if (lang == "ru") langLines.add('Name: "russian"; MessagesFile: "compiler:Languages\\Russian.isl"');
}
processed = processed.replaceAll(
RegExp(r'\{% for locale in LOCALES %\}.*?\{% endfor %\}', dotAll: true),
langLines.join('\n'),
);
processed = processed.replaceAllMapped(
RegExp(r"\{%\s*if\s+PRIVILEGES_REQUIRED\s*==\s*'admin'\s*%\}(.*?)\{%\s*endif\s*%\}", dotAll: true),
(m) => m.group(1)!,
);
final issOut = File(join(Build.distPath, "setup.iss"));
issOut.writeAsStringSync(processed);
await Build.exec(
name: "inno setup",
[r"C:\Program Files (x86)\Inno Setup 6\ISCC.exe", issOut.path],
);
issOut.deleteSync();
print("✅ EXE installer created");
}
if (msix) {
await Build.exec(
name: "create msix",
["dart", "run", "msix:create"],
);
final winArch2 = arch == Arch.arm64 ? "arm64" : "x64";
final msixDir = join(current, "build", "windows", winArch2, "runner", "Release");
final msixFiles = Directory(msixDir).listSync().where((f) => f.path.endsWith(".msix"));
if (msixFiles.isNotEmpty) {
final msixOutPath = join(Build.distPath, "${Build.appName}-windows-${arch.name}.msix");
Build.copyFile(msixFiles.first.path, msixOutPath);
print("✅ MSIX created: $msixOutPath");
}
}
}
_buildLinuxApp({
required Arch arch,
required String env,
required String coreVersion,
}) async {
final targetMap = {
Arch.arm64: "linux-arm64",
Arch.amd64: "linux-x64",
};
await Build.exec(
name: "flutter build linux",
[
"flutter", "build", "linux", "--release",
"--target-platform=${targetMap[arch]}",
"--dart-define=APP_ENV=$env",
"--dart-define=CORE_VERSION=$coreVersion",
"--dart-define=APP_VERSION=${Build.appVersion}",
],
);
final version = Build.readVersion();
final appName = Build.appName;
final archName = arch.name;
final bundleDir = join(current, "build", "linux", targetMap[arch]!.replaceAll("linux-", ""), "release", "bundle");
final distDir = Directory(Build.distPath);
if (!distDir.existsSync()) distDir.createSync(recursive: true);
final iconPath = join(current, "assets", "images", "icon.png");
final debArch = arch == Arch.amd64 ? "amd64" : "arm64";
final rpmArch = arch == Arch.amd64 ? "x86_64" : "aarch64";
// --- DEB ---
final debRoot = join(current, "build", "deb_root");
final debInstallDir = join(debRoot, "opt", appName);
final debDesktopDir = join(debRoot, "usr", "share", "applications");
final debIconDir = join(debRoot, "usr", "share", "icons", "hicolor", "256x256", "apps");
final debControlDir = join(debRoot, "DEBIAN");
for (final d in [debInstallDir, debDesktopDir, debIconDir, debControlDir]) {
await Directory(d).create(recursive: true);
}
await Build.exec(["cp", "-r", "$bundleDir/.", debInstallDir]);
File(join(debIconDir, "$appName.png")).writeAsBytesSync(File(iconPath).readAsBytesSync());
File(join(debDesktopDir, "com.follow.clashx.desktop")).writeAsStringSync(
"[Desktop Entry]\n"
"Type=Application\n"
"Name=$appName\n"
"GenericName=$appName\n"
"Comment=$appName\n"
"Exec=/opt/$appName/$appName\n"
"Icon=$appName\n"
"Terminal=false\n"
"Categories=Network;\n"
"Keywords=FlClashX;Clash;Proxy;\n"
"StartupNotify=true\n",
);
File(join(debControlDir, "control")).writeAsStringSync(
"Package: flclashx\n"
"Version: $version\n"
"Section: x11\n"
"Priority: optional\n"
"Architecture: $debArch\n"
"Depends: libayatana-appindicator3-dev, libkeybinder-3.0-dev\n"
"Maintainer: pluralplay <mail@pluralplay.rw>\n"
"Description: $appName\n",
);
final debPath = join(Build.distPath, "$appName-linux-$archName.deb");
await Build.exec(name: "build deb", ["dpkg-deb", "--build", debRoot, debPath]);
await Directory(debRoot).delete(recursive: true);
print("✅ DEB created: $debPath");
// --- RPM (amd64 only) ---
if (arch == Arch.amd64) {
final rpmBuildRoot = join(current, "build", "rpm_root");
final rpmInstallDir = join(rpmBuildRoot, "opt", appName);
final rpmDesktopDir = join(rpmBuildRoot, "usr", "share", "applications");
final rpmIconDir = join(rpmBuildRoot, "usr", "share", "icons", "hicolor", "256x256", "apps");
for (final d in [rpmInstallDir, rpmDesktopDir, rpmIconDir]) {
await Directory(d).create(recursive: true);
}
await Build.exec(["cp", "-r", "$bundleDir/.", rpmInstallDir]);
File(join(rpmIconDir, "$appName.png")).writeAsBytesSync(File(iconPath).readAsBytesSync());
File(join(rpmDesktopDir, "com.follow.clashx.desktop")).writeAsStringSync(
"[Desktop Entry]\n"
"Type=Application\n"
"Name=$appName\n"
"GenericName=$appName\n"
"Comment=$appName\n"
"Exec=/opt/$appName/$appName\n"
"Icon=$appName\n"
"Terminal=false\n"
"Categories=Network;\n"
"Keywords=FlClashX;Clash;Proxy;\n"
"StartupNotify=true\n",
);
final specPath = join(current, "build", "$appName.spec");
File(specPath).writeAsStringSync(
"Name: flclashx\n"
"Version: $version\n"
"Release: 1\n"
"Summary: $appName\n"
"License: Other\n"
"Group: Applications/Internet\n"
"Packager: pluralplay <mail@pluralplay.rw>\n"
"AutoReqProv: no\n"
"\n"
"%description\n"
"$appName proxy client\n"
"\n"
"%install\n"
"cp -r %{_builddir}/root/* %{buildroot}/\n"
"\n"
"%files\n"
"/opt/$appName/*\n"
"/usr/share/applications/com.follow.clashx.desktop\n"
"/usr/share/icons/hicolor/256x256/apps/$appName.png\n",
);
final rpmBuildDir = join(current, "build", "rpmbuild");
await Directory(join(rpmBuildDir, "BUILD", "root")).create(recursive: true);
await Build.exec(["cp", "-r", "$rpmBuildRoot/.", join(rpmBuildDir, "BUILD", "root")]);
await Build.exec(name: "build rpm", [
"rpmbuild", "-bb", specPath,
"--define", "_topdir $rpmBuildDir",
"--define", "_builddir ${join(rpmBuildDir, "BUILD")}",
"--target", rpmArch,
]);
final rpmOutputDir = join(rpmBuildDir, "RPMS", rpmArch);
final rpmFiles = Directory(rpmOutputDir).listSync().where((f) => f.path.endsWith(".rpm"));
if (rpmFiles.isNotEmpty) {
final rpmOutPath = join(Build.distPath, "$appName-linux-$archName.rpm");
Build.copyFile(rpmFiles.first.path, rpmOutPath);
print("✅ RPM created: $rpmOutPath");
}
await Directory(rpmBuildRoot).delete(recursive: true);
await Directory(rpmBuildDir).delete(recursive: true);
File(specPath).deleteSync();
}
// --- AppImage (amd64 only) ---
if (arch == Arch.amd64) {
final appDir = join(current, "build", "AppDir");
final appBinDir = join(appDir, "usr", "bin");
final appLibDir = join(appDir, "usr", "lib");
final appShareDesktop = join(appDir, "usr", "share", "applications");
final appShareIcon = join(appDir, "usr", "share", "icons", "hicolor", "256x256", "apps");
for (final d in [appBinDir, appLibDir, appShareDesktop, appShareIcon]) {
await Directory(d).create(recursive: true);
}
final bundleFiles = Directory(bundleDir).listSync();
for (final f in bundleFiles) {
final name = basename(f.path);
if (name == "lib") {
await Build.exec(["cp", "-r", f.path, appDir + "/usr/"]);
} else if (f is File) {
Build.copyFile(f.path, join(appBinDir, name));
} else {
await Build.exec(["cp", "-r", f.path, join(appBinDir, name)]);
}
}
// Bundle libkeybinder-3.0.so.0 — a system dependency of the global-hotkey
// plugin that is NOT part of the Flutter bundle. Without it the AppImage
// crashes on hosts that lack libkeybinder ("error while loading shared
// libraries: libkeybinder-3.0.so.0: cannot open shared object file").
var keybinderSrc = "";
final ldconfig = await Process.run(
"bash",
["-c", "ldconfig -p | grep -m1 'libkeybinder-3.0.so.0'"],
);
final ldOut = ldconfig.stdout.toString().trim();
if (ldOut.contains("=>")) {
keybinderSrc = ldOut.split("=>").last.trim();
}
if (keybinderSrc.isEmpty || !File(keybinderSrc).existsSync()) {
keybinderSrc = const [
"/usr/lib/x86_64-linux-gnu/libkeybinder-3.0.so.0",
"/usr/lib/libkeybinder-3.0.so.0",
"/lib/x86_64-linux-gnu/libkeybinder-3.0.so.0",
"/usr/local/lib/libkeybinder-3.0.so.0",
].firstWhere((p) => File(p).existsSync(), orElse: () => "");
}
if (keybinderSrc.isNotEmpty) {
// -L resolves the symlink so the real .so.0 file is copied into the AppDir.
await Build.exec(["cp", "-L", keybinderSrc, join(appLibDir, "libkeybinder-3.0.so.0")]);
print("✅ bundled libkeybinder from $keybinderSrc");
} else {
print("⚠️ libkeybinder-3.0.so.0 not found; AppImage may fail on hosts without it");
}
File(join(appShareIcon, "$appName.png")).writeAsBytesSync(File(iconPath).readAsBytesSync());
Build.copyFile(iconPath, join(appDir, "$appName.png"));
File(join(appShareDesktop, "com.follow.clashx.desktop")).writeAsStringSync(
"[Desktop Entry]\n"
"Type=Application\n"
"Name=$appName\n"
"GenericName=$appName\n"
"Comment=$appName\n"
"Exec=$appName\n"
"Icon=$appName\n"
"Terminal=false\n"
"Categories=Network;\n"
"Keywords=FlClashX;Clash;Proxy;\n"
"StartupNotify=true\n",
);
Build.copyFile(join(appShareDesktop, "com.follow.clashx.desktop"), join(appDir, "com.follow.clashx.desktop"));
File(join(appDir, "AppRun")).writeAsStringSync(
"#!/bin/bash\n"
'SELF=\$(readlink -f "\$0")\n'
'HERE=\${SELF%/*}\n'
'export PATH="\${HERE}/usr/bin:\${PATH}"\n'
'export LD_LIBRARY_PATH="\${HERE}/usr/lib:\${LD_LIBRARY_PATH}"\n'
'exec "\${HERE}/usr/bin/$appName" "\$@"\n',
);
await Build.exec(["chmod", "+x", join(appDir, "AppRun")]);
await Build.exec(["chmod", "+x", join(appBinDir, appName)]);
final appImagePath = join(Build.distPath, "$appName-linux-$archName.AppImage");
await Build.exec(
name: "build AppImage",
["appimagetool", appDir, appImagePath],
environment: {"ARCH": "x86_64"},
);
await Directory(appDir).delete(recursive: true);
print("✅ AppImage created: $appImagePath");
}
}
_buildAndroidApp({
required String env,
required String coreVersion,
}) async {
final distDir = Directory(Build.distPath);
if (!distDir.existsSync()) distDir.createSync(recursive: true);
await Build.exec(
name: "flutter build apk (split)",
[
"flutter", "build", "apk", "--release",
"--split-per-abi",
"--dart-define=APP_ENV=$env",
"--dart-define=CORE_VERSION=$coreVersion",
"--dart-define=APP_VERSION=${Build.appVersion}",
],
);
final splitDir = join(current, "build", "app", "outputs", "flutter-apk");
final archMap = {
"app-arm64-v8a-release.apk": "${Build.appName}-android-arm64-v8a.apk",
"app-armeabi-v7a-release.apk": "${Build.appName}-android-armeabi-v7a.apk",
"app-x86_64-release.apk": "${Build.appName}-android-x86_64.apk",
};
for (final f in Directory(splitDir).listSync()) {
final name = basename(f.path);
if (archMap.containsKey(name)) {
Build.copyFile(f.path, join(Build.distPath, archMap[name]!));
}
}
await Build.exec(
name: "flutter build apk (universal)",
[
"flutter", "build", "apk", "--release",
"--dart-define=APP_ENV=$env",
"--dart-define=CORE_VERSION=$coreVersion",
"--dart-define=APP_VERSION=${Build.appVersion}",
],
);
Build.copyFile(
join(splitDir, "app-release.apk"),
join(Build.distPath, "${Build.appName}-android-universal.apk"),
);
print("✅ APKs created in ${Build.distPath}");
}
Future<String?> get systemArch async {
if (Platform.isWindows) {
return Platform.environment["PROCESSOR_ARCHITECTURE"];
} else if (Platform.isLinux || Platform.isMacOS) {
final result = await Process.run('uname', ['-m']);
return result.stdout.toString().trim();
}
return null;
}
@override
Future<void> run() async {
final mode = target == Target.android ? Mode.lib : Mode.core;
final String out = argResults?["out"] ?? (target.same ? "app" : "core");
final archName = argResults?["arch"];
final env = argResults?["env"] ?? "pre";
final currentArches =
arches.where((element) => element.name == archName).toList();
final arch = currentArches.isEmpty ? null : currentArches.first;
if (arch == null && target != Target.android) {
throw "Invalid arch parameter";
}
await Build.syncCoreVersionDartFile();
final coreVersion = await Build.extractCoreVersion();
final corePaths = await Build.buildCore(
target: target,
arch: arch,
mode: mode,
coreVersion: coreVersion,
);
if (out != "app") {
return;
}
switch (target) {
case Target.windows:
final token = await Build.calcSha256(corePaths.first);
final buildMsix = argResults?["msix"] == true;
await Build.buildHelper(target, token, arch: arch);
await _buildWindowsApp(
arch: arch!,