Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
| 6 * **docgen** is a tool for creating machine readable representations of Dart | 6 * **docgen** is a tool for creating machine readable representations of Dart |
| 7 * code metadata, including: classes, members, comments and annotations. | 7 * code metadata, including: classes, members, comments and annotations. |
| 8 * | 8 * |
| 9 * docgen is run on a `.dart` file or a directory containing `.dart` files. | 9 * docgen is run on a `.dart` file or a directory containing `.dart` files. |
| 10 * | 10 * |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 } else { | 161 } else { |
| 162 // If DART_SDK is not defined in the environment, | 162 // If DART_SDK is not defined in the environment, |
| 163 // assuming the dart executable is from the Dart SDK folder inside bin. | 163 // assuming the dart executable is from the Dart SDK folder inside bin. |
| 164 sdkRoot = path.join(path.dirname(path.dirname(path.dirname(path.dirname( | 164 sdkRoot = path.join(path.dirname(path.dirname(path.dirname(path.dirname( |
| 165 path.absolute(new Options().script))))), 'sdk'); | 165 path.absolute(new Options().script))))), 'sdk'); |
| 166 logger.info('SDK Root: ${sdkRoot}'); | 166 logger.info('SDK Root: ${sdkRoot}'); |
| 167 } | 167 } |
| 168 return _analyzeLibraries(libraries, sdkRoot, packageRoot: packageRoot); | 168 return _analyzeLibraries(libraries, sdkRoot, packageRoot: packageRoot); |
| 169 } | 169 } |
| 170 | 170 |
| 171 // TODO(janicejl): Should make docgen fail gracefully, or output a friendly | |
| 172 // error message letting them know why it is failing to create a mirror system. | |
| 173 // If there is conflicting library names, should modify it with a hash at the | |
| 174 // end of it's library name. | |
| 175 /** | 171 /** |
| 176 * Analyzes set of libraries and provides a mirror system which can be used | 172 * Analyzes set of libraries and provides a mirror system which can be used |
| 177 * for static inspection of the source code. | 173 * for static inspection of the source code. |
| 178 */ | 174 */ |
| 179 Future<MirrorSystem> _analyzeLibraries(List<String> libraries, | 175 Future<MirrorSystem> _analyzeLibraries(List<String> libraries, |
| 180 String libraryRoot, {String packageRoot}) { | 176 String libraryRoot, {String packageRoot}) { |
| 181 SourceFileProvider provider = new SourceFileProvider(); | 177 SourceFileProvider provider = new SourceFileProvider(); |
| 182 api.DiagnosticHandler diagnosticHandler = | 178 api.DiagnosticHandler diagnosticHandler = |
| 183 new FormattingDiagnosticHandler(provider).diagnosticHandler; | 179 new FormattingDiagnosticHandler(provider).diagnosticHandler; |
| 184 Uri libraryUri = new Uri(scheme: 'file', path: appendSlash(libraryRoot)); | 180 Uri libraryUri = new Uri(scheme: 'file', path: appendSlash(libraryRoot)); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 | 319 |
| 324 var setters = {}; | 320 var setters = {}; |
| 325 var getters = {}; | 321 var getters = {}; |
| 326 var constructors = {}; | 322 var constructors = {}; |
| 327 var operators = {}; | 323 var operators = {}; |
| 328 var methods = {}; | 324 var methods = {}; |
| 329 | 325 |
| 330 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { | 326 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { |
| 331 if (includePrivate || !mirror.isPrivate) { | 327 if (includePrivate || !mirror.isPrivate) { |
| 332 var method = new Method(mirrorName, mirror.isStatic, mirror.isAbstract, | 328 var method = new Method(mirrorName, mirror.isStatic, mirror.isAbstract, |
| 333 mirror.isConstConstructor, mirror.returnType.qualifiedName, | 329 mirror.isConstConstructor, _getReturnType(mirror.returnType), |
| 334 _getComment(mirror), _getParameters(mirror.parameters), | 330 _getComment(mirror), _getParameters(mirror.parameters), |
| 335 _getAnnotations(mirror), mirror.qualifiedName); | 331 _getAnnotations(mirror), mirror.qualifiedName); |
| 336 _currentMember = mirror; | 332 _currentMember = mirror; |
| 337 if (mirror.isSetter) { | 333 if (mirror.isSetter) { |
| 338 setters[mirrorName] = method; | 334 setters[mirrorName] = method; |
| 339 } else if (mirror.isGetter) { | 335 } else if (mirror.isGetter) { |
| 340 getters[mirrorName] = method; | 336 getters[mirrorName] = method; |
| 341 } else if (mirror.isConstructor) { | 337 } else if (mirror.isConstructor) { |
| 342 constructors[mirrorName] = method; | 338 constructors[mirrorName] = method; |
| 343 } else if (mirror.isOperator) { | 339 } else if (mirror.isOperator) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 /** | 419 /** |
| 424 * Returns a map of [Generic] objects constructed from the class mirror. | 420 * Returns a map of [Generic] objects constructed from the class mirror. |
| 425 */ | 421 */ |
| 426 Map<String, Generic> _getGenerics(ClassMirror mirror) { | 422 Map<String, Generic> _getGenerics(ClassMirror mirror) { |
| 427 return new Map.fromIterable(mirror.typeVariables, | 423 return new Map.fromIterable(mirror.typeVariables, |
| 428 key: (e) => e.toString(), | 424 key: (e) => e.toString(), |
| 429 value: (e) => new Generic(e.toString(), e.upperBound.qualifiedName)); | 425 value: (e) => new Generic(e.toString(), e.upperBound.qualifiedName)); |
| 430 } | 426 } |
| 431 | 427 |
| 432 /** | 428 /** |
| 429 * Returns a single [ReturnType] object constructed from the Method.returnType | |
| 430 * Type mirror. | |
| 431 */ | |
|
Alan Knight
2013/07/24 23:06:44
I don't love these names. "get" is usually not pro
janicejl
2013/07/25 00:29:37
Done. That was a good point, it was not specific t
| |
| 432 ReturnType _getReturnType(TypeMirror mirror) { | |
| 433 return new ReturnType(mirror.qualifiedName, _getReturnTypeHelper(mirror)); | |
| 434 } | |
| 435 | |
| 436 /** | |
| 437 * Returns a list of [ReturnType] objects constructed from TypeMirrors. | |
| 438 */ | |
| 439 List<ReturnType> _getReturnTypeHelper(TypeMirror mirror) { | |
| 440 if (mirror is ClassMirror) { | |
| 441 var innerList = []; | |
| 442 mirror.typeArguments.forEach((e) { | |
| 443 innerList.add(new ReturnType(e.qualifiedName, _getReturnTypeHelper(e))); | |
| 444 }); | |
| 445 return innerList; | |
| 446 } | |
| 447 return []; | |
| 448 } | |
| 449 | |
| 450 /** | |
| 433 * Writes text to a file in the 'docs' directory. | 451 * Writes text to a file in the 'docs' directory. |
| 434 */ | 452 */ |
| 435 void _writeToFile(String text, String filename) { | 453 void _writeToFile(String text, String filename) { |
| 436 Directory dir = new Directory('docs'); | 454 Directory dir = new Directory('docs'); |
| 437 if (!dir.existsSync()) { | 455 if (!dir.existsSync()) { |
| 438 dir.createSync(); | 456 dir.createSync(); |
| 439 } | 457 } |
| 440 File file = new File('docs/$filename'); | 458 File file = new File('docs/$filename'); |
| 441 if (!file.existsSync()) { | 459 if (!file.existsSync()) { |
| 442 file.createSync(); | 460 file.createSync(); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 505 'comment': comment, | 523 'comment': comment, |
| 506 'variables': recurseMap(variables), | 524 'variables': recurseMap(variables), |
| 507 'functions': recurseMap(functions), | 525 'functions': recurseMap(functions), |
| 508 'classes': recurseMap(classes) | 526 'classes': recurseMap(classes) |
| 509 }; | 527 }; |
| 510 } | 528 } |
| 511 | 529 |
| 512 /** | 530 /** |
| 513 * A class containing contents of a Dart class. | 531 * A class containing contents of a Dart class. |
| 514 */ | 532 */ |
| 515 // TODO(tmandel): Figure out how to do typedefs (what is needed) | |
| 516 class Class extends Indexable { | 533 class Class extends Indexable { |
| 517 | 534 |
| 518 /// List of the names of interfaces that this class implements. | 535 /// List of the names of interfaces that this class implements. |
| 519 List<String> interfaces; | 536 List<String> interfaces; |
| 520 | 537 |
| 521 /// Top-level variables in the class. | 538 /// Top-level variables in the class. |
| 522 Map<String, Variable> variables; | 539 Map<String, Variable> variables; |
| 523 | 540 |
| 524 /// Methods in the class. | 541 /// Methods in the class. |
| 525 Map<String, Map<String, Method>> methods; | 542 Map<String, Map<String, Method>> methods; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 610 * A class containing properties of a Dart method. | 627 * A class containing properties of a Dart method. |
| 611 */ | 628 */ |
| 612 class Method extends Indexable { | 629 class Method extends Indexable { |
| 613 | 630 |
| 614 /// Parameters for this method. | 631 /// Parameters for this method. |
| 615 Map<String, Parameter> parameters; | 632 Map<String, Parameter> parameters; |
| 616 | 633 |
| 617 bool isStatic; | 634 bool isStatic; |
| 618 bool isAbstract; | 635 bool isAbstract; |
| 619 bool isConst; | 636 bool isConst; |
| 620 String returnType; | 637 ReturnType returnType; |
| 621 | 638 |
| 622 /// List of the meta annotations on the method. | 639 /// List of the meta annotations on the method. |
| 623 List<String> annotations; | 640 List<String> annotations; |
| 624 | 641 |
| 625 Method(String name, this.isStatic, this.isAbstract, this.isConst, | 642 Method(String name, this.isStatic, this.isAbstract, this.isConst, |
| 626 this.returnType, String comment, this.parameters, this.annotations, | 643 this.returnType, String comment, this.parameters, this.annotations, |
| 627 String qualifiedName) | 644 String qualifiedName) |
| 628 : super(name, comment, qualifiedName); | 645 : super(name, comment, qualifiedName); |
| 629 | 646 |
| 630 /// Generates a map describing the [Method] object. | 647 /// Generates a map describing the [Method] object. |
| 631 Map toMap() => { | 648 Map toMap() => { |
| 632 'name': name, | 649 'name': name, |
| 633 'qualifiedname': qualifiedName, | 650 'qualifiedname': qualifiedName, |
| 634 'comment': comment, | 651 'comment': comment, |
| 635 'static': isStatic.toString(), | 652 'static': isStatic.toString(), |
| 636 'abstract': isAbstract.toString(), | 653 'abstract': isAbstract.toString(), |
| 637 'constant': isConst.toString(), | 654 'constant': isConst.toString(), |
| 638 'return': returnType, | 655 'return': new List.filled(1, returnType.toMap()), |
| 639 'parameters': recurseMap(parameters), | 656 'parameters': recurseMap(parameters), |
| 640 'annotations': new List.from(annotations) | 657 'annotations': new List.from(annotations) |
| 641 }; | 658 }; |
| 642 } | 659 } |
| 643 | 660 |
| 644 /** | 661 /** |
| 645 * A class containing properties of a Dart method/function parameter. | 662 * A class containing properties of a Dart method/function parameter. |
| 646 */ | 663 */ |
| 647 class Parameter { | 664 class Parameter { |
| 648 | 665 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 677 class Generic { | 694 class Generic { |
| 678 String name; | 695 String name; |
| 679 String type; | 696 String type; |
| 680 | 697 |
| 681 Generic(this.name, this.type); | 698 Generic(this.name, this.type); |
| 682 | 699 |
| 683 Map toMap() => { | 700 Map toMap() => { |
| 684 'name': name, | 701 'name': name, |
| 685 'type': type | 702 'type': type |
| 686 }; | 703 }; |
| 704 } | |
| 705 | |
| 706 /** | |
| 707 * A class containing properties of a return type. | |
|
Alan Knight
2013/07/24 23:06:44
We know it's a class, what does "containing proper
janicejl
2013/07/25 00:29:37
Done.
| |
| 708 * | |
| 709 * Return types are of a form [outer]<[inner]>. | |
| 710 * If there is no [inner] part, [inner] will be an empty list. | |
| 711 * | |
| 712 * For example: | |
| 713 * int size() | |
| 714 * "return" : | |
| 715 * - "outer" : dart.core.int | |
| 716 * "inner" : | |
| 717 * | |
| 718 * List<String> toList() | |
| 719 * "return" : | |
| 720 * - "outer" : dart.core.List | |
| 721 * "inner" : | |
| 722 * - "outer" : dart.core.String | |
| 723 * "inner" : | |
| 724 */ | |
| 725 class ReturnType { | |
| 726 String outer; | |
| 727 List<ReturnType> inner; | |
| 728 | |
| 729 ReturnType(this.outer, this.inner); | |
| 730 | |
| 731 Map toMap() => { | |
| 732 'outer': outer, | |
| 733 'inner': new List.from(inner.map((e) => e.toMap())) | |
| 734 }; | |
| 687 } | 735 } |
| OLD | NEW |