-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_enums.dart
164 lines (150 loc) · 4.95 KB
/
generate_enums.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// ignore_for_file: avoid_print
import 'dart:io';
import 'package:dart_style/dart_style.dart';
/// A member of a [SynthizerEnum].
class SynthizerEnumMember {
/// Create an instance.
SynthizerEnumMember({
required this.dartName,
required this.synthizerName,
required this.value,
});
/// The Dart name for this member.
final String dartName;
/// The name of the Synthizer member.
final String synthizerName;
/// The Synthizer value for this member.
final String value;
/// Get the fully qualified synthizer member name.
String getSynthizerMemberName(final SynthizerEnum e) =>
'${e.synthizerName}.SYZ_$synthizerName';
/// Get the fully qualified Dart member name.
String getDartMemberName(final SynthizerEnum e) => '${e.dartName}.$dartName';
}
/// An enum to be written to disk.
class SynthizerEnum {
/// Create an instance.
SynthizerEnum(this.name) : members = <SynthizerEnumMember>[];
/// The truncated name of this enum.
final String name;
/// The Synthizer name of this enum.
String get synthizerName => 'SYZ_$name';
/// The Dart name for this enum.
String get dartName => getDartName(name, capitaliseFirst: true);
/// All the members of this enum.
final List<SynthizerEnumMember> members;
}
String getDartName(
final String synthizerName, {
final bool capitaliseFirst = false,
}) {
final names = [
for (final name in synthizerName.split('_'))
name[0].toUpperCase() + name.substring(1).toLowerCase(),
];
if (capitaliseFirst == false) {
names[0] = names[0].toLowerCase();
}
return names.join();
}
/// Generate `enumerations.dart`.
Future<void> main() async {
final prefixes = {
'OBJECT_TYPE': 'OTYPE',
'PROPERTIES': 'P',
'EVENT_TYPES': 'EVENT_TYPE',
'INTERPOLATION_TYPES': 'INTERPOLATION_TYPE',
'AUTOMATION_COMMANDS': 'AUTOMATION_COMMAND',
};
final enums = <SynthizerEnum>[];
final bindings = File('lib/src/synthizer_bindings.dart');
final classNamePattern = RegExp(r'^abstract class SYZ_([^ ]+) [{]$');
final classMemberPattern = RegExp(
r'^ static const int SYZ_([^ ]+) = ([^;]+);$',
);
SynthizerEnum? currentEnum;
for (final line in await bindings.readAsLines()) {
if (currentEnum == null) {
final match = classNamePattern.firstMatch(line);
if (match != null) {
currentEnum = SynthizerEnum(match.group(1)!);
print('Found class ${currentEnum.synthizerName}.');
}
} else if (line.startsWith('}')) {
enums.add(currentEnum);
currentEnum = null;
} else {
final match = classMemberPattern.firstMatch(line);
if (match != null) {
final memberName = match.group(1)!;
final memberValue = match.group(2)!;
var dartMemberName = memberName;
if (dartMemberName.startsWith(currentEnum.name)) {
dartMemberName =
dartMemberName.substring(currentEnum.name.length + 1);
}
final prefix = prefixes[currentEnum.name];
if (prefix != null) {
dartMemberName = dartMemberName.substring(prefix.length + 1);
}
currentEnum.members.add(
SynthizerEnumMember(
dartName: getDartName(dartMemberName),
synthizerName: memberName,
value: memberValue,
),
);
}
}
}
final buffer = StringBuffer()
..writeln("import 'synthizer_bindings.dart';")
..writeln("import 'synthizer_error.dart';");
for (final e in enums) {
buffer
..writeln('/// ${e.synthizerName}.')
..writeln('enum ${e.dartName} {');
for (final m in e.members) {
buffer
..writeln('/// SYZ_${m.synthizerName} = ${m.value}')
..writeln('${m.dartName},');
}
buffer
..writeln('}')
..writeln('/// An extension for converting Dart to Synthizer values.')
..writeln('extension ${e.dartName}ToInt on ${e.dartName} {')
..writeln('/// Return an integer.')
..writeln('int toInt() {')
..writeln('switch (this) {');
for (final m in e.members) {
buffer
..writeln('case ${m.getDartMemberName(e)}: ')
..writeln('return ${m.getSynthizerMemberName(e)};');
}
buffer.writeln('}}}');
}
buffer
..writeln('/// An extension for converting integers to Dart values.')
..writeln('extension IntToSynthizer on int {');
for (final e in enums) {
buffer
..writeln('/// Convert from a [${e.synthizerName}] member.')
..writeln('${e.dartName} to${e.dartName}() {')
..writeln('switch(this) {');
for (final m in e.members) {
buffer
..writeln('case ${m.getSynthizerMemberName(e)}:')
..writeln('return ${m.getDartMemberName(e)};');
}
buffer
..writeln('default:')
..writeln('throw SynthizerError(')
..writeln("'Unrecognised `${e.synthizerName}` member.', this,);")
..writeln('}}');
}
buffer.writeln('}');
final formatter = DartFormatter();
File('lib/src/enumerations.dart')
.writeAsStringSync(formatter.format(buffer.toString()));
print('Done.');
}