Chromium Code Reviews| 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'; |
| 11 | 11 |
| 12 import 'package:analyzer/src/generated/engine.dart' show ChangeSet; | 12 import 'package:analyzer/src/generated/engine.dart' show ChangeSet; |
| 13 import 'package:logging/logging.dart' show Level, Logger, LogRecord; | 13 import 'package:logging/logging.dart' show Level, Logger, LogRecord; |
| 14 import 'package:path/path.dart' as path; | 14 import 'package:path/path.dart' as path; |
| 15 import 'package:shelf/shelf.dart' as shelf; | 15 import 'package:shelf/shelf.dart' as shelf; |
| 16 import 'package:shelf/shelf_io.dart' as shelf; | 16 import 'package:shelf/shelf_io.dart' as shelf; |
| 17 import 'package:shelf_static/shelf_static.dart' as shelf_static; | 17 import 'package:shelf_static/shelf_static.dart' as shelf_static; |
| 18 | 18 |
| 19 import 'src/checker/checker.dart'; | 19 import 'src/checker/checker.dart'; |
| 20 import 'src/checker/dart_sdk.dart' show mockSdkSources; | 20 import 'src/checker/dart_sdk.dart' show mockSdkSources; |
| 21 import 'src/checker/resolver.dart'; | 21 import 'src/checker/resolver.dart'; |
| 22 import 'src/checker/rules.dart'; | 22 import 'src/checker/rules.dart'; |
| 23 import 'src/codegen/code_generator.dart' show CodeGenerator; | 23 import 'src/codegen/code_generator.dart' show CodeGenerator; |
| 24 import 'src/codegen/dart_codegen.dart'; | 24 import 'src/codegen/dart_codegen.dart'; |
| 25 import 'src/codegen/html_codegen.dart'; | 25 import 'src/codegen/html_codegen.dart'; |
| 26 import 'src/codegen/js_codegen.dart'; | 26 import 'src/codegen/js_codegen.dart'; |
| 27 import 'src/dependency_graph.dart'; | 27 import 'src/dependency_graph.dart'; |
| 28 import 'src/in_memory.dart'; | |
| 28 import 'src/info.dart' show LibraryInfo, CheckerResults, LibraryUnit; | 29 import 'src/info.dart' show LibraryInfo, CheckerResults, LibraryUnit; |
| 29 import 'src/options.dart'; | 30 import 'src/options.dart'; |
| 30 import 'src/report.dart'; | 31 import 'src/report.dart'; |
| 31 import 'src/utils.dart'; | 32 import 'src/utils.dart'; |
| 32 | 33 |
| 33 /// Sets up the type checker logger to print a span that highlights error | 34 /// Sets up the type checker logger to print a span that highlights error |
| 34 /// messages. | 35 /// messages. |
| 35 StreamSubscription setupLogger(Level level, printFn) { | 36 StreamSubscription setupLogger(Level level, printFn) { |
| 36 Logger.root.level = level; | 37 Logger.root.level = level; |
| 37 return Logger.root.onRecord.listen((LogRecord rec) { | 38 return Logger.root.onRecord.listen((LogRecord rec) { |
| 38 printFn('${rec.level.name.toLowerCase()}: ${rec.message}'); | 39 printFn('${rec.level.name.toLowerCase()}: ${rec.message}'); |
| 39 }); | 40 }); |
| 40 } | 41 } |
| 41 | 42 |
| 42 /// Encapsulates the logic to do a one-off compilation or a partial compilation | 43 /// Encapsulates the logic to do a one-off compilation or a partial compilation |
| 43 /// when the compiler is run as a development server. | 44 /// when the compiler is run as a development server. |
| 44 class Compiler { | 45 class Compiler { |
| 45 final CompilerOptions _options; | 46 final CompilerOptions _options; |
| 46 final TypeResolver _resolver; | 47 final TypeResolver _resolver; |
| 47 final CheckerReporter _reporter; | 48 final CheckerReporter _reporter; |
| 48 final TypeRules _rules; | 49 final TypeRules _rules; |
| 49 final CodeChecker _checker; | 50 final CodeChecker _checker; |
| 50 final SourceGraph _graph; | 51 final SourceGraph _graph; |
| 51 final SourceNode _entryNode; | 52 final SourceNode _entryNode; |
| 52 List<LibraryInfo> _libraries = <LibraryInfo>[]; | 53 List<LibraryInfo> _libraries = <LibraryInfo>[]; |
| 53 final List<CodeGenerator> _generators; | 54 final List<CodeGenerator> _generators; |
| 54 final bool _hashing; | 55 final bool _hashing; |
| 55 bool _failure = false; | 56 bool _failure = false; |
| 56 | 57 |
| 58 static const String _implicitEntryFile = 'index.html'; | |
| 59 | |
| 57 factory Compiler(CompilerOptions options, | 60 factory Compiler(CompilerOptions options, |
| 58 [TypeResolver resolver, CheckerReporter reporter]) { | 61 {TypeResolver resolver, CheckerReporter reporter, implicitHtml: false}) { |
| 62 var inputFile = options.entryPointFile; | |
| 63 var inputUri = inputFile.startsWith('dart:') || | |
| 64 inputFile.startsWith('package:') | |
| 65 ? Uri.parse(inputFile) | |
| 66 : new Uri.file(path.absolute(inputFile)); | |
| 67 | |
| 59 if (resolver == null) { | 68 if (resolver == null) { |
| 69 InMemoryUriResolver entryResolver = null; | |
|
Jennifer Messerly
2015/05/13 19:31:55
per suggestion below, moving implicitHtml into opt
vsm
2015/05/14 00:04:07
Done. Pushed it down to Resolver.
| |
| 70 if (implicitHtml) { | |
| 71 inputFile = _implicitEntryFile; | |
| 72 var entry = path.absolute(inputFile); | |
| 73 inputUri = new Uri.file(entry); | |
| 74 var src = path.absolute(options.entryPointFile); | |
| 75 var index = <String, String>{ | |
| 76 '$entry': | |
| 77 '<html><body><script type="application/dart" src="$src"></script>< /body></html>' | |
|
Jennifer Messerly
2015/05/13 19:31:55
long line. sadly formatter won't fix these or even
vsm
2015/05/14 00:04:08
Moved.
| |
| 78 }; | |
| 79 entryResolver = | |
| 80 new InMemoryUriResolver(index, representNonExistingFiles: false); | |
| 81 } | |
| 60 resolver = options.useMockSdk | 82 resolver = options.useMockSdk |
| 61 ? new TypeResolver.fromMock(mockSdkSources, options) | 83 ? new TypeResolver.fromMock(mockSdkSources, options, |
| 62 : new TypeResolver.fromDir(options.dartSdkPath, options); | 84 entryResolver: entryResolver) |
| 85 : new TypeResolver.fromDir(options.dartSdkPath, options, | |
| 86 entryResolver: entryResolver); | |
| 63 } | 87 } |
| 64 | 88 |
| 65 if (reporter == null) { | 89 if (reporter == null) { |
| 66 reporter = options.dumpInfo | 90 reporter = options.dumpInfo |
| 67 ? new SummaryReporter() | 91 ? new SummaryReporter() |
| 68 : new LogReporter(options.useColors); | 92 : new LogReporter(options.useColors); |
| 69 } | 93 } |
| 70 var graph = new SourceGraph(resolver.context, reporter, options); | 94 var graph = new SourceGraph(resolver.context, reporter, options); |
| 71 var rules = | 95 var rules = |
| 72 new RestrictedRules(resolver.context.typeProvider, options: options); | 96 new RestrictedRules(resolver.context.typeProvider, options: options); |
| 73 var checker = new CodeChecker(rules, reporter, options); | 97 var checker = new CodeChecker(rules, reporter, options); |
| 74 var inputFile = options.entryPointFile; | |
| 75 var uri = inputFile.startsWith('dart:') || inputFile.startsWith('package:') | |
| 76 ? Uri.parse(inputFile) | |
| 77 : new Uri.file(path.absolute(inputFile)); | |
| 78 var entryNode = graph.nodeFromUri(uri); | |
| 79 | 98 |
| 99 var entryNode = graph.nodeFromUri(inputUri); | |
| 80 var outputDir = options.outputDir; | 100 var outputDir = options.outputDir; |
| 81 var generators = <CodeGenerator>[]; | 101 var generators = <CodeGenerator>[]; |
| 82 if (options.dumpSrcDir != null) { | 102 if (options.dumpSrcDir != null) { |
| 83 generators.add(new EmptyDartGenerator( | 103 generators.add(new EmptyDartGenerator( |
| 84 options.dumpSrcDir, entryNode.uri, rules, options)); | 104 options.dumpSrcDir, entryNode.uri, rules, options)); |
| 85 } | 105 } |
| 86 if (outputDir != null) { | 106 if (outputDir != null) { |
| 87 generators.add(options.outputDart | 107 generators.add(options.outputDart |
| 88 ? new DartGenerator(outputDir, entryNode.uri, rules, options) | 108 ? new DartGenerator(outputDir, entryNode.uri, rules, options) |
| 89 : new JSGenerator(outputDir, entryNode.uri, rules, options)); | 109 : new JSGenerator(outputDir, entryNode.uri, rules, options)); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 | 260 |
| 241 class CompilerServer { | 261 class CompilerServer { |
| 242 final Compiler compiler; | 262 final Compiler compiler; |
| 243 final String outDir; | 263 final String outDir; |
| 244 final String host; | 264 final String host; |
| 245 final int port; | 265 final int port; |
| 246 final String _entryPath; | 266 final String _entryPath; |
| 247 | 267 |
| 248 factory CompilerServer(CompilerOptions options) { | 268 factory CompilerServer(CompilerOptions options) { |
| 249 var entryPath = path.basename(options.entryPointFile); | 269 var entryPath = path.basename(options.entryPointFile); |
| 250 if (path.extension(entryPath) != '.html') { | 270 var extension = path.extension(entryPath); |
| 251 print('error: devc in server mode requires an HTML entry point.'); | 271 bool implicitHtml = false; |
|
Jennifer Messerly
2015/05/13 19:31:55
suggestion: move this into options. It can still b
vsm
2015/05/14 00:04:07
Done.
| |
| 252 exit(1); | 272 if (extension != '.html') { |
| 273 if (extension == '.dart') { | |
| 274 implicitHtml = true; | |
| 275 } else { | |
| 276 print( | |
| 277 'error: devc in server mode requires an HTML or Dart entry point.'); | |
| 278 exit(1); | |
| 279 } | |
| 253 } | 280 } |
| 254 | 281 |
| 255 // TODO(sigmund): allow running without a dir, but keep output in memory? | 282 // TODO(sigmund): allow running without a dir, but keep output in memory? |
| 256 var outDir = options.outputDir; | 283 var outDir = options.outputDir; |
| 257 if (outDir == null) { | 284 if (outDir == null) { |
| 258 print('error: devc in server mode also requires specifying and ' | 285 print('error: devc in server mode also requires specifying and ' |
| 259 'output location for generated code.'); | 286 'output location for generated code.'); |
| 260 exit(1); | 287 exit(1); |
| 261 } | 288 } |
| 262 var port = options.port; | 289 var port = options.port; |
| 263 var host = options.host; | 290 var host = options.host; |
| 264 var compiler = new Compiler(options); | 291 var compiler = new Compiler(options, implicitHtml: implicitHtml); |
| 265 return new CompilerServer._(compiler, outDir, host, port, entryPath); | 292 return new CompilerServer._(compiler, outDir, host, port, |
| 293 implicitHtml ? Compiler._implicitEntryFile : entryPath); | |
| 266 } | 294 } |
| 267 | 295 |
| 268 CompilerServer._( | 296 CompilerServer._( |
| 269 this.compiler, this.outDir, this.host, this.port, this._entryPath); | 297 this.compiler, this.outDir, this.host, this.port, this._entryPath); |
| 270 | 298 |
| 271 Future start() async { | 299 Future start() async { |
| 272 // Create output directory if needed. shelf_static will fail otherwise. | 300 // Create output directory if needed. shelf_static will fail otherwise. |
| 273 var out = new Directory(outDir); | 301 var out = new Directory(outDir); |
| 274 if (!await out.exists()) await out.create(recursive: true); | 302 if (!await out.exists()) await out.create(recursive: true); |
| 275 | 303 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 300 // Note: the cache-control header should be enough, but this doesn't hurt | 328 // Note: the cache-control header should be enough, but this doesn't hurt |
| 301 // and can help renew the policy after it expires. | 329 // and can help renew the policy after it expires. |
| 302 headers['ETag'] = hash; | 330 headers['ETag'] = hash; |
| 303 } | 331 } |
| 304 return response.change(headers: headers); | 332 return response.change(headers: headers); |
| 305 }; | 333 }; |
| 306 } | 334 } |
| 307 | 335 |
| 308 final _log = new Logger('dev_compiler'); | 336 final _log = new Logger('dev_compiler'); |
| 309 final _earlyErrorResult = new CheckerResults(const [], null, true); | 337 final _earlyErrorResult = new CheckerResults(const [], null, true); |
| OLD | NEW |