Skip to content

Commit 71ada4b

Browse files
authored
[native_assets_cli] Use JSON instead of YAML (#1019)
* #991
1 parent ba431cb commit 71ada4b

35 files changed

+352
-249
lines changed

pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart

+7-7
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,14 @@ class NativeAssetsBuildRunner {
258258
required bool dryRun,
259259
}) async {
260260
final outDir = config.outputDirectory;
261-
final configFile = outDir.resolve('../config.yaml');
261+
final configFile = outDir.resolve('../config.json');
262262
final buildHook = config.packageRoot.resolve('hook/').resolve('build.dart');
263263
final buildHookLegacy = config.packageRoot.resolve('build.dart');
264-
final configFileContents = config.toYamlString();
265-
logger.info('config.yaml contents: $configFileContents');
264+
final configFileContents = config.toJsonString();
265+
logger.info('config.json contents: $configFileContents');
266266
await File.fromUri(configFile).writeAsString(configFileContents);
267267
final buildOutputFile =
268-
File.fromUri(outDir.resolve(BuildOutputImpl.fileName));
268+
File.fromUri(outDir.resolve(BuildOutputImpl.fileNameV1_1_0));
269269
if (await buildOutputFile.exists()) {
270270
// Ensure we'll never read outdated build results.
271271
await buildOutputFile.delete();
@@ -320,7 +320,7 @@ ${result.stdout}
320320
} on FormatException catch (e) {
321321
logger.severe('''
322322
Building native assets for package:${config.packageName} failed.
323-
build_output.yaml contained a format error.
323+
build_output.json contained a format error.
324324
${e.message}
325325
''');
326326
success = false;
@@ -332,14 +332,14 @@ ${e.message}
332332
} on TypeError {
333333
logger.severe('''
334334
Building native assets for package:${config.packageName} failed.
335-
build_output.yaml contained a format error.
335+
build_output.json contained a format error.
336336
''');
337337
success = false;
338338
return (<NativeCodeAssetImpl>[], <Uri>[], const Metadata({}), false);
339339
} finally {
340340
if (!success) {
341341
final buildOutputFile =
342-
File.fromUri(outDir.resolve(BuildOutputImpl.fileName));
342+
File.fromUri(outDir.resolve(BuildOutputImpl.fileNameV1_1_0));
343343
if (await buildOutputFile.exists()) {
344344
await buildOutputFile.delete();
345345
}

pkgs/native_assets_builder/lib/src/model/kernel_assets.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/// kernel file.
77
///
88
/// The `native_assets.yaml` embedded in a kernel file has a different format
9-
/// from the assets passed in the `build_output.yaml` from individual native
9+
/// from the assets passed in the `build_output.json` from individual native
1010
/// assets builds. This library defines the format of the former so that it
1111
/// can be reused in `package:dartdev` and `package:flutter_tools`.
1212
///
@@ -36,7 +36,7 @@ class KernelAssets {
3636
'native-assets': {
3737
for (final entry in assetsPerTarget.entries)
3838
entry.key.toString(): {
39-
for (final e in entry.value) e.id: e.path.toYaml(),
39+
for (final e in entry.value) e.id: e.path.toJson(),
4040
}
4141
},
4242
};
@@ -58,7 +58,7 @@ class KernelAsset {
5858
}
5959

6060
abstract class KernelAssetPath {
61-
List<String> toYaml();
61+
List<String> toJson();
6262
}
6363

6464
/// Asset at absolute path [uri] on the target device where Dart is run.
@@ -78,7 +78,7 @@ class KernelAssetAbsolutePath implements KernelAssetPath {
7878
int get hashCode => uri.hashCode;
7979

8080
@override
81-
List<String> toYaml() => [_pathTypeValue, uri.toFilePath()];
81+
List<String> toJson() => [_pathTypeValue, uri.toFilePath()];
8282
}
8383

8484
/// Asset at relative path [uri], relative to the 'dart file' executed.
@@ -111,7 +111,7 @@ class KernelAssetRelativePath implements KernelAssetPath {
111111
int get hashCode => uri.hashCode;
112112

113113
@override
114-
List<String> toYaml() => [_pathTypeValue, uri.toFilePath()];
114+
List<String> toJson() => [_pathTypeValue, uri.toFilePath()];
115115
}
116116

117117
/// Asset is available on the system `PATH`.
@@ -136,7 +136,7 @@ class KernelAssetSystemPath implements KernelAssetPath {
136136
String toString() => 'KernelAssetAbsolutePath($uri)';
137137

138138
@override
139-
List<String> toYaml() => [_pathTypeValue, uri.toFilePath()];
139+
List<String> toJson() => [_pathTypeValue, uri.toFilePath()];
140140
}
141141

142142
/// Asset is loaded in the process and symbols are available through
@@ -151,7 +151,7 @@ class KernelAssetInProcess implements KernelAssetPath {
151151
static const _pathTypeValue = 'process';
152152

153153
@override
154-
List<String> toYaml() => [_pathTypeValue];
154+
List<String> toJson() => [_pathTypeValue];
155155
}
156156

157157
/// Asset is embedded in executable and symbols are available through
@@ -166,5 +166,5 @@ class KernelAssetInExecutable implements KernelAssetPath {
166166
static const _pathTypeValue = 'executable';
167167

168168
@override
169-
List<String> toYaml() => [_pathTypeValue];
169+
List<String> toJson() => [_pathTypeValue];
170170
}

pkgs/native_assets_builder/test/build_runner/build_runner_build_output_format_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void main() async {
4545
} else {
4646
expect(
4747
fullLog,
48-
contains('build_output.yaml contained a format error.'),
48+
contains('build_output.json contained a format error.'),
4949
);
5050
}
5151
}

pkgs/native_assets_builder/test/build_runner/build_runner_dry_run_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ void main() async {
5656

5757
final dryRunDir = packageUri.resolve(
5858
'.dart_tool/native_assets_builder/dry_run_${Target.current.os}_dynamic/');
59-
expect(File.fromUri(dryRunDir.resolve('config.yaml')), exists);
60-
expect(File.fromUri(dryRunDir.resolve('out/build_output.yaml')), exists);
59+
expect(File.fromUri(dryRunDir.resolve('config.json')), exists);
60+
expect(File.fromUri(dryRunDir.resolve('out/build_output.json')), exists);
6161
//
6262
});
6363
});

