mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
186 lines
5.3 KiB
Dart
Executable File
186 lines
5.3 KiB
Dart
Executable File
#!/usr/bin/env dart
|
|
// ignore_for_file: avoid_print
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
const appName = 'ServerBox';
|
|
const buildDataFilePath = 'lib/data/res/build_data.dart';
|
|
const xcarchivePath = 'build/ios/archive/Runner.xcarchive';
|
|
var regiOSProjectVer = RegExp(r'CURRENT_PROJECT_VERSION = .+;');
|
|
var regiOSMarketVer = RegExp(r'MARKETING_VERSION = .+');
|
|
const skslFileSuffix = '.sksl.json';
|
|
|
|
int? build;
|
|
|
|
Future<int> getGitCommitCount() async {
|
|
final result = await Process.run('git', ['log', '--oneline']);
|
|
return (result.stdout as String)
|
|
.split('\n')
|
|
.where((line) => line.isNotEmpty)
|
|
.length;
|
|
}
|
|
|
|
Future<void> writeStaicConfigFile(
|
|
Map<String, dynamic> data, String className, String path) async {
|
|
final buffer = StringBuffer();
|
|
buffer.writeln('// This file is generated by ./make.dart');
|
|
buffer.writeln('');
|
|
buffer.writeln('class $className {');
|
|
for (var entry in data.entries) {
|
|
final type = entry.value.runtimeType;
|
|
final value = json.encode(entry.value);
|
|
buffer.writeln(' static const $type ${entry.key} = $value;');
|
|
}
|
|
buffer.writeln('}');
|
|
await File(path).writeAsString(buffer.toString());
|
|
}
|
|
|
|
Future<int> getGitModificationCount() async {
|
|
final result =
|
|
await Process.run('git', ['ls-files', '-mo', '--exclude-standard']);
|
|
return (result.stdout as String)
|
|
.split('\n')
|
|
.where((line) => line.isNotEmpty)
|
|
.length;
|
|
}
|
|
|
|
Future<String> getFlutterVersion() async {
|
|
final result = await Process.run('flutter', ['--version'], runInShell: true);
|
|
return (result.stdout as String);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getBuildData() async {
|
|
final data = {
|
|
'name': appName,
|
|
'build': build,
|
|
'engine': await getFlutterVersion(),
|
|
'buildAt': DateTime.now().toString(),
|
|
'modifications': await getGitModificationCount(),
|
|
};
|
|
return data;
|
|
}
|
|
|
|
String jsonEncodeWithIndent(Map<String, dynamic> json) {
|
|
const encoder = JsonEncoder.withIndent(' ');
|
|
return encoder.convert(json);
|
|
}
|
|
|
|
Future<void> updateBuildData() async {
|
|
print('Updating BuildData...');
|
|
final data = await getBuildData();
|
|
print(jsonEncodeWithIndent(data));
|
|
await writeStaicConfigFile(data, 'BuildData', buildDataFilePath);
|
|
}
|
|
|
|
Future<void> dartFormat() async {
|
|
final result = await Process.run('dart', ['format', '.']);
|
|
print('\n${result.stdout}');
|
|
if (result.exitCode != 0) {
|
|
print(result.stderr);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
void flutterRun(String? mode) {
|
|
Process.start('flutter', mode == null ? ['run'] : ['run', '--$mode'],
|
|
mode: ProcessStartMode.inheritStdio, runInShell: true);
|
|
}
|
|
|
|
Future<void> flutterBuild(String source, String target, bool isAndroid) async {
|
|
final args = [
|
|
'build',
|
|
isAndroid ? 'apk' : 'ipa',
|
|
'--target-platform=android-arm64',
|
|
'--build-number=$build',
|
|
'--build-name=1.0.$build',
|
|
'--bundle-sksl-path=${isAndroid ? 'android' : 'ios'}$skslFileSuffix',
|
|
];
|
|
if (!isAndroid) args.removeAt(2);
|
|
print('Building with args: ${args.join(' ')}');
|
|
final buildResult = await Process.run('flutter', args, runInShell: true);
|
|
final exitCode = buildResult.exitCode;
|
|
|
|
if (exitCode == 0) {
|
|
target = target.replaceFirst('build', build.toString());
|
|
print('Copying from $source to $target');
|
|
if (isAndroid) {
|
|
await File(source).copy(target);
|
|
} else {
|
|
final result = await Process.run('cp', ['-r', source, target]);
|
|
if (result.exitCode != 0) {
|
|
print(result.stderr);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
print('Done.\n');
|
|
} else {
|
|
print(buildResult.stderr.toString());
|
|
print('\nBuild failed with exit code $exitCode');
|
|
exit(exitCode);
|
|
}
|
|
}
|
|
|
|
Future<void> flutterBuildIOS() async {
|
|
await changeiOSVersion();
|
|
await flutterBuild(
|
|
xcarchivePath, './release/${appName}_build.xcarchive', false);
|
|
}
|
|
|
|
Future<void> flutterBuildAndroid() async {
|
|
await flutterBuild('./build/app/outputs/flutter-apk/app-release.apk',
|
|
'./release/${appName}_build_Arm64.apk', true);
|
|
}
|
|
|
|
Future<void> changeiOSVersion() async {
|
|
final file = File('ios/Runner.xcodeproj/project.pbxproj');
|
|
final contents = await file.readAsString();
|
|
final newContents = contents
|
|
.replaceAll(regiOSMarketVer, 'MARKETING_VERSION = 1.0.$build;')
|
|
.replaceAll(regiOSProjectVer, 'CURRENT_PROJECT_VERSION = $build;');
|
|
await file.writeAsString(newContents);
|
|
}
|
|
|
|
void main(List<String> args) async {
|
|
if (args.isEmpty) {
|
|
print('No action. Exit.');
|
|
return;
|
|
}
|
|
|
|
final command = args[0];
|
|
|
|
switch (command) {
|
|
case 'run':
|
|
return flutterRun(args.length == 2 ? args[1] : null);
|
|
case 'build':
|
|
final stopwatch = Stopwatch()..start();
|
|
final buildFunc = [flutterBuildIOS, flutterBuildAndroid];
|
|
build = await getGitCommitCount();
|
|
await updateBuildData();
|
|
await dartFormat();
|
|
if (args.length > 1) {
|
|
final platform = args[1];
|
|
switch (platform) {
|
|
case 'ios':
|
|
buildFunc.remove(flutterBuildAndroid);
|
|
break;
|
|
case 'android':
|
|
buildFunc.remove(flutterBuildIOS);
|
|
break;
|
|
default:
|
|
print('Unknown platform: $platform');
|
|
exit(1);
|
|
}
|
|
}
|
|
for (final func in buildFunc) {
|
|
await func();
|
|
}
|
|
print('Build finished in ${stopwatch.elapsed}');
|
|
return;
|
|
default:
|
|
print('Unsupported command: $command');
|
|
return;
|
|
}
|
|
}
|