| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Command line tool to run the checker on a Dart program. | 5 /// Command line tool to run the checker on a Dart program. |
| 6 library dev_compiler.devc; | 6 library dev_compiler.devc; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:convert'; | 9 import 'dart:convert'; |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 ? path.join(_options.outputDir, 'messages.json') | 234 ? path.join(_options.outputDir, 'messages.json') |
| 235 : _options.dumpInfoFile; | 235 : _options.dumpInfoFile; |
| 236 if (filepath == null) return; | 236 if (filepath == null) return; |
| 237 new File(filepath).writeAsStringSync(JSON.encode(result.toJsonMap())); | 237 new File(filepath).writeAsStringSync(JSON.encode(result.toJsonMap())); |
| 238 } | 238 } |
| 239 } | 239 } |
| 240 | 240 |
| 241 class CompilerServer { | 241 class CompilerServer { |
| 242 final Compiler compiler; | 242 final Compiler compiler; |
| 243 final String outDir; | 243 final String outDir; |
| 244 final String host; |
| 244 final int port; | 245 final int port; |
| 245 final String _entryPath; | 246 final String _entryPath; |
| 246 | 247 |
| 247 factory CompilerServer(CompilerOptions options) { | 248 factory CompilerServer(CompilerOptions options) { |
| 248 var entryPath = path.basename(options.entryPointFile); | 249 var entryPath = path.basename(options.entryPointFile); |
| 249 if (path.extension(entryPath) != '.html') { | 250 if (path.extension(entryPath) != '.html') { |
| 250 print('error: devc in server mode requires an HTML entry point.'); | 251 print('error: devc in server mode requires an HTML entry point.'); |
| 251 exit(1); | 252 exit(1); |
| 252 } | 253 } |
| 253 | 254 |
| 254 // TODO(sigmund): allow running without a dir, but keep output in memory? | 255 // TODO(sigmund): allow running without a dir, but keep output in memory? |
| 255 var outDir = options.outputDir; | 256 var outDir = options.outputDir; |
| 256 if (outDir == null) { | 257 if (outDir == null) { |
| 257 print('error: devc in server mode also requires specifying and ' | 258 print('error: devc in server mode also requires specifying and ' |
| 258 'output location for generated code.'); | 259 'output location for generated code.'); |
| 259 exit(1); | 260 exit(1); |
| 260 } | 261 } |
| 261 var port = options.port; | 262 var port = options.port; |
| 262 _log.fine('Serving $entryPath at http://0.0.0.0:$port/'); | 263 var host = options.host; |
| 264 _log.fine('Serving $entryPath at http://$host:$port/'); |
| 263 var compiler = new Compiler(options); | 265 var compiler = new Compiler(options); |
| 264 return new CompilerServer._(compiler, outDir, port, entryPath); | 266 return new CompilerServer._(compiler, outDir, host, port, entryPath); |
| 265 } | 267 } |
| 266 | 268 |
| 267 CompilerServer._(this.compiler, this.outDir, this.port, this._entryPath); | 269 CompilerServer._( |
| 270 this.compiler, this.outDir, this.host, this.port, this._entryPath); |
| 268 | 271 |
| 269 Future start() async { | 272 Future start() async { |
| 270 // Create output directory if needed. shelf_static will fail otherwise. | 273 // Create output directory if needed. shelf_static will fail otherwise. |
| 271 var out = new Directory(outDir); | 274 var out = new Directory(outDir); |
| 272 if (!await out.exists()) await out.create(recursive: true); | 275 if (!await out.exists()) await out.create(recursive: true); |
| 273 | 276 |
| 274 var handler = const shelf.Pipeline() | 277 var handler = const shelf.Pipeline() |
| 275 .addMiddleware(rebuildAndCache) | 278 .addMiddleware(rebuildAndCache) |
| 276 .addHandler(shelf_static.createStaticHandler(outDir, | 279 .addHandler(shelf_static.createStaticHandler(outDir, |
| 277 defaultDocument: _entryPath)); | 280 defaultDocument: _entryPath)); |
| 278 await shelf.serve(handler, '0.0.0.0', port); | 281 await shelf.serve(handler, host, port); |
| 279 compiler.run(); | 282 compiler.run(); |
| 280 } | 283 } |
| 281 | 284 |
| 282 shelf.Handler rebuildAndCache(shelf.Handler handler) => (request) { | 285 shelf.Handler rebuildAndCache(shelf.Handler handler) => (request) { |
| 283 _log.fine('requested $GREEN_COLOR${request.url}$NO_COLOR'); | 286 _log.fine('requested $GREEN_COLOR${request.url}$NO_COLOR'); |
| 284 // Trigger recompile only when requesting the HTML page. | 287 // Trigger recompile only when requesting the HTML page. |
| 285 var segments = request.url.pathSegments; | 288 var segments = request.url.pathSegments; |
| 286 bool isEntryPage = segments.length == 0 || segments[0] == _entryPath; | 289 bool isEntryPage = segments.length == 0 || segments[0] == _entryPath; |
| 287 if (isEntryPage) compiler._runAgain(); | 290 if (isEntryPage) compiler._runAgain(); |
| 288 | 291 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 303 // Note: the cache-control header should be enough, but this doesn't hurt | 306 // Note: the cache-control header should be enough, but this doesn't hurt |
| 304 // and can help renew the policy after it expires. | 307 // and can help renew the policy after it expires. |
| 305 headers['ETag'] = segments[1]; | 308 headers['ETag'] = segments[1]; |
| 306 } | 309 } |
| 307 return response.change(headers: headers); | 310 return response.change(headers: headers); |
| 308 }; | 311 }; |
| 309 } | 312 } |
| 310 | 313 |
| 311 final _log = new Logger('dev_compiler'); | 314 final _log = new Logger('dev_compiler'); |
| 312 final _earlyErrorResult = new CheckerResults(const [], null, true); | 315 final _earlyErrorResult = new CheckerResults(const [], null, true); |
| OLD | NEW |