-
Notifications
You must be signed in to change notification settings - Fork 36
/
build_release.dart
198 lines (177 loc) · 6.14 KB
/
build_release.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
// ignore_for_file: avoid_print
/*
* Copyright (C) 2024 DanXi-Dev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'dart:io';
import 'package:archive/archive_io.dart';
import 'package:args/args.dart';
import 'package:git/git.dart';
import 'package:path/path.dart' as p;
String flutterExecutable = 'flutter';
String dartExecutable = 'dart';
void main(List<String> arguments) async {
var parser = ArgParser()
..addOption("target",
allowed: ['android', 'windows', 'aab', 'linux'],
help: "The target to build for.",
mandatory: true)
..addOption("versionCode",
help:
"The version code to use. If not provided, the user will be prompted.",
mandatory: false)
..addOption("flutterPath",
help: "The path to the flutter executable.",
mandatory: false,
defaultsTo: "flutter")
..addOption("dartPath",
help: "The path to the dart executable.",
mandatory: false,
defaultsTo: "dart");
final args = parser.parse(arguments);
print('Warning: Before building task, ensure that you have uncommented');
print(
'the line "signingConfig signingConfigs.release" in android/app/build.gradle,');
print('and choose your signing key in android/key.properties.');
String? versionCode = args['versionCode'];
flutterExecutable = args['flutterPath'];
dartExecutable = args['dartPath'];
if (versionCode != null) {
print('Version code: $versionCode');
} else {
print('Please enter the version code:');
versionCode = stdin.readLineSync();
}
String gitHash;
if (await GitDir.isGitDir(p.current)) {
final gitDir = await GitDir.fromExisting(p.current);
final head = await gitDir.currentBranch();
gitHash = head.sha.substring(0, 7);
} else {
print(
'This script must be run in a directory containing a git repository.');
exit(1);
}
print('Start building...');
print('Run build_runner...');
await runFlutterProcess(
['pub', 'run', 'build_runner', 'build', '--delete-conflicting-outputs']);
switch (args['target']) {
case 'android':
await buildAndroid(versionCode, gitHash);
break;
case 'windows':
await buildWindows(versionCode, gitHash);
break;
case 'aab':
await buildAppBundle(versionCode, gitHash);
break;
case 'linux':
await buildLinux(versionCode, gitHash);
break;
}
}
Future<int> runFlutterProcess(List<String> args) async {
final buildProcess =
await Process.start(flutterExecutable, args, runInShell: true);
stdout.addStream(buildProcess.stdout);
stderr.addStream(buildProcess.stderr);
return await buildProcess.exitCode;
}
Future<int> runDartProcess(List<String> args) async {
final buildProcess =
await Process.start(dartExecutable, args, runInShell: true);
stdout.addStream(buildProcess.stdout);
stderr.addStream(buildProcess.stderr);
return await buildProcess.exitCode;
}
Future<void> buildAndroid(String? versionCode, String gitHash) async {
print('Build for Android...');
await runFlutterProcess([
'build',
'apk',
'--release',
'--dart-define=GIT_HASH=$gitHash',
]);
print('Clean old files...');
File oldFile = File('build/app/DanXi-$versionCode-release.android.apk');
if (oldFile.existsSync()) {
oldFile.deleteSync();
}
print('Copy file...');
File newFile = File('build/app/DanXi-$versionCode-release.android.apk');
File sourceFile = File('build/app/outputs/flutter-apk/app-release.apk');
sourceFile.copySync(newFile.path);
print('Build success.');
}
Future<void> buildWindows(String? versionCode, String gitHash) async {
print('Build for Windows...');
await runFlutterProcess([
'build',
'windows',
'--release',
'--dart-define=GIT_HASH=$gitHash',
]);
print('Clean old files...');
File oldFile = File('build/app/DanXi-$versionCode-release.windows-x64.zip');
if (oldFile.existsSync()) {
oldFile.deleteSync();
}
print('Compress file...');
var encoder = ZipFileEncoder();
File newFile = File('build/app/DanXi-$versionCode-release.windows-x64.zip');
Directory sourceDir = Directory('build/windows/x64/runner/Release');
encoder.zipDirectory(sourceDir, filename: newFile.path);
print('Build success.');
}
Future<void> buildAppBundle(String? versionCode, String gitHash) async {
print('Build for App Bundle (Google Play Distribution)...');
await runFlutterProcess([
'build',
'appbundle',
'--release',
'--dart-define=GIT_HASH=$gitHash',
]);
print('Clean old files...');
File oldFile = File('build/app/DanXi-$versionCode-release.android.aab');
if (oldFile.existsSync()) {
oldFile.deleteSync();
}
print('Copy file...');
File newFile = File('build/app/DanXi-$versionCode-release.android.aab');
File sourceFile = File('build/app/outputs/bundle/release/app-release.aab');
sourceFile.copySync(newFile.path);
print('Build success.');
}
Future<void> buildLinux(String? versionCode, String gitHash) async {
print('Build for Linux...');
await runFlutterProcess([
'build',
'linux',
'--release',
'--dart-define=GIT_HASH=$gitHash',
]);
print('Clean old files...');
File oldFile = File('build/app/DanXi-$versionCode-release.linux-x64.zip');
if (oldFile.existsSync()) {
oldFile.deleteSync();
}
print('Compress file...');
var encoder = ZipFileEncoder();
File newFile = File('build/app/DanXi-$versionCode-release.linux-x64.zip');
Directory sourceDir = Directory('build/linux/x64/release/bundle');
encoder.zipDirectory(sourceDir, filename: newFile.path);
print('Build success.');
}