| Index: third_party/pkg/angular/lib/tools/template_cache_generator.dart
|
| diff --git a/third_party/pkg/angular/lib/tools/template_cache_generator.dart b/third_party/pkg/angular/lib/tools/template_cache_generator.dart
|
| index cd806b78707a5e60af0bc4c25e8156d52e375eb2..3c896b8c35ffd72c7d8b9aef0c122c5a0a3ce933 100644
|
| --- a/third_party/pkg/angular/lib/tools/template_cache_generator.dart
|
| +++ b/third_party/pkg/angular/lib/tools/template_cache_generator.dart
|
| @@ -5,7 +5,9 @@ import 'dart:async';
|
| import 'dart:collection';
|
|
|
| import 'package:analyzer/src/generated/ast.dart';
|
| -import 'package:angular/tools/source_crawler_impl.dart';
|
| +import 'package:analyzer/src/generated/source.dart';
|
| +import 'package:analyzer/src/generated/element.dart';
|
| +import 'package:di/generator.dart';
|
|
|
| const String PACKAGE_PREFIX = 'package:';
|
| const String DART_PACKAGE_PREFIX = 'dart:';
|
| @@ -23,38 +25,41 @@ const SYSTEM_PACKAGE_ROOT = '%SYSTEM_PACKAGE_ROOT%';
|
|
|
| main(args) {
|
| if (args.length < 4) {
|
| - print('Usage: templace_cache_generator path_to_entry_point output '
|
| - 'package_root1,package_root2,...|$SYSTEM_PACKAGE_ROOT '
|
| + print('Usage: templace_cache_generator path_to_entry_point sdk_path '
|
| + 'output package_root1,package_root2,...|$SYSTEM_PACKAGE_ROOT '
|
| 'patternUrl1,rewriteTo1;patternUrl2,rewriteTo2 '
|
| 'blacklistClass1,blacklistClass2');
|
| exit(1);
|
| }
|
|
|
| var entryPoint = args[0];
|
| - var output = args[1];
|
| - var outputLibrary = args[2];
|
| - var packageRoots = args[3] == SYSTEM_PACKAGE_ROOT ?
|
| - [Platform.packageRoot] : args[3].split(',');
|
| - Map<RegExp, String> urlRewriters = parseUrlRemapping(args[4]);
|
| - Set<String> blacklistedClasses = (args.length > 5)
|
| - ? new Set.from(args[5].split(','))
|
| + var sdkPath = args[1];
|
| + var output = args[2];
|
| + var outputLibrary = args[3];
|
| + var packageRoots = args[4] == SYSTEM_PACKAGE_ROOT ?
|
| + [Platform.packageRoot] : args[4].split(',');
|
| + Map<RegExp, String> urlRewriters = parseUrlRemapping(args[5]);
|
| + Set<String> blacklistedClasses = (args.length > 6)
|
| + ? new Set.from(args[6].split(','))
|
| : new Set();
|
|
|
| + print('sdkPath: $sdkPath');
|
| print('entryPoint: $entryPoint');
|
| print('output: $output');
|
| print('outputLibrary: $outputLibrary');
|
| print('packageRoots: $packageRoots');
|
| - print('url rewritters: ' + args[4]);
|
| + print('url rewritters: ' + args[5]);
|
| print('blacklistedClasses: ' + blacklistedClasses.join(', '));
|
|
|
|
|
| Map<String, String> templates = {};
|
|
|
| - var c = new SourceCrawlerImpl(packageRoots);
|
| + var c = new SourceCrawler(sdkPath, packageRoots);
|
| var visitor =
|
| new TemplateCollectingVisitor(templates, blacklistedClasses, c);
|
| - c.crawl(entryPoint, (CompilationUnit compilationUnit) =>
|
| - visitor(compilationUnit));
|
| + c.crawl(entryPoint,
|
| + (CompilationUnitElement compilationUnit, SourceFile source) =>
|
| + visitor(compilationUnit, source.canonicalPath));
|
|
|
| var sink = new File(output).openWrite();
|
| return printTemplateCache(
|
| @@ -83,21 +88,24 @@ printTemplateCache(Map<String, String> templateKeyMap,
|
|
|
| outSink.write(fileHeader(outputLibrary));
|
|
|
| - List<Future> reads = <Future>[];
|
| - templateKeyMap.forEach((uri, templateFile) {
|
| - reads.add(new File(templateFile).readAsString().then((fileStr) {
|
| - fileStr = fileStr.replaceAll('"""', r'\"\"\"');
|
| - String resultUri = uri;
|
| - urlRewriters.forEach((regexp, replacement) {
|
| - resultUri = resultUri.replaceFirst(regexp, replacement);
|
| + Future future = new Future.value(0);
|
| + List uris = templateKeyMap.keys.toList()..sort()..forEach((uri) {
|
| + var templateFile = templateKeyMap[uri];
|
| + future = future.then((_) {
|
| + return new File(templateFile).readAsString().then((fileStr) {
|
| + fileStr = fileStr.replaceAll('"""', r'\"\"\"');
|
| + String resultUri = uri;
|
| + urlRewriters.forEach((regexp, replacement) {
|
| + resultUri = resultUri.replaceFirst(regexp, replacement);
|
| + });
|
| + outSink.write(
|
| + 'tc.put("$resultUri", new HttpResponse(200, r"""$fileStr"""));\n');
|
| });
|
| - outSink.write(
|
| - 'tc.put("$resultUri", new HttpResponse(200, r"""$fileStr"""));\n');
|
| - }));
|
| + });
|
| });
|
|
|
| // Wait until all templates files are processed.
|
| - return Future.wait(reads).then((_) {
|
| + return future.then((_) {
|
| outSink.write(FILE_FOOTER);
|
| });
|
| }
|
| @@ -105,12 +113,14 @@ printTemplateCache(Map<String, String> templateKeyMap,
|
| class TemplateCollectingVisitor {
|
| Map<String, String> templates;
|
| Set<String> blacklistedClasses;
|
| - SourceCrawlerImpl sourceCrawlerImpl;
|
| + SourceCrawler sourceCrawler;
|
|
|
| TemplateCollectingVisitor(this.templates, this.blacklistedClasses,
|
| - this.sourceCrawlerImpl);
|
| + this.sourceCrawler);
|
|
|
| - call(CompilationUnit cu) {
|
| + call(CompilationUnitElement cue, String srcPath) {
|
| + CompilationUnit cu = sourceCrawler.context
|
| + .resolveCompilationUnit(cue.source, cue.library);
|
| cu.declarations.forEach((CompilationUnitMember declaration) {
|
| // We only care about classes.
|
| if (declaration is! ClassDeclaration) return;
|
| @@ -119,7 +129,6 @@ class TemplateCollectingVisitor {
|
| bool cache = true;
|
| clazz.metadata.forEach((Annotation ann) {
|
| if (ann.arguments == null) return; // Ignore non-class annotations.
|
| - // TODO(tsander): Add library name as class name could conflict.
|
| if (blacklistedClasses.contains(clazz.name.name)) return;
|
|
|
| switch (ann.name.name) {
|
| @@ -130,7 +139,10 @@ class TemplateCollectingVisitor {
|
| }
|
| });
|
| if (cache && cacheUris.isNotEmpty) {
|
| - cacheUris.forEach((uri) => storeUriAsset(uri));
|
| + var srcDirUri = new Uri.file(srcPath);
|
| + Source currentSrcDir = sourceCrawler.context.sourceFactory
|
| + .resolveUri2(null, srcDirUri);
|
| + cacheUris..sort()..forEach((uri) => storeUriAsset(uri, currentSrcDir));
|
| }
|
| });
|
| }
|
| @@ -140,8 +152,15 @@ class TemplateCollectingVisitor {
|
| if (arg is NamedExpression) {
|
| NamedExpression namedArg = arg;
|
| var paramName = namedArg.name.label.name;
|
| - if (paramName == 'templateUrl' || paramName == 'cssUrl') {
|
| + if (paramName == 'templateUrl') {
|
| cacheUris.add(assertString(namedArg.expression).stringValue);
|
| + } else if (paramName == 'cssUrl') {
|
| + if (namedArg.expression is StringLiteral) {
|
| + cacheUris.add(assertString(namedArg.expression).stringValue);
|
| + } else {
|
| + cacheUris.addAll(assertList(namedArg.expression).elements.map((e) =>
|
| + assertString(e).stringValue));
|
| + }
|
| }
|
| }
|
| });
|
| @@ -156,7 +175,7 @@ class TemplateCollectingVisitor {
|
| var paramName = namedArg.name.label.name;
|
| if (paramName == 'preCacheUrls') {
|
| assertList(namedArg.expression).elements
|
| - .forEach((expression) =>
|
| + ..forEach((expression) =>
|
| cacheUris.add(assertString(expression).stringValue));
|
| }
|
| if (paramName == 'cache') {
|
| @@ -167,10 +186,8 @@ class TemplateCollectingVisitor {
|
| return cache;
|
| }
|
|
|
| - void storeUriAsset(String uri) {
|
| - String assetFileLocation =
|
| - uri.startsWith('package:') ?
|
| - sourceCrawlerImpl.resolvePackagePath(uri) : uri;
|
| + void storeUriAsset(String uri, Source srcPath) {
|
| + String assetFileLocation = findAssetFileLocation(uri, srcPath);
|
| if (assetFileLocation == null) {
|
| print("Could not find asset for uri: $uri");
|
| } else {
|
| @@ -178,6 +195,17 @@ class TemplateCollectingVisitor {
|
| }
|
| }
|
|
|
| + String findAssetFileLocation(String uri, Source srcPath) {
|
| + if (uri.startsWith('/')) {
|
| + // Absolute Path from working directory.
|
| + return '.${uri}';
|
| + }
|
| + // Otherwise let the sourceFactory resolve for packages, and relative paths.
|
| + Source source = sourceCrawler.context.sourceFactory
|
| + .resolveUri(srcPath, uri);
|
| + return (source != null) ? source.fullName : null;
|
| + }
|
| +
|
| BooleanLiteral assertBoolean(Expression key) {
|
| if (key is! BooleanLiteral) {
|
| throw 'must be a boolean literal: ${key.runtimeType}';
|
|
|