Skip to content

Commit

Permalink
[native_assets_cli] Fix V1_0_0 backwards compatibility (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored Mar 25, 2024
1 parent c141234 commit 6e12c3a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 11 deletions.
5 changes: 5 additions & 0 deletions pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
42 changes: 32 additions & 10 deletions pkgs/native_assets_cli/lib/src/model/build_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,38 @@ final class BuildOutputImpl implements BuildOutput {
);
}

Map<String, Object> 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<String, Object> toJson(Version version) {
final assets = <AssetImpl>[];
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
53 changes: 53 additions & 0 deletions pkgs/native_assets_cli/test/model/build_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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', () {
Expand Down

0 comments on commit 6e12c3a

Please sign in to comment.