Skip to content

Commit

Permalink
[native_assets_cli] InputBuilder.targetConfig..setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes committed Jan 7, 2025
1 parent 1785de5 commit dd5e5af
Show file tree
Hide file tree
Showing 35 changed files with 114 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void main() async {
final targetOS = OS.current;
const defaultMacOSVersion = 13;
BuildInputBuilder inputCreator() => BuildInputBuilder()
..targetConfig.setupTargetConfig(buildAssetTypes: [CodeAsset.type])
..targetConfig.setupCodeConfig(
targetArchitecture: Architecture.current,
targetOS: OS.current,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ void main(List<String> args) async {
// Set up the code input, so that the builds for different targets are
// in different directories.
inputCreator: () => BuildInputBuilder()
..targetConfig.setupTargetConfig(buildAssetTypes: [
CodeAsset.type,
DataAsset.type,
])
..targetConfig.setupCodeConfig(
targetArchitecture: target.architecture,
targetOS: targetOS,
macOSConfig: targetOS == OS.macOS
? MacOSConfig(targetVersion: defaultMacOSVersion)
: null,
linkModePreference: LinkModePreference.dynamic,
)
..targetConfig.setupDataConfig(),
),

workingDirectory: packageUri,
linkingEnabled: false,
inputValidator: validateDataAssetBuildInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ void main(List<String> args) async {
fileSystem: const LocalFileSystem(),
).build(
inputCreator: () => BuildInputBuilder()
..targetConfig.setupTargetConfig(buildAssetTypes: [
CodeAsset.type,
DataAsset.type,
])
..targetConfig.setupCodeConfig(
targetArchitecture: Architecture.current,
targetOS: targetOS,
Expand All @@ -37,8 +41,7 @@ void main(List<String> args) async {
macOSConfig: targetOS == OS.macOS
? MacOSConfig(targetVersion: defaultMacOSVersion)
: null,
)
..targetConfig.setupDataConfig(),
),
workingDirectory: packageUri,
linkingEnabled: false,
inputValidator: (input) async => [
Expand Down
24 changes: 8 additions & 16 deletions pkgs/native_assets_builder/test/build_runner/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Future<BuildResult?> build(
hookEnvironment: hookEnvironment,
).build(
inputCreator: () {
final inputBuilder = BuildInputBuilder();
final inputBuilder = BuildInputBuilder()
..targetConfig.setupTargetConfig(buildAssetTypes: buildAssetTypes);
if (buildAssetTypes.contains(CodeAsset.type)) {
inputBuilder.targetConfig.setupCodeConfig(
targetArchitecture: target?.architecture ?? Architecture.current,
Expand All @@ -81,9 +82,6 @@ Future<BuildResult?> build(
: null,
);
}
if (buildAssetTypes.contains(DataAsset.type)) {
inputBuilder.targetConfig.setupDataConfig();
}
return inputBuilder;
},
inputValidator: inputValidator,
Expand Down Expand Up @@ -135,7 +133,8 @@ Future<LinkResult?> link(
fileSystem: const LocalFileSystem(),
).link(
inputCreator: () {
final inputBuilder = LinkInputBuilder();
final inputBuilder = LinkInputBuilder()
..targetConfig.setupTargetConfig(buildAssetTypes: buildAssetTypes);
if (buildAssetTypes.contains(CodeAsset.type)) {
inputBuilder.targetConfig.setupCodeConfig(
targetArchitecture: target?.architecture ?? Architecture.current,
Expand All @@ -157,9 +156,6 @@ Future<LinkResult?> link(
: null,
);
}
if (buildAssetTypes.contains(DataAsset.type)) {
inputBuilder.targetConfig.setupDataConfig();
}
return inputBuilder;
},
inputValidator: inputValidator,
Expand Down Expand Up @@ -210,7 +206,8 @@ Future<(BuildResult?, LinkResult?)> buildAndLink(
final targetOS = target?.os ?? OS.current;
final buildResult = await buildRunner.build(
inputCreator: () {
final inputBuilder = BuildInputBuilder();
final inputBuilder = BuildInputBuilder()
..targetConfig.setupTargetConfig(buildAssetTypes: buildAssetTypes);
if (buildAssetTypes.contains(CodeAsset.type)) {
inputBuilder.targetConfig.setupCodeConfig(
targetArchitecture: target?.architecture ?? Architecture.current,
Expand All @@ -232,9 +229,6 @@ Future<(BuildResult?, LinkResult?)> buildAndLink(
: null,
);
}
if (buildAssetTypes.contains(DataAsset.type)) {
inputBuilder.targetConfig.setupDataConfig();
}
return inputBuilder;
},
inputValidator: buildInputValidator,
Expand All @@ -258,7 +252,8 @@ Future<(BuildResult?, LinkResult?)> buildAndLink(

final linkResult = await buildRunner.link(
inputCreator: () {
final inputBuilder = LinkInputBuilder();
final inputBuilder = LinkInputBuilder()
..targetConfig.setupTargetConfig(buildAssetTypes: buildAssetTypes);
if (buildAssetTypes.contains(CodeAsset.type)) {
inputBuilder.targetConfig.setupCodeConfig(
targetArchitecture: target?.architecture ?? Architecture.current,
Expand All @@ -280,9 +275,6 @@ Future<(BuildResult?, LinkResult?)> buildAndLink(
: null,
);
}
if (buildAssetTypes.contains(DataAsset.type)) {
inputBuilder.targetConfig.setupDataConfig();
}
return inputBuilder;
},
inputValidator: linkInputValidator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void main() async {
outputDirectoryShared: outputDirectoryShared,
)
..targetConfig.setupBuildConfig(dryRun: false, linkingEnabled: false)
..targetConfig.setupTargetConfig(buildAssetTypes: [CodeAsset.type])
..targetConfig.setupCodeConfig(
targetArchitecture: Architecture.current,
targetOS: targetOS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@ void main() async {
outputDirectoryShared: outputDirectoryShared,
)
..targetConfig.setupBuildConfig(dryRun: false, linkingEnabled: false)
..targetConfig.setupTargetConfig(buildAssetTypes: [
CodeAsset.type,
DataAsset.type,
])
..targetConfig.setupCodeConfig(
targetArchitecture: architecture,
targetOS: targetOS,
macOSConfig: targetOS == OS.macOS
? MacOSConfig(targetVersion: defaultMacOSVersion)
: null,
linkModePreference: LinkModePreference.dynamic,
)
..targetConfig.setupDataConfig();
);

final buildInputUri = testTempUri.resolve('build_input.json');
File.fromUri(buildInputUri)
Expand Down
3 changes: 2 additions & 1 deletion pkgs/native_assets_cli/lib/src/code_assets/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import 'os.dart';
extension CodeAssetTargetConfig on TargetConfig {
/// Code asset specific configuration.
CodeConfig get codeConfig => CodeConfig.fromJson(json);

bool get buildCodeAssets => buildAssetTypes.contains(CodeAsset.type);
}

/// Extension to the [LinkInput] providing access to configuration specific to
Expand Down Expand Up @@ -254,7 +256,6 @@ extension CodeAssetBuildInputBuilder on TargetConfigBuilder {
IOSConfig? iOSConfig,
MacOSConfig? macOSConfig,
}) {
builder.addBuildAssetType(CodeAsset.type);
if (targetArchitecture != null) {
json[_targetArchitectureKey] = targetArchitecture.toString();
}
Expand Down
1 change: 1 addition & 0 deletions pkgs/native_assets_cli/lib/src/code_assets/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Future<void> testCodeBuildHook({
await testBuildHook(
mainMethod: mainMethod,
extraInputSetup: (input) {
input.targetConfig.setupTargetConfig(buildAssetTypes: [CodeAsset.type]);
input.targetConfig.setupCodeConfig(
linkModePreference: linkModePreference ?? LinkModePreference.dynamic,
cCompilerConfig: cCompiler,
Expand Down
15 changes: 7 additions & 8 deletions pkgs/native_assets_cli/lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,10 @@ sealed class HookInputBuilder {
}) {
json[_packageNameInputKey] = packageName;
json[_packageRootInputKey] = packageRoot.toFilePath();
json[_buildAssetTypesKey] ??= <String>[];
json[_supportedAssetTypesKey] ??= <String>[];
json[_outDirInputKey] = outputDirectory.toFilePath();
json[_outDirSharedInputKey] = outputDirectoryShared.toFilePath();
}

void addBuildAssetType(String assetType) {
((json[_buildAssetTypesKey] ??= <String>[]) as List<String>).add(assetType);
((json[_supportedAssetTypesKey] ??= <String>[]) as List<String>)
.add(assetType);
}

/// Constructs a checksum for a [BuildInput].
///
/// This can be used to construct an output directory name specific to the
Expand Down Expand Up @@ -178,6 +170,13 @@ final class TargetConfigBuilder {
Map<String, Object?> get json => builder.json;

TargetConfigBuilder._(this.builder);

void setupTargetConfig({
required List<String> buildAssetTypes,
}) {
json[_buildAssetTypesKey] = buildAssetTypes;
json[_supportedAssetTypesKey] = buildAssetTypes;
}
}

final class BuildTargetConfigBuilder extends TargetConfigBuilder {
Expand Down
12 changes: 7 additions & 5 deletions pkgs/native_assets_cli/lib/src/data_assets/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import '../config.dart';

import 'data_asset.dart';

/// Extension to initialize data specific configuration on link/build inputs.
extension DataAssetBuildInputBuilder on TargetConfigBuilder {
void setupDataConfig() {
builder.addBuildAssetType(DataAsset.type);
}
/// Extension to the [TargetConfig] providing access to configuration specific
/// to data assets.
extension CodeAssetTargetConfig on TargetConfig {
bool get buildDataAssets => buildAssetTypes.contains(DataAsset.type);
}

/// Extension to initialize data specific configuration on link/build inputs.
extension DataAssetBuildInputBuilder on TargetConfigBuilder {}

/// Link output extension for data assets.
extension DataAssetLinkInput on LinkInputAssets {
// Returns the data assets that were sent to this linker.
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/test/api/build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() async {
outputDirectory: outDirUri,
outputDirectoryShared: outputDirectoryShared,
)
..addBuildAssetType('foo')
..targetConfig.setupTargetConfig(buildAssetTypes: ['foo'])
..targetConfig.setupBuildConfig(
dryRun: false,
linkingEnabled: false,
Expand Down
7 changes: 4 additions & 3 deletions pkgs/native_assets_cli/test/build_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void main() async {
outputDirectory: outDirUri,
outputDirectoryShared: outputDirectoryShared,
)
..addBuildAssetType('my-asset-type')
..targetConfig.setupTargetConfig(buildAssetTypes: ['my-asset-type'])
..targetConfig.setupBuildConfig(
linkingEnabled: false,
dryRun: false,
Expand Down Expand Up @@ -98,11 +98,12 @@ void main() async {
outputDirectory: outDirUri,
outputDirectoryShared: outputDirectoryShared,
)
..addBuildAssetType('my-asset-type')
..targetConfig.setupTargetConfig(buildAssetTypes: ['my-asset-type'])
..targetConfig.setupBuildConfig(
linkingEnabled: true,
dryRun: true,
);
)
..setupBuildInput();
final input = BuildInput(inputBuilder.json);

final expectedInputJson = {
Expand Down
44 changes: 22 additions & 22 deletions pkgs/native_assets_cli/test/checksum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ void main() {
dryRun: dryRun,
linkingEnabled: linking,
);
builder.targetConfig
.setupTargetConfig(buildAssetTypes: [assetType]);
if (assetType == CodeAsset.type) {
builder.targetConfig.setupCodeConfig(
targetArchitecture: architecture,
Expand All @@ -41,8 +43,6 @@ void main() {
: null,
linkModePreference: LinkModePreference.dynamic,
);
} else if (assetType == DataAsset.type) {
builder.targetConfig.setupDataConfig();
}
inputs.add(
const JsonEncoder.withIndent(' ').convert(builder.json),
Expand All @@ -62,26 +62,26 @@ void main() {
// needing to update this list).

final expectedChecksums = <String>[
'925d600dcf296b3090580facc3edf2b7',
'a35c6ee872e040d4ca593607411d55c7',
'7ed08edc547bdbe7c23bfeb784a5d90d',
'c59378da8732d8620ba1cda6a14fa9cf',
'1613613b3b24c7dac73624418db30f41',
'9f7fe6e014687f1ce831239367b68904',
'128835661e973a964f225e7ff5304bd7',
'3c26493d1d349d36f58742a241d77f46',
'cb00634068dd970a26a4a47284d21b6d',
'cb235612943ce5da476fb59b9586d2f9',
'713c68c5c49c82a9dc6beb9a04a91c66',
'cd13b83470137df0fb784764b37ab354',
'db469c8d73e15e3101112c69e480c1ff',
'b61381f045bacd8dba8c7f194d41bdcd',
'f50070c54e062007738c89fc7cde22a7',
'62de27e9d1c6ed6a413f42d111cc8255',
'58e02cb492c5c5b4ed1a85f6fcfa5819',
'cc2a451252f53a570c05b392f366c599',
'f65f000e8501a32c20ec7c0270b0b9ae',
'4ef9faec8957250d56603a9e13c8fe80',
'079353bf7f09a5a78aad5dfb74d160ce',
'3f77f197abd56950f6878720f40f1d78',
'95afb0b082567b75143a5a3fe23ded7f',
'20a82fc01786d82ec2e70c81e384ca53',
'9b4a6923ab07618074631e9ee8ea5451',
'1ba05dccc6b7d760f2c085dfb3b3e9be',
'86ca6c0d669baa2f0d29b81aa9db65d9',
'ad261f0ea9c06e69eaf9870161da902f',
'4973835d48a96917eb2b4fe3266c60f4',
'1c4bddfe0111033df5fd1903123a759b',
'07d32fc26a66558589bd2ef3d243a1bf',
'6237121ffae0bb44b6489913f859e595',
'5ab1d45dc1b8365df17dc13bfe725e3e',
'efff82f6321e8f2ad2d83f7b87890d12',
'9939ebdbc70de2750b26857ccdf7a308',
'2ac3ed117b93db3686b51c8d3da6947d',
'47d6a16f82e7dc71156af0578e1cde4c',
'07eaf868dd64ef1ffd717111d7a73263',
'5df468122615987a0daa4da40c3f4e07',
'0e551c890758631dfc1dd549d8278fc8',
];
printOnFailure('final expectedChecksums = <String>[');
printOnFailure(checksums.map((e) => " '$e',").join('\n'));
Expand Down
3 changes: 3 additions & 0 deletions pkgs/native_assets_cli/test/code_assets/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void main() async {
linkingEnabled: true,
dryRun: true,
)
..targetConfig.setupTargetConfig(buildAssetTypes: [CodeAsset.type])
..targetConfig.setupCodeConfig(
targetOS: OS.android,
androidConfig: null, // not available in dry run
Expand All @@ -119,6 +120,7 @@ void main() async {
linkingEnabled: false,
dryRun: false,
)
..targetConfig.setupTargetConfig(buildAssetTypes: [CodeAsset.type])
..targetConfig.setupCodeConfig(
targetOS: OS.android,
targetArchitecture: Architecture.arm64,
Expand Down Expand Up @@ -148,6 +150,7 @@ void main() async {
assets: assets,
recordedUsesFile: null,
)
..targetConfig.setupTargetConfig(buildAssetTypes: [CodeAsset.type])
..targetConfig.setupCodeConfig(
targetOS: OS.android,
targetArchitecture: Architecture.arm64,
Expand Down
Loading

0 comments on commit dd5e5af

Please sign in to comment.