Skip to content

[native_assets_cli] Stop reading version #2143

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

Merged
merged 1 commit into from
Mar 28, 2025
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
4 changes: 0 additions & 4 deletions pkgs/hooks/doc/schema/sdk/shared_definitions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
"HookOutput": {},
"LinkInput": {
"$ref": "../shared/shared_definitions.schema.json#/definitions/LinkInput",
"required": [
"out_file",
"version"
],
"unevaluatedProperties": false
},
"LinkOutput": {}
Expand Down
6 changes: 2 additions & 4 deletions pkgs/hooks/doc/schema/shared/shared_definitions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@
"out_dir",
"out_dir_shared",
"package_name",
"package_root",
"version"
"package_root"
]
},
"HookOutput": {
Expand Down Expand Up @@ -169,8 +168,7 @@
}
},
"required": [
"timestamp",
"version"
"timestamp"
]
},
"LinkInput": {
Expand Down
15 changes: 10 additions & 5 deletions pkgs/hooks/test/schema/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,21 @@ FieldsReturn _hookFields({
required Party party,
}) {
void versionMissingExpectation(ValidationResults result) {
if ((party == Party.sdk && inputOrOutput == InputOrOutput.input) ||
(party == Party.hook && inputOrOutput == InputOrOutput.output)) {
if (party == Party.sdk && inputOrOutput == InputOrOutput.input) {
// The writer must output this field. SDK must support older hooks reading
// it.
expect(result.isValid, isFalse);
} else if (party == Party.hook && inputOrOutput == InputOrOutput.output) {
// The writer must output this field. SDK must support older hooks reading
// it.
// Note: For some reason party:hook output does not register as required
// `package:json_schema`, but the JSON validator in vscode does properly
// mark it as required.
// Ignore this issue for now, we'll remove version soon.
// expect(result.isValid, isFalse);
} else {
// Newer hooks must support future SDKs not outputting a this field.
// TODO: Stop requiring version in the reader.
// expect(result.isValid, isTrue);
expect(result.isValid, isFalse);
expect(result.isValid, isTrue);
}
}

Expand Down
57 changes: 0 additions & 57 deletions pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:native_assets_cli/native_assets_cli_internal.dart';
import 'package:package_config/package_config.dart';
import 'package:pub_semver/pub_semver.dart';

import '../dependencies_hash_file/dependencies_hash_file.dart';
import '../locking/locking.dart';
Expand Down Expand Up @@ -91,10 +90,6 @@ class NativeAssetsBuildRunner {
);
if (buildPlan == null) return null;

if (!await _ensureNativeAssetsCliProtocolVersion()) {
return null;
}

var hookResult = HookResult();
final globalMetadata = <String, Metadata>{};
for (final package in buildPlan) {
Expand Down Expand Up @@ -833,58 +828,6 @@ ${compileResult.stdout}
? BuildOutput(hookOutputJson)
: LinkOutput(hookOutputJson);
}

Future<bool> _ensureNativeAssetsCliProtocolVersion() async {
final package =
packageLayout.packageConfig['native_assets_cli'] ??
packageLayout.packageConfig['hook']; // Anticipate rename.
if (package == null) {
// No dependencies with a hook or using a different protocol helper
// package.
return true;
}
final packageRoot = package.root.normalizePath();
final hookVersion = await _nativeAssetsCliProtocolVersion(packageRoot);
if (hookVersion == null) {
logger.fine(
'Could not determine the protocol version of '
'${packageRoot.toFilePath()}.',
);
// This is most likely due to a newer version of the package.
return true;
}
if (latestParsableVersion > hookVersion) {
// The hook is too old.
logger.shout(
'The protocol version of ${packageRoot.toFilePath()} is '
'$hookVersion, which is no longer supported. Please update your '
'dependencies.',
);
return false;
}
return true;
}

Future<Version?> _nativeAssetsCliProtocolVersion(Uri packageRoot) async {
const files = ['lib/src/config.dart', 'lib/src/model/hook_config.dart'];
for (final fileName in files) {
final file = _fileSystem.file(packageRoot.resolve(fileName));
if (!await file.exists()) {
continue;
}
final contents = await file.readAsString();
final regex = RegExp(r'latestVersion = Version\((\d+), (\d+), (\d+)\);');
final match = regex.firstMatch(contents);
if (match == null) {
continue;
}
final major = int.parse(match.group(1)!);
final minor = int.parse(match.group(2)!);
final patch = int.parse(match.group(3)!);
return Version(major, minor, patch);
}
return null;
}
}

/// Parses depfile contents.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ void main() async {
expect(result, isNull);
expect(
logMessages.join('\n'),
stringContainsInOrder([
'The protocol version of ',
'native_assets_cli',
' is 1.3.0, which is no longer supported.',
'Please update your dependencies.',
]),
stringContainsInOrder(['Unhandled exception']),
);
});
},
Expand Down
1 change: 0 additions & 1 deletion pkgs/native_assets_cli/lib/native_assets_cli_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
library;

export 'native_assets_cli_builder.dart';
export 'src/config.dart' show latestParsableVersion;
export 'src/model/hook.dart';
43 changes: 2 additions & 41 deletions pkgs/native_assets_cli/lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ sealed class HookInput {
/// The underlying json configuration of this [HookInput].
Map<String, Object?> get json => _syntax.json;

/// The version of the [HookInput].
Version get version => Version.parse(_syntax.version);

/// The directory in which output and intermediate artifacts that are unique
/// to the [config] can be placed.
///
Expand Down Expand Up @@ -82,13 +79,6 @@ sealed class HookInput {

HookInput(Map<String, Object?> json)
: _syntax = syntax.HookInput.fromJson(json) {
if (version.major != latestVersion.major ||
version < latestParsableVersion) {
throw FormatException(
'Only compatible versions with $latestVersion are supported '
'(was: $version).',
);
}
// Trigger validation, remove with cleanup.
outputDirectory;
outputDirectoryShared;
Expand Down Expand Up @@ -290,9 +280,6 @@ sealed class HookOutput {
/// The underlying json configuration of this [HookOutput].
Map<String, Object?> get json => _syntax.json;

/// The version of the [HookInput].
Version get version => Version.parse(_syntax.version);

/// Start time for the build of this output.
///
/// The [timestamp] is rounded down to whole seconds, because
Expand All @@ -316,15 +303,7 @@ sealed class HookOutput {

syntax.HookOutput get _syntax;

HookOutput._(Map<String, Object?> json) {
if (version.major != latestVersion.major ||
version < latestParsableVersion) {
throw FormatException(
'Only compatible versions with $latestVersion are supported '
'(was: $version).',
);
}
}
HookOutput._(Map<String, Object?> json);

@override
String toString() => const JsonEncoder.withIndent(' ').convert(json);
Expand Down Expand Up @@ -607,27 +586,9 @@ extension type EncodedAssetLinkOutputBuilder._(LinkOutputBuilder _builder) {
syntax.LinkOutput.fromJson(_builder._syntax.json);
}

/// The latest supported input version.
///
/// We'll never bump the major version. Removing old keys from the input and
/// output is done via modifying [latestParsableVersion].
// Deprecated, still emitted for backwards compatibility purposes.
final latestVersion = Version(1, 9, 0);

/// The parser can deal with inputs and outputs down to this version.
///
/// This version can be bumped when:
///
/// 1. The stable version of Dart / Flutter uses a newer version _and_ the SDK
/// constraint is bumped in the pubspec of this package to that stable
/// version. (This prevents input parsing from failing.)
/// 2. A stable version of this package is published uses a newer version, _and_
/// most users have migrated to it. (This prevents the output parsing from
/// failing.)
///
/// When updating this number, update the version_skew_test.dart. (This test
/// catches issues with 2.)
final latestParsableVersion = Version(1, 7, 0);

/// The configuration for a build or link hook invocation.
final class HookConfig {
Map<String, Object?> get json => _syntax.json;
Expand Down
16 changes: 8 additions & 8 deletions pkgs/native_assets_cli/lib/src/hooks/syntax.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class HookInput {
required Uri? outFile,
required String packageName,
required Uri packageRoot,
required String version,
required String? version,
}) : json = {},
path = const [] {
this.config = config;
Expand Down Expand Up @@ -428,14 +428,14 @@ class HookInput {

List<String> _validatePackageRoot() => _reader.validatePath('package_root');

String get version => _reader.get<String>('version');
String? get version => _reader.get<String?>('version');

set version(String value) {
set version(String? value) {
json.setOrRemove('version', value);
json.sortOnKey();
}

List<String> _validateVersion() => _reader.validate<String>('version');
List<String> _validateVersion() => _reader.validate<String?>('version');

List<String> validate() => [
..._validateConfig(),
Expand Down Expand Up @@ -464,7 +464,7 @@ class HookOutput {
required List<Asset>? assets,
required List<Uri>? dependencies,
required String timestamp,
required String version,
required String? version,
}) : json = {},
path = const [] {
this.assets = assets;
Expand Down Expand Up @@ -528,14 +528,14 @@ class HookOutput {

List<String> _validateTimestamp() => _reader.validate<String>('timestamp');

String get version => _reader.get<String>('version');
String? get version => _reader.get<String?>('version');

set version(String value) {
set version(String? value) {
json.setOrRemove('version', value);
json.sortOnKey();
}

List<String> _validateVersion() => _reader.validate<String>('version');
List<String> _validateVersion() => _reader.validate<String?>('version');

List<String> validate() => [
..._validateAssets(),
Expand Down
76 changes: 0 additions & 76 deletions pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md

This file was deleted.

Loading