OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 library initialize.plugin_transformer; | |
5 | |
6 import 'dart:async'; | |
7 import 'package:analyzer/analyzer.dart'; | |
8 import 'package:barback/barback.dart'; | |
9 import 'package:source_maps/refactor.dart'; | |
10 import 'package:source_span/source_span.dart'; | |
11 | |
12 /// Abstract transformer which will call [initEntry] for every [InitEntry] | |
Jennifer Messerly
2015/01/30 20:34:26
could we do the initEntry here instead? it would s
jakemac
2015/01/30 22:16:17
Yes, especially with transforms since they are jus
| |
13 /// expression found in the bootstrap file. This is used to centralize logic | |
14 /// for initializers which want to do something special at transform time. | |
15 abstract class InitializePluginTransformer extends AggregateTransformer { | |
16 // Path to the bootstrap file created by the initialize transformer. | |
17 final String _bootstrapFile; | |
18 | |
19 // All the extra assets that were found, if child classes override | |
20 // classifyPrimary this lets them get at the other assets easily. | |
21 final allAssets = <Asset>[]; | |
22 | |
23 TransformLogger _logger; | |
24 | |
25 InitializePluginTransformer(this._bootstrapFile); | |
26 | |
27 classifyPrimary(AssetId id) { | |
28 if (_bootstrapFile == id.path) return _bootstrapFile; | |
Jennifer Messerly
2015/01/30 20:34:26
silly, but I'd usually format this as:
=> _boots
jakemac
2015/01/30 22:16:18
Done.
| |
29 return null; | |
30 } | |
31 | |
32 Future apply(AggregateTransform transform) { | |
33 _logger = transform.logger; | |
34 var done = new Completer(); | |
35 var listener = transform.primaryInputs.listen((Asset asset) { | |
36 allAssets.add(asset); | |
37 var id = asset.id; | |
38 if (id.path != _bootstrapFile) return; | |
39 // Calls initEntry for each InitEntry expression. | |
40 asset.readAsString().then((dartCode) { | |
41 var url = id.path.startsWith('lib/') | |
42 ? 'package:${id.package}/${id.path.substring(4)}' | |
43 : id.path; | |
44 var source = new SourceFile(dartCode, url: url); | |
45 var transaction = new TextEditTransaction(dartCode, source); | |
46 (parseCompilationUnit(dartCode, | |
47 suppressErrors: true) as dynamic).declarations.firstWhere( | |
Jennifer Messerly
2015/01/30 20:34:26
is there a reason for `as dynamic`?
jakemac
2015/01/30 22:16:18
This is just to get rid of a bunch of casts and ty
| |
48 (d) => d.name.toString() == | |
Jennifer Messerly
2015/01/30 20:34:26
in general not all CompilationUnitMembers have nam
jakemac
2015/01/30 22:16:17
Related to the previous comment, I did start going
| |
49 'main').functionExpression.body.block.statements.firstWhere( | |
50 (statement) { | |
51 return statement.expression.target.toString() == 'initializers' && | |
52 statement.expression.methodName.toString() == 'addAll'; | |
53 }).expression.argumentList.arguments[0].elements | |
54 .where((arg) => arg is InstanceCreationExpression) | |
55 .forEach((e) => initEntry(e, transaction)); | |
56 | |
57 // Apply any transformations. | |
58 if (!transaction.hasEdits) { | |
59 transform.addOutput(asset); | |
60 } else { | |
61 var printer = transaction.commit(); | |
62 printer.build(url); | |
63 transform.addOutput(new Asset.fromString(id, printer.text)); | |
64 } | |
65 done.complete(); | |
66 }); | |
67 }); | |
68 | |
69 // Make sure all the assets are read before returning. | |
70 return Future.wait([done.future, listener.asFuture()]); | |
Jennifer Messerly
2015/01/30 20:34:26
if you're returning listener.asFuture(), do you ne
jakemac
2015/01/30 22:16:17
Looks like you are right, removed it.
| |
71 } | |
72 | |
73 /// Gets called once for each generated [InitEntry] expression in the | |
74 /// bootstrap file. A [TextEditTransaction] is supplied so that the user can | |
75 /// modify the expression however they see fit. | |
76 void initEntry( | |
77 InstanceCreationExpression expression, TextEditTransaction transaction); | |
78 | |
79 /// Convenience method to delete an Initializer expression completely. | |
80 void removeInitializer( | |
81 InstanceCreationExpression expression, TextEditTransaction transaction) { | |
82 // Delete the entire line. | |
83 var line = transaction.file.getLine(expression.offset); | |
84 transaction.edit(transaction.file.getOffset(line), | |
85 transaction.file.getOffset(line + 1), ''); | |
86 } | |
87 } | |
OLD | NEW |