/
hooks.dart
476 lines (412 loc) · 13 KB
/
hooks.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
part of 'generator.dart';
/// {@template hook_dependency_install_failure}
/// Thrown when an error occurs while installing hook dependencies.
/// {@endtemplate}
class HookDependencyInstallFailure extends MasonException {
/// {@macro hook_dependency_install_failure}
HookDependencyInstallFailure(String path, String error)
: super(
'''
Unable to install dependencies for hook: $path.
Error: $error''',
);
}
/// {@template hook_invalid_characters_exception}
/// Thrown when a hook contains non-ascii characters.
/// {@endtemplate}
class HookInvalidCharactersException extends MasonException {
/// {@macro hook_invalid_characters_exception}
HookInvalidCharactersException(String path)
: super(
'''
Unable to execute hook: $path.
Error: Hook contains invalid characters.
Ensure the hook does not contain non-ascii characters.''',
);
}
/// {@template hook_missing_run_exception}
/// Thrown when a hook does not contain a 'run' method.
/// {@endtemplate}
class HookMissingRunException extends MasonException {
/// {@macro hook_missing_run_exception}
HookMissingRunException(String path)
: super(
'''
Unable to execute hook: $path.
Error: Method 'run' not found.
Ensure the hook contains a 'run' method:
import 'package:mason/mason.dart';
void run(HookContext context) {...}''',
);
}
/// {@template hook_run_exception}
/// Thrown when an error occurs when trying to run hook.
/// {@endtemplate}
class HookRunException extends MasonException {
/// {@macro hook_run_exception}
HookRunException(String path, String error)
: super(
'''
Unable to execute hook: $path.
Error: $error''',
);
}
/// {@template hook_execution_exception}
/// Thrown when an error occurs during hook execution.
/// {@endtemplate}
class HookExecutionException extends MasonException {
/// {@macro hook_execution_exception}
HookExecutionException(String path, String error)
: super(
'''
An exception occurred while executing hook: $path.
Error: $error''',
);
}
/// Supported types of [GeneratorHooks].
enum GeneratorHook {
/// Hook run immediately before the `generate` method is invoked.
preGen,
/// Hook run immediately after the `generate` method is invoked.
postGen,
}
/// Extension on [GeneratorHook] for converting
/// a [GeneratorHook] to the corresponding file name.
extension GeneratorHookToFileName on GeneratorHook {
/// Converts a [GeneratorHook] to the corresponding file name.
String toFileName() {
switch (this) {
case GeneratorHook.preGen:
return 'pre_gen.dart';
case GeneratorHook.postGen:
return 'post_gen.dart';
}
}
}
/// {@template generator_hooks}
/// Scripts that run automatically whenever a particular event occurs
/// in a [Generator].
/// {@endtemplate}
class GeneratorHooks {
/// {@macro generator_hooks}
const GeneratorHooks({this.preGenHook, this.postGenHook, this.pubspec});
/// Creates [GeneratorHooks] from a provided [MasonBundle].
factory GeneratorHooks.fromBundle(MasonBundle bundle) {
HookFile? _decodeHookFile(MasonBundledFile? file) {
if (file == null) return null;
final path = file.path;
final raw = file.data.replaceAll(_whiteSpace, '');
final decoded = base64.decode(raw);
try {
return HookFile.fromBytes(path, decoded);
} catch (_) {
return null;
}
}
List<int>? _decodeHookPubspec(MasonBundledFile? file) {
if (file == null) return null;
final raw = file.data.replaceAll(_whiteSpace, '');
return base64.decode(raw);
}
final preGen = bundle.hooks.firstWhereOrNull(
(element) {
return p.basename(element.path) == GeneratorHook.preGen.toFileName();
},
);
final postGen = bundle.hooks.firstWhereOrNull(
(element) {
return p.basename(element.path) == GeneratorHook.postGen.toFileName();
},
);
final pubspec = bundle.hooks.firstWhereOrNull(
(element) {
return p.basename(element.path) == 'pubspec.yaml';
},
);
return GeneratorHooks(
preGenHook: _decodeHookFile(preGen),
postGenHook: _decodeHookFile(postGen),
pubspec: _decodeHookPubspec(pubspec),
);
}
/// Creates [GeneratorHooks] from a provided [BrickYaml].
static Future<GeneratorHooks> fromBrickYaml(BrickYaml brick) async {
Future<HookFile?> getHookFile(GeneratorHook hook) async {
try {
final brickRoot = File(brick.path!).parent.path;
final hooksDirectory = Directory(p.join(brickRoot, BrickYaml.hooks));
final file =
hooksDirectory.listSync().whereType<File>().firstWhereOrNull(
(element) => p.basename(element.path) == hook.toFileName(),
);
if (file == null) return null;
final content = await file.readAsBytes();
return HookFile.fromBytes(file.path, content);
} catch (_) {
return null;
}
}
Future<List<int>?> getHookPubspec() async {
try {
final brickRoot = File(brick.path!).parent.path;
final hooksDirectory = Directory(p.join(brickRoot, BrickYaml.hooks));
final file =
hooksDirectory.listSync().whereType<File>().firstWhereOrNull(
(element) => p.basename(element.path) == 'pubspec.yaml',
);
if (file == null) return null;
return await file.readAsBytes();
} catch (_) {
return null;
}
}
return GeneratorHooks(
preGenHook: await getHookFile(GeneratorHook.preGen),
postGenHook: await getHookFile(GeneratorHook.postGen),
pubspec: await getHookPubspec(),
);
}
/// Hook run immediately before the `generate` method is invoked.
final HookFile? preGenHook;
/// Hook run immediately after the `generate` method is invoked.
final HookFile? postGenHook;
/// Contents of the hooks `pubspec.yaml` if exists.
final List<int>? pubspec;
/// Runs the pre-generation (pre_gen) hook with the specified [vars].
/// An optional [workingDirectory] can also be specified.
Future<void> preGen({
Map<String, dynamic> vars = const <String, dynamic>{},
String? workingDirectory,
void Function(Map<String, dynamic> vars)? onVarsChanged,
}) async {
final preGenHook = this.preGenHook;
if (preGenHook != null && pubspec != null) {
return _runHook(
hook: preGenHook,
vars: vars,
workingDirectory: workingDirectory,
onVarsChanged: onVarsChanged,
);
}
}
/// Runs the post-generation (post_gen) hook with the specified [vars].
/// An optional [workingDirectory] can also be specified.
Future<void> postGen({
Map<String, dynamic> vars = const <String, dynamic>{},
String? workingDirectory,
void Function(Map<String, dynamic> vars)? onVarsChanged,
}) async {
final postGenHook = this.postGenHook;
if (postGenHook != null && pubspec != null) {
return _runHook(
hook: postGenHook,
vars: vars,
workingDirectory: workingDirectory,
onVarsChanged: onVarsChanged,
);
}
}
/// Runs the provided [hook] with the specified [vars].
/// An optional [workingDirectory] can also be specified.
Future<void> _runHook({
required HookFile hook,
Map<String, dynamic> vars = const <String, dynamic>{},
void Function(Map<String, dynamic> vars)? onVarsChanged,
String? workingDirectory,
}) async {
final pubspec = this.pubspec;
final subscriptions = <StreamSubscription>[];
final messagePort = ReceivePort();
final errorPort = ReceivePort();
final exitPort = ReceivePort();
dynamic hookError;
subscriptions.add(errorPort.listen((dynamic error) => hookError = error));
if (onVarsChanged != null) {
subscriptions.add(
messagePort.listen((dynamic message) {
if (message is String) {
onVarsChanged(
json.decode(message) as Map<String, dynamic>,
);
}
}),
);
}
Uri? packageConfigUri;
if (pubspec != null) {
final directoryHash = sha1.convert(pubspec).toString();
final directory = Directory(
p.join(Directory.systemTemp.path, '.mason', directoryHash),
);
final packageConfigFile = File(
p.join(directory.path, '.dart_tool', 'package_config.json'),
);
if (!packageConfigFile.existsSync()) {
await directory.create(recursive: true);
await File(
p.join(directory.path, 'pubspec.yaml'),
).writeAsBytes(pubspec);
final result = await Process.run(
'dart',
['pub', 'get'],
workingDirectory: directory.path,
runInShell: true,
);
if (result.exitCode != 0) {
throw HookDependencyInstallFailure(hook.path, '${result.stderr}');
}
}
packageConfigUri = packageConfigFile.uri;
}
Uri? uri;
try {
uri = _getHookUri(hook.content);
// ignore: avoid_catching_errors
} on ArgumentError {
throw HookInvalidCharactersException(hook.path);
}
if (uri == null) throw HookMissingRunException(hook.path);
final cwd = Directory.current;
Isolate? isolate;
try {
if (workingDirectory != null) Directory.current = workingDirectory;
isolate = await Isolate.spawnUri(
uri,
[json.encode(vars)],
messagePort.sendPort,
paused: true,
packageConfig: packageConfigUri,
);
} on IsolateSpawnException catch (error) {
Directory.current = cwd;
final msg = error.message;
final content = msg.contains('Error: ') ? msg.split('Error: ').last : msg;
throw HookRunException(hook.path, content.trim());
}
isolate
..addErrorListener(errorPort.sendPort)
..addOnExitListener(exitPort.sendPort)
..resume(isolate.pauseCapability!);
try {
await exitPort.first;
} finally {
Directory.current = cwd;
}
for (final subscription in subscriptions) {
unawaited(subscription.cancel());
}
if (hookError != null) {
final dynamic error = hookError;
final content =
error is List && error.isNotEmpty ? '${error.first}' : '$error';
throw HookExecutionException(hook.path, content);
}
}
}
/// {@template hook_file}
/// This class represents a hook file in a generator.
/// The contents should be text and may contain mustache.
/// {@endtemplate}
class HookFile {
/// {@macro hook_file}
HookFile.fromBytes(this.path, this.content);
/// The template file path.
final String path;
/// The template file content.
final List<int> content;
}
/// A reference to core mason APIs to be used within hooks.
///
/// Each hook is defined as a `run` method which accepts a
/// [HookContext] instance.
///
/// [HookContext] exposes APIs to:
/// * read/write template vars
/// * access a [Logger] instance
///
/// ```dart
/// // pre_gen.dart
/// import 'package:mason/mason.dart';
///
/// void run(HookContext context) {
/// // Read/Write vars
/// context.vars = {...context.vars, 'custom_var': 'foo'};
///
/// // Use the logger
/// context.logger.info('hello from pre_gen.dart');
/// }
/// ```
abstract class HookContext {
/// Getter that returns the current map of variables.
Map<String, dynamic> get vars;
/// Setter that enables updating the current map of variables.
set vars(Map<String, dynamic> value);
/// Getter that returns a [Logger] instance.
Logger get logger;
}
final _runRegExp = RegExp(
r'((void||Future<void>)\srun\(HookContext)',
multiLine: true,
);
Uri? _getHookUri(List<int> content) {
final decoded = utf8.decode(content);
if (_runRegExp.hasMatch(decoded)) {
final code = _generatedHookCode(decoded);
return Uri.dataFromString(code, mimeType: 'application/dart');
}
return null;
}
String _generatedHookCode(String content) => '''
// GENERATED CODE - DO NOT MODIFY BY HAND
import 'dart:collection';
import 'dart:convert';
import 'dart:isolate';
$content
void main(List<String> args, SendPort port) {
run(_HookContext._(port, vars: json.decode(args.first)));
}
class _HookContext implements HookContext {
_HookContext._(this._port, {Map<String, dynamic>? vars})
: _vars = _Vars(_port, vars: vars);
final SendPort _port;
_Vars _vars;
@override
Map<String, dynamic> get vars => _vars;
@override
final logger = Logger();
@override
set vars(Map<String, dynamic> value) {
_vars = _Vars(_port, vars: value);
_port.send(json.encode(_vars));
}
}
class _Vars with MapMixin<String, dynamic> {
const _Vars(
this._port, {
Map<String, dynamic>? vars,
}) : _vars = vars ?? const <String, dynamic>{};
final SendPort _port;
final Map<String, dynamic> _vars;
@override
dynamic operator [](Object? key) => _vars[key];
@override
void operator []=(String key, dynamic value) {
_vars[key] = value;
_updateVars();
}
@override
void clear() {
_vars.clear();
_updateVars();
}
@override
Iterable<String> get keys => _vars.keys;
@override
dynamic remove(Object? key) {
final dynamic result = _vars.remove(key);
_updateVars();
return result;
}
void _updateVars() => _port.send(json.encode(_vars));
}
''';