Skip to content

Commit 8ce7899

Browse files
authored
[pub_formats] Generate package_graph.json API (#2457)
1 parent 68ddbca commit 8ce7899

File tree

11 files changed

+759
-31
lines changed

11 files changed

+759
-31
lines changed

pkgs/hooks/tool/normalize.dart

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -225,36 +225,47 @@ dynamic sortJson(dynamic data, String filePath) {
225225
return data;
226226
}
227227

228+
int _compareTwoItems(dynamic a, dynamic b) {
229+
if (a is Map && b is Map) {
230+
return compareMaps(a, b);
231+
}
232+
if (a is String && b is String) {
233+
return a.compareTo(b);
234+
}
235+
if (a is List && b is List) {
236+
return compareLists(a, b);
237+
}
238+
if (a is int && b is int) {
239+
return a.compareTo(b);
240+
}
241+
throw UnimplementedError('Not implemented to compare $a and $b.');
242+
}
243+
228244
int compareMaps(Map<dynamic, dynamic> a, Map<dynamic, dynamic> b) {
229245
final aKeys = a.keys.toList();
230246
final bKeys = b.keys.toList();
231247
for (var i = 0; i < aKeys.length && i < bKeys.length; i++) {
232-
final comparison = aKeys[i].toString().compareTo(bKeys[i].toString());
233-
if (comparison != 0) {
234-
return comparison;
248+
final keyComparison = _compareTwoItems(aKeys[i], bKeys[i]);
249+
if (keyComparison != 0) {
250+
return keyComparison;
235251
}
236252
final aValue = a[aKeys[i]];
237253
final bValue = b[bKeys[i]];
238-
if (aValue is String && bValue is String) {
239-
final valueComparison = aValue.compareTo(bValue);
240-
if (valueComparison != 0) {
241-
return valueComparison;
242-
} else {
243-
continue;
244-
}
254+
final valueComparison = _compareTwoItems(aValue, bValue);
255+
if (valueComparison != 0) {
256+
return valueComparison;
245257
}
246-
if (aValue is Map && bValue is Map) {
247-
final valueComparison = compareMaps(aValue, bValue);
248-
if (valueComparison != 0) {
249-
return valueComparison;
250-
} else {
251-
continue;
252-
}
253-
}
254-
if (aValue == bValue) {
255-
continue;
256-
}
257-
throw UnimplementedError('Not implemented to compare $aValue and $bValue.');
258258
}
259259
return 0;
260260
}
261+
262+
int compareLists(List<dynamic> a, List<dynamic> b) {
263+
for (var i = 0; i < a.length && i < b.length; i++) {
264+
final comparison = _compareTwoItems(a[i], b[i]);
265+
if (comparison != 0) {
266+
return comparison;
267+
}
268+
}
269+
// If all common elements are equal, the shorter list comes first.
270+
return a.length.compareTo(b.length);
271+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class SchemaAnalyzer {
440440
final itemType = items.type;
441441
switch (itemType) {
442442
case SchemaType.string:
443-
if (items.patterns.isNotEmpty) {
443+
if (items.generateUri) {
444444
dartType = ListDartType(
445445
itemType: const UriDartType(isNullable: false),
446446
isNullable: !required,
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$ref": "#/definitions/PackageGraphFile",
4+
"title": "Package Graph JSON Schema",
5+
"description": "Schema for a Dart package_graph.json file. Note that this is reverse engineered and not the source of truth.",
6+
"definitions": {
7+
"GraphPackage": {
8+
"type": "object",
9+
"properties": {
10+
"dependencies": {
11+
"description": "A list of package names this package directly depends on.",
12+
"type": "array",
13+
"items": {
14+
"$ref": "#/definitions/packageName"
15+
}
16+
},
17+
"devDependencies": {
18+
"description": "A list of package names this package directly depends on for development.",
19+
"type": "array",
20+
"items": {
21+
"$ref": "#/definitions/packageName"
22+
}
23+
},
24+
"name": {
25+
"$ref": "#/definitions/packageName"
26+
},
27+
"version": {
28+
"description": "The semantic version of the package.",
29+
"type": "string"
30+
}
31+
},
32+
"required": [
33+
"dependencies",
34+
"name",
35+
"version"
36+
],
37+
"additionalProperties": false
38+
},
39+
"PackageGraphFile": {
40+
"type": "object",
41+
"properties": {
42+
"configVersion": {
43+
"description": "Version of the package graph configuration.",
44+
"type": "integer",
45+
"minimum": 1
46+
},
47+
"packages": {
48+
"description": "Details of each package in the graph.",
49+
"type": "array",
50+
"items": {
51+
"$ref": "#/definitions/GraphPackage"
52+
}
53+
},
54+
"roots": {
55+
"description": "A list of root package names in the graph.",
56+
"type": "array",
57+
"items": {
58+
"$ref": "#/definitions/packageName"
59+
}
60+
}
61+
},
62+
"required": [
63+
"packages",
64+
"roots"
65+
],
66+
"additionalProperties": false
67+
},
68+
"packageName": {
69+
"description": "The name of the package. Must be a valid Dart identifier.",
70+
"type": "string",
71+
"pattern": "^[a-zA-Z_]\\w*$"
72+
}
73+
}
74+
}

pkgs/pub_formats/doc/schema/pubspec.schema.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@
194194
"type": "string"
195195
},
196196
"name": {
197-
"description": "The name of the package. Must be a valid Dart identifier.",
198-
"type": "string",
199-
"pattern": "^[a-zA-Z_]\\w*$"
197+
"$ref": "#/definitions/packageName"
200198
},
201199
"publish_to": {
202200
"type": "string"
@@ -234,6 +232,11 @@
234232
"$ref": "#/definitions/DependencySource"
235233
}
236234
]
235+
},
236+
"packageName": {
237+
"description": "The name of the package. Must be a valid Dart identifier.",
238+
"type": "string",
239+
"pattern": "^[a-zA-Z_]\\w*$"
237240
}
238241
}
239242
}

pkgs/pub_formats/doc/schema/pubspec_lock.schema.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@
6363
"type": "object",
6464
"properties": {
6565
"name": {
66-
"description": "Name of the package.",
67-
"type": "string",
68-
"pattern": "^[a-zA-Z_]\\w*$"
66+
"$ref": "#/definitions/packageName"
6967
},
7068
"sha256": {
7169
"description": "SHA256 checksum of the package.",
@@ -196,6 +194,11 @@
196194
"dart"
197195
],
198196
"additionalProperties": false
197+
},
198+
"packageName": {
199+
"description": "The name of the package. Must be a valid Dart identifier.",
200+
"type": "string",
201+
"pattern": "^[a-zA-Z_]\\w*$"
199202
}
200203
}
201204
}

pkgs/pub_formats/lib/pubspec_formats.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
/// SDK, so when doing breaking changes please roll ASAP to the Dart SDK.
99
library;
1010

11-
export 'src/pubspec_syntax.g.dart' hide JsonObjectSyntax;
11+
export 'src/package_graph_syntax.g.dart' hide JsonObjectSyntax;
1212
export 'src/pubspec_lock_syntax.g.dart' hide JsonObjectSyntax;
13+
export 'src/pubspec_syntax.g.dart' hide JsonObjectSyntax;

0 commit comments

Comments
 (0)