Skip to content

Commit

Permalink
Handle fail-fast correctly in build.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 14, 2024
1 parent 72afc26 commit 57e8c24
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/realm_dart/tool/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,29 @@ class _BuildNativeCommand extends _BaseCommand {
Future<int?> runProc(List<String> args, {required Logger logger, String? message}) async {
final p = await io.Process.start(args.first, args.skip(1).toList());
Progress? progress;
message ??= args.join(' ');
final command = args.join(' ');
message ??= command;
if (verbose) {
logger.info(message);
await Future.wait([io.stdout.addStream(p.stdout), io.stderr.addStream(p.stderr)]);
} else {
// trim message to fit terminal width
final width = io.stdout.hasTerminal ? io.stdout.terminalColumns - 12 : 80;
message = message.padRight(width).substring(0, width);
message = message.padRight(width);
if (message.length > width) {
message = '${message.substring(0, width - 4)} ...';
}

progress = logger.progress(message);
await for (final _ in StreamGroup.merge([p.stdout, p.stderr])) {
// update progress when there's output in child process
progress.update(message);
}
}
final exitCode = await p.exitCode;
if (exitCode < 0) {
if (exitCode != 0) {
progress?.fail(message);
logger.err('Error: "$message}" exited with code $exitCode');
logger.err('Error: "$command" exited with code $exitCode');
return exitCode;
} else {
progress?.complete(message);
Expand All @@ -199,9 +206,9 @@ class _BuildNativeCommand extends _BaseCommand {
? iOSSdk.values
: iosSdkOptions.map((o) => iOSSdk.from(o)).whereNotNull();

int? exitCode;
for (final target in targets) {
logger.info('Building for ${target.name} in ${buildMode.name} mode');
int? exitCode;
switch (target.os) {
case OS.iOS:
for (final sdk in iosSdks) {
Expand Down Expand Up @@ -235,15 +242,16 @@ class _BuildNativeCommand extends _BaseCommand {
'--build',
'--preset=$preset',
'--config=${buildMode.cmakeName}',
if (target.os == OS.android) '--target=strip',
if (target.os == OS.android && buildMode == BuildMode.release) '--target=strip',
],
logger: logger,
);
break;
}
io.stdout.writeln();
if (exitCode != null) return exitCode; // return first non-zero exit code
}
return exitCode ?? 0;
return 0; // success
}
}

Expand Down Expand Up @@ -281,7 +289,7 @@ Future<void> main(List<String> arguments) async {
..argParser.addFlag('verbose', abbr: 'v', help: 'Print verbose output', defaultsTo: false);
try {
final exitCode = await runner.run(arguments);
io.exit(exitCode!);
io.exit(exitCode ?? 0);
} on UsageException catch (error) {
logger.err('$error');
io.exit(64); // Exit code 64 indicates a usage error.
Expand Down

0 comments on commit 57e8c24

Please sign in to comment.