@@ -9,6 +9,7 @@ import 'package:analyzer/src/generated/source.dart';
9
9
import 'package:analyzer/src/generated/element.dart' ;
10
10
import 'package:args/args.dart' ;
11
11
import 'package:di/generator.dart' ;
12
+ import 'dart:convert' ;
12
13
13
14
const String PACKAGE_PREFIX = 'package:' ;
14
15
const String DART_PACKAGE_PREFIX = 'dart:' ;
@@ -56,7 +57,8 @@ main(List arguments) {
56
57
sink = f.openWrite ();
57
58
}
58
59
return printTemplateCache (
59
- templates, options.urlRewrites, options.outputLibrary, sink)
60
+ templates, options.urlRewrites, options.outputLibrary, sink,
61
+ options.cssRewriter)
60
62
.then ((_) => sink.flush ());
61
63
}
62
64
@@ -70,6 +72,7 @@ class Options {
70
72
Map <RegExp , String > urlRewrites;
71
73
Set <String > skippedClasses;
72
74
bool verbose;
75
+ String cssRewriter;
73
76
}
74
77
75
78
Options parseArgs (List arguments) {
@@ -89,6 +92,9 @@ Options parseArgs(List arguments) {
89
92
'patternUrl,rewriteTo' )
90
93
..addOption ('skip-classes' , abbr: 'b' ,
91
94
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.' )
92
98
..addFlag ('verbose' , abbr: 'v' , help: 'verbose output' )
93
99
..addFlag ('help' , abbr: 'h' , negatable: false , help: 'show this help' );
94
100
@@ -142,31 +148,48 @@ Options parseArgs(List arguments) {
142
148
if (args.rest.length != 2 ) {
143
149
fail ('unexpected arguments: ${args .rest .join (' ' )}' );
144
150
}
151
+ options.cssRewriter = args['css-rewriter' ];
145
152
options.entryPoint = args.rest[0 ];
146
153
options.outputLibrary = args.rest[1 ];
147
154
return options;
148
155
}
149
156
150
157
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) {
154
162
155
163
outSink.write (fileHeader (outputLibrary));
156
164
157
165
Future future = new Future .value (0 );
158
166
List uris = templateKeyMap.keys.toList ()..sort ()..forEach ((uri) {
159
167
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
+ };
160
177
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
+ });
166
189
});
167
- outSink. write (
168
- 'tc.put("$ resultUri ", new HttpResponse(200, r"""$ fileStr """)); \n ' );
169
- });
190
+ } else {
191
+ return fileContentFuture. then (putToCache );
192
+ }
170
193
});
171
194
});
172
195
0 commit comments