-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathbuild.dart
121 lines (98 loc) · 3.81 KB
/
build.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
// ignore_for_file: avoid_print
import 'dart:io';
/// This script automatically generates the release files for the current version,
/// and stores the release files in /release directory.
void buildRelease() {
// Check if Flutter is installed
print('Checking if Flutter is installed...');
ProcessResult flutterResult = Process.runSync('flutter', ['--version']);
if (flutterResult.exitCode != 0) {
print('Flutter is not installed. Please install Flutter and try again.');
return;
} else {
print('Starting Flutter release builds for Android and iOS...');
}
// Get current release version
print('\nFetching current version from pubspec.yaml...');
ProcessResult ciderResult = Process.runSync('cider', ['version']);
String version = ciderResult.stdout.toString().trim();
print(version);
print('\nRemoving previous build artifacts...');
removeBuildArtifacts();
// Build for Android
print('\nStarting Android build...');
ProcessResult androidResult = Process.runSync('flutter', ['build', 'apk', '--release']);
stdout.write(androidResult.stdout);
stderr.write(androidResult.stderr);
if (androidResult.exitCode == 0) {
print('Android build successful!');
createAPKFile(version);
} else {
print('Failed to build for Android. Error: ${androidResult.stderr}');
}
// Build for iOS
print('\nStarting iOS build...');
ProcessResult iosResult = Process.runSync('flutter', ['build', 'ios', '--release']);
stdout.write(iosResult.stdout);
stderr.write(iosResult.stderr);
if (iosResult.exitCode == 0) {
print('iOS build successful!');
createIPAFile(version);
} else {
print('Failed to build for iOS. Error: ${iosResult.stderr}');
}
}
void removeBuildArtifacts() {
Directory releaseDirectory = Directory('release');
try {
// Remove previous build artifacts if they exist
releaseDirectory.deleteSync(recursive: true);
} catch (e) {
print(e.toString());
} finally {
print('Finished removing previous build artifacts...');
}
}
void createAPKFile(String version) {
print('\nCreating APK release file...');
// Create the "release" directory
Directory releaseDir = Directory('release');
releaseDir.createSync();
// Copy the APK file to the "release" directory and rename it
File apkFile = File('build/app/outputs/flutter-apk/app-release.apk');
String newApkPath = '${releaseDir.path}/thunder-v$version.apk';
File renamedApkFile = apkFile.copySync(newApkPath);
print('APK file copied and renamed successfully!');
}
// Creates a IPA file from the flutter build
void createIPAFile(String version) {
print('\nCreating IPA release file...');
String outputDirectoryPath = 'release/Payload/';
String runnerAppPath = 'build/ios/iphoneos/Runner.app';
Directory outputDirectory = Directory(outputDirectoryPath);
// Create the "Payload" directory
Directory payloadRunnerDir = Directory('$outputDirectoryPath/Runner.app');
payloadRunnerDir.createSync(recursive: true);
// Copy the Runner.app directory to the "Payload" directory
Directory runnerAppDir = Directory(runnerAppPath);
runnerAppDir.listSync(recursive: true).forEach((file) {
String newPath = file.path.replaceFirst(runnerAppDir.path, payloadRunnerDir.path);
if (file is File) {
File newFile = File(newPath);
newFile.createSync(recursive: true);
newFile.writeAsBytesSync(file.readAsBytesSync());
}
});
// Compress the "Payload" directory into a zip file, and rename it to .ipa
ProcessResult zipResult = Process.runSync('bash', ['-c', 'cd release && zip -r thunder-v$version.ipa Payload']);
if (zipResult.exitCode == 0) {
print('IPA file created successfully!');
} else {
print('Failed to create IPA file. Error: ${zipResult.stdout}');
}
// Remove Payload directory
outputDirectory.delete(recursive: true);
}
void main() {
buildRelease();
}