Chromium Code Reviews| Index: sdk/lib/_internal/pub/lib/src/barback/rewrite_import_transformer.dart |
| diff --git a/sdk/lib/_internal/pub/lib/src/barback/rewrite_import_transformer.dart b/sdk/lib/_internal/pub/lib/src/barback/rewrite_import_transformer.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..74ec61ef804df69338dfd8db364075aa39c0b679 |
| --- /dev/null |
| +++ b/sdk/lib/_internal/pub/lib/src/barback/rewrite_import_transformer.dart |
| @@ -0,0 +1,52 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library pub.rewrite_import_transformer; |
| + |
| +import 'package:barback/barback.dart'; |
| +import 'package:analyzer_experimental/analyzer.dart'; |
| + |
| +/// A transformer used internally to rewrite "package:" imports so they point to |
| +/// the barback server rather than to pub's package root. |
|
Bob Nystrom
2013/08/27 22:12:30
So meta.
|
| +class RewriteImportTransformer extends Transformer { |
| + RewriteImportTransformer(); |
|
Bob Nystrom
2013/08/27 22:12:30
Remove this?
nweiz
2013/08/28 20:45:23
Done.
|
| + |
| + String get allowedExtensions => '.dart'; |
| + |
| + Future apply(Transform transform) { |
| + return transform.primaryInput |
| + .then((input) => input.readAsString()) |
| + .then((contents) { |
| + var collector = new _ImportExportCollector(); |
| + parseCompilationUnit(contents, name: transform.primaryId.toString()) |
| + .accept(collector); |
| + |
| + var buffer = new StringBuffer(); |
| + var index = 0; |
| + for (var directive in collector.directives) { |
| + var uri = Uri.parse(directive.uri.stringValue); |
| + if (uri.scheme != 'package') continue; |
| + |
| + buffer |
| + ..write(contents.substring(index, directive.uri.literal.offset)) |
| + ..write('"/packages/${uri.path}"'); |
| + index = directive.uri.literal.end; |
|
Bob Nystrom
2013/08/27 22:12:30
Very nice.
nweiz
2013/08/28 20:45:23
Such as it is.
|
| + } |
| + buffer.write(contents.substring(index, contents.length)); |
| + |
| + transform.addOutput( |
| + new Asset.fromString(transform.primaryId, buffer.toString())); |
| + }); |
| + } |
| +} |
| + |
| +/// A simple visitor that collects import and export nodes. |
|
Bob Nystrom
2013/08/27 22:12:30
What about parts?
nweiz
2013/08/28 20:45:23
Ugh. Done.
|
| +class _ImportExportCollector extends GeneralizingASTVisitor { |
| + final directives = <NamespaceDirective>[]; |
| + |
| + _ImportExportCollector(); |
|
Bob Nystrom
2013/08/27 22:12:30
Remove this?
nweiz
2013/08/28 20:45:23
Done.
|
| + |
| + visitImportDirective(ImportDirective node) => directives.add(node); |
| + visitExportDirective(ExportDirective node) => directives.add(node); |
| +} |