Skip to content
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
2 changes: 1 addition & 1 deletion pkgs/code_assets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.19.5-wip
## 0.19.4-wip

- Bump `package:hooks` to 0.20.0.

Expand Down
16 changes: 12 additions & 4 deletions pkgs/code_assets/lib/src/code_assets/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,19 @@ final class LinkOutputCodeAssetBuilder {

LinkOutputCodeAssetBuilder._(this._output);

/// Adds the given [asset] to the link hook output.
void add(CodeAsset asset) => _output.addEncodedAsset(asset.encode());
/// Adds the given [asset] to the hook output with [routing].
void add(CodeAsset asset, {LinkAssetRouting routing = const ToAppBundle()}) =>
_output.addEncodedAsset(asset.encode(), routing: routing);

/// Adds the given [assets] to the link hook output.
void addAll(Iterable<CodeAsset> assets) => assets.forEach(add);
/// Adds the given [assets] to the hook output with [routing].
void addAll(
Iterable<CodeAsset> assets, {
LinkAssetRouting routing = const ToAppBundle(),
}) {
for (final asset in assets) {
add(asset, routing: routing);
}
}
}

/// Extension to initialize code specific configuration on link/build inputs.
Expand Down
9 changes: 4 additions & 5 deletions pkgs/code_assets/lib/src/code_assets/validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ Future<ValidationErrors> validateCodeAssetBuildOutput(
output.assets.encodedAssets,
[
...output.assets.encodedAssetsForBuild,
for (final assetList in output.assets.encodedAssetsForLinking.values)
...assetList,
...output.assets.encodedAssetsForLinking.values.expand((assets) => assets),
],
output,
true,
Expand All @@ -106,7 +105,7 @@ Future<ValidationErrors> validateCodeAssetLinkOutput(
input,
input.config.code,
output.assets.encodedAssets,
[],
output.assets.encodedAssetsForLink.values.expand((assets) => assets),
output,
false,
);
Expand Down Expand Up @@ -135,8 +134,8 @@ Future<ValidationErrors> validateCodeAssetInApplication(
Future<ValidationErrors> _validateCodeAssetBuildOrLinkOutput(
HookInput input,
CodeConfig codeConfig,
List<EncodedAsset> encodedAssetsBundled,
List<EncodedAsset> encodedAssetsNotBundled,
Iterable<EncodedAsset> encodedAssetsBundled,
Iterable<EncodedAsset> encodedAssetsNotBundled,
HookOutput output,
bool isBuild,
) async {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/code_assets/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >-
This library contains the hook protocol specification for bundling native code
with Dart packages.

version: 0.19.5-wip
version: 0.19.4-wip

repository: https://github.com/dart-lang/native/tree/main/pkgs/code_assets

Expand Down
89 changes: 87 additions & 2 deletions pkgs/code_assets/test/code_assets/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ void main() async {
},
},
],
if (hookType == 'link')
'assets_from_linking': [
{
'type': 'code_assets/code',
'encoding': {
'file': 'not there',
'id': 'package:my_package/name2',
'link_mode': {'type': 'dynamic_loading_bundle'},
},
},
],
'config': {
'build_asset_types': ['code_assets/code'],
'extensions': {'code_assets': codeConfig},
Expand All @@ -94,6 +105,27 @@ void main() async {
};
}

// Full JSON to see where the config sits in the full JSON.
// When removing the non-hierarchical JSON, we can change this test to only
// check the nested key.
Map<String, Object> linkOutputJson() => {
'assets': [
{'some_key': 'some_value', 'type': 'some_asset_type'},
{'some_other_key': 'some_value', 'type': 'some_other_asset_type'},
],
'assets_for_linking': {
'package_with_linker': [
{
'encoding': {'key': 'foo', 'value': 'bar'},
'type': 'hooks/metadata',
},
],
},
'dependencies': ['/assets/data_2.json', '/assets/data_3.json'],
'status': 'success',
'timestamp': '2025-02-11 11:20:20.000',
};

void expectCorrectCodeConfig(
CodeConfig codeCondig, {
OS targetOS = OS.android,
Expand Down Expand Up @@ -168,7 +200,18 @@ void main() async {
outputFile: outFile,
outputDirectoryShared: outputDirectoryShared,
)
..setupLink(assets: assets, recordedUsesFile: null)
..setupLink(
assets: assets,
recordedUsesFile: null,
assetsFromLinking: [
CodeAsset(
name: 'name2',
package: 'my_package',
file: Uri.file('not there'),
linkMode: DynamicLoadingBundled(),
).encode(),
],
)
..addExtension(
CodeAssetExtension(
targetOS: OS.android,
Expand Down Expand Up @@ -282,12 +325,54 @@ void main() async {
]).remove('link_mode');
expect(
() => LinkInput(input).assets.code.first.linkMode,
throwsA(
predicate(
(e) =>
e is FormatException &&
e.message.contains("""
No value was provided for 'assets.0.encoding.link_mode'."""),
),
),
);
});

test('LinkInput.assets_from_linking.0.encoding.link_mode missing', () {
final input = inputJson(hookType: 'link');
traverseJson<Map<String, Object?>>(input, [
'assets_from_linking',
0,
'encoding',
]).remove('link_mode');
expect(
() =>
LinkInput(input).assets.assetsFromLinking.first.asCodeAsset.linkMode,
throwsA(
predicate(
(e) =>
e is FormatException &&
e.message.contains("""
No value was provided for 'assets_from_linking.0.encoding.link_mode'."""),
),
),
);
});

test('LinkOutput.assets_for_linking.package_with_linker.0.type missing', () {
final input = linkOutputJson();
traverseJson<Map<String, Object?>>(input, [
'assets_for_linking',
'package_with_linker',
0,
]).remove('type');
expect(
() =>
LinkOutput(input).assets.encodedAssetsForLink.values.first.first.type,
throwsA(
predicate(
(e) =>
e is FormatException &&
e.message.contains(
"No value was provided for 'assets.0.encoding.link_mode'.",
"No value was provided for 'package_with_linker.0.type'.",
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion pkgs/data_assets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 0.19.2-wip

- Bump `package:hooks` to 0.20.0.
- Bump `package:hooks` to 0.20.0-wip.

## 0.19.1

Expand Down
47 changes: 28 additions & 19 deletions pkgs/data_assets/lib/src/data_assets/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,23 @@ extension BuildOutputBuilderAddDataAssetsDirectories on BuildOutputBuilder {
List<String> paths, {
required BuildInput input,
bool recursive = false,
AssetRouting routing = const ToAppBundle(),
}) async {
String assetName(Uri assetUri) => assetUri
.toFilePath(windows: false)
.substring(input.packageRoot.toFilePath().length);
.substring(input.packageRoot.toFilePath(windows: false).length);

void addAsset(File file) {
assets.data.add(
DataAsset(
package: input.packageName,
name: assetName(file.uri),
file: file.uri,
),
routing: routing,
);
addDependency(file.uri);
}

for (final path in paths) {
final resolvedUri = input.packageRoot.resolve(path);
Expand All @@ -45,15 +58,10 @@ extension BuildOutputBuilderAddDataAssetsDirectories on BuildOutputBuilder {
followLinks: false,
)) {
if (entity is File) {
assets.data.add(
DataAsset(
package: input.packageName,
name: assetName(entity.uri),
file: entity.uri,
),
);
addAsset(entity);
} else {
addDependency(entity.uri);
}
addDependency(entity.uri);
}
} on FileSystemException catch (e) {
throw FileSystemException(
Expand All @@ -63,14 +71,7 @@ extension BuildOutputBuilderAddDataAssetsDirectories on BuildOutputBuilder {
);
}
} else if (await file.exists()) {
assets.data.add(
DataAsset(
package: input.packageName,
name: assetName(file.uri),
file: file.uri,
),
);
addDependency(file.uri);
addAsset(file);
} else {
throw FileSystemException(
'Path does not exist',
Expand Down Expand Up @@ -140,10 +141,18 @@ final class LinkOutputDataAssetsBuilder {
LinkOutputDataAssetsBuilder(this._output);

/// Adds the given [asset] to the link hook output.
void add(DataAsset asset) => _output.addEncodedAsset(asset.encode());
void add(DataAsset asset, {LinkAssetRouting routing = const ToAppBundle()}) =>
_output.addEncodedAsset(asset.encode(), routing: routing);

/// Adds the given [assets] to the link hook output.
void addAll(Iterable<DataAsset> assets) => assets.forEach(add);
void addAll(
Iterable<DataAsset> assets, {
LinkAssetRouting routing = const ToAppBundle(),
}) {
for (final asset in assets) {
add(asset, routing: routing);
}
}
}

/// Provides access to [DataAsset]s from a build hook output.
Expand Down
2 changes: 2 additions & 0 deletions pkgs/hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* **Breaking change** Rename `EncodedAsset.jsonPath` to
`EncodedAsset.encodingJsonPath`. This field only governs the `EncodedAsset.encoding` field, not the whole object.
* Enable passing metadata from link hooks of a package to the link hooks in
dependencies, by fixing the link hook execution order.

## 0.19.5

Expand Down
25 changes: 16 additions & 9 deletions pkgs/hooks/doc/schema/shared/shared_definitions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,6 @@
"items": {
"$ref": "#/definitions/Asset"
}
},
"assets_for_linking": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"$ref": "#/definitions/Asset"
}
}
}
},
"allOf": [
Expand Down Expand Up @@ -206,6 +197,15 @@
"$ref": "#/definitions/Asset"
}
},
"assets_for_linking": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"$ref": "#/definitions/Asset"
}
}
},
"dependencies": {
"type": "array",
"items": {
Expand Down Expand Up @@ -250,6 +250,12 @@
"$ref": "#/definitions/Asset"
}
},
"assets_from_linking": {
"type": "array",
"items": {
"$ref": "#/definitions/Asset"
}
},
"resource_identifiers": {
"$ref": "#/definitions/absolutePath"
}
Expand All @@ -261,6 +267,7 @@
]
},
"LinkOutput": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/HookOutput"
Expand Down
2 changes: 2 additions & 0 deletions pkgs/hooks/lib/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export 'src/config.dart'
HookOutputBuilder,
HookOutputFailure,
InfraError,
LinkAssetRouting,
LinkConfig,
LinkConfigBuilder,
LinkInput,
Expand All @@ -52,6 +53,7 @@ export 'src/config.dart'
LinkOutputBuilder,
LinkOutputFailure,
LinkOutputMaybeFailure,
LinkOutputMetadataBuilder,
PackageMetadata,
ToAppBundle,
ToBuildHooks,
Expand Down
Loading