Skip to content

[native_assets_builder] Introduce ProtocolExtension #2088

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

Closed
dcharkes opened this issue Mar 11, 2025 · 0 comments · Fixed by #2089
Closed

[native_assets_builder] Introduce ProtocolExtension #2088

dcharkes opened this issue Mar 11, 2025 · 0 comments · Fixed by #2089

Comments

@dcharkes
Copy link
Collaborator

dcharkes commented Mar 11, 2025

For every extension to the protocol, the SDK authors have to pass 5 pieces of information currently:

    final BuildResult? buildResult = await buildRunner.build(
      buildAssetTypes: <String>[CodeAsset.type],
      inputCreator:
          () =>
              BuildInputBuilder()
                ..config.setupCode(
                  targetArchitecture: architecture,
                  linkModePreference: LinkModePreference.dynamic,
                  cCompiler: cCompilerConfig,
                  targetOS: targetOS!,
                  android: androidConfig,
                  iOS: iosConfig,
                  macOS: macOSConfig,
                ),
      inputValidator:
          (BuildInput config) async => <String>[...await validateCodeAssetBuildInput(config)],
      buildValidator:
          (BuildInput config, BuildOutput output) async => <String>[
            ...await validateCodeAssetBuildOutput(config, output),
          ],
      applicationAssetValidator:
          (List<EncodedAsset> assets) async => <String>[
            ...await validateCodeAssetInApplication(assets),
          ],

https://github.com/flutter/flutter/blob/d452d04a07f3c37d3ed60989e01e64e0f5c34913/packages/flutter_tools/lib/src/isolated/native_assets/native_assets.dart#L604-L626

Once we also add syntax validators next to semantic validators, it will be 7 pieces of information for the build invocation and 7 slightly different pieces of information for the link invocation.

This doesn't scale.

We should introduce a concept of a protocol extension that will ensure either all or none setup/validate callbacks are passed in to the native assets builder.

A sketch:

/// An extension to the base protocol for `hook/build.dart` and
/// `hook/link.dart`.
///
/// The extension contains callbacks to
/// 1. setup the config,
/// 2. validate the protocol syntax, and
/// 3. validate semantic constraints.
abstract interface class ProtocolExtension {
  /// The [HookConfig.buildAssetTypes] this extension adds.
  List<String> get buildAssetTypes;

  /// Setup the [BuildConfig] for this extension.
  void setupBuildInput(BuildInputBuilder input);

  /// Setup the [HookConfig] for this extension.
  void setupLinkInput(LinkInputBuilder input);

  /// Reports syntactic errors from this extension on the [BuildInput].
  ValidationErrors validateBuildInputSyntax(Map<String, Object?> json);

  /// Reports syntactic errors from this extension on the [LinkInput].
  ValidationErrors validateLinkInputSyntax(Map<String, Object?> json);

  /// Reports syntactic errors from this extension on the [BuildOutput].
  ValidationErrors validateBuildOutputSyntax(Map<String, Object?> json);

  /// Reports syntactic errors from this extension on the [LinkOutput].
  ValidationErrors validateLinkOutputSyntax(Map<String, Object?> json);

  /// Reports semantic errors from this extension on the [BuildInput].
  ///
  /// Only run if [validateBuildInputSyntax] reports no errors.
  Future<ValidationErrors> validateBuildInput(BuildInput input);

  /// Reports semantic errors from this extension on the [LinkInput].
  ///
  /// Only run if [validateLinkInputSyntax] reports no errors.
  Future<ValidationErrors> validateBuildOutput(
    BuildInput input,
    BuildOutput output,
  );

  /// Reports semantic errors from this extension on the [LinkInput].
  ///
  /// Only run if [validateLinkInputSyntax] reports no errors.
  Future<ValidationErrors> validateLinkInput(LinkInput input);

  /// Reports semantic errors from this extension on the [LinkOutput].
  ///
  /// Only run if [validateLinkOutputSyntax] reports no errors.
  Future<ValidationErrors> validateLinkOutput(
    LinkInput input,
    LinkOutput output,
  );

  /// Reports errors on the complete set of assets after all hooks are run.
  ///
  /// Can be used to validate that there are no asset-id or shared library name
  /// collisions.
  ///
  /// Only run if [validateBuildOutputSyntax] and [validateLinkOutputSyntax]
  /// report no errors.
  Future<ValidationErrors> validateApplicationAssets(List<EncodedAsset> assets);
}

@mosuem Would this fit well with the FontAssets you've been looking at? What is the JSON structure and semantic API you've come up with so far?

auto-submit bot pushed a commit that referenced this issue Mar 11, 2025
This PR introduces a concept of protocol extensions, which package together everything belonging to a protocol extension.

Closes: #2088

As a bonus, this better abstraction reduces LOC.

The `validation` methods are now all internal to the extensions. The `setup` methods stay exported because they are used extensively in hook-helper-packages (e.g. `native_toolchain_c`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant