From 6e12c3aaa92e83a0d33c0484102d54c7709b128e Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 25 Mar 2024 13:35:37 +0100 Subject: [PATCH] [native_assets_cli] Fix V1_0_0 backwards compatibility (#1055) --- pkgs/native_assets_cli/CHANGELOG.md | 5 ++ .../lib/src/model/build_output.dart | 42 +++++++++++---- .../lib/src/model/build_output_CHANGELOG.md | 2 + pkgs/native_assets_cli/pubspec.yaml | 2 +- .../test/model/build_output_test.dart | 53 +++++++++++++++++++ 5 files changed, 93 insertions(+), 11 deletions(-) diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index 3b885e57d2..c9a79e67d1 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.5.3 + +- Fix V1_0_0 dry run backwards compatibility. + https://github.com/dart-lang/native/issues/1053 + ## 0.5.2 - Fix test. diff --git a/pkgs/native_assets_cli/lib/src/model/build_output.dart b/pkgs/native_assets_cli/lib/src/model/build_output.dart index 155c75b4c1..0da94f94a5 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -90,16 +90,38 @@ final class BuildOutputImpl implements BuildOutput { ); } - Map toJson(Version version) => { - _timestampKey: timestamp.toString(), - _assetsKey: [ - for (final asset in _assets) asset.toJson(version), - ], - if (_dependencies.dependencies.isNotEmpty) - _dependenciesKey: _dependencies.toJson(), - _metadataKey: _metadata.toJson(), - _versionKey: version.toString(), - }..sortOnKey(); + Map toJson(Version version) { + final assets = []; + for (final asset in _assets) { + switch (asset) { + case NativeCodeAssetImpl _: + if (version <= Version(1, 0, 0) && asset.architecture == null) { + // Dry run does not report architecture. But old Dart and Flutter + // expect architecture to be populated. So, populate assets for all + // architectures. + for (final architecture in asset.os.architectures) { + assets.add(asset.copyWith( + architecture: architecture, + )); + } + } else { + assets.add(asset); + } + default: + assets.add(asset); + } + } + return { + _timestampKey: timestamp.toString(), + _assetsKey: [ + for (final asset in assets) asset.toJson(version), + ], + if (_dependencies.dependencies.isNotEmpty) + _dependenciesKey: _dependencies.toJson(), + _metadataKey: _metadata.toJson(), + _versionKey: version.toString(), + }..sortOnKey(); + } String toJsonString(Version version) => const JsonEncoder.withIndent(' ').convert(toJson(version)); diff --git a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md index 221276c6d5..17cbf88c8d 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md +++ b/pkgs/native_assets_cli/lib/src/model/build_output_CHANGELOG.md @@ -18,6 +18,8 @@ Backwards compatibility older SDKs: emit the old format if an older BuildConfig was passed in. - Added `DataAsset`s. Backwards compatibility: These are ignored on older SDKs. +- `architecture` is optional. + Backwards compatibility older SDKs: expand the assets to include all architectures. ## 1.0.0 diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index 83e0f9dae7..081d6a6546 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -4,7 +4,7 @@ description: >- native assets CLI. # Note: Bump BuildConfig.version and BuildOutput.version on breaking changes! -version: 0.5.2 +version: 0.5.3 repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli topics: diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index a7ec89ca5b..0472bf0394 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -58,6 +58,18 @@ void main() { }), ); + final dryRunOutput = BuildOutputImpl( + timestamp: DateTime.parse('2022-11-10 13:25:01.000'), + assets: [ + NativeCodeAssetImpl( + id: 'package:my_package/foo', + file: Uri(path: 'path/to/libfoo.so'), + linkMode: DynamicLoadingBundledImpl(), + os: OSImpl.android, + ), + ], + ); + const yamlEncodingV1_0_0 = '''timestamp: 2022-11-10 13:25:01.000 assets: - id: package:my_package/foo @@ -88,6 +100,41 @@ metadata: key: value version: 1.0.0'''; + const yamlEncodingV1_0_0dryRun = '''timestamp: 2022-11-10 13:25:01.000 +assets: + - id: package:my_package/foo + link_mode: dynamic + path: + path_type: absolute + uri: path/to/libfoo.so + target: android_arm + - id: package:my_package/foo + link_mode: dynamic + path: + path_type: absolute + uri: path/to/libfoo.so + target: android_arm64 + - id: package:my_package/foo + link_mode: dynamic + path: + path_type: absolute + uri: path/to/libfoo.so + target: android_ia32 + - id: package:my_package/foo + link_mode: dynamic + path: + path_type: absolute + uri: path/to/libfoo.so + target: android_x64 + - id: package:my_package/foo + link_mode: dynamic + path: + path_type: absolute + uri: path/to/libfoo.so + target: android_riscv64 +metadata: {} +version: 1.0.0'''; + final yamlEncoding = '''timestamp: 2022-11-10 13:25:01.000 assets: - architecture: x64 @@ -200,6 +247,12 @@ version: ${BuildOutputImpl.latestVersion}'''; expect(yamlEncoding, yamlEncodingV1_0_0); }); + test('built info yaml v1.0.0 serialization keeps working dry run', () { + final yamlEncoding = + yamlEncode(dryRunOutput.toJson(Version(1, 0, 0))).replaceAll('\\', '/'); + expect(yamlEncoding, yamlEncodingV1_0_0dryRun); + }); + test('BuildOutput.toString', buildOutput.toString); test('BuildOutput.hashCode', () {