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

Commit f5bf7ef

Browse files
cbrackenmhevery
authored andcommitted
feat(template_cache_generator): Support custom template path resolution
Closes #923
1 parent d040b60 commit f5bf7ef

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

lib/tools/template_cache_generator.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ main(List arguments) {
3131
print('output: ${options.output}');
3232
print('sdk-path: ${options.sdkPath}');
3333
print('package-root: ${options.packageRoots.join(",")}');
34+
print('template-root: ${options.templateRoots.join(",")}');
3435
var rewrites = options.urlRewrites.keys
3536
.map((k) => '${k.pattern},${options.urlRewrites[k]}')
3637
.join(';');
@@ -41,8 +42,8 @@ main(List arguments) {
4142
Map<String, String> templates = {};
4243

4344
var c = new SourceCrawler(options.sdkPath, options.packageRoots);
44-
var visitor =
45-
new TemplateCollectingVisitor(templates, options.skippedClasses, c);
45+
var visitor = new TemplateCollectingVisitor(templates, options.skippedClasses,
46+
c, options.templateRoots);
4647
c.crawl(options.entryPoint,
4748
(CompilationUnitElement compilationUnit, SourceFile source) =>
4849
visitor(compilationUnit, source.canonicalPath));
@@ -64,6 +65,7 @@ class Options {
6465
String outputLibrary;
6566
String sdkPath;
6667
List<String> packageRoots;
68+
List<String> templateRoots;
6769
String output;
6870
Map<RegExp, String> urlRewrites;
6971
Set<String> skippedClasses;
@@ -77,6 +79,9 @@ Options parseArgs(List arguments) {
7779
help: 'Dart SDK Path')
7880
..addOption('package-root', abbr: 'p', defaultsTo: Platform.packageRoot,
7981
help: 'comma-separated list of package roots')
82+
..addOption('template-root', abbr: 't', defaultsTo: '.',
83+
help: 'comma-separated list of paths from which templates with'
84+
'absolute paths can be fetched')
8085
..addOption('out', abbr: 'o', defaultsTo: '-',
8186
help: 'output file or "-" for stdout')
8287
..addOption('url-rewrites', abbr: 'u',
@@ -118,6 +123,7 @@ Options parseArgs(List arguments) {
118123
var options = new Options();
119124
options.sdkPath = args['sdk-path'];
120125
options.packageRoots = args['package-root'].split(',');
126+
options.templateRoots = args['template-root'].split(',');
121127
options.output = args['out'];
122128
if (args['url-rewrites'] != null) {
123129
options.urlRewrites = new LinkedHashMap.fromIterable(
@@ -174,9 +180,10 @@ class TemplateCollectingVisitor {
174180
Map<String, String> templates;
175181
Set<String> skippedClasses;
176182
SourceCrawler sourceCrawler;
183+
List<String> templateRoots;
177184

178185
TemplateCollectingVisitor(this.templates, this.skippedClasses,
179-
this.sourceCrawler);
186+
this.sourceCrawler, this.templateRoots);
180187

181188
void call(CompilationUnitElement cue, String srcPath) {
182189
processDeclarations(cue, srcPath);
@@ -209,7 +216,8 @@ class TemplateCollectingVisitor {
209216
if (cache && cacheUris.isNotEmpty) {
210217
Source currentSrcDir = sourceCrawler.context.sourceFactory
211218
.resolveUri(null, 'file://$srcPath');
212-
cacheUris..sort()..forEach((uri) => storeUriAsset(uri, currentSrcDir));
219+
cacheUris..sort()..forEach(
220+
(uri) => storeUriAsset(uri, currentSrcDir, templateRoots));
213221
}
214222
});
215223
}
@@ -252,19 +260,21 @@ class TemplateCollectingVisitor {
252260
return cache;
253261
}
254262

255-
void storeUriAsset(String uri, Source srcPath) {
256-
String assetFileLocation = findAssetFileLocation(uri, srcPath);
263+
void storeUriAsset(String uri, Source srcPath, templateRoots) {
264+
String assetFileLocation = findAssetLocation(uri, srcPath, templateRoots);
257265
if (assetFileLocation == null) {
258266
print("Could not find asset for uri: $uri");
259267
} else {
260268
templates[uri] = assetFileLocation;
261269
}
262270
}
263271

264-
String findAssetFileLocation(String uri, Source srcPath) {
272+
String findAssetLocation(String uri, Source srcPath, List<String>
273+
templateRoots) {
265274
if (uri.startsWith('/')) {
266-
// Absolute Path from working directory.
267-
return '.${uri}';
275+
var paths = templateRoots.map((r) => '$r/$uri');
276+
return paths.firstWhere((p) => new File(p).existsSync(),
277+
orElse: () => paths.first);
268278
}
269279
// Otherwise let the sourceFactory resolve for packages, and relative paths.
270280
Source source = sourceCrawler.context.sourceFactory

0 commit comments

Comments
 (0)