pkgs/native_assets_builder/test/helpers.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Future<void> copyTestProjects({
120120
final manifestString = await manifestFile.readAsString();
121121
final manifestYaml = loadYamlDocument(manifestString);
122122
final manifest = [
123-
for (final path in manifestYaml.contents as YamlList)
123+
for (final path in manifestYaml.contents as List<Object?>)
124124
Uri(path: path as String)
125125
];
126126
final filesToCopy = manifest

pkgs/native_assets_builder/test_data/wrong_build_output/hook/build.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart';
99
void main(List<String> args) async {
1010
final buildConfig = BuildConfigImpl.fromArguments(args);
1111
await File.fromUri(
12-
buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName))
12+
buildConfig.outputDirectory.resolve(BuildOutputImpl.fileNameV1_1_0))
1313
.writeAsString(_wrongContents);
1414
}
1515

pkgs/native_assets_builder/test_data/wrong_build_output_2/hook/build.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart';
99
void main(List<String> args) async {
1010
final buildConfig = BuildConfigImpl.fromArguments(args);
1111
await File.fromUri(
12-
buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName))
12+
buildConfig.outputDirectory.resolve(BuildOutputImpl.fileNameV1_1_0))
1313
.writeAsString(_wrongContents);
1414
}
1515

pkgs/native_assets_builder/test_data/wrong_build_output_3/hook/build.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:native_assets_cli/native_assets_cli_internal.dart';
99
void main(List<String> args) async {
1010
final buildConfig = BuildConfigImpl.fromArguments(args);
1111
await File.fromUri(
12-
buildConfig.outputDirectory.resolve(BuildOutputImpl.fileName))
12+
buildConfig.outputDirectory.resolve(BuildOutputImpl.fileNameV1_1_0))
1313
.writeAsString(_rightContents);
1414
exit(1);
1515
}

pkgs/native_assets_cli/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
https://github.com/dart-lang/native/pull/946
55
- **Breaking change** Move `build.dart` to `hook/build.dart`.
66
https://github.com/dart-lang/native/issues/823
7+
- **Breaking change** Use JSON instead of YAML in the protocol.
8+
https://github.com/dart-lang/native/issues/991
79
- Bump examples dependencies to path dependencies.
810

911
## 0.4.2

pkgs/native_assets_cli/lib/src/api/asset.dart

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:pub_semver/pub_semver.dart';
6-
import 'package:yaml/yaml.dart';
76

87
import '../model/target.dart';
8+
import '../utils/json.dart';
99
import '../utils/map.dart';
10-
import '../utils/yaml.dart';
1110
import 'architecture.dart';
1211
import 'build_config.dart';
1312
import 'build_output.dart';
1413
import 'os.dart';
1514

