diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index b158b2ae71..8de8c55668 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -258,14 +258,14 @@ class NativeAssetsBuildRunner { required bool dryRun, }) async { final outDir = config.outputDirectory; - final configFile = outDir.resolve('../config.yaml'); + final configFile = outDir.resolve('../config.json'); final buildHook = config.packageRoot.resolve('hook/').resolve('build.dart'); final buildHookLegacy = config.packageRoot.resolve('build.dart'); - final configFileContents = config.toYamlString(); - logger.info('config.yaml contents: $configFileContents'); + final configFileContents = config.toJsonString(); + logger.info('config.json contents: $configFileContents'); await File.fromUri(configFile).writeAsString(configFileContents); final buildOutputFile = - File.fromUri(outDir.resolve(BuildOutputImpl.fileName)); + File.fromUri(outDir.resolve(BuildOutputImpl.fileNameV1_1_0)); if (await buildOutputFile.exists()) { // Ensure we'll never read outdated build results. await buildOutputFile.delete(); @@ -320,7 +320,7 @@ ${result.stdout} } on FormatException catch (e) { logger.severe(''' Building native assets for package:${config.packageName} failed. -build_output.yaml contained a format error. +build_output.json contained a format error. ${e.message} '''); success = false; @@ -332,14 +332,14 @@ ${e.message} } on TypeError { logger.severe(''' Building native assets for package:${config.packageName} failed. -build_output.yaml contained a format error. +build_output.json contained a format error. '''); success = false; return ([], [], const Metadata({}), false); } finally { if (!success) { final buildOutputFile = - File.fromUri(outDir.resolve(BuildOutputImpl.fileName)); + File.fromUri(outDir.resolve(BuildOutputImpl.fileNameV1_1_0)); if (await buildOutputFile.exists()) { await buildOutputFile.delete(); } diff --git a/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart b/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart index 980d65c566..b430282ed2 100644 --- a/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart +++ b/pkgs/native_assets_builder/lib/src/model/kernel_assets.dart @@ -6,7 +6,7 @@ /// kernel file. /// /// The `native_assets.yaml` embedded in a kernel file has a different format -/// from the assets passed in the `build_output.yaml` from individual native +/// from the assets passed in the `build_output.json` from individual native /// assets builds. This library defines the format of the former so that it /// can be reused in `package:dartdev` and `package:flutter_tools`. /// @@ -36,7 +36,7 @@ class KernelAssets { 'native-assets': { for (final entry in assetsPerTarget.entries) entry.key.toString(): { - for (final e in entry.value) e.id: e.path.toYaml(), + for (final e in entry.value) e.id: e.path.toJson(), } }, }; @@ -58,7 +58,7 @@ class KernelAsset { } abstract class KernelAssetPath { - List toYaml(); + List toJson(); } /// Asset at absolute path [uri] on the target device where Dart is run. @@ -78,7 +78,7 @@ class KernelAssetAbsolutePath implements KernelAssetPath { int get hashCode => uri.hashCode; @override - List toYaml() => [_pathTypeValue, uri.toFilePath()]; + List toJson() => [_pathTypeValue, uri.toFilePath()]; } /// Asset at relative path [uri], relative to the 'dart file' executed. @@ -111,7 +111,7 @@ class KernelAssetRelativePath implements KernelAssetPath { int get hashCode => uri.hashCode; @override - List toYaml() => [_pathTypeValue, uri.toFilePath()]; + List toJson() => [_pathTypeValue, uri.toFilePath()]; } /// Asset is available on the system `PATH`. @@ -136,7 +136,7 @@ class KernelAssetSystemPath implements KernelAssetPath { String toString() => 'KernelAssetAbsolutePath($uri)'; @override - List toYaml() => [_pathTypeValue, uri.toFilePath()]; + List toJson() => [_pathTypeValue, uri.toFilePath()]; } /// Asset is loaded in the process and symbols are available through @@ -151,7 +151,7 @@ class KernelAssetInProcess implements KernelAssetPath { static const _pathTypeValue = 'process'; @override - List toYaml() => [_pathTypeValue]; + List toJson() => [_pathTypeValue]; } /// Asset is embedded in executable and symbols are available through @@ -166,5 +166,5 @@ class KernelAssetInExecutable implements KernelAssetPath { static const _pathTypeValue = 'executable'; @override - List toYaml() => [_pathTypeValue]; + List toJson() => [_pathTypeValue]; } diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_build_output_format_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_build_output_format_test.dart index 21f15f921c..e148f16de4 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_build_output_format_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_build_output_format_test.dart @@ -45,7 +45,7 @@ void main() async { } else { expect( fullLog, - contains('build_output.yaml contained a format error.'), + contains('build_output.json contained a format error.'), ); } } diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart index a7b4614b96..b0007dc8fd 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart @@ -56,8 +56,8 @@ void main() async { final dryRunDir = packageUri.resolve( '.dart_tool/native_assets_builder/dry_run_${Target.current.os}_dynamic/'); - expect(File.fromUri(dryRunDir.resolve('config.yaml')), exists); - expect(File.fromUri(dryRunDir.resolve('out/build_output.yaml')), exists); + expect(File.fromUri(dryRunDir.resolve('config.json')), exists); + expect(File.fromUri(dryRunDir.resolve('out/build_output.json')), exists); // }); }); diff --git a/pkgs/native_assets_builder/test/helpers.dart b/pkgs/native_assets_builder/test/helpers.dart index 41e79e3a8c..51d42897f3 100644 --- a/pkgs/native_assets_builder/test/helpers.dart +++ b/pkgs/native_assets_builder/test/helpers.dart @@ -120,7 +120,7 @@ Future copyTestProjects({ final manifestString = await manifestFile.readAsString(); final manifestYaml = loadYamlDocument(manifestString); final manifest = [ - for (final path in manifestYaml.contents as YamlList) + for (final path in manifestYaml.contents as List) Uri(path: path as String) ]; final filesToCopy = manifest diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output/hook/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output/hook/build.dart index f36522b9dd..77785e79cd 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output/hook/build.dart @@ -9,7 +9,7 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { final buildConfig = BuildConfigImpl.fromArguments(args); await File.fromUri( - buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) + buildConfig.outputDirectory.resolve(BuildOutputImpl.fileNameV1_1_0)) .writeAsString(_wrongContents); } diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_2/hook/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_2/hook/build.dart index ae90eac446..c8412752ef 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_2/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_2/hook/build.dart @@ -9,7 +9,7 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { final buildConfig = BuildConfigImpl.fromArguments(args); await File.fromUri( - buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) + buildConfig.outputDirectory.resolve(BuildOutputImpl.fileNameV1_1_0)) .writeAsString(_wrongContents); } diff --git a/pkgs/native_assets_builder/test_data/wrong_build_output_3/hook/build.dart b/pkgs/native_assets_builder/test_data/wrong_build_output_3/hook/build.dart index 1a660dc60e..bc1fbca2e3 100644 --- a/pkgs/native_assets_builder/test_data/wrong_build_output_3/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_build_output_3/hook/build.dart @@ -9,7 +9,7 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart'; void main(List args) async { final buildConfig = BuildConfigImpl.fromArguments(args); await File.fromUri( - buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName)) + buildConfig.outputDirectory.resolve(BuildOutputImpl.fileNameV1_1_0)) .writeAsString(_rightContents); exit(1); } diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index 291f6b5511..146b9994b0 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -4,6 +4,8 @@ https://github.com/dart-lang/native/pull/946 - **Breaking change** Move `build.dart` to `hook/build.dart`. https://github.com/dart-lang/native/issues/823 +- **Breaking change** Use JSON instead of YAML in the protocol. + https://github.com/dart-lang/native/issues/991 - Bump examples dependencies to path dependencies. ## 0.4.2 diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/api/asset.dart index 00fcc9c4a3..96c47e2df0 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/api/asset.dart @@ -3,21 +3,20 @@ // BSD-style license that can be found in the LICENSE file. import 'package:pub_semver/pub_semver.dart'; -import 'package:yaml/yaml.dart'; import '../model/target.dart'; +import '../utils/json.dart'; import '../utils/map.dart'; -import '../utils/yaml.dart'; import 'architecture.dart'; import 'build_config.dart'; import 'build_output.dart'; import 'os.dart'; -part 'native_code_asset.dart'; -part 'data_asset.dart'; part '../model/asset.dart'; -part '../model/native_code_asset.dart'; part '../model/data_asset.dart'; +part '../model/native_code_asset.dart'; +part 'data_asset.dart'; +part 'native_code_asset.dart'; /// Data or code bundled with a Dart or Flutter application. /// diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart index 715439cd98..8d6926e510 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -12,8 +12,8 @@ import 'package:pub_semver/pub_semver.dart'; import '../model/metadata.dart'; import '../model/target.dart'; +import '../utils/json.dart'; import '../utils/map.dart'; -import '../utils/yaml.dart'; import 'architecture.dart'; import 'asset.dart'; import 'build.dart'; @@ -22,9 +22,9 @@ import 'ios_sdk.dart'; import 'link_mode_preference.dart'; import 'os.dart'; -part 'c_compiler_config.dart'; part '../model/build_config.dart'; part '../model/c_compiler_config.dart'; +part 'c_compiler_config.dart'; /// The configuration for a `build.dart` invocation. /// diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index e7004a413a..170b8fcb47 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -2,18 +2,19 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:convert'; import 'dart:io'; import 'package:collection/collection.dart'; import 'package:pub_semver/pub_semver.dart'; -import 'package:yaml/yaml.dart'; +import 'package:yaml/yaml.dart' show loadYaml; import '../model/dependencies.dart'; import '../model/metadata.dart'; import '../utils/datetime.dart'; import '../utils/file.dart'; +import '../utils/json.dart'; import '../utils/map.dart'; -import '../utils/yaml.dart'; import 'architecture.dart'; import 'asset.dart'; import 'build_config.dart'; diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/asset.dart index d8f8ea19f5..eec072e5d7 100644 --- a/pkgs/native_assets_cli/lib/src/model/asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/asset.dart @@ -5,19 +5,19 @@ part of '../api/asset.dart'; abstract final class AssetImpl implements Asset { - Map toYaml(Version version); + Map toJson(Version version); - static List listFromYamlList(YamlList list) { + static List listFromJsonList(List list) { final assets = []; - for (final yamlElement in list) { - final yamlMap = as(yamlElement); - final type = yamlMap[NativeCodeAssetImpl.typeKey]; + for (final jsonElement in list) { + final jsonMap = as>(jsonElement); + final type = jsonMap[NativeCodeAssetImpl.typeKey]; switch (type) { case NativeCodeAsset.type: case null: // Backwards compatibility with v1.0.0. - assets.add(NativeCodeAssetImpl.fromYaml(yamlMap)); + assets.add(NativeCodeAssetImpl.fromJson(jsonMap)); case DataAsset.type: - assets.add(DataAssetImpl.fromYaml(yamlMap)); + assets.add(DataAssetImpl.fromJson(jsonMap)); default: // Do nothing, some other launcher might define it's own asset types. } diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart index dc807060ab..9a8dabfc9c 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -103,9 +103,10 @@ final class BuildConfigImpl implements BuildConfig { required LinkModePreferenceImpl linkModePreference, Map? dependencyMetadata, Iterable? supportedAssetTypes, + Version? version, }) { final nonValidated = BuildConfigImpl._() - .._version = latestVersion + .._version = version ?? latestVersion .._outDir = outDir .._packageName = packageName .._packageRoot = packageRoot @@ -120,7 +121,7 @@ final class BuildConfigImpl implements BuildConfig { .._dryRun = false .._supportedAssetTypes = _supportedAssetTypesBackwardsCompatibility(supportedAssetTypes); - final parsedConfigFile = nonValidated.toYaml(); + final parsedConfigFile = nonValidated.toJson(); final config = Config(fileParsed: parsedConfigFile); return BuildConfigImpl.fromConfig(config); } @@ -145,7 +146,7 @@ final class BuildConfigImpl implements BuildConfig { .._dryRun = true .._supportedAssetTypes = _supportedAssetTypesBackwardsCompatibility(supportedAssetTypes); - final parsedConfigFile = nonValidated.toYaml(); + final parsedConfigFile = nonValidated.toJson(); final config = Config(fileParsed: parsedConfigFile); return BuildConfigImpl.fromConfig(config); } @@ -189,7 +190,7 @@ final class BuildConfigImpl implements BuildConfig { if (dependencyMetadata != null) for (final entry in dependencyMetadata.entries) ...[ entry.key, - json.encode(entry.value.toYaml()), + json.encode(entry.value.toJson()), ], ..._supportedAssetTypesBackwardsCompatibility(supportedAssetTypes), ].join('###'); @@ -217,9 +218,9 @@ final class BuildConfigImpl implements BuildConfig { /// and packages through `build.dart` invocations. /// /// If we ever were to make breaking changes, it would be useful to give - /// proper error messages rather than just fail to parse the YAML + /// proper error messages rather than just fail to parse the JSON /// representation in the protocol. - static Version latestVersion = Version(1, 1, 0); + static Version latestVersion = Version(1, 2, 0); factory BuildConfigImpl.fromConfig(Config config) { final result = BuildConfigImpl._().._cCompiler = CCompilerConfigImpl._(); @@ -246,6 +247,9 @@ final class BuildConfigImpl implements BuildConfig { Map? environment, Uri? workingDirectory, }) { + // TODO(https://github.com/dart-lang/native/issues/1000): At some point, + // migrate away from package:cli_config, to get rid of package:yaml + // dependency. final config = Config.fromArgumentsSync( arguments: args, environment: environment, @@ -451,10 +455,10 @@ final class BuildConfigImpl implements BuildConfig { return result.sortOnKey(); } - Map toYaml() { - late Map cCompilerYaml; + Map toJson() { + late Map cCompilerJson; if (!dryRun) { - cCompilerYaml = _cCompiler.toYaml(); + cCompilerJson = _cCompiler.toJson(); } return { @@ -464,7 +468,7 @@ final class BuildConfigImpl implements BuildConfig { OSImpl.configKey: _targetOS.toString(), LinkModePreferenceImpl.configKey: _linkModePreference.toString(), supportedAssetTypesKey: _supportedAssetTypes, - _versionKey: latestVersion.toString(), + _versionKey: version.toString(), if (dryRun) dryRunConfigKey: dryRun, if (!dryRun) ...{ BuildModeImpl.configKey: _buildMode.toString(), @@ -473,18 +477,18 @@ final class BuildConfigImpl implements BuildConfig { IOSSdkImpl.configKey: _targetIOSSdk.toString(), if (_targetAndroidNdkApi != null) targetAndroidNdkApiConfigKey: _targetAndroidNdkApi, - if (cCompilerYaml.isNotEmpty) - CCompilerConfigImpl.configKey: cCompilerYaml, + if (cCompilerJson.isNotEmpty) + CCompilerConfigImpl.configKey: cCompilerJson, if (_dependencyMetadata != null && _dependencyMetadata.isNotEmpty) dependencyMetadataConfigKey: { for (final entry in _dependencyMetadata.entries) - entry.key: entry.value.toYaml(), + entry.key: entry.value.toJson(), }, }, }.sortOnKey(); } - String toYamlString() => yamlEncode(toYaml()); + String toJsonString() => const JsonEncoder.withIndent(' ').convert(toJson()); @override bool operator ==(Object other) { @@ -533,7 +537,7 @@ final class BuildConfigImpl implements BuildConfig { ]); @override - String toString() => 'BuildConfig.build(${toYaml()})'; + String toString() => 'BuildConfig.build(${toJson()})'; void _ensureNotDryRun() { if (dryRun) { diff --git a/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md b/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md index 5975085a52..3f11f16d76 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md +++ b/pkgs/native_assets_cli/lib/src/model/build_config_CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.2.0 + +- Changed default encoding to JSON. + Backwards compatibility: JSON is parsable as YAML. +- Changed default filename. + Compatibility: The filename is explicitly passed in with --config. + ## 1.1.0 - Added supported asset types. 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 fd549287af..529a5a2109 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_output.dart @@ -48,13 +48,20 @@ final class BuildOutputImpl implements BuildOutput { static const _timestampKey = 'timestamp'; static const _versionKey = 'version'; - factory BuildOutputImpl.fromYamlString(String yaml) { - final yamlObject = loadYaml(yaml); - return BuildOutputImpl.fromYaml(as(yamlObject)); + factory BuildOutputImpl.fromJsonString(String jsonString) { + final Object? json; + if (jsonString.startsWith('{')) { + json = jsonDecode(jsonString); + } else { + // TODO(https://github.com/dart-lang/native/issues/1000): At some point + // remove the YAML fallback. + json = loadYaml(jsonString); + } + return BuildOutputImpl.fromJson(as>(json)); } - factory BuildOutputImpl.fromYaml(YamlMap yamlMap) { - final outputVersion = Version.parse(as(yamlMap['version'])); + factory BuildOutputImpl.fromJson(Map jsonMap) { + final outputVersion = Version.parse(as(jsonMap['version'])); if (outputVersion.major > latestVersion.major) { throw FormatException( 'The output version $outputVersion is newer than the ' @@ -71,29 +78,31 @@ final class BuildOutputImpl implements BuildOutput { } final assets = - AssetImpl.listFromYamlList(as(yamlMap[_assetsKey])); + AssetImpl.listFromJsonList(as>(jsonMap[_assetsKey])); return BuildOutputImpl( - timestamp: DateTime.parse(as(yamlMap[_timestampKey])), + timestamp: DateTime.parse(as(jsonMap[_timestampKey])), assets: assets, dependencies: - Dependencies.fromYaml(as(yamlMap[_dependenciesKey])), - metadata: Metadata.fromYaml(as(yamlMap[_metadataKey])), + Dependencies.fromJson(as?>(jsonMap[_dependenciesKey])), + metadata: + Metadata.fromJson(as?>(jsonMap[_metadataKey])), ); } - Map toYaml(Version version) => { + Map toJson(Version version) => { _timestampKey: timestamp.toString(), _assetsKey: [ - for (final asset in _assets) asset.toYaml(version), + for (final asset in _assets) asset.toJson(version), ], if (_dependencies.dependencies.isNotEmpty) - _dependenciesKey: _dependencies.toYaml(), - _metadataKey: _metadata.toYaml(), + _dependenciesKey: _dependencies.toJson(), + _metadataKey: _metadata.toJson(), _versionKey: version.toString(), }..sortOnKey(); - String toYamlString(Version version) => yamlEncode(toYaml(version)); + String toJsonString(Version version) => + const JsonEncoder.withIndent(' ').convert(toJson(version)); /// The version of [BuildOutputImpl]. /// @@ -105,34 +114,50 @@ final class BuildOutputImpl implements BuildOutput { /// representation in the protocol. /// /// [BuildOutput.latestVersion] is tied to [BuildConfig.latestVersion]. This - /// enables making the yaml serialization in `build.dart` dependent on the + /// enables making the JSON serialization in `build.dart` dependent on the /// version of the Dart or Flutter SDK. When there is a need to split the /// versions of BuildConfig and BuildOutput, the BuildConfig should start /// passing the highest supported version of BuildOutput. static Version latestVersion = BuildConfigImpl.latestVersion; - static const fileName = 'build_output.yaml'; + static const fileName = 'build_output.json'; + static const fileNameV1_1_0 = 'build_output.yaml'; - /// Reads the YAML file from [outDir]/[fileName]. + /// Reads the JSON file from [outDir]/[fileName]. static Future readFromFile({required Uri outDir}) async { final buildOutputUri = outDir.resolve(fileName); final buildOutputFile = File.fromUri(buildOutputUri); - if (!await buildOutputFile.exists()) { - return null; + if (await buildOutputFile.exists()) { + return BuildOutputImpl.fromJsonString( + await buildOutputFile.readAsString()); + } + + final buildOutputUriV1_1_0 = outDir.resolve(fileNameV1_1_0); + final buildOutputFileV1_1_0 = File.fromUri(buildOutputUriV1_1_0); + if (await buildOutputFileV1_1_0.exists()) { + return BuildOutputImpl.fromJsonString( + await buildOutputFileV1_1_0.readAsString()); } - return BuildOutputImpl.fromYamlString(await buildOutputFile.readAsString()); + + return null; } - /// Writes the [toYamlString] to [BuildConfig.outputDirectory]/[fileName]. + /// Writes the [toJsonString] to [BuildConfig.outputDirectory]/[fileName]. Future writeToFile({required BuildConfig config}) async { + final configVersion = (config as BuildConfigImpl).version; final outDir = config.outputDirectory; - final buildOutputUri = outDir.resolve(fileName); - final yamlString = toYamlString((config as BuildConfigImpl).version); - await File.fromUri(buildOutputUri).writeAsStringCreateDirectory(yamlString); + final Uri buildOutputUri; + if (configVersion <= Version(1, 1, 0)) { + buildOutputUri = outDir.resolve(fileNameV1_1_0); + } else { + buildOutputUri = outDir.resolve(fileName); + } + final jsonString = toJsonString(configVersion); + await File.fromUri(buildOutputUri).writeAsStringCreateDirectory(jsonString); } @override - String toString() => toYamlString(BuildConfigImpl.latestVersion); + String toString() => toJsonString(BuildConfigImpl.latestVersion); @override bool operator ==(Object other) { 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 a807846fe2..221276c6d5 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 @@ -1,3 +1,10 @@ +## 1.2.0 + +- Changed default encoding to JSON. + Backwards compatibility: JSON is parsable as YAML. +- Changed filename from `build_output.yaml` to `build_output.json`. + Backwards compatibility older SDKs: write to the old file name if an older BuildConfig was passed in. + ## 1.1.0 - Assets now have a `type`. diff --git a/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart b/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart index 10416e4e06..1dc496c0a2 100644 --- a/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/c_compiler_config.dart @@ -60,7 +60,7 @@ final class CCompilerConfigImpl implements CCompilerConfig { static const envScriptArgsConfigKeyFull = '$configKey.$envScriptArgsConfigKey'; - Map toYaml() => { + Map toJson() => { if (_archiver != null) arConfigKey: _archiver.toFilePath(), if (_compiler != null) ccConfigKey: _compiler.toFilePath(), if (_linker != null) ldConfigKey: _linker.toFilePath(), diff --git a/pkgs/native_assets_cli/lib/src/model/data_asset.dart b/pkgs/native_assets_cli/lib/src/model/data_asset.dart index 16df695e74..3de286b8f1 100644 --- a/pkgs/native_assets_cli/lib/src/model/data_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/data_asset.dart @@ -16,9 +16,10 @@ final class DataAssetImpl implements DataAsset, AssetImpl { required this.id, }); - factory DataAssetImpl.fromYaml(YamlMap yamlMap) => DataAssetImpl( - id: as(yamlMap[_idKey]), - file: Uri(path: as(yamlMap[_fileKey])), + factory DataAssetImpl.fromJson(Map jsonMap) => + DataAssetImpl( + id: as(jsonMap[_idKey]), + file: Uri(path: as(jsonMap[_fileKey])), ); @override @@ -36,7 +37,7 @@ final class DataAssetImpl implements DataAsset, AssetImpl { ); @override - Map toYaml(Version version) => { + Map toJson(Version version) => { _idKey: id, _fileKey: file.toFilePath(), typeKey: DataAsset.type, @@ -47,5 +48,5 @@ final class DataAssetImpl implements DataAsset, AssetImpl { static const _fileKey = 'file'; @override - String toString() => 'DataAsset(${toYaml(BuildOutput.latestVersion)})'; + String toString() => 'DataAsset(${toJson(BuildOutput.latestVersion)})'; } diff --git a/pkgs/native_assets_cli/lib/src/model/dependencies.dart b/pkgs/native_assets_cli/lib/src/model/dependencies.dart index 9ecc2cb91f..42dec07a47 100644 --- a/pkgs/native_assets_cli/lib/src/model/dependencies.dart +++ b/pkgs/native_assets_cli/lib/src/model/dependencies.dart @@ -2,12 +2,13 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:convert'; + import 'package:collection/collection.dart'; -import 'package:yaml/yaml.dart'; import '../utils/file.dart'; +import '../utils/json.dart'; import '../utils/uri.dart'; -import '../utils/yaml.dart'; class Dependencies { /// The dependencies a build relied on. @@ -15,29 +16,18 @@ class Dependencies { const Dependencies(this.dependencies); - factory Dependencies.fromYamlString(String yamlString) { - final yaml = loadYaml(yamlString); - if (yaml is YamlList) { - return Dependencies.fromYaml(yaml); - } - // ignore: prefer_const_constructors - return Dependencies([]); - } - - factory Dependencies.fromYaml(YamlList? yamlList) => Dependencies([ - if (yamlList != null) - for (final dependency in yamlList) + factory Dependencies.fromJson(List? jsonList) => Dependencies([ + if (jsonList != null) + for (final dependency in jsonList) fileSystemPathToUri(as(dependency)), ]); - List toYaml() => [ + List toJson() => [ for (final dependency in dependencies) dependency.toFilePath(), ]; - String toYamlString() => yamlEncode(toYaml()); - @override - String toString() => toYamlString(); + String toString() => const JsonEncoder.withIndent(' ').convert(toJson()); Future lastModified() => dependencies.map((u) => u.fileSystemEntity).lastModified(); diff --git a/pkgs/native_assets_cli/lib/src/model/metadata.dart b/pkgs/native_assets_cli/lib/src/model/metadata.dart index 34949b1d42..0a492b98ff 100644 --- a/pkgs/native_assets_cli/lib/src/model/metadata.dart +++ b/pkgs/native_assets_cli/lib/src/model/metadata.dart @@ -3,27 +3,19 @@ // BSD-style license that can be found in the LICENSE file. import 'package:collection/collection.dart'; -import 'package:yaml/yaml.dart'; +import '../utils/json.dart'; import '../utils/map.dart'; -import '../utils/yaml.dart'; class Metadata { final Map metadata; const Metadata(this.metadata); - factory Metadata.fromYaml(YamlMap? yamlMap) => - Metadata(yamlMap?.formatCast() ?? {}); + factory Metadata.fromJson(Map? jsonMap) => + Metadata(jsonMap?.formatCast() ?? {}); - factory Metadata.fromYamlString(String yaml) { - final yamlObject = as(loadYaml(yaml)); - return Metadata.fromYaml(yamlObject); - } - - Map toYaml() => metadata..sortOnKey(); - - String toYamlString() => yamlEncode(toYaml()); + Map toJson() => metadata..sortOnKey(); @override bool operator ==(Object other) { @@ -37,5 +29,5 @@ class Metadata { int get hashCode => const DeepCollectionEquality().hash(metadata); @override - String toString() => 'Metadata(${toYaml()})'; + String toString() => 'Metadata(${toJson()})'; } diff --git a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart index 1608c8ed6b..3073a58550 100644 --- a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart @@ -5,15 +5,15 @@ part of '../api/asset.dart'; abstract final class LinkModeImpl implements LinkMode { - /// Serialization of the current version. + /// Serialization of v1.1.0 and newer /// - /// v1.1.0 does not include the toplevel keys. + /// Does not include the toplevel keys. /// /// ``` /// type: dynamic_loading_system /// uri: ${foo3Uri.toFilePath()} /// ``` - Map toYaml(); + Map toJson(); /// Backwards compatibility with v1.0.0 of the protocol /// @@ -25,7 +25,7 @@ abstract final class LinkModeImpl implements LinkMode { /// path_type: system /// uri: ${foo3Uri.toFilePath()} /// ``` - Map toYamlV1_0_0(Uri? file); + Map toJsonV1_0_0(Uri? file); factory LinkModeImpl(String type, Uri? uri) { switch (type) { @@ -47,10 +47,10 @@ abstract final class LinkModeImpl implements LinkMode { throw FormatException('Unknown type: $type.'); } - /// v1.1.0 only. - factory LinkModeImpl.fromYaml(YamlMap yamlMap) { - final type = as(yamlMap[_typeKey]); - final uriString = as(yamlMap[_uriKey]); + /// v1.1.0 and newer. + factory LinkModeImpl.fromJson(Map jsonMap) { + final type = as(jsonMap[_typeKey]); + final uriString = as(jsonMap[_uriKey]); final uri = uriString != null ? Uri(path: uriString) : null; return LinkModeImpl(type, uri); } @@ -61,22 +61,6 @@ abstract final class LinkModeImpl implements LinkMode { abstract final class DynamicLoadingImpl implements LinkModeImpl, DynamicLoading { - factory DynamicLoadingImpl(String type, Uri? uri) { - switch (type) { - case DynamicLoadingBundledImpl._typeValueV1_0_0: - // For backwards compatibility. - case DynamicLoadingBundledImpl._typeValue: - return DynamicLoadingBundledImpl(); - case DynamicLoadingSystemImpl._typeValue: - return DynamicLoadingSystemImpl(uri!); - case LookupInExecutableImpl._typeValue: - return LookupInExecutableImpl(); - case LookupInProcessImpl._typeValue: - return LookupInProcessImpl(); - } - throw FormatException('Unknown type: $type.'); - } - static const _pathTypeKeyV1_0_0 = 'path_type'; static const _typeKey = 'type'; static const _uriKey = 'uri'; @@ -97,12 +81,12 @@ final class DynamicLoadingBundledImpl static const _typeValue = 'dynamic_loading_bundle'; @override - Map toYaml() => { + Map toJson() => { DynamicLoadingImpl._typeKey: _typeValue, }; @override - Map toYamlV1_0_0(Uri? file) => { + Map toJsonV1_0_0(Uri? file) => { NativeCodeAssetImpl._linkModeKey: DynamicLoadingImpl._typeValueV1_0_0, NativeCodeAssetImpl._pathKey: { DynamicLoadingImpl._pathTypeKeyV1_0_0: _typeValueV1_0_0, @@ -122,13 +106,13 @@ final class DynamicLoadingSystemImpl static const _typeValueV1_0_0 = 'system'; @override - Map toYaml() => { + Map toJson() => { DynamicLoadingImpl._typeKey: _typeValue, DynamicLoadingImpl._uriKey: uri.toFilePath(), }; @override - Map toYamlV1_0_0(Uri? file) => { + Map toJsonV1_0_0(Uri? file) => { NativeCodeAssetImpl._linkModeKey: DynamicLoadingImpl._typeValueV1_0_0, NativeCodeAssetImpl._pathKey: { DynamicLoadingImpl._pathTypeKeyV1_0_0: _typeValueV1_0_0, @@ -159,12 +143,12 @@ final class LookupInProcessImpl implements DynamicLoadingImpl, LookupInProcess { static const _typeValueV1_0_0 = 'process'; @override - Map toYaml() => { + Map toJson() => { DynamicLoadingImpl._typeKey: _typeValue, }; @override - Map toYamlV1_0_0(Uri? file) => { + Map toJsonV1_0_0(Uri? file) => { NativeCodeAssetImpl._linkModeKey: DynamicLoadingImpl._typeValueV1_0_0, NativeCodeAssetImpl._pathKey: { DynamicLoadingImpl._pathTypeKeyV1_0_0: _typeValueV1_0_0, @@ -184,12 +168,12 @@ final class LookupInExecutableImpl static const _typeValueV1_0_0 = 'executable'; @override - Map toYaml() => { + Map toJson() => { DynamicLoadingImpl._typeKey: _typeValue, }; @override - Map toYamlV1_0_0(Uri? file) => { + Map toJsonV1_0_0(Uri? file) => { NativeCodeAssetImpl._linkModeKey: DynamicLoadingImpl._typeValueV1_0_0, NativeCodeAssetImpl._pathKey: { DynamicLoadingImpl._pathTypeKeyV1_0_0: _typeValueV1_0_0, @@ -207,12 +191,12 @@ final class StaticLinkingImpl implements LinkModeImpl, StaticLinking { static const _typeValue = 'static'; @override - Map toYaml() => { + Map toJson() => { DynamicLoadingImpl._typeKey: _typeValue, }; @override - Map toYamlV1_0_0(Uri? file) => { + Map toJsonV1_0_0(Uri? file) => { NativeCodeAssetImpl._linkModeKey: _typeValue, }; } @@ -251,42 +235,42 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { } } - factory NativeCodeAssetImpl.fromYaml(YamlMap yamlMap) { + factory NativeCodeAssetImpl.fromJson(Map jsonMap) { final LinkModeImpl linkMode; - final linkModeYaml = yamlMap[_linkModeKey]; - if (linkModeYaml is String) { + final linkModeJson = jsonMap[_linkModeKey]; + if (linkModeJson is String) { // v1.0.0 - if (linkModeYaml == StaticLinkingImpl._typeValue) { + if (linkModeJson == StaticLinkingImpl._typeValue) { linkMode = StaticLinkingImpl(); } else { - assert(linkModeYaml == DynamicLoadingImpl._typeValueV1_0_0); - final pathYaml = as(yamlMap[_pathKey]); + assert(linkModeJson == DynamicLoadingImpl._typeValueV1_0_0); + final pathJson = as>(jsonMap[_pathKey]); final type = - as(pathYaml[DynamicLoadingImpl._pathTypeKeyV1_0_0]); - final uriString = as(pathYaml[DynamicLoadingImpl._uriKey]); + as(pathJson[DynamicLoadingImpl._pathTypeKeyV1_0_0]); + final uriString = as(pathJson[DynamicLoadingImpl._uriKey]); final uri = uriString != null ? Uri(path: uriString) : null; linkMode = LinkModeImpl(type, uri); } } else { - // v1.1.0 - linkMode = LinkModeImpl.fromYaml(as(linkModeYaml)); + // v1.1.0 and newer. + linkMode = LinkModeImpl.fromJson(as>(linkModeJson)); } - final fileString = as(yamlMap[_fileKey]); + final fileString = as(jsonMap[_fileKey]); final Uri? file; if (fileString != null) { file = Uri(path: fileString); } else if ((linkMode is DynamicLoadingBundledImpl || linkMode is StaticLinkingImpl) && - yamlMap[_pathKey] != null) { + jsonMap[_pathKey] != null) { // Compatibility with v1.0.0. - final oldPath = as( - (yamlMap[_pathKey] as YamlMap)[DynamicLoadingImpl._uriKey]); + final oldPath = as((jsonMap[_pathKey] + as Map)[DynamicLoadingImpl._uriKey]); file = oldPath != null ? Uri(path: oldPath) : null; } else { file = null; } - final targetString = as(yamlMap[_targetKey]); + final targetString = as(jsonMap[_targetKey]); final ArchitectureImpl? architecture; final OSImpl os; if (targetString != null) { @@ -295,8 +279,8 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { os = target.os; architecture = target.architecture; } else { - os = OSImpl.fromString(as(yamlMap[_osKey])); - final architectureString = as(yamlMap[_architectureKey]); + os = OSImpl.fromString(as(jsonMap[_osKey])); + final architectureString = as(jsonMap[_architectureKey]); if (architectureString != null) { architecture = ArchitectureImpl.fromString(architectureString); } else { @@ -305,7 +289,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { } return NativeCodeAssetImpl( - id: as(yamlMap[_idKey]), + id: as(jsonMap[_idKey]), os: os, architecture: architecture, linkMode: linkMode, @@ -350,11 +334,11 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { ); @override - Map toYaml(Version version) { + Map toJson(Version version) { if (version == Version(1, 0, 0)) { return { _idKey: id, - ...linkMode.toYamlV1_0_0(file), + ...linkMode.toJsonV1_0_0(file), _targetKey: Target.fromArchitectureAndOS(architecture!, os).toString(), }..sortOnKey(); } @@ -362,7 +346,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { if (architecture != null) _architectureKey: architecture.toString(), if (file != null) _fileKey: file!.toFilePath(), _idKey: id, - _linkModeKey: linkMode.toYaml(), + _linkModeKey: linkMode.toJson(), _osKey: os.toString(), typeKey: NativeCodeAsset.type, }..sortOnKey(); @@ -379,5 +363,5 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { @override String toString() => - 'NativeCodeAsset(${toYaml(BuildOutputImpl.latestVersion)})'; + 'NativeCodeAsset(${toJson(BuildOutputImpl.latestVersion)})'; } diff --git a/pkgs/native_assets_cli/lib/src/utils/yaml.dart b/pkgs/native_assets_cli/lib/src/utils/json.dart similarity index 52% rename from pkgs/native_assets_cli/lib/src/utils/yaml.dart rename to pkgs/native_assets_cli/lib/src/utils/json.dart index 6e1b9e3235..bd18d97b84 100644 --- a/pkgs/native_assets_cli/lib/src/utils/yaml.dart +++ b/pkgs/native_assets_cli/lib/src/utils/json.dart @@ -2,31 +2,16 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'package:yaml/yaml.dart'; -import 'package:yaml_edit/yaml_edit.dart'; - -String yamlEncode(Object yamlEncoding) { - final editor = YamlEditor(''); - editor.update( - [], - wrapAsYamlNode( - yamlEncoding, - collectionStyle: CollectionStyle.BLOCK, - ), - ); - return editor.toString(); -} - T as(Object? object) { if (object is T) { return object; } throw FormatException( - "Unexpected value '$object' in YAML. Expected a $T.", + "Unexpected value '$object' in JSON. Expected a $T.", ); } -extension YamlMapCast on YamlMap { +extension MapCast on Map { Map formatCast() => { for (final e in entries) as(e.key): as(e.value), }; diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index fe02649a91..c239a71537 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -22,9 +22,9 @@ dependencies: logging: ^1.2.0 pub_semver: ^2.1.3 yaml: ^3.1.1 - yaml_edit: ^2.1.0 dev_dependencies: dart_flutter_team_lints: ^2.1.1 file_testing: ^3.0.0 test: ^1.21.0 + yaml_edit: ^2.1.0 diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index cc0204b350..ec58b0ecd0 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -206,8 +206,8 @@ void main() async { buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, ); - final configFileContents = (buildConfig as BuildConfigImpl).toYamlString(); - final configUri = tempUri.resolve('config.yaml'); + final configFileContents = (buildConfig as BuildConfigImpl).toJsonString(); + final configUri = tempUri.resolve('config.json'); final configFile = File.fromUri(configUri); await configFile.writeAsString(configFileContents); final buildConfigFromArgs = BuildConfig( diff --git a/pkgs/native_assets_cli/test/api/build_test.dart b/pkgs/native_assets_cli/test/api/build_test.dart index 85b74d8ee3..f9c4e5874f 100644 --- a/pkgs/native_assets_cli/test/api/build_test.dart +++ b/pkgs/native_assets_cli/test/api/build_test.dart @@ -55,9 +55,9 @@ void main() async { buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferDynamic, ); - final configYaml = (config1 as BuildConfigImpl).toYamlString(); - buildConfigUri = tempUri.resolve('build_config.yaml'); - await File.fromUri(buildConfigUri).writeAsString(configYaml); + final configJson = (config1 as BuildConfigImpl).toJsonString(); + buildConfigUri = tempUri.resolve('build_config.json'); + await File.fromUri(buildConfigUri).writeAsString(configJson); }); test('build method', () async { diff --git a/pkgs/native_assets_cli/test/example/local_asset_test.dart b/pkgs/native_assets_cli/test/example/local_asset_test.dart index f1b0bec746..3edd81a5fe 100644 --- a/pkgs/native_assets_cli/test/example/local_asset_test.dart +++ b/pkgs/native_assets_cli/test/example/local_asset_test.dart @@ -69,8 +69,8 @@ void main() async { } expect(processResult.exitCode, 0); - final buildOutputUri = tempUri.resolve('build_output.yaml'); - final buildOutput = BuildOutputImpl.fromYamlString( + final buildOutputUri = tempUri.resolve('build_output.json'); + final buildOutput = BuildOutputImpl.fromJsonString( await File.fromUri(buildOutputUri).readAsString()); final assets = buildOutput.assets; final dependencies = buildOutput.dependencies; diff --git a/pkgs/native_assets_cli/test/example/native_add_library_test.dart b/pkgs/native_assets_cli/test/example/native_add_library_test.dart index d5adf5d7c8..3ddbe7b31c 100644 --- a/pkgs/native_assets_cli/test/example/native_add_library_test.dart +++ b/pkgs/native_assets_cli/test/example/native_add_library_test.dart @@ -69,8 +69,8 @@ void main() async { } expect(processResult.exitCode, 0); - final buildOutputUri = tempUri.resolve('build_output.yaml'); - final buildOutput = BuildOutputImpl.fromYamlString( + final buildOutputUri = tempUri.resolve('build_output.json'); + final buildOutput = BuildOutputImpl.fromJsonString( await File.fromUri(buildOutputUri).readAsString()); final assets = buildOutput.assets; final dependencies = buildOutput.dependencies; diff --git a/pkgs/native_assets_cli/test/helpers.dart b/pkgs/native_assets_cli/test/helpers.dart index 1b70aa739c..ae34a37d1b 100644 --- a/pkgs/native_assets_cli/test/helpers.dart +++ b/pkgs/native_assets_cli/test/helpers.dart @@ -6,6 +6,8 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli.dart'; import 'package:native_assets_cli/native_assets_cli_internal.dart' as internal; +import 'package:yaml/yaml.dart'; +import 'package:yaml_edit/yaml_edit.dart'; const keepTempKey = 'KEEP_TEMPORARY_DIRECTORIES'; @@ -139,3 +141,15 @@ extension UriExtension on Uri { return File.fromUri(this); } } + +String yamlEncode(Object yamlEncoding) { + final editor = YamlEditor(''); + editor.update( + [], + wrapAsYamlNode( + yamlEncoding, + collectionStyle: CollectionStyle.BLOCK, + ), + ); + return editor.toString(); +} diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 901932cc2b..d6f5e2013a 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -4,11 +4,11 @@ import 'package:collection/collection.dart'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; -import 'package:native_assets_cli/src/api/asset.dart'; -import 'package:native_assets_cli/src/utils/yaml.dart'; import 'package:test/test.dart'; import 'package:yaml/yaml.dart'; +import '../helpers.dart'; + void main() { final fooUri = Uri.file('path/to/libfoo.so'); final foo3Uri = Uri(path: 'libfoo3.so'); @@ -156,22 +156,22 @@ void main() { test('asset yaml', () { final yaml = yamlEncode([ - for (final item in assets) item.toYaml(BuildOutputImpl.latestVersion) + for (final item in assets) item.toJson(BuildOutputImpl.latestVersion) ]); expect(yaml, assetsYamlEncoding); - final assets2 = AssetImpl.listFromYamlList(loadYaml(yaml) as YamlList); + final assets2 = AssetImpl.listFromJsonList(loadYaml(yaml) as List); expect(assets, assets2); }); test('build_output protocol v1.0.0 keeps working', () { - final assets2 = AssetImpl.listFromYamlList( - loadYaml(assetsYamlEncodingV1_0_0) as YamlList); + final assets2 = AssetImpl.listFromJsonList( + loadYaml(assetsYamlEncodingV1_0_0) as List); expect(nativeCodeAssets, assets2); }); test('AssetPath factory', () async { expect( - () => DynamicLoadingImpl('wrong', null), + () => LinkModeImpl('wrong', null), throwsA(predicate( (e) => e is FormatException && e.message.contains('Unknown type'), )), diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index 6eeb63535d..0c906f7d0c 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -146,7 +146,7 @@ void main() async { expect(fromConfig, equals(buildConfig2)); }); - test('BuildConfig toYaml fromConfig', () { + test('BuildConfig toJson fromConfig', () { final buildConfig1 = BuildConfigImpl( outDir: outDirUri, packageName: packageName, @@ -162,7 +162,7 @@ void main() async { linkModePreference: LinkModePreferenceImpl.preferStatic, ); - final configFile = buildConfig1.toYaml(); + final configFile = buildConfig1.toJson(); final config = Config(fileParsed: configFile); final fromConfig = BuildConfigImpl.fromConfig(config); expect(fromConfig, equals(buildConfig1)); @@ -213,7 +213,7 @@ void main() async { expect(buildConfig1.hashCode == buildConfig2.hashCode, false); }); - test('BuildConfig toYaml fromYaml', () { + test('BuildConfig toJson fromJson', () { final outDir = outDirUri; final buildConfig1 = BuildConfigImpl( outDir: outDir, @@ -239,7 +239,8 @@ void main() async { }), }, ); - final yamlString = buildConfig1.toYamlString(); + + final yamlString = yamlEncode(buildConfig1.toJson()); final expectedYamlString = '''build_mode: release c_compiler: cc: ${fakeClang.toFilePath()} @@ -264,6 +265,42 @@ target_os: ios version: ${BuildConfigImpl.latestVersion}'''; expect(yamlString, equals(expectedYamlString)); + final jsonString = buildConfig1.toJsonString(); + final expectedJsonString = '''{ + "build_mode": "release", + "c_compiler": { + "cc": "${fakeClang.toFilePath()}", + "ld": "${fakeLd.toFilePath()}" + }, + "dependency_metadata": { + "bar": { + "key": "value" + }, + "foo": { + "a": 321, + "z": [ + "z", + "a" + ] + } + }, + "link_mode_preference": "prefer-static", + "out_dir": "${outDir.toFilePath()}", + "package_name": "$packageName", + "package_root": "${tempUri.toFilePath()}", + "supported_asset_types": [ + "${NativeCodeAsset.type}" + ], + "target_architecture": "arm64", + "target_ios_sdk": "iphoneos", + "target_os": "ios", + "version": "${BuildConfigImpl.latestVersion}" +}'''; + expect( + jsonString.replaceAll('\\\\', '/'), + equals(expectedJsonString.replaceAll('\\', '/')), + ); + final buildConfig2 = BuildConfigImpl.fromConfig( Config.fromConfigFileContents( fileContents: yamlString, @@ -272,7 +309,7 @@ version: ${BuildConfigImpl.latestVersion}'''; expect(buildConfig2, buildConfig1); }); - test('BuildConfig fromYaml v1.0.0 keeps working', () { + test('BuildConfig from yaml v1.0.0 keeps working', () { final outDir = outDirUri; final yamlString = '''build_mode: release c_compiler: @@ -442,7 +479,7 @@ version: 1.0.0'''; buildMode: BuildModeImpl.release, linkModePreference: LinkModePreferenceImpl.preferStatic, ); - final configFileContents = buildConfig.toYamlString(); + final configFileContents = buildConfig.toJsonString(); final configUri = tempUri.resolve('config.yaml'); final configFile = File.fromUri(configUri); await configFile.writeAsString(configFileContents); @@ -497,7 +534,7 @@ version: 1.0.0'''; linkModePreference: LinkModePreferenceImpl.dynamic, ); - final configFile = buildConfig1.toYaml(); + final configFile = buildConfig1.toJson(); final config = Config(fileParsed: configFile); final fromConfig = BuildConfigImpl.fromConfig(config); expect(fromConfig, equals(buildConfig1)); @@ -543,7 +580,7 @@ version: 1.0.0'''; ); // Using the checksum for a build folder should be stable. - expect(name1, 'd95547e3b45e88a475739eb4fe03600a'); + expect(name1, '6723f3af2ba4cd70660494965ac55c2a'); // Build folder different due to metadata. final name2 = BuildConfigImpl.checksum( @@ -661,7 +698,7 @@ version: 1.0.0'''; targetOS: OSImpl.windows, linkModePreference: LinkModePreferenceImpl.dynamic, ); - buildConfig.toYamlString(); + buildConfig.toJsonString(); // No crash. }); 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 3b6ea00d30..a7ec89ca5b 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -5,10 +5,11 @@ import 'dart:io'; import 'package:native_assets_cli/native_assets_cli_internal.dart'; -import 'package:native_assets_cli/src/utils/yaml.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:test/test.dart'; +import '../helpers.dart'; + void main() { late Uri tempUri; @@ -121,32 +122,88 @@ metadata: key: value version: ${BuildOutputImpl.latestVersion}'''; + final jsonEncoding = '''{ + "timestamp": "2022-11-10 13:25:01.000", + "assets": [ + { + "architecture": "x64", + "file": "path/to/libfoo.so", + "id": "package:my_package/foo", + "link_mode": { + "type": "dynamic_loading_bundle" + }, + "os": "android", + "type": "native_code" + }, + { + "architecture": "x64", + "id": "package:my_package/foo2", + "link_mode": { + "type": "dynamic_loading_system", + "uri": "path/to/libfoo2.so" + }, + "os": "android", + "type": "native_code" + }, + { + "architecture": "x64", + "id": "package:my_package/foo3", + "link_mode": { + "type": "dynamic_loading_process" + }, + "os": "android", + "type": "native_code" + }, + { + "architecture": "x64", + "id": "package:my_package/foo4", + "link_mode": { + "type": "dynamic_loading_executable" + }, + "os": "android", + "type": "native_code" + } + ], + "dependencies": [ + "path/to/file.ext" + ], + "metadata": { + "key": "value" + }, + "version": "${BuildOutputImpl.latestVersion}" +}'''; + test('built info yaml', () { - final yaml = buildOutput - .toYamlString(BuildOutputImpl.latestVersion) + final yaml = yamlEncode(buildOutput.toJson(BuildOutputImpl.latestVersion)) .replaceAll('\\', '/'); expect(yaml, yamlEncoding); - final buildOutput2 = BuildOutputImpl.fromYamlString(yaml); + + final json = buildOutput + .toJsonString(BuildOutputImpl.latestVersion) + .replaceAll('\\\\', '/'); + expect(json, jsonEncoding); + + final buildOutput2 = BuildOutputImpl.fromJsonString(yaml); expect(buildOutput.hashCode, buildOutput2.hashCode); expect(buildOutput, buildOutput2); }); test('built info yaml v1.0.0 parsing keeps working', () { - final buildOutput2 = BuildOutputImpl.fromYamlString(yamlEncodingV1_0_0); + final buildOutput2 = BuildOutputImpl.fromJsonString(yamlEncodingV1_0_0); expect(buildOutput.hashCode, buildOutput2.hashCode); expect(buildOutput, buildOutput2); }); test('built info yaml v1.0.0 serialization keeps working', () { final yamlEncoding = - yamlEncode(buildOutput.toYaml(Version(1, 0, 0))).replaceAll('\\', '/'); + yamlEncode(buildOutput.toJson(Version(1, 0, 0))).replaceAll('\\', '/'); expect(yamlEncoding, yamlEncodingV1_0_0); }); test('BuildOutput.toString', buildOutput.toString); test('BuildOutput.hashCode', () { - final buildOutput2 = BuildOutputImpl.fromYamlString(yamlEncoding); + final buildOutput2 = BuildOutputImpl.fromJsonString(yamlEncoding); expect(buildOutput.hashCode, buildOutput2.hashCode); final buildOutput3 = BuildOutputImpl( @@ -174,6 +231,26 @@ version: ${BuildOutputImpl.latestVersion}'''; expect(buildOutput2, buildOutput); }); + test('BuildOutput.readFromFile BuildOutput.writeToFile V1.1.0', () async { + final outDir = tempUri.resolve('out_dir/'); + final packageRoot = tempUri.resolve('package_root/'); + await Directory.fromUri(outDir).create(); + await Directory.fromUri(packageRoot).create(); + final config = BuildConfigImpl( + outDir: outDir, + packageName: 'dontcare', + packageRoot: packageRoot, + buildMode: BuildModeImpl.debug, + targetArchitecture: ArchitectureImpl.arm64, + targetOS: OSImpl.macOS, + linkModePreference: LinkModePreferenceImpl.dynamic, + version: Version(1, 1, 0), + ); + await buildOutput.writeToFile(config: config); + final buildOutput2 = await BuildOutputImpl.readFromFile(outDir: outDir); + expect(buildOutput2, buildOutput); + }); + test('Round timestamp', () { final buildOutput3 = BuildOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.372257'), @@ -184,7 +261,7 @@ version: ${BuildOutputImpl.latestVersion}'''; for (final version in ['9001.0.0', '0.0.1']) { test('BuildOutput version $version', () { expect( - () => BuildOutputImpl.fromYamlString('version: $version'), + () => BuildOutputImpl.fromJsonString('version: $version'), throwsA(predicate( (e) => e is FormatException && @@ -197,7 +274,7 @@ version: ${BuildOutputImpl.latestVersion}'''; test('format exception', () { expect( - () => BuildOutputImpl.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 + () => BuildOutputImpl.fromJsonString('''timestamp: 2022-11-10 13:25:01.000 assets: - name: foo link_mode: dynamic @@ -213,7 +290,7 @@ version: 1.0.0'''), throwsFormatException, ); expect( - () => BuildOutputImpl.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 + () => BuildOutputImpl.fromJsonString('''timestamp: 2022-11-10 13:25:01.000 assets: - name: foo link_mode: dynamic @@ -229,7 +306,7 @@ version: 1.0.0'''), throwsFormatException, ); expect( - () => BuildOutputImpl.fromYamlString('''timestamp: 2022-11-10 13:25:01.000 + () => BuildOutputImpl.fromJsonString('''timestamp: 2022-11-10 13:25:01.000 assets: - name: foo link_mode: dynamic diff --git a/pkgs/native_assets_cli/test/model/dependencies_test.dart b/pkgs/native_assets_cli/test/model/dependencies_test.dart index 38eb20b3dd..e11aeb9548 100644 --- a/pkgs/native_assets_cli/test/model/dependencies_test.dart +++ b/pkgs/native_assets_cli/test/model/dependencies_test.dart @@ -25,26 +25,8 @@ void main() { Uri.file('build.dart'), ]); - const yamlEncoding = '''- src/bar.c -- src/baz.c -- src/bla/ -- build.dart'''; - - test('dependencies yaml', () { - final yaml = dependencies.toYamlString().replaceAll('\\', '/'); - expect(yaml, yamlEncoding); - final dependencies2 = Dependencies.fromYamlString(yaml); - expect(dependencies.hashCode, dependencies2.hashCode); - expect(dependencies, dependencies2); - }); - test('dependencies toString', dependencies.toString); - test('dependencies fromYamlString', () { - final dependencies = Dependencies.fromYamlString(''); - expect(dependencies, const Dependencies([])); - }); - test('dependencies lastModified', () async { final dirUri = tempUri.resolve('foo/'); final dir = Directory.fromUri(dirUri); diff --git a/pkgs/native_assets_cli/test/model/metadata_test.dart b/pkgs/native_assets_cli/test/model/metadata_test.dart index 5711bf2b32..e1e2dfa682 100644 --- a/pkgs/native_assets_cli/test/model/metadata_test.dart +++ b/pkgs/native_assets_cli/test/model/metadata_test.dart @@ -16,10 +16,4 @@ void main() { }); test('Metadata toString', metadata.toString); - - test('Metadata toYamlString fromYamlString', () { - final yamlString = metadata.toYamlString(); - final metadata2 = Metadata.fromYamlString(yamlString); - expect(metadata2, metadata); - }); } diff --git a/pkgs/native_toolchain_c/CHANGELOG.md b/pkgs/native_toolchain_c/CHANGELOG.md index dc54048462..7811b50a33 100644 --- a/pkgs/native_toolchain_c/CHANGELOG.md +++ b/pkgs/native_toolchain_c/CHANGELOG.md @@ -3,6 +3,8 @@ - **Breaking change** Completely rewritten API in `native_assets_cli`. - **Breaking change** No longer assumes `build.dart` to be the main script. https://github.com/dart-lang/native/issues/823 +- **Breaking change** Use JSON instead of YAML in the protocol. + https://github.com/dart-lang/native/issues/991 ## 0.3.4+1