Skip to content

Commit

Permalink
[native_assets_builder] Write hook stdout and stderr to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes committed Jan 13, 2025
1 parent 3832e4b commit c54b1c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -556,6 +557,32 @@ ${e.message}
}
}

Future<Logger> _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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,5 +27,7 @@ void main(List<String> arguments) async {
print('${record.level.name}: ${record.time}: ${record.message}');
}),
);
stdout.writeln('Some stdout.');
stderr.writeln('Some stderr.');
});
}

0 comments on commit c54b1c4

Please sign in to comment.