16-
part 'native_code_asset.dart';
17-
part 'data_asset.dart';
1815
part '../model/asset.dart';
19-
part '../model/native_code_asset.dart';
2016
part '../model/data_asset.dart';
17+
part '../model/native_code_asset.dart';
18+
part 'data_asset.dart';
19+
part 'native_code_asset.dart';
2120

2221
/// Data or code bundled with a Dart or Flutter application.
2322
///

pkgs/native_assets_cli/lib/src/api/build_config.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import 'package:pub_semver/pub_semver.dart';
1212

1313
import '../model/metadata.dart';
1414
import '../model/target.dart';
15+
import '../utils/json.dart';
1516
import '../utils/map.dart';
16-
import '../utils/yaml.dart';
1717
import 'architecture.dart';
1818
import 'asset.dart';
1919
import 'build.dart';
@@ -22,9 +22,9 @@ import 'ios_sdk.dart';
2222
import 'link_mode_preference.dart';
2323
import 'os.dart';
2424

25-
part 'c_compiler_config.dart';
2625
part '../model/build_config.dart';
2726
part '../model/c_compiler_config.dart';
27+
part 'c_compiler_config.dart';
2828

2929
/// The configuration for a `build.dart` invocation.
3030
///

pkgs/native_assets_cli/lib/src/api/build_output.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
56
import 'dart:io';
67

78
import 'package:collection/collection.dart';
89
import 'package:pub_semver/pub_semver.dart';
9-
import 'package:yaml/yaml.dart';
10+
import 'package:yaml/yaml.dart' show loadYaml;
1011

1112
import '../model/dependencies.dart';
1213
import '../model/metadata.dart';
1314
import '../utils/datetime.dart';
1415
import '../utils/file.dart';
16+
import '../utils/json.dart';
1517
import '../utils/map.dart';
16-
import '../utils/yaml.dart';
1718
import 'architecture.dart';
1819
import 'asset.dart';
1920
import 'build_config.dart';

pkgs/native_assets_cli/lib/src/model/asset.dart

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
part of '../api/asset.dart';
66

