Skip to content

Commit b903d80

Browse files
committed
[native_assets_cli] Namespace asset types
1 parent 2a9abd6 commit b903d80

File tree

20 files changed

+399
-135
lines changed

20 files changed

+399
-135
lines changed

pkgs/code_assets/doc/schema/shared/shared_definitions.schema.json

+40-17
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,55 @@
4040
{
4141
"const": "native_code"
4242
},
43+
{
44+
"const": "code_assets/code"
45+
},
4346
{
4447
"type": "string"
4548
}
4649
]
4750
}
4851
},
49-
"if": {
50-
"properties": {
51-
"type": {
52-
"const": "native_code"
53-
}
54-
}
55-
},
56-
"then": {
57-
"properties": {
58-
"encoding": {
59-
"$ref": "#/definitions/NativeCodeAssetEncoding"
52+
"allOf": [
53+
{
54+
"if": {
55+
"properties": {
56+
"type": {
57+
"const": "native_code"
58+
}
59+
}
60+
},
61+
"then": {
62+
"properties": {
63+
"encoding": {
64+
"$ref": "#/definitions/NativeCodeAssetEncoding"
65+
}
66+
},
67+
"allOf": [
68+
{
69+
"$comment": "Also include the fields parent object for backwards compatibility.",
70+
"$ref": "#/definitions/NativeCodeAssetEncoding"
71+
}
72+
]
6073
}
6174
},
62-
"allOf": [
63-
{
64-
"$comment": "Also include the fields parent object for backwards compatibility.",
65-
"$ref": "#/definitions/NativeCodeAssetEncoding"
75+
{
76+
"if": {
77+
"properties": {
78+
"type": {
79+
"const": "code_assets/code"
80+
}
81+
}
82+
},
83+
"then": {
84+
"properties": {
85+
"encoding": {
86+
"$ref": "#/definitions/NativeCodeAssetEncoding"
87+
}
88+
}
6689
}
67-
]
68-
}
90+
}
91+
]
6992
},
7093
"CCompilerConfig": {
7194
"type": "object",

pkgs/code_assets/test/data/link_input_macos.json

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
},
2020
"os": "macos",
2121
"type": "native_code"
22+
},
23+
{
24+
"encoding": {
25+
"architecture": "arm64",
26+
"file": "/private/var/folders/2y/mngq9h194yzglt4kzttzfq6800klzg/T/iv6i0d/native_add/.dart_tool/native_assets_builder/native_add/c6b312c90c95d2d98ffb6760a738fb36/out/libnative_add.a",
27+
"id": "package:native_add/src/native_add_bindings_generated.dart",
28+
"link_mode": {
29+
"type": "static"
30+
},
31+
"os": "macos"
32+
},
33+
"type": "code_assets/code"
2234
}
2335
],
2436
"config": {

pkgs/code_assets/test/schema/schema_test.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ FieldsFunction _codeFields(AllTestData allTestData) {
113113
expectRequiredFieldMissing,
114114
),
115115
if (hook == Hook.link) ...[
116-
for (final (field, expect) in codeAssetFields)
116+
for (final (field, expect) in codeAssetFields) ...[
117117
for (final encoding in _encoding)
118118
(['assets', 0, ...encoding, ...field], expect),
119+
(['assets', 1, 'encoding', ...field], expect),
120+
],
119121
],
120122
],
121123
if (inputOrOutput == InputOrOutput.output) ...[

pkgs/data_assets/doc/schema/shared/shared_definitions.schema.json

+39-16
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,54 @@
1111
{
1212
"const": "data"
1313
},
14+
{
15+
"const": "data_assets/data"
16+
},
1417
{
1518
"type": "string"
1619
}
1720
]
1821
}
1922
},
20-
"if": {
21-
"properties": {
22-
"type": {
23-
"const": "data"
24-
}
25-
}
26-
},
27-
"then": {
28-
"properties": {
29-
"encoding": {
30-
"$ref": "#/definitions/DataAssetEncoding"
23+
"allOf": [
24+
{
25+
"if": {
26+
"properties": {
27+
"type": {
28+
"const": "data"
29+
}
30+
}
31+
},
32+
"then": {
33+
"properties": {
34+
"encoding": {
35+
"$ref": "#/definitions/DataAssetEncoding"
36+
}
37+
},
38+
"allOf": [
39+
{
40+
"$ref": "#/definitions/DataAssetEncoding"
41+
}
42+
]
3143
}
3244
},
33-
"allOf": [
34-
{
35-
"$ref": "#/definitions/DataAssetEncoding"
45+
{
46+
"if": {
47+
"properties": {
48+
"type": {
49+
"const": "data_assets/data"
50+
}
51+
}
52+
},
53+
"then": {
54+
"properties": {
55+
"encoding": {
56+
"$ref": "#/definitions/DataAssetEncoding"
57+
}
58+
}
3659
}
37-
]
38-
}
60+
}
61+
]
3962
},
4063
"DataAssetEncoding": {
4164
"type": "object",

pkgs/data_assets/test/data/link_input.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
"name": "assets/data_1.json",
1919
"package": "simple_link"
2020
},
21-
"file": "/private/var/folders/2y/mngq9h194yzglt4kzttzfq6800klzg/T/0s5bKi/simple_link/assets/data_1.json",
22-
"name": "assets/data_1.json",
23-
"package": "simple_link",
24-
"type": "data"
21+
"type": "data_assets/data"
2522
}
2623
],
2724
"config": {

pkgs/data_assets/test/schema/schema_test.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ List<(List<Object>, void Function(ValidationResults result))> _dataFields({
4545
}) => <(List<Object>, void Function(ValidationResults result))>[
4646
if (inputOrOutput == InputOrOutput.input) ...[
4747
if (hook == Hook.link) ...[
48-
for (final field in _dataAssetFields)
48+
for (final field in _dataAssetFields) ...[
4949
for (final encoding in _encoding)
5050
(['assets', 0, ...encoding, field], expectRequiredFieldMissing),
51+
(['assets', 1, 'encoding', field], expectRequiredFieldMissing),
52+
],
5153
],
5254
],
5355
if (inputOrOutput == InputOrOutput.output) ...[

pkgs/hooks/tool/generate_syntax.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ void main(List<String> args) {
2828
final analyzedSchema =
2929
SchemaAnalyzer(
3030
schema,
31-
capitalizationOverrides: {
31+
nameOverrides: {
3232
'ios': 'iOS',
3333
'Ios': 'IOS',
3434
'macos': 'macOS',
3535
'Macos': 'MacOS',
3636
'prefer-dynamic': 'preferDynamicOld',
3737
'prefer-static': 'preferStaticOld',
3838
'assetsForLinking': 'assetsForLinkingOld',
39+
'CodeAssetsCodeAsset': 'NativeCodeAssetNew',
40+
'DataAssetsDataAsset': 'DataAssetNew',
3941
},
4042
publicSetters: [
4143
'BuildOutput',

pkgs/json_syntax_generator/lib/src/parser/schema_analyzer.dart

+11-7
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ import '../model/schema_info.dart';
2727
/// * Naming according to "effective Dart". This class expects a JSON schema
2828
/// with snake-cased keys and produces [SchemaInfo] with camel-cased Dart
2929
/// names and types.
30-
/// * Renaming with [capitalizationOverrides].
30+
/// * Renaming with [nameOverrides].
3131
/// * Whether setters are public or not with [publicSetters].
3232
/// * Output sorting alphabetically or with [classSorting].
3333
class SchemaAnalyzer {
3434
final JsonSchema schema;
3535

36-
/// Overriding configuration for capitalization of camel cased strings.
37-
final Map<String, String> capitalizationOverrides;
36+
/// Overriding for names.
37+
///
38+
/// Useful for
39+
/// * capitalization overrides (`macos` -> `macOS`),
40+
/// * naming conflicts (to prevent `myName` and `my_name` clasing), and
41+
/// * renames as preferred.
42+
final Map<String, String> nameOverrides;
3843

3944
/// Optional custom ordering for the output classes.
4045
///
@@ -49,7 +54,7 @@ class SchemaAnalyzer {
4954

5055
SchemaAnalyzer(
5156
this.schema, {
52-
this.capitalizationOverrides = const {},
57+
this.nameOverrides = const {},
5358
this.classSorting,
5459
this.publicSetters = const [],
5560
this.visbleUnionTagValues = const [],
@@ -399,10 +404,9 @@ class SchemaAnalyzer {
399404
return '';
400405
}
401406

402-
final parts = string.split('_');
407+
final parts = string.replaceAll('/', '_').split('_');
403408

404-
String remapCapitalization(String input) =>
405-
capitalizationOverrides[input] ?? input;
409+
String remapCapitalization(String input) => nameOverrides[input] ?? input;
406410

407411
var result = StringBuffer();
408412
result += remapCapitalization(parts[0]);

pkgs/native_assets_cli/lib/src/code_assets/code_asset.dart

+14-6
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,27 @@ final class CodeAsset {
163163
linkMode: linkMode.toSyntax(),
164164
os: _os?.toSyntax(),
165165
);
166-
return EncodedAsset(CodeAssetType.type, encoding.json);
166+
return EncodedAsset(CodeAssetType.typeForAsset, encoding.json);
167167
}
168168
}
169169

170170
extension CodeAssetType on CodeAsset {
171-
// TODO(https://github.com/dart-lang/native/issues/2132): Change to be
172-
// namespaced by package name. (We'll temporarily need to support both the
173-
// old a new type.)
174-
static const String type = syntax.NativeCodeAsset.typeValue;
171+
/// Recognize both new and old type.
172+
///
173+
/// And add both types to builtAssetTypes.
174+
static const typesForBuildAssetTypes = [
175+
syntax.NativeCodeAssetNew.typeValue,
176+
syntax.NativeCodeAsset.typeValue,
177+
];
178+
179+
/// Write the old type to prevent old hooks and SDKs from failing.
180+
// TODO(https://github.com/dart-lang/native/issues/2132): Change this to the
181+
// new value after it has rolled.
182+
static const String typeForAsset = syntax.NativeCodeAsset.typeValue;
175183
}
176184

177185
extension EncodedCodeAsset on EncodedAsset {
178-
bool get isCodeAsset => type == CodeAssetType.type;
186+
bool get isCodeAsset => CodeAssetType.typesForBuildAssetTypes.contains(type);
179187
CodeAsset get asCodeAsset => CodeAsset.fromEncoded(this);
180188
}
181189

pkgs/native_assets_cli/lib/src/code_assets/config.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ extension CodeAssetHookConfig on HookConfig {
1818
/// Code asset specific configuration.
1919
CodeConfig get code => CodeConfig._fromJson(json, path);
2020

21-
bool get buildCodeAssets => buildAssetTypes.contains(CodeAssetType.type);
21+
bool get buildCodeAssets =>
22+
buildAssetTypes
23+
.where((e) => CodeAssetType.typesForBuildAssetTypes.contains(e))
24+
.isNotEmpty;
2225
}
2326

2427
/// Extension to the [LinkInput] providing access to configuration specific to

pkgs/native_assets_cli/lib/src/code_assets/extension.dart

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ final class CodeAssetExtension implements ProtocolExtension {
3434
this.macOS,
3535
});
3636

37-
static const List<String> _buildAssetTypes = [CodeAssetType.type];
38-
3937
@override
4038
void setupBuildInput(BuildInputBuilder input) {
4139
_setupConfig(input);
@@ -47,7 +45,7 @@ final class CodeAssetExtension implements ProtocolExtension {
4745
}
4846

4947
void _setupConfig(HookInputBuilder input) {
50-
input.config.addBuildAssetTypes(_buildAssetTypes);
48+
input.config.addBuildAssetTypes(CodeAssetType.typesForBuildAssetTypes);
5149
// ignore: deprecated_member_use_from_same_package
5250
input.config.setupCode(
5351
targetArchitecture: targetArchitecture,

pkgs/native_assets_cli/lib/src/code_assets/syntax.g.dart

+56
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class Asset {
9797
List<Object> path = const [],
9898
}) {
9999
final result = Asset._fromJson(json, path: path);
100+
if (result.isNativeCodeAssetNew) {
101+
return result.asNativeCodeAssetNew;
102+
}
100103
if (result.isNativeCodeAsset) {
101104
return result.asNativeCodeAsset;
102105
}
@@ -1075,6 +1078,59 @@ class NativeCodeAssetEncoding {
10751078
String toString() => 'NativeCodeAssetEncoding($json)';
10761079
}
10771080

1081+
class NativeCodeAssetNew extends Asset {
1082+
static const typeValue = 'code_assets/code';
1083+
1084+
NativeCodeAssetNew.fromJson(super.json, {super.path}) : super._fromJson();
1085+
1086+
NativeCodeAssetNew({required NativeCodeAssetEncoding? encoding})
1087+
: super(type: 'code_assets/code') {
1088+
_encoding = encoding;
1089+
json.sortOnKey();
1090+
}
1091+
1092+
/// Setup all fields for [NativeCodeAssetNew] that are not in
1093+
/// [Asset].
1094+
void setup({required NativeCodeAssetEncoding? encoding}) {
1095+
_encoding = encoding;
1096+
json.sortOnKey();
1097+
}
1098+
1099+
NativeCodeAssetEncoding? get encoding {
1100+
final jsonValue = _reader.optionalMap('encoding');
1101+
if (jsonValue == null) return null;
1102+
return NativeCodeAssetEncoding.fromJson(
1103+
jsonValue,
1104+
path: [...path, 'encoding'],
1105+
);
1106+
}
1107+
1108+
set _encoding(NativeCodeAssetEncoding? value) {
1109+
json.setOrRemove('encoding', value?.json);
1110+
}
1111+
1112+
List<String> _validateEncoding() {
1113+
final mapErrors = _reader.validate<Map<String, Object?>?>('encoding');
1114+
if (mapErrors.isNotEmpty) {
1115+
return mapErrors;
1116+
}
1117+
return encoding?.validate() ?? [];
1118+
}
1119+
1120+
@override
1121+
List<String> validate() => [...super.validate(), ..._validateEncoding()];
1122+
1123+
@override
1124+
String toString() => 'NativeCodeAssetNew($json)';
1125+
}
1126+
1127+
extension NativeCodeAssetNewExtension on Asset {
1128+
bool get isNativeCodeAssetNew => type == 'code_assets/code';
1129+
1130+
NativeCodeAssetNew get asNativeCodeAssetNew =>
1131+
NativeCodeAssetNew.fromJson(json, path: path);
1132+
}
1133+
10781134
class OS {
10791135
final String name;
10801136

0 commit comments

Comments
 (0)