Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1094)

Unified Diff: pkg/polymer/lib/src/transform/code_extractor.dart

Issue 23445009: Prune the old deploy code. This CL does a few changes: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/polymer/lib/src/transform.dart ('k') | pkg/polymer/lib/src/transform/import_inliner.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/polymer/lib/src/transform/code_extractor.dart
diff --git a/pkg/polymer/lib/src/transform/code_extractor.dart b/pkg/polymer/lib/src/transform/code_extractor.dart
index 0e6d4d535ee9eb6103c5a052e78fa399576fa97a..a3ac240813bece87e7fbf49763360bade74c98bb 100644
--- a/pkg/polymer/lib/src/transform/code_extractor.dart
+++ b/pkg/polymer/lib/src/transform/code_extractor.dart
@@ -7,6 +7,10 @@ library polymer.src.transformers;
import 'dart:async';
+import 'package:analyzer_experimental/src/generated/ast.dart';
+import 'package:analyzer_experimental/src/generated/error.dart';
+import 'package:analyzer_experimental/src/generated/parser.dart';
+import 'package:analyzer_experimental/src/generated/scanner.dart';
import 'package:barback/barback.dart';
import 'package:path/path.dart' as path;
@@ -17,8 +21,9 @@ import 'common.dart';
* separate file for each.
*/
class InlineCodeExtractor extends Transformer {
- Future<bool> isPrimary(Asset input) =>
- new Future.value(input.id.extension == ".html");
+ /** Only run this transformer on .html files. */
+ final String allowedExtensions = ".html";
+
Future apply(Transform transform) {
var inputId = transform.primaryId;
@@ -40,14 +45,18 @@ class InlineCodeExtractor extends Transformer {
continue;
}
- // TODO(sigmund): should we automatically include a library directive
- // if it doesn't have one?
var filename = path.url.basename(inputId.path);
// TODO(sigmund): ensure this filename is unique (dartbug.com/12618).
tag.attributes['src'] = '$filename.$count.dart';
var textContent = tag.nodes.first;
+ var code = textContent.value;
var id = inputId.addExtension('.$count.dart');
- transform.addOutput(new Asset.fromString(id, textContent.value));
+ if (!_hasLibraryDirective(code)) {
+ var libname = path.withoutExtension(id.path)
+ .replaceAll(new RegExp('[-./]'), '_');
+ code = "library $libname;\n$code";
+ }
+ transform.addOutput(new Asset.fromString(id, code));
textContent.remove();
count++;
}
@@ -56,3 +65,16 @@ class InlineCodeExtractor extends Transformer {
});
}
}
+
+/** Parse [code] and determine whether it has a library directive. */
+bool _hasLibraryDirective(String code) {
+ var errorListener = new _ErrorCollector();
+ var token = new StringScanner(null, code, errorListener).tokenize();
+ var unit = new Parser(null, errorListener).parseCompilationUnit(token);
+ return unit.directives.any((d) => d is LibraryDirective);
+}
+
+class _ErrorCollector extends AnalysisErrorListener {
+ final errors = <AnalysisError>[];
+ onError(error) => errors.add(error);
+}
« no previous file with comments | « pkg/polymer/lib/src/transform.dart ('k') | pkg/polymer/lib/src/transform/import_inliner.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698