Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(expression_extractor): Add source path to source crawler
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Sander authored and pavelgj committed Jan 27, 2014
1 parent 11b38ba commit 6597f73
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
63 changes: 41 additions & 22 deletions lib/tools/template_cache_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ 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:analyzer/src/generated/engine.dart';
import 'package:di/generator.dart';

const String PACKAGE_PREFIX = 'package:';
const String DART_PACKAGE_PREFIX = 'dart:';
Expand All @@ -23,23 +26,25 @@ 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');
Expand All @@ -50,11 +55,12 @@ main(args) {

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(
Expand Down Expand Up @@ -105,12 +111,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;
Expand All @@ -119,7 +127,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) {
Expand All @@ -130,7 +137,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.forEach((uri) => storeUriAsset(uri, currentSrcDir));
}
});
}
Expand Down Expand Up @@ -167,17 +177,26 @@ 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 {
templates[uri] = assetFileLocation;
}
}

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}';
Expand Down
6 changes: 4 additions & 2 deletions test/io/template_cache_generator_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ main() => describe('template_cache_generator', () {
Future flush;
try {
flush = generator.main(['test/io/test_files/templates/main.dart',
'${tmpDir.path}/generated.dart', 'generated', '%SYSTEM_PACKAGE_ROOT%',
'test/io/test_files,rewritten', 'MyComponent3']);
Platform.environment['DART_SDK'],
'${tmpDir.path}/generated.dart', 'generated',
'%SYSTEM_PACKAGE_ROOT%',
'/test/io/test_files,rewritten', 'MyComponent3']);
} catch(_) {
tmpDir.deleteSync(recursive: true);
rethrow;
Expand Down
6 changes: 3 additions & 3 deletions test/io/test_files/templates/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import 'package:angular/tools/template_cache_annotation.dart';

@NgComponent(
selector: 'my-component',
templateUrl: 'test/io/test_files/templates/main.html'
templateUrl: '/test/io/test_files/templates/main.html'
)
@NgTemplateCache()
class MyComponent {
}

@NgComponent(
selector: 'my-component2',
templateUrl: 'test/io/test_files/templates/dont.html'
templateUrl: '/test/io/test_files/templates/dont.html'
)
@NgTemplateCache(cache: false)
class MyComponent2 {
Expand All @@ -22,7 +22,7 @@ class MyComponent2 {

@NgComponent(
selector: 'my-component3',
templateUrl: 'test/io/test_files/templates/dont.html'
templateUrl: '/test/io/test_files/templates/dont.html'
)
@NgTemplateCache(cache: true)
class MyComponent3 {
Expand Down

0 comments on commit 6597f73

Please sign in to comment.