OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library driver; | 5 library driver; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:math'; | 9 import 'dart:math'; |
10 | 10 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 /// Creates a new command line parser | 65 /// Creates a new command line parser |
66 CommandLineParser({bool alwaysIgnoreUnrecognized: false}) | 66 CommandLineParser({bool alwaysIgnoreUnrecognized: false}) |
67 : _knownFlags = <String>[], | 67 : _knownFlags = <String>[], |
68 _alwaysIgnoreUnrecognized = alwaysIgnoreUnrecognized, | 68 _alwaysIgnoreUnrecognized = alwaysIgnoreUnrecognized, |
69 _parser = new ArgParser(allowTrailingOptions: true); | 69 _parser = new ArgParser(allowTrailingOptions: true); |
70 | 70 |
71 ArgParser get parser => _parser; | 71 ArgParser get parser => _parser; |
72 | 72 |
73 /// Defines a flag. | 73 /// Defines a flag. |
74 /// See [ArgParser.addFlag()]. | 74 /// See [ArgParser.addFlag()]. |
75 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, | 75 void addFlag(String name, |
76 bool negatable: true, void callback(bool value), bool hide: false}) { | 76 {String abbr, |
| 77 String help, |
| 78 bool defaultsTo: false, |
| 79 bool negatable: true, |
| 80 void callback(bool value), |
| 81 bool hide: false}) { |
77 _knownFlags.add(name); | 82 _knownFlags.add(name); |
78 _parser.addFlag(name, | 83 _parser.addFlag(name, |
79 abbr: abbr, | 84 abbr: abbr, |
80 help: help, | 85 help: help, |
81 defaultsTo: defaultsTo, | 86 defaultsTo: defaultsTo, |
82 negatable: negatable, | 87 negatable: negatable, |
83 callback: callback, | 88 callback: callback, |
84 hide: hide); | 89 hide: hide); |
85 } | 90 } |
86 | 91 |
87 /// Defines a value-taking option. | 92 /// Defines a value-taking option. |
88 /// See [ArgParser.addOption()]. | 93 /// See [ArgParser.addOption()]. |
89 void addOption(String name, {String abbr, String help, List<String> allowed, | 94 void addOption(String name, |
90 Map<String, String> allowedHelp, String defaultsTo, void callback(value), | 95 {String abbr, |
| 96 String help, |
| 97 List<String> allowed, |
| 98 Map<String, String> allowedHelp, |
| 99 String defaultsTo, |
| 100 void callback(value), |
91 bool allowMultiple: false}) { | 101 bool allowMultiple: false}) { |
92 _knownFlags.add(name); | 102 _knownFlags.add(name); |
93 _parser.addOption(name, | 103 _parser.addOption(name, |
94 abbr: abbr, | 104 abbr: abbr, |
95 help: help, | 105 help: help, |
96 allowed: allowed, | 106 allowed: allowed, |
97 allowedHelp: allowedHelp, | 107 allowedHelp: allowedHelp, |
98 defaultsTo: defaultsTo, | 108 defaultsTo: defaultsTo, |
99 callback: callback, | 109 callback: callback, |
100 allowMultiple: allowMultiple); | 110 allowMultiple: allowMultiple); |
101 } | 111 } |
102 | 112 |
103 /// Generates a string displaying usage information for the defined options. | 113 /// Generates a string displaying usage information for the defined options. |
104 /// See [ArgParser.usage]. | 114 /// See [ArgParser.usage]. |
105 String getUsage() => _parser.usage; | 115 String getUsage() => _parser.usage; |
106 | 116 |
107 /// Parses [args], a list of command-line arguments, matches them against the | 117 /// Parses [args], a list of command-line arguments, matches them against the |
108 /// flags and options defined by this parser, and returns the result. The | 118 /// flags and options defined by this parser, and returns the result. The |
109 /// values of any defined variables are captured in the given map. | 119 /// values of any defined variables are captured in the given map. |
110 /// See [ArgParser]. | 120 /// See [ArgParser]. |
111 ArgResults parse( | 121 ArgResults parse(List<String> args, Map<String, String> definedVariables) => |
112 List<String> args, Map<String, String> definedVariables) => _parser | 122 _parser.parse( |
113 .parse(_filterUnknowns(parseDefinedVariables(args, definedVariables))); | 123 _filterUnknowns(parseDefinedVariables(args, definedVariables))); |
114 | 124 |
115 List<String> parseDefinedVariables( | 125 List<String> parseDefinedVariables( |
116 List<String> args, Map<String, String> definedVariables) { | 126 List<String> args, Map<String, String> definedVariables) { |
117 int count = args.length; | 127 int count = args.length; |
118 List<String> remainingArgs = <String>[]; | 128 List<String> remainingArgs = <String>[]; |
119 for (int i = 0; i < count; i++) { | 129 for (int i = 0; i < count; i++) { |
120 String arg = args[i]; | 130 String arg = args[i]; |
121 if (arg == '--') { | 131 if (arg == '--') { |
122 while (i < count) { | 132 while (i < count) { |
123 remainingArgs.add(args[i++]); | 133 remainingArgs.add(args[i++]); |
124 } | 134 } |
125 } else if (arg.startsWith("-D")) { | 135 } else if (arg.startsWith("-D")) { |
126 definedVariables[arg.substring(2)] = args[++i]; | 136 definedVariables[arg.substring(2)] = args[++i]; |
127 } else { | 137 } else { |
128 remainingArgs.add(arg); | 138 remainingArgs.add(arg); |
129 } | 139 } |
130 } | 140 } |
131 return remainingArgs; | 141 return remainingArgs; |
132 } | 142 } |
133 | 143 |
134 List<String> _filterUnknowns(List<String> args) { | 144 List<String> _filterUnknowns(List<String> args) { |
135 | |
136 // Only filter args if the ignore flag is specified, or if | 145 // Only filter args if the ignore flag is specified, or if |
137 // _alwaysIgnoreUnrecognized was set to true | 146 // _alwaysIgnoreUnrecognized was set to true |
138 if (_alwaysIgnoreUnrecognized || | 147 if (_alwaysIgnoreUnrecognized || |
139 args.contains('--ignore-unrecognized-flags')) { | 148 args.contains('--ignore-unrecognized-flags')) { |
140 | |
141 // Filter all unrecognized flags and options. | 149 // Filter all unrecognized flags and options. |
142 List<String> filtered = <String>[]; | 150 List<String> filtered = <String>[]; |
143 for (int i = 0; i < args.length; ++i) { | 151 for (int i = 0; i < args.length; ++i) { |
144 String arg = args[i]; | 152 String arg = args[i]; |
145 if (arg.startsWith('--') && arg.length > 2) { | 153 if (arg.startsWith('--') && arg.length > 2) { |
146 String option = arg.substring(2); | 154 String option = arg.substring(2); |
147 // strip the last '=value' | 155 // strip the last '=value' |
148 int equalsOffset = option.lastIndexOf('='); | 156 int equalsOffset = option.lastIndexOf('='); |
149 if (equalsOffset != -1) { | 157 if (equalsOffset != -1) { |
150 option = option.substring(0, equalsOffset); | 158 option = option.substring(0, equalsOffset); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 | 431 |
424 _captureExceptions(service, () { | 432 _captureExceptions(service, () { |
425 stdioServer.serveStdio().then((_) { | 433 stdioServer.serveStdio().then((_) { |
426 if (serve_http) { | 434 if (serve_http) { |
427 httpServer.close(); | 435 httpServer.close(); |
428 } | 436 } |
429 service.shutdown(); | 437 service.shutdown(); |
430 exit(0); | 438 exit(0); |
431 }); | 439 }); |
432 }, | 440 }, |
433 print: results[INTERNAL_PRINT_TO_CONSOLE] | 441 print: |
434 ? null | 442 results[INTERNAL_PRINT_TO_CONSOLE] ? null : httpServer.recordPrint); |
435 : httpServer.recordPrint); | |
436 } | 443 } |
437 | 444 |
438 /** | 445 /** |
439 * Execute the given [callback] within a zone that will capture any unhandled | 446 * Execute the given [callback] within a zone that will capture any unhandled |
440 * exceptions and both report them to the client and send them to the given | 447 * exceptions and both report them to the client and send them to the given |
441 * instrumentation [service]. If a [print] function is provided, then also | 448 * instrumentation [service]. If a [print] function is provided, then also |
442 * capture any data printed by the callback and redirect it to the function. | 449 * capture any data printed by the callback and redirect it to the function. |
443 */ | 450 */ |
444 dynamic _captureExceptions(InstrumentationService service, dynamic callback(), | 451 dynamic _captureExceptions(InstrumentationService service, dynamic callback(), |
445 {void print(String line)}) { | 452 {void print(String line)}) { |
446 Function errorFunction = (Zone self, ZoneDelegate parent, Zone zone, | 453 Function errorFunction = (Zone self, ZoneDelegate parent, Zone zone, |
447 dynamic exception, StackTrace stackTrace) { | 454 dynamic exception, StackTrace stackTrace) { |
448 service.logPriorityException(exception, stackTrace); | 455 service.logPriorityException(exception, stackTrace); |
449 AnalysisServer analysisServer = socketServer.analysisServer; | 456 AnalysisServer analysisServer = socketServer.analysisServer; |
450 analysisServer.sendServerErrorNotification(exception, stackTrace); | 457 analysisServer.sendServerErrorNotification(exception, stackTrace); |
451 throw exception; | 458 throw exception; |
452 }; | 459 }; |
453 Function printFunction = print == null | 460 Function printFunction = print == null |
454 ? null | 461 ? null |
455 : (Zone self, ZoneDelegate parent, Zone zone, String line) { | 462 : (Zone self, ZoneDelegate parent, Zone zone, String line) { |
456 // Note: we don't pass the line on to stdout, because that is reserved | 463 // Note: we don't pass the line on to stdout, because that is reserv
ed |
457 // for communication to the client. | 464 // for communication to the client. |
458 print(line); | 465 print(line); |
459 }; | 466 }; |
460 ZoneSpecification zoneSpecification = new ZoneSpecification( | 467 ZoneSpecification zoneSpecification = new ZoneSpecification( |
461 handleUncaughtError: errorFunction, print: printFunction); | 468 handleUncaughtError: errorFunction, print: printFunction); |
462 return runZoned(callback, zoneSpecification: zoneSpecification); | 469 return runZoned(callback, zoneSpecification: zoneSpecification); |
463 } | 470 } |
464 | 471 |
465 /** | 472 /** |
466 * Create and return the parser used to parse the command-line arguments. | 473 * Create and return the parser used to parse the command-line arguments. |
467 */ | 474 */ |
468 CommandLineParser _createArgParser() { | 475 CommandLineParser _createArgParser() { |
469 CommandLineParser parser = | 476 CommandLineParser parser = |
(...skipping 18 matching lines...) Expand all Loading... |
488 help: "print this help message without starting a server", | 495 help: "print this help message without starting a server", |
489 defaultsTo: false, | 496 defaultsTo: false, |
490 negatable: false); | 497 negatable: false); |
491 parser.addOption(INCREMENTAL_RESOLUTION_LOG, | 498 parser.addOption(INCREMENTAL_RESOLUTION_LOG, |
492 help: "the description of the incremental resolution log"); | 499 help: "the description of the incremental resolution log"); |
493 parser.addFlag(INCREMENTAL_RESOLUTION_VALIDATION, | 500 parser.addFlag(INCREMENTAL_RESOLUTION_VALIDATION, |
494 help: "enable validation of incremental resolution results (slow)", | 501 help: "enable validation of incremental resolution results (slow)", |
495 defaultsTo: false, | 502 defaultsTo: false, |
496 negatable: false); | 503 negatable: false); |
497 parser.addOption(INSTRUMENTATION_LOG_FILE, | 504 parser.addOption(INSTRUMENTATION_LOG_FILE, |
498 help: "the path of the file to which instrumentation data will be writte
n"); | 505 help: |
| 506 "the path of the file to which instrumentation data will be written"
); |
499 parser.addFlag(INTERNAL_PRINT_TO_CONSOLE, | 507 parser.addFlag(INTERNAL_PRINT_TO_CONSOLE, |
500 help: "enable sending `print` output to the console", | 508 help: "enable sending `print` output to the console", |
501 defaultsTo: false, | 509 defaultsTo: false, |
502 negatable: false); | 510 negatable: false); |
503 parser.addOption(PORT_OPTION, | 511 parser.addOption(PORT_OPTION, |
504 help: "the http diagnostic port on which the server provides" | 512 help: "the http diagnostic port on which the server provides" |
505 " status and performance information"); | 513 " status and performance information"); |
506 parser.addOption(INTERNAL_DELAY_FREQUENCY); | 514 parser.addOption(INTERNAL_DELAY_FREQUENCY); |
507 parser.addOption(SDK_OPTION, help: "[path] the path to the sdk"); | 515 parser.addOption(SDK_OPTION, help: "[path] the path to the sdk"); |
508 parser.addFlag(NO_ERROR_NOTIFICATION, | 516 parser.addFlag(NO_ERROR_NOTIFICATION, |
509 help: "disable sending all analysis error notifications to the server", | 517 help: "disable sending all analysis error notifications to the server", |
510 defaultsTo: false, | 518 defaultsTo: false, |
511 negatable: false); | 519 negatable: false); |
512 parser.addFlag(NO_INDEX, | 520 parser.addFlag(NO_INDEX, |
513 help: "disable indexing sources", defaultsTo: false, negatable: false); | 521 help: "disable indexing sources", defaultsTo: false, negatable: false); |
514 parser.addFlag(USE_ANALISYS_HIGHLIGHT2, | 522 parser.addFlag(USE_ANALISYS_HIGHLIGHT2, |
515 help: "enable version 2 of semantic highlight", | 523 help: "enable version 2 of semantic highlight", |
516 defaultsTo: false, | 524 defaultsTo: false, |
517 negatable: false); | 525 negatable: false); |
518 parser.addOption(FILE_READ_MODE, | 526 parser.addOption(FILE_READ_MODE, |
519 help: "an option of the ways files can be read from disk, " + | 527 help: "an option of the ways files can be read from disk, " + |
520 "some clients normalize end of line characters which would make " + | 528 "some clients normalize end of line characters which would make " + |
521 "the file offset and range information incorrect.", | 529 "the file offset and range information incorrect.", |
522 allowed: ["as-is", "normalize-eol-always"], | 530 allowed: ["as-is", "normalize-eol-always"], |
523 allowedHelp: { | 531 allowedHelp: { |
524 "as-is": "file contents are read as-is, no file changes occur", | 532 "as-is": "file contents are read as-is, no file changes occur", |
525 "normalize-eol-always": | 533 "normalize-eol-always": |
526 r'file contents normalize the end of line characters to the single cha
racter new line `\n`' | 534 r'file contents normalize the end of line characters to the single
character new line `\n`' |
527 }, | 535 }, |
528 defaultsTo: "as-is"); | 536 defaultsTo: "as-is"); |
529 | 537 |
530 return parser; | 538 return parser; |
531 } | 539 } |
532 | 540 |
533 /** | 541 /** |
534 * Print information about how to use the server. | 542 * Print information about how to use the server. |
535 */ | 543 */ |
536 void _printUsage(ArgParser parser) { | 544 void _printUsage(ArgParser parser) { |
537 print('Usage: $BINARY_NAME [flags]'); | 545 print('Usage: $BINARY_NAME [flags]'); |
538 print(''); | 546 print(''); |
539 print('Supported flags are:'); | 547 print('Supported flags are:'); |
540 print(parser.usage); | 548 print(parser.usage); |
541 } | 549 } |
542 | 550 |
543 /** | 551 /** |
544 * Read the UUID from disk, generating and storing a new one if necessary. | 552 * Read the UUID from disk, generating and storing a new one if necessary. |
545 */ | 553 */ |
546 String _readUuid(InstrumentationService service) { | 554 String _readUuid(InstrumentationService service) { |
547 File uuidFile = new File(PhysicalResourceProvider.INSTANCE | 555 File uuidFile = new File(PhysicalResourceProvider.INSTANCE |
548 .getStateLocation('.instrumentation') | 556 .getStateLocation('.instrumentation') |
549 .getChild('uuid.txt').path); | 557 .getChild('uuid.txt') |
| 558 .path); |
550 try { | 559 try { |
551 if (uuidFile.existsSync()) { | 560 if (uuidFile.existsSync()) { |
552 String uuid = uuidFile.readAsStringSync(); | 561 String uuid = uuidFile.readAsStringSync(); |
553 if (uuid != null && uuid.length > 5) { | 562 if (uuid != null && uuid.length > 5) { |
554 return uuid; | 563 return uuid; |
555 } | 564 } |
556 } | 565 } |
557 } catch (exception, stackTrace) { | 566 } catch (exception, stackTrace) { |
558 service.logPriorityException(exception, stackTrace); | 567 service.logPriorityException(exception, stackTrace); |
559 } | 568 } |
560 int millisecondsSinceEpoch = new DateTime.now().millisecondsSinceEpoch; | 569 int millisecondsSinceEpoch = new DateTime.now().millisecondsSinceEpoch; |
561 int random = new Random().nextInt(0x3fffffff); | 570 int random = new Random().nextInt(0x3fffffff); |
562 String uuid = '$millisecondsSinceEpoch$random'; | 571 String uuid = '$millisecondsSinceEpoch$random'; |
563 try { | 572 try { |
564 uuidFile.parent.createSync(recursive: true); | 573 uuidFile.parent.createSync(recursive: true); |
565 uuidFile.writeAsStringSync(uuid); | 574 uuidFile.writeAsStringSync(uuid); |
566 } catch (exception, stackTrace) { | 575 } catch (exception, stackTrace) { |
567 service.logPriorityException(exception, stackTrace); | 576 service.logPriorityException(exception, stackTrace); |
568 // Slightly alter the uuid to indicate it was not persisted | 577 // Slightly alter the uuid to indicate it was not persisted |
569 uuid = 'temp-$uuid'; | 578 uuid = 'temp-$uuid'; |
570 } | 579 } |
571 return uuid; | 580 return uuid; |
572 } | 581 } |
573 } | 582 } |
OLD | NEW |