Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native_assets_builder] Write hook stdout and stderr to disk #1886

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.');
});
}
Loading