Skip to content

Commit a3aad77

Browse files
authored
[native_assets_cli] Separate out json_syntax_generator into separate package (#2101)
Bug: #1826 This refactors `package:hook`s `tool/generate_syntax.dart`. * Moved into its own package `package:json_syntax_generator`. * Documented the feature set: optional/required fields, subclassing, tagged unions, and enums. * Refactored from a single-pass code generator, to a two step pipeline. (1) analyze the JSON schema and extra into a data structure, (2) generate code from the data structure. * Sprinkled comments about the "why" on the most important classes and fields. This PR is intended to be a NOP w.r.t. the syntax that's generated. (Some whitespace and `toString` changes.) Having this as a separate package will enable us to reuse it for the syntax in different places. For now, we're not intending to publish it, instead we can use it as a git dependency in the dev dependencies (or we publish it unlisted). @mosuem Please let me know if it is possible for you to generate the syntax for the `FontAsset`s. Open issue: * The generated code requires a `utils/json.dart`. We should make the generated code be standalone. I'll do that in a follow up PR.
1 parent 8abee90 commit a3aad77

17 files changed

+1788
-1128
lines changed

pkgs/hook/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ environment:
1010
dev_dependencies:
1111
dart_flutter_team_lints: ^2.1.1
1212
json_schema: ^5.2.0
13+
json_syntax_generator:
14+
path: ../json_syntax_generator/
1315
path: ^1.9.1
1416
test: ^1.25.15

pkgs/hook/test/schema/helpers.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ AllSchemas loadSchemas(List<Uri> directories) {
9696
);
9797
allSchemas[entry.key] = schema;
9898
}
99+
100+
final allSchemasInverted = allSchemas.map(
101+
(key, value) => MapEntry(value, key),
102+
);
103+
if (allSchemas.length != allSchemasInverted.length) {
104+
throw StateError(
105+
'Some schemas are not unique, try adding a unique title field.',
106+
);
107+
}
108+
99109
return allSchemas;
100110
}
101111

pkgs/hook/tool/generate_schemas.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'dart:convert';
66
import 'dart:io';
77

88
import '../test/schema/helpers.dart';
9-
import 'generate_syntax.dart';
109
import 'normalize.dart';
1110

1211
void main() {
@@ -184,3 +183,10 @@ void generateEntryPoints() {
184183

185184
String definitionName(Hook hook, InputOrOutput inputOrOutput) =>
186185
'${ucFirst(hook.name)}${ucFirst(inputOrOutput.name)}';
186+
187+
String ucFirst(String str) {
188+
if (str.isEmpty) {
189+
return '';
190+
}
191+
return str[0].toUpperCase() + str.substring(1);
192+
}

0 commit comments

Comments
 (0)