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

Commit 25d85fb

Browse files
rkjchirayuk
authored andcommitted
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. TESTING: The test uses a simple sed script to exercise the integration. The test runs only if sed is present on the running system. The test uses CSS with simple rules that are being translated for RTL locales. Closes #1052
1 parent ed13697 commit 25d85fb

File tree

4 files changed

+84
-12
lines changed

4 files changed

+84
-12
lines changed

lib/tools/template_cache_generator.dart

+35-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:analyzer/src/generated/source.dart';
99
import 'package:analyzer/src/generated/element.dart';
1010
import 'package:args/args.dart';
1111
import 'package:di/generator.dart';
12+
import 'dart:convert';
1213

1314
const String PACKAGE_PREFIX = 'package:';
1415
const String DART_PACKAGE_PREFIX = 'dart:';
@@ -56,7 +57,8 @@ main(List arguments) {
5657
sink = f.openWrite();
5758
}
5859
return printTemplateCache(
59-
templates, options.urlRewrites, options.outputLibrary, sink)
60+
templates, options.urlRewrites, options.outputLibrary, sink,
61+
options.cssRewriter)
6062
.then((_) => sink.flush());
6163
}
6264

@@ -70,6 +72,7 @@ class Options {
7072
Map<RegExp, String> urlRewrites;
7173
Set<String> skippedClasses;
7274
bool verbose;
75+
String cssRewriter;
7376
}
7477

7578
Options parseArgs(List arguments) {
@@ -89,6 +92,9 @@ Options parseArgs(List arguments) {
8992
'patternUrl,rewriteTo')
9093
..addOption('skip-classes', abbr: 'b',
9194
help: 'comma-separated list of classes to skip templating')
95+
..addOption('css-rewriter', defaultsTo: null,
96+
help: 'application used to rewrite css. Each css file will be passed '
97+
'to stdin and rewriten one is expected on stdout.')
9298
..addFlag('verbose', abbr: 'v', help: 'verbose output')
9399
..addFlag('help', abbr: 'h', negatable: false, help: 'show this help');
94100

@@ -142,31 +148,48 @@ Options parseArgs(List arguments) {
142148
if (args.rest.length != 2) {
143149
fail('unexpected arguments: ${args.rest.join(' ')}');
144150
}
151+
options.cssRewriter = args['css-rewriter'];
145152
options.entryPoint = args.rest[0];
146153
options.outputLibrary = args.rest[1];
147154
return options;
148155
}
149156

150157
printTemplateCache(Map<String, String> templateKeyMap,
151-
Map<RegExp, String> urlRewriters,
152-
String outputLibrary,
153-
IOSink outSink) {
158+
Map<RegExp, String> urlRewriters,
159+
String outputLibrary,
160+
IOSink outSink,
161+
String cssRewriter) {
154162

155163
outSink.write(fileHeader(outputLibrary));
156164

157165
Future future = new Future.value(0);
158166
List uris = templateKeyMap.keys.toList()..sort()..forEach((uri) {
159167
var templateFile = templateKeyMap[uri];
168+
String resultUri = uri;
169+
urlRewriters.forEach((regexp, replacement) {
170+
resultUri = resultUri.replaceFirst(regexp, replacement);
171+
});
172+
var putToCache = (String content) {
173+
var out = content.replaceAll('"""', r'\"\"\"');
174+
outSink.write(
175+
'tc.put("$resultUri", new HttpResponse(200, r"""$out"""));\n');
176+
};
160177
future = future.then((_) {
161-
return new File(templateFile).readAsString().then((fileStr) {
162-
fileStr = fileStr.replaceAll('"""', r'\"\"\"');
163-
String resultUri = uri;
164-
urlRewriters.forEach((regexp, replacement) {
165-
resultUri = resultUri.replaceFirst(regexp, replacement);
178+
var fileContentFuture = new File(templateFile).readAsString();
179+
if (templateFile.endsWith(".css") && cssRewriter != null) {
180+
return fileContentFuture.then((fileStr) {
181+
return Process.start(cssRewriter, []).then((process) {
182+
process.stdin.write(fileStr);
183+
process.stdin.close();
184+
return process.stdout
185+
.transform(UTF8.decoder)
186+
.join("")
187+
.then(putToCache);
188+
});
166189
});
167-
outSink.write(
168-
'tc.put("$resultUri", new HttpResponse(200, r"""$fileStr"""));\n');
169-
});
190+
} else {
191+
return fileContentFuture.then(putToCache);
192+
}
170193
});
171194
});
172195

test/io/template_cache_generator_spec.dart

+39
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,45 @@ void main() {
7070
});
7171
});
7272

73+
if (new File("/bin/sed").existsSync()) {
74+
it('should correctly use simple sed rewritter to fake css mirroring', () {
75+
var tmpDir = Directory.systemTemp.createTempSync();
76+
Future flush;
77+
try {
78+
flush = generator.main([
79+
'--out=${tmpDir.path}/generated.dart',
80+
'--url-rewrites=/test/io/test_files,rewritten',
81+
'--css-rewriter=test/io/test_files/rewritter.sh',
82+
'test/io/test_files/cssUrls/main.dart',
83+
'generated']);
84+
} catch(_) {
85+
tmpDir.deleteSync(recursive: true);
86+
rethrow;
87+
}
88+
return flush.then((_) {
89+
expect(new File('${tmpDir.path}/generated.dart').readAsStringSync(),
90+
'''// GENERATED, DO NOT EDIT!
91+
library generated;
7392
93+
import 'package:angular/angular.dart';
94+
95+
primeTemplateCache(TemplateCache tc) {
96+
tc.put("rewritten/cssUrls/four.css", new HttpResponse(200, r""".float-right {
97+
float: right;
98+
}
99+
.right-margin {
100+
margin-right: 20px;
101+
}
102+
103+
"""));
104+
tc.put("rewritten/cssUrls/one.css", new HttpResponse(200, r"""body {}"""));
105+
tc.put("rewritten/cssUrls/three.css", new HttpResponse(200, r"""body {}"""));
106+
tc.put("rewritten/cssUrls/two.css", new HttpResponse(200, r"""body {}"""));
107+
}''');
108+
}).whenComplete(() {
109+
tmpDir.deleteSync(recursive: true);
110+
});
111+
});
112+
}
74113
});
75114
}

test/io/test_files/cssUrls/four.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.float-left {
2+
float: left;
3+
}
4+
.left-margin {
5+
margin-left: 20px;
6+
}
7+

test/io/test_files/rewritter.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
/bin/sed 's/left/right/g'

0 commit comments

Comments
 (0)