| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 final TypeRules _rules; | 48 final TypeRules _rules; |
| 49 final CodeChecker _checker; | 49 final CodeChecker _checker; |
| 50 final SourceGraph _graph; | 50 final SourceGraph _graph; |
| 51 final SourceNode _entryNode; | 51 final SourceNode _entryNode; |
| 52 List<LibraryInfo> _libraries = <LibraryInfo>[]; | 52 List<LibraryInfo> _libraries = <LibraryInfo>[]; |
| 53 final List<CodeGenerator> _generators; | 53 final List<CodeGenerator> _generators; |
| 54 final bool _hashing; | 54 final bool _hashing; |
| 55 bool _failure = false; | 55 bool _failure = false; |
| 56 | 56 |
| 57 factory Compiler(CompilerOptions options, | 57 factory Compiler(CompilerOptions options, |
| 58 [TypeResolver resolver, CheckerReporter reporter]) { | 58 {TypeResolver resolver, CheckerReporter reporter}) { |
| 59 if (resolver == null) { | 59 if (resolver == null) { |
| 60 resolver = options.useMockSdk | 60 resolver = options.useMockSdk |
| 61 ? new TypeResolver.fromMock(mockSdkSources, options) | 61 ? new TypeResolver.fromMock(mockSdkSources, options) |
| 62 : new TypeResolver.fromDir(options.dartSdkPath, options); | 62 : new TypeResolver.fromDir(options.dartSdkPath, options); |
| 63 } | 63 } |
| 64 | 64 |
| 65 if (reporter == null) { | 65 if (reporter == null) { |
| 66 reporter = options.dumpInfo | 66 reporter = options.dumpInfo |
| 67 ? new SummaryReporter() | 67 ? new SummaryReporter() |
| 68 : new LogReporter(options.useColors); | 68 : new LogReporter(options.useColors); |
| 69 } | 69 } |
| 70 var graph = new SourceGraph(resolver.context, reporter, options); | 70 var graph = new SourceGraph(resolver.context, reporter, options); |
| 71 var rules = | 71 var rules = |
| 72 new RestrictedRules(resolver.context.typeProvider, options: options); | 72 new RestrictedRules(resolver.context.typeProvider, options: options); |
| 73 var checker = new CodeChecker(rules, reporter, options); | 73 var checker = new CodeChecker(rules, reporter, options); |
| 74 |
| 74 var inputFile = options.entryPointFile; | 75 var inputFile = options.entryPointFile; |
| 75 var uri = inputFile.startsWith('dart:') || inputFile.startsWith('package:') | 76 var inputUri = inputFile.startsWith('dart:') || |
| 77 inputFile.startsWith('package:') |
| 76 ? Uri.parse(inputFile) | 78 ? Uri.parse(inputFile) |
| 77 : new Uri.file(path.absolute(inputFile)); | 79 : new Uri.file(path.absolute(options.useImplicitHtml |
| 78 var entryNode = graph.nodeFromUri(uri); | 80 ? ResolverOptions.implicitHtmlFile |
| 79 | 81 : inputFile)); |
| 82 var entryNode = graph.nodeFromUri(inputUri); |
| 80 var outputDir = options.outputDir; | 83 var outputDir = options.outputDir; |
| 81 var generators = <CodeGenerator>[]; | 84 var generators = <CodeGenerator>[]; |
| 82 if (options.dumpSrcDir != null) { | 85 if (options.dumpSrcDir != null) { |
| 83 generators.add(new EmptyDartGenerator( | 86 generators.add(new EmptyDartGenerator( |
| 84 options.dumpSrcDir, entryNode.uri, rules, options)); | 87 options.dumpSrcDir, entryNode.uri, rules, options)); |
| 85 } | 88 } |
| 86 if (outputDir != null) { | 89 if (outputDir != null) { |
| 87 generators.add(options.outputDart | 90 generators.add(options.outputDart |
| 88 ? new DartGenerator(outputDir, entryNode.uri, rules, options) | 91 ? new DartGenerator(outputDir, entryNode.uri, rules, options) |
| 89 : new JSGenerator(outputDir, entryNode.uri, rules, options)); | 92 : new JSGenerator(outputDir, entryNode.uri, rules, options)); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 243 |
| 241 class CompilerServer { | 244 class CompilerServer { |
| 242 final Compiler compiler; | 245 final Compiler compiler; |
| 243 final String outDir; | 246 final String outDir; |
| 244 final String host; | 247 final String host; |
| 245 final int port; | 248 final int port; |
| 246 final String _entryPath; | 249 final String _entryPath; |
| 247 | 250 |
| 248 factory CompilerServer(CompilerOptions options) { | 251 factory CompilerServer(CompilerOptions options) { |
| 249 var entryPath = path.basename(options.entryPointFile); | 252 var entryPath = path.basename(options.entryPointFile); |
| 250 if (path.extension(entryPath) != '.html') { | 253 var extension = path.extension(entryPath); |
| 251 print('error: devc in server mode requires an HTML entry point.'); | 254 if (extension != '.html' && !options.useImplicitHtml) { |
| 255 print('error: devc in server mode requires an HTML or Dart entry point.'); |
| 252 exit(1); | 256 exit(1); |
| 253 } | 257 } |
| 254 | 258 |
| 255 // TODO(sigmund): allow running without a dir, but keep output in memory? | 259 // TODO(sigmund): allow running without a dir, but keep output in memory? |
| 256 var outDir = options.outputDir; | 260 var outDir = options.outputDir; |
| 257 if (outDir == null) { | 261 if (outDir == null) { |
| 258 print('error: devc in server mode also requires specifying and ' | 262 print('error: devc in server mode also requires specifying and ' |
| 259 'output location for generated code.'); | 263 'output location for generated code.'); |
| 260 exit(1); | 264 exit(1); |
| 261 } | 265 } |
| 262 var port = options.port; | 266 var port = options.port; |
| 263 var host = options.host; | 267 var host = options.host; |
| 264 var compiler = new Compiler(options); | 268 var compiler = new Compiler(options); |
| 265 return new CompilerServer._(compiler, outDir, host, port, entryPath); | 269 return new CompilerServer._(compiler, outDir, host, port, entryPath); |
| 266 } | 270 } |
| 267 | 271 |
| 268 CompilerServer._( | 272 CompilerServer._( |
| 269 this.compiler, this.outDir, this.host, this.port, this._entryPath); | 273 Compiler compiler, this.outDir, this.host, this.port, String entryPath) |
| 274 : this.compiler = compiler, |
| 275 this._entryPath = compiler._options.useImplicitHtml |
| 276 ? ResolverOptions.implicitHtmlFile |
| 277 : entryPath; |
| 270 | 278 |
| 271 Future start() async { | 279 Future start() async { |
| 272 // Create output directory if needed. shelf_static will fail otherwise. | 280 // Create output directory if needed. shelf_static will fail otherwise. |
| 273 var out = new Directory(outDir); | 281 var out = new Directory(outDir); |
| 274 if (!await out.exists()) await out.create(recursive: true); | 282 if (!await out.exists()) await out.create(recursive: true); |
| 275 | 283 |
| 276 var handler = const shelf.Pipeline() | 284 var handler = const shelf.Pipeline() |
| 277 .addMiddleware(rebuildAndCache) | 285 .addMiddleware(rebuildAndCache) |
| 278 .addHandler(shelf_static.createStaticHandler(outDir, | 286 .addHandler(shelf_static.createStaticHandler(outDir, |
| 279 defaultDocument: _entryPath)); | 287 defaultDocument: _entryPath)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 300 // Note: the cache-control header should be enough, but this doesn't hurt | 308 // Note: the cache-control header should be enough, but this doesn't hurt |
| 301 // and can help renew the policy after it expires. | 309 // and can help renew the policy after it expires. |
| 302 headers['ETag'] = hash; | 310 headers['ETag'] = hash; |
| 303 } | 311 } |
| 304 return response.change(headers: headers); | 312 return response.change(headers: headers); |
| 305 }; | 313 }; |
| 306 } | 314 } |
| 307 | 315 |
| 308 final _log = new Logger('dev_compiler'); | 316 final _log = new Logger('dev_compiler'); |
| 309 final _earlyErrorResult = new CheckerResults(const [], null, true); | 317 final _earlyErrorResult = new CheckerResults(const [], null, true); |
| OLD | NEW |