This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
make.dart
264 lines (226 loc) · 7.95 KB
/
make.dart
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 'dart:io';
import 'lib/constants.dart';
import 'versions.dart' as vers;
//import 'package:flutter_tools/src/build_system/build_system.dart';
late String shortVersion;
late String version;
late String buildNumber;
String appinfo(version, buildnum) => """const String appVersion = '$version';
const String buildNumber = '$buildnum';
""";
final flags = '--release --suppress-analytics --split-debug-info=bin';
final winFlags = flags;
final linuxX86Flags = '$flags --target-platform linux-x64';
final linuxARMFlags = '$flags --target-platform linux-arm64';
String get pkgFlags =>
'$flags --build-name=$version --build-number $buildNumber';
String get iosFlags => '$pkgFlags --no-codesign';
//--target-platform android-arm,android-arm64,android-x64
String get apkFlags => '$pkgFlags --shrink';
String get aabFlags => apkFlags;
String get macFlags => pkgFlags;
final testFlags =
'--coverage -j 100 --test-randomize-ordering-seed random -r expanded';
Future<String> system(
String cmd, {
bool throwOnFail = false,
bool printInput = true,
bool printOutput = true,
bool powershell = false,
}) async {
if (printInput) stderr.writeln(cmd);
final p = await (Platform.isWindows
? (powershell
? Process.run('powershell', ['-command', cmd])
: Process.run('cmd', ['/c', cmd]))
: Process.run('sh', ['-c', cmd]));
if (printOutput) {
stderr.write(p.stderr);
stderr.write(p.stdout);
}
if (p.exitCode != 0 && throwOnFail) throw p.exitCode;
return p.stdout.trimRight();
}
Future<String> readfile(path) async => File(path).readAsString();
Future writefile(path, content) async => File(path).writeAsString(content);
Future<void> rm(f) async {
if (await File(f).exists()) {
await File(f).delete();
}
}
Future<void> rmd(d) async {
if (await Directory(d).exists()) {
await Directory(d).delete(recursive: true);
}
}
Future cp(from, to) => File(from).copy(to);
Future<String> mv(from, to) =>
File(from).rename(to).then((value) => value.path);
Future mvd(from, to) => Directory(from).rename(to);
Future zip(from, to, [rootDir = '.']) => system(
Platform.isWindows
? 'Compress-Archive -LiteralPath $from -DestinationPath $to'
: 'cd $rootDir && zip -r -9 $to $from',
throwOnFail: true,
powershell: true);
Future mkdirs(d) => Directory(d).create(recursive: true);
Future<String> md5(String path) =>
system("md5sum '$path' | cut -d' ' -f1", printOutput: false);
Future<void> flutter(String cmd, {bool throwOnFail = true}) =>
system('flutter $cmd', throwOnFail: throwOnFail);
Future build(String cmd, String flags) => flutter('build $cmd $flags');
Future<void> strip(String files) =>
system('strip -u -r $files', printOutput: false);
String aid(plat, arch) => '${AMP_APP.toLowerCase()}-$version-$plat-$arch';
String filename(o, plat, arch, ext) => '$o/${aid(plat, arch)}.$ext';
Future<void> iosapp([String o = 'bin']) async {
const buildDir = 'build/ios/Release-iphoneos/Runner.app';
await build('ios', iosFlags);
await system(
'xcrun bitcode_strip $buildDir/Frameworks/Flutter.framework/Flutter -r -o tmpfltr');
await mv('tmpfltr', '$buildDir/Frameworks/Flutter.framework/Flutter');
await system('rm -f $buildDir/Frameworks/libswift*');
await strip('$buildDir/Runner $buildDir/Frameworks/*.framework/*');
}
Future<String> ipa([String o = 'bin']) async {
//await flutter('build ipa $iosFlags');
await system('cp -rp build/ios/Release-iphoneos/Runner.app tmp/Payload');
await zip('Payload', 'tmp.ipa', 'tmp');
return await mv('tmp/tmp.ipa', filename(o, 'ios', 'arm64', 'ipa'));
}
Future<String> apk([String o = 'bin']) async {
await build('apk', apkFlags);
return await mv('build/app/outputs/flutter-apk/app-release.apk',
filename(o, 'android', 'universal', 'apk'));
}
Future<String> aab([String o = 'bin']) async {
await build('appbundle', aabFlags);
return await mv('build/app/outputs/bundle/release/app-release.aab',
filename(o, 'android', 'universal', 'aab'));
}
Future<void> test() async {
await flutter('test $testFlags');
await system('genhtml -o coverage/html coverage/lcov.info');
await system('lcov -l coverage/lcov.info');
}
Future<void> ios() async {
await iosapp();
await ipa();
}
Future<void> android() async {
await apk();
await aab();
}
Future win([String o = 'bin']) async {
await flutter('config --enable-windows-desktop');
await build('windows', winFlags);
final id = aid('windows', 'x86_64');
await mvd('build/windows/runner/Release', 'tmp/$id');
await zip('tmp/$id', '$o/$id.zip');
}
Future<String> mac([String o = 'bin']) async {
await flutter('config --enable-macos-desktop');
await build('macos', macFlags);
final file = filename(o, 'macos', 'x86_64', 'dmg');
await system('cp -r build/macos/Build/Products/Release/$AMP_APP.app tmp/dmg');
await system('rm -f tmp/dmg/Contents/Frameworks/libswift*');
await system('ln -s /Applications "tmp/dmg/drop here"');
await system('hdiutil create \'$file\' -ov '
'-srcfolder tmp/dmg -volname \'$AMP_APP $shortVersion\' '
// 106M RW
// 86M RO
// 39M UDCO (adc)
// 34M UDZO (zlib)
// 31M ULFO (lzfse)
// 29M UDBZ (bzip2)
// 25M ULMO (lzma)
'-fs APFS -format ULMO');
return file;
}
Future<void> generalLinuxNAI(String o, String a, String fa, String f) async {
await flutter('config --enable-linux-desktop');
await build('linux', f);
final id = aid('linux', a);
await mkdirs('build/linux/$fa/release/bundle/usr/share/icons');
await cp('assets/logo.svg',
'build/linux/$fa/release/bundle/usr/share/icons/logo.svg');
await mvd('build/linux/$fa/release/bundle', 'tmp/$id');
await zip(id, '../$o/$id.zip', 'tmp');
}
Future<void> generalLinux(String o, String a, String fa, String f) async {
await generalLinuxNAI(o, a, fa, f);
final id = aid('linux', a);
await mvd('tmp/$id', 'tmp/AppDir-$a');
await system('appimage-builder --recipe AppImageBuilder.$a.yml');
await mv('Amplissimus-latest-$a.AppImage', '$o/$id.AppImage');
}
Future<void> linuxX86NAI([String o = 'bin']) =>
generalLinuxNAI(o, 'x86_64', 'x64', linuxX86Flags);
Future<void> linuxArmNAI([String o = 'bin']) =>
generalLinuxNAI(o, 'arm64', 'arm64', linuxARMFlags);
Future<void> linuxX86([String o = 'bin']) =>
generalLinux(o, 'x86_64', 'x64', linuxX86Flags);
Future<void> linuxArm([String o = 'bin']) =>
generalLinux(o, 'arm64', 'arm64', linuxARMFlags);
Future<void> linux([o = 'bin']) => linuxX86(o).then((_) => linuxArm(o));
Future<void> ver() async {
print(version);
}
Future<void> clean() async {
await rmd('tmp');
await rmd('build');
await rmd('bin');
}
Future env(s, d) async {
if (Platform.environment.containsKey(s)) {
print('Found $s in environment.');
return Platform.environment[s];
} else {
final def = await d();
print('Using default value $def for $s.');
return def;
}
}
Future<void> init() async {
shortVersion = await env('short_version', vers.shortVersion);
buildNumber = await env('commit_count', vers.commitCount);
version = await env('version', vers.version);
await rmd('tmp');
await mkdirs('bin');
await mkdirs('tmp/Payload');
await mkdirs('tmp/dmg');
await File('lib/appinfo.dart').writeAsString(appinfo(version, buildNumber));
}
Future<void> cleanup() async {
await rmd('tmp');
await rmd('build');
await File('lib/appinfo.dart').writeAsString(appinfo('0.0.0-1', '0'));
}
const targets = {
'ios': ios,
'android': android,
'test': test,
'win': win,
'mac': mac,
'linux': linux,
'linux-x86_64': linuxX86,
'linux-arm64': linuxArm,
'linux-x86_64-nai': linuxX86NAI,
'linux-arm64-nai': linuxArmNAI,
'ver': ver,
'clean': clean,
};
Future<void> main(List<String> argv) async {
try {
await init();
for (final target in argv) {
if (!targets.containsKey(target)) throw 'Target $target doesn\'t exist.';
await targets[target]!();
}
} catch (e) {
stderr.writeln(e);
if (e is Error) stderr.writeln(e.stackTrace);
exitCode = e is int ? e : -1;
}
await cleanup();
}