From c54b1c42bdf2bb5622ceeb42e9f639a17f2be6cd Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 13 Jan 2025 18:38:33 +0100 Subject: [PATCH] [native_assets_builder] Write hook stdout and stderr to disk --- .../lib/src/build_runner/build_runner.dart | 29 ++++++++++++++++++- .../test/build_runner/build_runner_test.dart | 14 +++++++++ .../test_data/native_add/hook/build.dart | 4 +++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 78cebc353..709d0d682 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -487,12 +487,13 @@ ${e.message} '--config=${inputFile.toFilePath()}', if (resources != null) resources.toFilePath(), ]; + final wrappedLogger = await _createFileStreamingLogger(input); final result = await runProcess( filesystem: _fileSystem, workingDirectory: workingDirectory, executable: dartExecutable, arguments: arguments, - logger: logger, + logger: wrappedLogger, includeParentEnvironment: false, environment: environment, ); @@ -556,6 +557,32 @@ ${e.message} } } + Future _createFileStreamingLogger(HookInput input) async { + final stdoutFile = + _fileSystem.file(input.outputDirectory.resolve('../stdout.txt')); + await stdoutFile.writeAsString(''); + final stderrFile = + _fileSystem.file(input.outputDirectory.resolve('../stderr.txt')); + await stderrFile.writeAsString(''); + final wrappedLogger = Logger.detached('') + ..level = Level.ALL + ..onRecord.listen((record) async { + logger.log(record.level, record.message); + if (record.level <= Level.INFO) { + await stdoutFile.writeAsString( + '${record.message}\n', + mode: FileMode.append, + ); + } else { + await stderrFile.writeAsString( + '${record.message}\n', + mode: FileMode.append, + ); + } + }); + return wrappedLogger; + } + /// Compiles the hook to kernel and caches the kernel. /// /// If any of the Dart source files, or the package config changed after diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_test.dart index a9df15982..39cc77780 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_test.dart @@ -45,6 +45,20 @@ void main() async { '${Platform.pathSeparator}build.dart', ])); expect(result.encodedAssets.length, 1); + + // Check that invocation logs are written to disk. + final packgeBuildDirectory = Directory.fromUri( + packageUri.resolve('.dart_tool/native_assets_builder/native_add/')); + final buildDirectory = + packgeBuildDirectory.listSync().single as Directory; + final stdoutFile = + File.fromUri(buildDirectory.uri.resolve('stdout.txt')); + final stderrFile = + File.fromUri(buildDirectory.uri.resolve('stderr.txt')); + expect(stdoutFile.existsSync(), true); + expect(stdoutFile.readAsStringSync(), contains('Some stdout.')); + expect(stderrFile.existsSync(), true); + expect(stderrFile.readAsStringSync(), contains('Some stderr.')); } // Trigger a build, should not invoke anything. diff --git a/pkgs/native_assets_builder/test_data/native_add/hook/build.dart b/pkgs/native_assets_builder/test_data/native_add/hook/build.dart index bdeb571bf..179b649f3 100644 --- a/pkgs/native_assets_builder/test_data/native_add/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add/hook/build.dart @@ -2,6 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:io'; + import 'package:logging/logging.dart'; import 'package:native_assets_cli/native_assets_cli.dart'; import 'package:native_toolchain_c/native_toolchain_c.dart'; @@ -25,5 +27,7 @@ void main(List arguments) async { print('${record.level.name}: ${record.time}: ${record.message}'); }), ); + stdout.writeln('Some stdout.'); + stderr.writeln('Some stderr.'); }); }