This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(template_cache_transformer): Implement template cache as a trans…
…former Implement the template cache generation as a transformer. This is implemented as an optional transformer option generate_template_cache. Closes #1646
- Loading branch information
Showing
6 changed files
with
426 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
library angular_transformers.template_cache_generator; | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:angular/tools/transformer/options.dart'; | ||
import 'package:angular/tools/transformer/referenced_uris.dart'; | ||
import 'package:barback/barback.dart'; | ||
import 'package:code_transformers/resolver.dart'; | ||
import 'package:path/path.dart' as path; | ||
|
||
/// Transformer which gathers all templates from the Angular application and | ||
/// adds to the Template Cache. | ||
class TemplateCacheGenerator extends Transformer with ResolverTransformer { | ||
static const String generatedFilename = '_generated_template_cache.dart'; | ||
|
||
final TransformOptions options; | ||
|
||
TemplateCacheGenerator(this.options, Resolvers resolvers) { | ||
this.resolvers = resolvers; | ||
} | ||
|
||
Future applyResolver(Transform transform, Resolver resolver) { | ||
if (!options.generateTemplateCache) return new Future.value(); | ||
|
||
var asset = transform.primaryInput; | ||
var id = asset.id; | ||
var outputFilename = '${path.url.basenameWithoutExtension(id.path)}' | ||
'$generatedFilename'; | ||
var outputPath = path.url.join(path.url.dirname(id.path), outputFilename); | ||
var outputId = new AssetId(id.package, outputPath); | ||
var outputBuffer = new StringBuffer(); | ||
|
||
return gatherReferencedUris(transform, resolver, options, | ||
templatesOnly: false).then((templates) { | ||
_writeTemplateCacheHeader(asset.id, outputBuffer); | ||
templates.forEach((uri, contents) { | ||
contents = contents.replaceAll("'''", r"\'\'\'"); | ||
outputBuffer.write(" r'$uri' : r'''$contents''',\n"); | ||
}); | ||
_writeTemplateCacheFooter(outputBuffer); | ||
transform.addOutput( | ||
new Asset.fromString(outputId, outputBuffer.toString())); | ||
}); | ||
} | ||
|
||
void _writeTemplateCacheHeader(AssetId id, StringSink sink) { | ||
var libPath = path.url.withoutExtension(id.path).replaceAll('/', '.'); | ||
sink.write(''' | ||
library ${id.package}.$libPath.generated_template_cache; | ||
import 'package:angular/angular.dart'; | ||
import 'package:di/di.dart' show Module; | ||
Module get templateCacheModule => | ||
new Module()..bind(TemplateCache, toFactory: () { | ||
var templateCache = new TemplateCache(); | ||
_cache.forEach((key, value) { | ||
templateCache.put(key, new HttpResponse(200, value)); | ||
}); | ||
return templateCache; | ||
}); | ||
const Map<String, String> _cache = const <String, String> { | ||
'''); | ||
} | ||
|
||
void _writeTemplateCacheFooter(StringSink sink) { | ||
sink.write('''}; | ||
'''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.