From 854a8068b97b53736ff79079374c9d4eabd843b8 Mon Sep 17 00:00:00 2001 From: Roman Kamyk Date: Fri, 16 May 2014 14:57:50 -0700 Subject: [PATCH 1/2] feat: add option to use external css rewriter in template_cache_generator. The provided rewriter will be used to transform every css read by the TemplateCache, the original css is provided on stdin, and result is read from the stdout of provided binary. --- lib/tools/template_cache_generator.dart | 47 ++++++++++++++++++------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/tools/template_cache_generator.dart b/lib/tools/template_cache_generator.dart index 28167465f..8276468be 100644 --- a/lib/tools/template_cache_generator.dart +++ b/lib/tools/template_cache_generator.dart @@ -9,6 +9,7 @@ import 'package:analyzer/src/generated/source.dart'; import 'package:analyzer/src/generated/element.dart'; import 'package:args/args.dart'; import 'package:di/generator.dart'; +import 'dart:convert'; const String PACKAGE_PREFIX = 'package:'; const String DART_PACKAGE_PREFIX = 'dart:'; @@ -56,7 +57,8 @@ main(List arguments) { sink = f.openWrite(); } return printTemplateCache( - templates, options.urlRewrites, options.outputLibrary, sink) + templates, options.urlRewrites, options.outputLibrary, sink, + options.cssRewriter) .then((_) => sink.flush()); } @@ -70,6 +72,7 @@ class Options { Map urlRewrites; Set skippedClasses; bool verbose; + String cssRewriter; } Options parseArgs(List arguments) { @@ -89,6 +92,9 @@ Options parseArgs(List arguments) { 'patternUrl,rewriteTo') ..addOption('skip-classes', abbr: 'b', help: 'comma-separated list of classes to skip templating') + ..addOption('css-rewriter', defaultsTo: null, + help: 'application used to rewrite css. Each css file will be passed ' + 'to stdin and rewriten one is expected on stdout.') ..addFlag('verbose', abbr: 'v', help: 'verbose output') ..addFlag('help', abbr: 'h', negatable: false, help: 'show this help'); @@ -142,31 +148,48 @@ Options parseArgs(List arguments) { if (args.rest.length != 2) { fail('unexpected arguments: ${args.rest.join(' ')}'); } + options.cssRewriter = args['css-rewriter']; options.entryPoint = args.rest[0]; options.outputLibrary = args.rest[1]; return options; } printTemplateCache(Map templateKeyMap, - Map urlRewriters, - String outputLibrary, - IOSink outSink) { + Map urlRewriters, + String outputLibrary, + IOSink outSink, + String cssRewriter) { outSink.write(fileHeader(outputLibrary)); Future future = new Future.value(0); List uris = templateKeyMap.keys.toList()..sort()..forEach((uri) { var templateFile = templateKeyMap[uri]; + String resultUri = uri; + urlRewriters.forEach((regexp, replacement) { + resultUri = resultUri.replaceFirst(regexp, replacement); + }); + var putToCache = (String content) { + var out = content.replaceAll('"""', r'\"\"\"'); + outSink.write( + 'tc.put("$resultUri", new HttpResponse(200, r"""$out"""));\n'); + }; 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); + var fileContentFuture = new File(templateFile).readAsString(); + if (templateFile.endsWith(".css") && cssRewriter != null) { + return fileContentFuture.then((fileStr) { + return Process.start(cssRewriter, []).then((process) { + process.stdin.write(fileStr); + process.stdin.close(); + return process.stdout + .transform(UTF8.decoder) + .join("") + .then(putToCache); + }); }); - outSink.write( - 'tc.put("$resultUri", new HttpResponse(200, r"""$fileStr"""));\n'); - }); + } else { + return fileContentFuture.then(putToCache); + } }); }); From 247a30ecf41daa85048e42a1b3274a655c3de60a Mon Sep 17 00:00:00 2001 From: Roman Kamyk Date: Fri, 16 May 2014 15:11:37 -0700 Subject: [PATCH 2/2] test: add test for template_cache_generator css rewriting It use simple sed script to showcase integration is working. Test runs only if sed is present on the running system. The test uses css with simple rules that are getting traslated for RTL locales. --- test/io/template_cache_generator_spec.dart | 39 ++++++++++++++++++++++ test/io/test_files/cssUrls/four.css | 7 ++++ test/io/test_files/rewritter.sh | 3 ++ 3 files changed, 49 insertions(+) create mode 100644 test/io/test_files/cssUrls/four.css create mode 100755 test/io/test_files/rewritter.sh diff --git a/test/io/template_cache_generator_spec.dart b/test/io/template_cache_generator_spec.dart index 91c4748c2..93e2173b4 100644 --- a/test/io/template_cache_generator_spec.dart +++ b/test/io/template_cache_generator_spec.dart @@ -70,6 +70,45 @@ void main() { }); }); + if (new File("/bin/sed").existsSync()) { + it('should correctly use simple sed rewritter to fake css mirroring', () { + var tmpDir = Directory.systemTemp.createTempSync(); + Future flush; + try { + flush = generator.main([ + '--out=${tmpDir.path}/generated.dart', + '--url-rewrites=/test/io/test_files,rewritten', + '--css-rewriter=test/io/test_files/rewritter.sh', + 'test/io/test_files/cssUrls/main.dart', + 'generated']); + } catch(_) { + tmpDir.deleteSync(recursive: true); + rethrow; + } + return flush.then((_) { + expect(new File('${tmpDir.path}/generated.dart').readAsStringSync(), +'''// GENERATED, DO NOT EDIT! +library generated; +import 'package:angular/angular.dart'; + +primeTemplateCache(TemplateCache tc) { +tc.put("rewritten/cssUrls/four.css", new HttpResponse(200, r""".float-right { + float: right; +} +.right-margin { + margin-right: 20px; +} + +""")); +tc.put("rewritten/cssUrls/one.css", new HttpResponse(200, r"""body {}""")); +tc.put("rewritten/cssUrls/three.css", new HttpResponse(200, r"""body {}""")); +tc.put("rewritten/cssUrls/two.css", new HttpResponse(200, r"""body {}""")); +}'''); + }).whenComplete(() { + tmpDir.deleteSync(recursive: true); + }); + }); + } }); } diff --git a/test/io/test_files/cssUrls/four.css b/test/io/test_files/cssUrls/four.css new file mode 100644 index 000000000..2077038e2 --- /dev/null +++ b/test/io/test_files/cssUrls/four.css @@ -0,0 +1,7 @@ +.float-left { + float: left; +} +.left-margin { + margin-left: 20px; +} + diff --git a/test/io/test_files/rewritter.sh b/test/io/test_files/rewritter.sh new file mode 100755 index 000000000..44574dbd6 --- /dev/null +++ b/test/io/test_files/rewritter.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +/bin/sed 's/left/right/g'