Skip to content

Commit

Permalink
chore: refactor build scripts into Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
w568w committed Mar 7, 2024
1 parent 5f20fd9 commit aad5f32
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 223 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Build APK
run: |
bash ./run_build.sh android dummy
dart build_release.dart android dummy
- if: github.event_name == 'workflow_dispatch' && github.event.inputs.tag_name != 'nightly'
name: Release
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/ci_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Workaround - Export pub environment variable on Windows
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
echo "PUB_CACHE=$LOCALAPPDATA\\Pub\\Cache" >> $GITHUB_ENV
fi
shell: bash
- name: Install Flutter
uses: subosito/flutter-action@v2
with:
Expand All @@ -28,11 +33,7 @@ jobs:
- name: Build Executable
run: |
.\run_build.bat windows dummy
- name: Package Executable
run: 7z a -r -sse ..\..\..\..\build\app\app-release.zip *
working-directory: build\windows\runner\Release
dart build_release.dart windows dummy
- if: github.event_name == 'workflow_dispatch' && github.event.inputs.tag_name != 'nightly'
name: Release
Expand Down
153 changes: 153 additions & 0 deletions build_release.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// 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:git/git.dart';
import 'package:path/path.dart' as p;

void main(List<String> arguments) async {
if (arguments.isEmpty ||
!['android', 'windows', 'aab'].contains(arguments[0])) {
print('A valid target is required: android, windows, aab');
exit(1);
}

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;
if (arguments.length > 1) {
versionCode = arguments[1];
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 runDartProcess(
['run', 'build_runner', 'build', '--delete-conflicting-outputs']);

switch (arguments[0]) {
case 'android':
await buildAndroid(versionCode, gitHash);
break;
case 'windows':
await buildWindows(versionCode, gitHash);
break;
case 'aab':
await buildAppBundle(versionCode, gitHash);
break;
}
}

Future<int> runFlutterProcess(List<String> args) async {
final buildProcess = await Process.start('flutter', 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('dart', 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/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.');
}
12 changes: 10 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ packages:
source: hosted
version: "6.4.1"
archive:
dependency: transitive
dependency: "direct dev"
description:
name: archive
sha256: "22600aa1e926be775fa5fe7e6894e7fb3df9efda8891c73f70fb3262399a432d"
Expand Down Expand Up @@ -824,6 +824,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.0"
git:
dependency: "direct dev"
description:
name: git
sha256: "1982737427ef1ef2bb69027ea0234469774495e86afe202de81ee46d37364e55"
url: "https://pub.dev"
source: hosted
version: "2.2.1"
glob:
dependency: transitive
description:
Expand Down Expand Up @@ -1196,7 +1204,7 @@ packages:
source: hosted
version: "2.1.0"
path:
dependency: transitive
dependency: "direct dev"
description:
name: path
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ dev_dependencies:
sdk: flutter
flutter_lints: ^3.0.0
intl_utils: ^2.8.5
# below used in build.dart
archive: ^3.4.10
git: ^2.2.1
path: ^1.9.0

flutter_intl:
enabled: true
Expand Down
69 changes: 0 additions & 69 deletions run_build.bat

This file was deleted.

61 changes: 0 additions & 61 deletions run_build.ps1

This file was deleted.

Loading

0 comments on commit aad5f32

Please sign in to comment.