-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate_cli.dart
329 lines (302 loc) · 10 KB
/
generate_cli.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import 'dart:convert' show jsonDecode;
import 'dart:io';
import 'package:wasm_wit_component/generator.dart';
import 'package:wasm_wit_component/src/result.dart';
/// Generates Dart code from a Wasm WIT file.
///
/// ```bash
/// dart run wasm_wit_component/bin/generate.dart wit/host.wit wasm_wit_component/bin/host.dart
/// ```
Future<void> generateCli(List<String> arguments) async {
final args = GeneratorCLIArgs.fromArgs(arguments);
final witInputPath = args.witInputPath;
// TODO: multiple files or directory
final dartFilePath = args.dartFilePath ??
(witInputPath.endsWith('.wit')
? '${witInputPath.substring(0, witInputPath.length - 4)}_wit.gen.dart'
: '${witInputPath}_wit.gen.dart');
final watch = args.watch;
final wasiConfig = wasiConfigFromPath(witInputPath);
final world = await createDartWitGenerator(wasiConfig: wasiConfig);
final config = args.config.copyWith(
inputs: FileSystemPaths(
inputPath: Uri.file((args.config.inputs as FileSystemPaths).inputPath)
// wasi does not support windows "\" sepparated paths
.toFilePath(windows: false),
),
);
Future<void> generate() async {
final result = world.generate(config: config);
switch (result) {
case Ok(ok: final file):
final ioFile = await File(dartFilePath).create(recursive: true);
await ioFile.writeAsString(file.contents);
try {
await Process.run('dart', ['format', dartFilePath]);
} catch (_) {}
case Err(:final error):
if (watch) {
print('Wit generation error: $error');
} else {
throw Exception(error);
}
}
}
await generate();
if (watch) {
final watchDir = parentFromPath(witInputPath).$1;
await for (final event in watchDir.watch(recursive: true)) {
if (event.path.endsWith('.wit')) {
print('reloading...');
await generate();
}
}
}
}
(Directory parent, File? file) parentFromPath(String path) {
final type = FileSystemEntity.typeSync(path);
if (type == FileSystemEntityType.notFound) {
throw Exception('wit file not found: $path');
}
final isFile = type == FileSystemEntityType.file;
final parent = isFile ? File(path).parent : Directory(path);
final dartFilePath = isFile ? File(path) : null;
return (parent, dartFilePath);
}
/// The CLI arguments for the generator.
class GeneratorCLIArgs {
final WitGeneratorConfig config;
final String witInputPath;
final String? dartFilePath;
final bool watch;
/// The CLI arguments for the generator.
const GeneratorCLIArgs({
required this.config,
required this.witInputPath,
required this.dartFilePath,
required this.watch,
});
/// Creates a new [GeneratorCLIArgs] from the given [arguments].
factory GeneratorCLIArgs.fromArgs(List<String> arguments) {
final args = _CLIArgs(arguments: arguments, valueNames: _Arg.allValues);
final positional = args.positional;
if (positional.isEmpty) {
throw Exception('Missing positional argument `witInputPath`.');
} else if (positional.length > 2) {
throw Exception(
'Too many positional arguments.'
' Expected` witInputPath` and `dartOutputPath` (optional).',
);
}
for (final e in args.namedBool.keys) {
if (!_Arg.allBool.contains(e)) {
throw Exception('Unknown argument "$e".');
}
}
final witInputPath = positional[0];
final dartFilePath = positional.length > 1 ? positional[1] : null;
final watch = args.namedBool[_Arg.watch] ?? false;
final enableDefault = args.namedBool[_Arg.default_] ?? true;
final config = WitGeneratorConfig(
inputs: FileSystemPaths(inputPath: witInputPath),
jsonSerialization:
args.namedBool[_Arg.jsonSerialization] ?? enableDefault,
copyWith_: args.namedBool[_Arg.copyWith] ?? enableDefault,
equalityAndHashCode: args.namedBool[_Arg.equality] ?? enableDefault,
toString_: args.namedBool[_Arg.toString_] ?? enableDefault,
generateDocs: args.namedBool[_Arg.generateDocs] ?? enableDefault,
fileHeader: args.singleArgValue(_Arg.fileHeader),
requiredOption: args.namedBool[_Arg.requiredOption] ?? false,
useNullForOption: args.namedBool[_Arg.useNullForOption] ?? true,
typedNumberLists: args.namedBool[_Arg.typedNumberLists] ?? true,
asyncWorker: args.namedBool[_Arg.asyncWorker] ?? false,
sameClassUnion: args.namedBool[_Arg.sameClassUnion] ?? true,
objectComparator: args.singleArgValue(_Arg.objectComparator),
int64Type: args.singleArgEnum(_Arg.int64Type, Int64TypeConfig.values) ??
Int64TypeConfig.bigInt,
);
return GeneratorCLIArgs(
dartFilePath: dartFilePath,
witInputPath: witInputPath,
watch: watch,
config: config,
);
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is GeneratorCLIArgs &&
runtimeType == other.runtimeType &&
config == other.config &&
witInputPath == other.witInputPath &&
dartFilePath == other.dartFilePath &&
watch == other.watch;
@override
int get hashCode =>
config.hashCode ^
witInputPath.hashCode ^
dartFilePath.hashCode ^
watch.hashCode;
@override
String toString() {
return 'GeneratorCLIArgs{config: $config, witInputPath: $witInputPath,'
' dartFilePath: $dartFilePath, watch: $watch}';
}
}
class _Arg {
static const default_ = 'default';
static const jsonSerialization = 'json-serialization';
static const copyWith = 'copy-with';
static const equality = 'equality';
static const toString_ = 'to-string';
static const generateDocs = 'generate-docs';
static const requiredOption = 'required-option';
static const useNullForOption = 'null-for-option';
static const typedNumberLists = 'typed-number-lists';
static const asyncWorker = 'async-worker';
static const sameClassUnion = 'same-union-class';
static const watch = 'watch';
static const fileHeader = 'file-header';
static const objectComparator = 'object-comparator';
static const int64Type = 'int64-type';
static const configFile = 'config-file';
static const allBool = [
default_,
jsonSerialization,
copyWith,
equality,
toString_,
generateDocs,
useNullForOption,
typedNumberLists,
requiredOption,
asyncWorker,
sameClassUnion,
watch,
];
static const allValues = [
int64Type,
fileHeader,
objectComparator,
configFile,
];
}
class _CLIArgs {
final List<String> arguments;
final List<String> positional;
final Map<String, bool> namedBool;
final Map<String, List<String>> namedValues;
_CLIArgs({
required this.arguments,
required List<String>? valueNames,
String? configFile,
}) : positional = [],
namedBool = {},
namedValues = {} {
final Set<int> usedIndices = {};
for (final arg in arguments.indexed) {
if (!arg.$2.startsWith('-')) {
// Positional
if (!usedIndices.contains(arg.$1)) {
positional.add(arg.$2);
}
continue;
}
if (!arg.$2.startsWith('--')) {
throw Exception('Invalid argument $arg. Should be --<name>');
}
final parts = arg.$2.substring(2).split('=');
String name = parts[0];
if (name == configFile) {
_addFromFile(parts[1]);
continue;
}
if (valueNames != null && valueNames.contains(name)) {
// Named Values
final list = namedValues.putIfAbsent(name, () => []);
if (parts.length == 1) {
final next = arguments.elementAtOrNull(arg.$1 + 1);
if (next == null || next.startsWith('-')) {
throw Exception('Missing value for argument $arg.');
}
usedIndices.add(arg.$1 + 1);
list.add(next);
} else if (parts.length == 2) {
list.add(parts[1]);
} else {
list.add(parts.sublist(1).join('='));
}
} else {
// Bool
final isNegated = name.startsWith('no-');
if (isNegated) {
name = name.substring(3);
}
if (namedBool.containsKey(name)) {
throw Exception('Duplicate argument $arg.');
}
if (parts.length == 1) {
namedBool[name] = !isNegated;
} else if (parts.length == 2) {
if (!const ['true', 'false'].contains(parts[1])) {
throw Exception('Invalid argument $arg. Should be true or false.');
}
namedBool[name] = parts[1] == 'true';
} else {
throw Exception('Invalid argument $arg.');
}
}
}
}
String? singleArgValue(String name) {
final values = namedValues[name];
if (values != null && values.length > 1) {
throw Exception(
'Too many values for argument `name`. $values',
);
}
return values?[0];
}
T? singleArgEnum<T extends Enum>(String name, List<T> options) {
final value = singleArgValue(name);
if (value == null) return null;
return options.firstWhere(
(e) => e.name == value,
orElse: () => throw Exception(
'Unknown $name: $value. Options: '
'${options.map((e) => e.name).join(', ')}',
),
);
}
void _addFromFile(String path) {
final contents = File(path).readAsStringSync();
final wrongConfigFile = Exception(
'Invalid config file $path. Should be a map or list of maps.',
);
var valuesJson = jsonDecode(contents);
if (valuesJson is Map<String, dynamic>) {
valuesJson = [valuesJson];
}
if (valuesJson is! List) {
// TODO: multiple config files?
throw wrongConfigFile;
}
for (final item in valuesJson) {
if (item is! Map<String, dynamic>) {
throw wrongConfigFile;
}
for (final entry in item.entries) {
final key = entry.key;
final value = entry.value;
if (value is bool) {
namedBool[key] = value;
} else if (value is String) {
final list = namedValues.putIfAbsent(key, () => []);
list.add(value);
} else {
throw wrongConfigFile;
}
}
}
}
}