| 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..9cc97e8411d94f998ed6c8f82c145a1340f2a0c5
|
| --- /dev/null
|
| +++ b/sdk/lib/_internal/pub/lib/src/barback/rewrite_import_transformer.dart
|
| @@ -0,0 +1,47 @@
|
| +// 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.
|
| +class RewriteImportTransformer extends Transformer {
|
| + String get allowedExtensions => '.dart';
|
| +
|
| + Future apply(Transform transform) {
|
| + return transform.primaryInput
|
| + .then((input) => input.readAsString())
|
| + .then((contents) {
|
| + var collector = new _DirectiveCollector();
|
| + 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;
|
| + }
|
| + 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.
|
| +class _DirectiveCollector extends GeneralizingASTVisitor {
|
| + final directives = <UriBasedDirective>[];
|
| +
|
| + visitUriBasedDirective(UriBasedDirective node) => directives.add(node);
|
| +}
|
|
|