Skip to content

Commit 16ae2d8

Browse files
mosuemdcharkes
andauthored
[hooks] Run link hooks in order (#2417)
* Run link hooks in order * change version * Switch back to assets * add to test data * normalized * schema tests * Add more data for some reason * changes as per review * fix pubspec versions * Fix in other pubspecs * fix * fix message * fix another message --------- Co-authored-by: Daco Harkes <[email protected]>
1 parent f6d16c8 commit 16ae2d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1097
-255
lines changed

pkgs/code_assets/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 0.19.5-wip
1+
## 0.19.4-wip
22

33
- Bump `package:hooks` to 0.20.0.
44

pkgs/code_assets/lib/src/code_assets/config.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,19 @@ final class LinkOutputCodeAssetBuilder {
185185

186186
LinkOutputCodeAssetBuilder._(this._output);
187187

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

191-
/// Adds the given [assets] to the link hook output.
192-
void addAll(Iterable<CodeAsset> assets) => assets.forEach(add);
192+
/// Adds the given [assets] to the hook output with [routing].
193+
void addAll(
194+
Iterable<CodeAsset> assets, {
195+
LinkAssetRouting routing = const ToAppBundle(),
196+
}) {
197+
for (final asset in assets) {
198+
add(asset, routing: routing);
199+
}
200+
}
193201
}
194202

195203
/// Extension to initialize code specific configuration on link/build inputs.

pkgs/code_assets/lib/src/code_assets/validation.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ Future<ValidationErrors> validateCodeAssetBuildOutput(
9191
output.assets.encodedAssets,
9292
[
9393
...output.assets.encodedAssetsForBuild,
94-
for (final assetList in output.assets.encodedAssetsForLinking.values)
95-
...assetList,
94+
...output.assets.encodedAssetsForLinking.values.expand((assets) => assets),
9695
],
9796
output,
9897
true,
@@ -106,7 +105,7 @@ Future<ValidationErrors> validateCodeAssetLinkOutput(
106105
input,
107106
input.config.code,
108107
output.assets.encodedAssets,
109-
[],
108+
output.assets.encodedAssetsForLink.values.expand((assets) => assets),
110109
output,
111110
false,
112111
);
@@ -135,8 +134,8 @@ Future<ValidationErrors> validateCodeAssetInApplication(
135134
Future<ValidationErrors> _validateCodeAssetBuildOrLinkOutput(
136135
HookInput input,
137136
CodeConfig codeConfig,
138-
List<EncodedAsset> encodedAssetsBundled,
139-
List<EncodedAsset> encodedAssetsNotBundled,
137+
Iterable<EncodedAsset> encodedAssetsBundled,
138+
Iterable<EncodedAsset> encodedAssetsNotBundled,
140139
HookOutput output,
141140
bool isBuild,
142141
) async {

pkgs/code_assets/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: >-
33
This library contains the hook protocol specification for bundling native code
44
with Dart packages.
55
6-
version: 0.19.5-wip
6+
version: 0.19.4-wip
77

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

pkgs/code_assets/test/code_assets/config_test.dart

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ void main() async {
8282
},
8383
},
8484
],
85+
if (hookType == 'link')
86+
'assets_from_linking': [
87+
{
88+
'type': 'code_assets/code',
89+
'encoding': {
90+
'file': 'not there',
91+
'id': 'package:my_package/name2',
92+
'link_mode': {'type': 'dynamic_loading_bundle'},
93+
},
94+
},
95+
],
8596
'config': {
8697
'build_asset_types': ['code_assets/code'],
8798
'extensions': {'code_assets': codeConfig},
@@ -94,6 +105,27 @@ void main() async {
94105
};
95106
}
96107

108+
// Full JSON to see where the config sits in the full JSON.
109+
// When removing the non-hierarchical JSON, we can change this test to only
110+
// check the nested key.
111+
Map<String, Object> linkOutputJson() => {
112+
'assets': [
113+
{'some_key': 'some_value', 'type': 'some_asset_type'},
114+
{'some_other_key': 'some_value', 'type': 'some_other_asset_type'},
115+
],
116+
'assets_for_linking': {
117+
'package_with_linker': [
118+
{
119+
'encoding': {'key': 'foo', 'value': 'bar'},
120+
'type': 'hooks/metadata',
121+
},
122+
],
123+
},
124+
'dependencies': ['/assets/data_2.json', '/assets/data_3.json'],
125+
'status': 'success',
126+
'timestamp': '2025-02-11 11:20:20.000',
127+
};
128+
97129
void expectCorrectCodeConfig(
98130
CodeConfig codeCondig, {
99131
OS targetOS = OS.android,
@@ -168,7 +200,18 @@ void main() async {
168200
outputFile: outFile,
169201
outputDirectoryShared: outputDirectoryShared,
170202
)
171-
..setupLink(assets: assets, recordedUsesFile: null)
203+
..setupLink(
204+
assets: assets,
205+
recordedUsesFile: null,
206+
assetsFromLinking: [
207+
CodeAsset(
208+
name: 'name2',
209+
package: 'my_package',
210+
file: Uri.file('not there'),
211+
linkMode: DynamicLoadingBundled(),
212+
).encode(),
213+
],
214+
)
172215
..addExtension(
173216
CodeAssetExtension(
174217
targetOS: OS.android,
@@ -282,12 +325,54 @@ void main() async {
282325
]).remove('link_mode');
283326
expect(
284327
() => LinkInput(input).assets.code.first.linkMode,
328+
throwsA(
329+
predicate(
330+
(e) =>
331+
e is FormatException &&
332+
e.message.contains("""
333+
No value was provided for 'assets.0.encoding.link_mode'."""),
334+
),
335+
),
336+
);
337+
});
338+
339+
test('LinkInput.assets_from_linking.0.encoding.link_mode missing', () {
340+
final input = inputJson(hookType: 'link');
341+
traverseJson<Map<String, Object?>>(input, [
342+
'assets_from_linking',
343+
0,
344+
'encoding',
345+
]).remove('link_mode');
346+
expect(
347+
() =>
348+
LinkInput(input).assets.assetsFromLinking.first.asCodeAsset.linkMode,
349+
throwsA(
350+
predicate(
351+
(e) =>
352+
e is FormatException &&
353+
e.message.contains("""
354+
No value was provided for 'assets_from_linking.0.encoding.link_mode'."""),
355+
),
356+
),
357+
);
358+
});
359+
360+
test('LinkOutput.assets_for_linking.package_with_linker.0.type missing', () {
361+
final input = linkOutputJson();
362+
traverseJson<Map<String, Object?>>(input, [
363+
'assets_for_linking',
364+
'package_with_linker',
365+
0,
366+
]).remove('type');
367+
expect(
368+
() =>
369+
LinkOutput(input).assets.encodedAssetsForLink.values.first.first.type,
285370
throwsA(
286371
predicate(
287372
(e) =>
288373
e is FormatException &&
289374
e.message.contains(
290-
"No value was provided for 'assets.0.encoding.link_mode'.",
375+
"No value was provided for 'package_with_linker.0.type'.",
291376
),
292377
),
293378
),

pkgs/data_assets/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 0.19.2-wip
22

3-
- Bump `package:hooks` to 0.20.0.
3+
- Bump `package:hooks` to 0.20.0-wip.
44

55
## 0.19.1
66

pkgs/data_assets/lib/src/data_assets/config.dart

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,23 @@ extension BuildOutputBuilderAddDataAssetsDirectories on BuildOutputBuilder {
2727
List<String> paths, {
2828
required BuildInput input,
2929
bool recursive = false,
30+
AssetRouting routing = const ToAppBundle(),
3031
}) async {
3132
String assetName(Uri assetUri) => assetUri
3233
.toFilePath(windows: false)
33-
.substring(input.packageRoot.toFilePath().length);
34+
.substring(input.packageRoot.toFilePath(windows: false).length);
35+
36+
void addAsset(File file) {
37+
assets.data.add(
38+
DataAsset(
39+
package: input.packageName,
40+
name: assetName(file.uri),
41+
file: file.uri,
42+
),
43+
routing: routing,
44+
);
45+
addDependency(file.uri);
46+
}
3447

3548
for (final path in paths) {
3649
final resolvedUri = input.packageRoot.resolve(path);
@@ -45,15 +58,10 @@ extension BuildOutputBuilderAddDataAssetsDirectories on BuildOutputBuilder {
4558
followLinks: false,
4659
)) {
4760
if (entity is File) {
48-
assets.data.add(
49-
DataAsset(
50-
package: input.packageName,
51-
name: assetName(entity.uri),
52-
file: entity.uri,
53-
),
54-
);
61+
addAsset(entity);
62+
} else {
63+
addDependency(entity.uri);
5564
}
56-
addDependency(entity.uri);
5765
}
5866
} on FileSystemException catch (e) {
5967
throw FileSystemException(
@@ -63,14 +71,7 @@ extension BuildOutputBuilderAddDataAssetsDirectories on BuildOutputBuilder {
6371
);
6472
}
6573
} else if (await file.exists()) {
66-
assets.data.add(
67-
DataAsset(
68-
package: input.packageName,
69-
name: assetName(file.uri),
70-
file: file.uri,
71-
),
72-
);
73-
addDependency(file.uri);
74+
addAsset(file);
7475
} else {
7576
throw FileSystemException(
7677
'Path does not exist',
@@ -140,10 +141,18 @@ final class LinkOutputDataAssetsBuilder {
140141
LinkOutputDataAssetsBuilder(this._output);
141142

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

145147
/// Adds the given [assets] to the link hook output.
146-
void addAll(Iterable<DataAsset> assets) => assets.forEach(add);
148+
void addAll(
149+
Iterable<DataAsset> assets, {
150+
LinkAssetRouting routing = const ToAppBundle(),
151+
}) {
152+
for (final asset in assets) {
153+
add(asset, routing: routing);
154+
}
155+
}
147156
}
148157

149158
/// Provides access to [DataAsset]s from a build hook output.

pkgs/hooks/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

68
## 0.19.5
79

pkgs/hooks/doc/schema/shared/shared_definitions.schema.json

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,6 @@
9292
"items": {
9393
"$ref": "#/definitions/Asset"
9494
}
95-
},
96-
"assets_for_linking": {
97-
"type": "object",
98-
"additionalProperties": {
99-
"type": "array",
100-
"items": {
101-
"$ref": "#/definitions/Asset"
102-
}
103-
}
10495
}
10596
},
10697
"allOf": [
@@ -206,6 +197,15 @@
206197
"$ref": "#/definitions/Asset"
207198
}
208199
},
200+
"assets_for_linking": {
201+
"type": "object",
202+
"additionalProperties": {
203+
"type": "array",
204+
"items": {
205+
"$ref": "#/definitions/Asset"
206+
}
207+
}
208+
},
209209
"dependencies": {
210210
"type": "array",
211211
"items": {
@@ -250,6 +250,12 @@
250250
"$ref": "#/definitions/Asset"
251251
}
252252
},
253+
"assets_from_linking": {
254+
"type": "array",
255+
"items": {
256+
"$ref": "#/definitions/Asset"
257+
}
258+
},
253259
"resource_identifiers": {
254260
"$ref": "#/definitions/absolutePath"
255261
}
@@ -261,6 +267,7 @@
261267
]
262268
},
263269
"LinkOutput": {
270+
"type": "object",
264271
"allOf": [
265272
{
266273
"$ref": "#/definitions/HookOutput"

pkgs/hooks/lib/hooks.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export 'src/config.dart'
4141
HookOutputBuilder,
4242
HookOutputFailure,
4343
InfraError,
44+
LinkAssetRouting,
4445
LinkConfig,
4546
LinkConfigBuilder,
4647
LinkInput,
@@ -52,6 +53,7 @@ export 'src/config.dart'
5253
LinkOutputBuilder,
5354
LinkOutputFailure,
5455
LinkOutputMaybeFailure,
56+
LinkOutputMetadataBuilder,
5557
PackageMetadata,
5658
ToAppBundle,
5759
ToBuildHooks,

0 commit comments

Comments
 (0)