77
abstract final class AssetImpl implements Asset {
8-
Map<String, Object> toYaml(Version version);
8+
Map<String, Object> toJson(Version version);
99

10-
static List<AssetImpl> listFromYamlList(YamlList list) {
10+
static List<AssetImpl> listFromJsonList(List<Object?> list) {
1111
final assets = <AssetImpl>[];
12-
for (final yamlElement in list) {
13-
final yamlMap = as<YamlMap>(yamlElement);
14-
final type = yamlMap[NativeCodeAssetImpl.typeKey];
12+
for (final jsonElement in list) {
13+
final jsonMap = as<Map<Object?, Object?>>(jsonElement);
14+
final type = jsonMap[NativeCodeAssetImpl.typeKey];
1515
switch (type) {
1616
case NativeCodeAsset.type:
1717
case null: // Backwards compatibility with v1.0.0.
18-
assets.add(NativeCodeAssetImpl.fromYaml(yamlMap));
18+
assets.add(NativeCodeAssetImpl.fromJson(jsonMap));
1919
case DataAsset.type:
20-
assets.add(DataAssetImpl.fromYaml(yamlMap));
20+
assets.add(DataAssetImpl.fromJson(jsonMap));
2121
default:
2222
// Do nothing, some other launcher might define it's own asset types.
2323
}

pkgs/native_assets_cli/lib/src/model/build_config.dart

+19-15
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ final class BuildConfigImpl implements BuildConfig {
103103
required LinkModePreferenceImpl linkModePreference,
104104
Map<String, Metadata>? dependencyMetadata,
105105
Iterable<String>? supportedAssetTypes,
106+
Version? version,
106107
}) {
107108
final nonValidated = BuildConfigImpl._()
108-
.._version = latestVersion
109+
.._version = version ?? latestVersion
109110
.._outDir = outDir
110111
.._packageName = packageName
111112
.._packageRoot = packageRoot
@@ -120,7 +121,7 @@ final class BuildConfigImpl implements BuildConfig {
120121
.._dryRun = false
121122
.._supportedAssetTypes =
122123
_supportedAssetTypesBackwardsCompatibility(supportedAssetTypes);
123-
final parsedConfigFile = nonValidated.toYaml();
124+
final parsedConfigFile = nonValidated.toJson();
124125
final config = Config(fileParsed: parsedConfigFile);
125126
return BuildConfigImpl.fromConfig(config);
126127
}
@@ -145,7 +146,7 @@ final class BuildConfigImpl implements BuildConfig {
145146
.._dryRun = true
146147
.._supportedAssetTypes =
147148
_supportedAssetTypesBackwardsCompatibility(supportedAssetTypes);
148-
final parsedConfigFile = nonValidated.toYaml();
149+
final parsedConfigFile = nonValidated.toJson();
149150
final config = Config(fileParsed: parsedConfigFile);
150151
return BuildConfigImpl.fromConfig(config);
151152
}
@@ -189,7 +190,7 @@ final class BuildConfigImpl implements BuildConfig {
189190
if (dependencyMetadata != null)
190191
for (final entry in dependencyMetadata.entries) ...[
191192
entry.key,
192-
json.encode(entry.value.toYaml()),
193+
json.encode(entry.value.toJson()),
193194
],
194195
..._supportedAssetTypesBackwardsCompatibility(supportedAssetTypes),
195196
].join('###');
@@ -217,9 +218,9 @@ final class BuildConfigImpl implements BuildConfig {
217218
/// and packages through `build.dart` invocations.
218219
///
219220
/// If we ever were to make breaking changes, it would be useful to give
220-
/// proper error messages rather than just fail to parse the YAML
221+
/// proper error messages rather than just fail to parse the JSON
221222
/// representation in the protocol.
222-
static Version latestVersion = Version(1, 1, 0);
223+
static Version latestVersion = Version(1, 2, 0);
223224

224225
factory BuildConfigImpl.fromConfig(Config config) {
225226
final result = BuildConfigImpl._().._cCompiler = CCompilerConfigImpl._();
@@ -246,6 +247,9 @@ final class BuildConfigImpl implements BuildConfig {
246247
Map<String, String>? environment,
247248
Uri? workingDirectory,
248249
}) {
250+
// TODO(https://github.com/dart-lang/native/issues/1000): At some point,
251+
// migrate away from package:cli_config, to get rid of package:yaml
252+
// dependency.
249253
final config = Config.fromArgumentsSync(
250254
arguments: args,
251255
environment: environment,
@@ -451,10 +455,10 @@ final class BuildConfigImpl implements BuildConfig {
451455
return result.sortOnKey();
452456
}
453457

454-
Map<String, Object> toYaml() {
455-
late Map<String, Object> cCompilerYaml;
458+
Map<String, Object> toJson() {
459+
late Map<String, Object> cCompilerJson;
456460
if (!dryRun) {
457-
cCompilerYaml = _cCompiler.toYaml();
461+
cCompilerJson = _cCompiler.toJson();
458462
}
459463

460464
return {
@@ -464,7 +468,7 @@ final class BuildConfigImpl implements BuildConfig {
464468
OSImpl.configKey: _targetOS.toString(),
465469
LinkModePreferenceImpl.configKey: _linkModePreference.toString(),
466470
supportedAssetTypesKey: _supportedAssetTypes,
467-
_versionKey: latestVersion.toString(),
471+
_versionKey: version.toString(),
468472
if (dryRun) dryRunConfigKey: dryRun,
469473
if (!dryRun) ...{
470474
BuildModeImpl.configKey: _buildMode.toString(),
@@ -473,18 +477,18 @@ final class BuildConfigImpl implements BuildConfig {
473477
IOSSdkImpl.configKey: _targetIOSSdk.toString(),
474478
if (_targetAndroidNdkApi != null)
475479
targetAndroidNdkApiConfigKey: _targetAndroidNdkApi,
476-
if (cCompilerYaml.isNotEmpty)
477-
CCompilerConfigImpl.configKey: cCompilerYaml,
480+
if (cCompilerJson.isNotEmpty)
481+
CCompilerConfigImpl.configKey: cCompilerJson,
478482
if (_dependencyMetadata != null && _dependencyMetadata.isNotEmpty)
479483
dependencyMetadataConfigKey: {
480484
for (final entry in _dependencyMetadata.entries)
481-
entry.key: entry.value.toYaml(),
485+
entry.key: entry.value.toJson(),
482486
},
483487
},
484488
}.sortOnKey();
485489
}
486490

487-
String toYamlString() => yamlEncode(toYaml());
491+
String toJsonString() => const JsonEncoder.withIndent(' ').convert(toJson());
488492

489493
@override
490494
bool operator ==(Object other) {
@@ -533,7 +537,7 @@ final class BuildConfigImpl implements BuildConfig {
533537
]);
534538

535539
@override
536-
String toString() => 'BuildConfig.build(${toYaml()})';
540+
String toString() => 'BuildConfig.build(${toJson()})';
537541

538542
void _ensureNotDryRun() {
539543
if (dryRun) {

0 commit comments

Comments
 (0)