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

Commit

Permalink
feat(template_cache_generator): Support custom template path resolution
Browse files Browse the repository at this point in the history
Closes #923
  • Loading branch information
cbracken authored and mhevery committed Apr 17, 2014
1 parent d040b60 commit f5bf7ef
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/tools/template_cache_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ main(List arguments) {
print('output: ${options.output}');
print('sdk-path: ${options.sdkPath}');
print('package-root: ${options.packageRoots.join(",")}');
print('template-root: ${options.templateRoots.join(",")}');
var rewrites = options.urlRewrites.keys
.map((k) => '${k.pattern},${options.urlRewrites[k]}')
.join(';');
Expand All @@ -41,8 +42,8 @@ main(List arguments) {
Map<String, String> templates = {};

var c = new SourceCrawler(options.sdkPath, options.packageRoots);
var visitor =
new TemplateCollectingVisitor(templates, options.skippedClasses, c);
var visitor = new TemplateCollectingVisitor(templates, options.skippedClasses,
c, options.templateRoots);
c.crawl(options.entryPoint,
(CompilationUnitElement compilationUnit, SourceFile source) =>
visitor(compilationUnit, source.canonicalPath));
Expand All @@ -64,6 +65,7 @@ class Options {
String outputLibrary;
String sdkPath;
List<String> packageRoots;
List<String> templateRoots;
String output;
Map<RegExp, String> urlRewrites;
Set<String> skippedClasses;
Expand All @@ -77,6 +79,9 @@ Options parseArgs(List arguments) {
help: 'Dart SDK Path')
..addOption('package-root', abbr: 'p', defaultsTo: Platform.packageRoot,
help: 'comma-separated list of package roots')
..addOption('template-root', abbr: 't', defaultsTo: '.',
help: 'comma-separated list of paths from which templates with'
'absolute paths can be fetched')
..addOption('out', abbr: 'o', defaultsTo: '-',
help: 'output file or "-" for stdout')
..addOption('url-rewrites', abbr: 'u',
Expand Down Expand Up @@ -118,6 +123,7 @@ Options parseArgs(List arguments) {
var options = new Options();
options.sdkPath = args['sdk-path'];
options.packageRoots = args['package-root'].split(',');
options.templateRoots = args['template-root'].split(',');
options.output = args['out'];
if (args['url-rewrites'] != null) {
options.urlRewrites = new LinkedHashMap.fromIterable(
Expand Down Expand Up @@ -174,9 +180,10 @@ class TemplateCollectingVisitor {
Map<String, String> templates;
Set<String> skippedClasses;
SourceCrawler sourceCrawler;
List<String> templateRoots;

TemplateCollectingVisitor(this.templates, this.skippedClasses,
this.sourceCrawler);
this.sourceCrawler, this.templateRoots);

void call(CompilationUnitElement cue, String srcPath) {
processDeclarations(cue, srcPath);
Expand Down Expand Up @@ -209,7 +216,8 @@ class TemplateCollectingVisitor {
if (cache && cacheUris.isNotEmpty) {
Source currentSrcDir = sourceCrawler.context.sourceFactory
.resolveUri(null, 'file://$srcPath');
cacheUris..sort()..forEach((uri) => storeUriAsset(uri, currentSrcDir));
cacheUris..sort()..forEach(
(uri) => storeUriAsset(uri, currentSrcDir, templateRoots));
}
});
}
Expand Down Expand Up @@ -252,19 +260,21 @@ class TemplateCollectingVisitor {
return cache;
}

void storeUriAsset(String uri, Source srcPath) {
String assetFileLocation = findAssetFileLocation(uri, srcPath);
void storeUriAsset(String uri, Source srcPath, templateRoots) {
String assetFileLocation = findAssetLocation(uri, srcPath, templateRoots);
if (assetFileLocation == null) {
print("Could not find asset for uri: $uri");
} else {
templates[uri] = assetFileLocation;
}
}

String findAssetFileLocation(String uri, Source srcPath) {
String findAssetLocation(String uri, Source srcPath, List<String>
templateRoots) {
if (uri.startsWith('/')) {
// Absolute Path from working directory.
return '.${uri}';
var paths = templateRoots.map((r) => '$r/$uri');
return paths.firstWhere((p) => new File(p).existsSync(),
orElse: () => paths.first);
}
// Otherwise let the sourceFactory resolve for packages, and relative paths.
Source source = sourceCrawler.context.sourceFactory
Expand Down

0 comments on commit f5bf7ef

Please sign in to comment.