Skip to content

[hooks] Run link hooks in order #2417

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions pkgs/code_assets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.20.0

* Enable passing metadata from link hooks of a package to the link hooks in
depending packages, by fixing the link hook execution order.

## 0.19.4

* Add doc comments to all public members.
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((l) => l),
],
output,
true,
Expand All @@ -106,7 +105,7 @@ Future<ValidationErrors> validateCodeAssetLinkOutput(
input,
input.config.code,
output.assets.encodedAssets,
[],
output.assets.encodedAssetsForLink.values.expand((l) => l),
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
4 changes: 2 additions & 2 deletions 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.4
version: 0.20.0

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

Expand All @@ -21,7 +21,7 @@ environment:

dependencies:
collection: ^1.19.1
hooks: ^0.19.5
hooks: ^0.20.0

dev_dependencies:
custom_lint: ^0.7.5
Expand Down
3 changes: 2 additions & 1 deletion pkgs/code_assets/test/code_assets/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void main() async {
'extensions': {'code_assets': codeConfig},
if (hookType == 'build') 'linking_enabled': false,
},
if (hookType == 'link') 'internal_assets': [],
'out_dir_shared': outputDirectoryShared.toFilePath(),
'out_file': outFile.toFilePath(),
'package_name': packageName,
Expand Down Expand Up @@ -168,7 +169,7 @@ void main() async {
outputFile: outFile,
outputDirectoryShared: outputDirectoryShared,
)
..setupLink(assets: assets, recordedUsesFile: null)
..setupLink(assets: assets, recordedUsesFile: null, internalAssets: [])
..addExtension(
CodeAssetExtension(
targetOS: OS.android,
Expand Down
5 changes: 5 additions & 0 deletions pkgs/data_assets/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.20.0

* Enable passing metadata from link hooks of a package to the link hooks in
depending packages, by fixing the link hook execution order.

## 0.19.1

* Bump the SDK constraint to at least the one from `package:hooks` to fix
Expand Down
45 changes: 27 additions & 18 deletions pkgs/data_assets/lib/src/data_assets/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,24 @@ 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);

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);
final directory = Directory.fromUri(resolvedUri);
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
4 changes: 2 additions & 2 deletions pkgs/data_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 data assets
with Dart packages.

version: 0.19.1
version: 0.20.0

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

Expand All @@ -17,7 +17,7 @@ environment:
sdk: '>=3.9.0-21.0.dev <4.0.0'

dependencies:
hooks: ^0.19.5
hooks: ^0.20.0

dev_dependencies:
custom_lint: ^0.7.5
Expand Down
5 changes: 5 additions & 0 deletions pkgs/hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.20.0

* Enable passing metadata from link hooks of a package to the link hooks in
depending packages, by fixing the link hook execution order.

## 0.19.5

* Stop leaking unexported symbols.
Expand Down
18 changes: 18 additions & 0 deletions pkgs/hooks/doc/schema/shared/shared_definitions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@
"$ref": "#/definitions/Asset"
}
},
"internal_assets": {
"type": "array",
"items": {
"$ref": "#/definitions/Asset"
}
},
"resource_identifiers": {
"$ref": "#/definitions/absolutePath"
}
Expand All @@ -261,6 +267,18 @@
]
},
"LinkOutput": {
"type": "object",
"properties": {
"assets_for_link": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to the HookOutput not duplicated between LinkOutput and BuildOutput.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was thinking about that, and I am unsure if this is more of a spurious correlation or not. These are different things in principle which just happen to have the same structure. I am more in favor of keeping it this way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel they are kind of similar, also in the LinkInput they should show up as similar/parallel assetsFromBuid assetsFromLink.

"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"$ref": "#/definitions/Asset"
}
}
}
},
"allOf": [
{
"$ref": "#/definitions/HookOutput"
Expand Down
1 change: 1 addition & 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 Down
Loading
Loading