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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 */ | 297 */ |
| 302 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap, | 298 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap, |
| 303 bool includePrivate) { | 299 bool includePrivate) { |
| 304 var data = {}; | 300 var data = {}; |
| 305 // TODO(janicejl): When map to map feature is created, replace the below with | 301 // TODO(janicejl): When map to map feature is created, replace the below with |
| 306 // a filter. Issue(#9590). | 302 // a filter. Issue(#9590). |
| 307 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { | 303 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { |
| 308 if (includePrivate || !mirror.isPrivate) { | 304 if (includePrivate || !mirror.isPrivate) { |
| 309 _currentMember = mirror; | 305 _currentMember = mirror; |
| 310 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, | 306 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, |
| 311 mirror.isStatic, mirror.isConst, mirror.type.qualifiedName, | 307 mirror.isStatic, mirror.isConst, _type(mirror.type), |
| 312 _getComment(mirror), _getAnnotations(mirror), mirror.qualifiedName); | 308 _getComment(mirror), _getAnnotations(mirror), mirror.qualifiedName); |
| 313 } | 309 } |
| 314 }); | 310 }); |
| 315 return data; | 311 return data; |
| 316 } | 312 } |
| 317 | 313 |
| 318 /** | 314 /** |
| 319 * Returns a map of [Method] objects constructed from [mirrorMap]. | 315 * Returns a map of [Method] objects constructed from [mirrorMap]. |
| 320 */ | 316 */ |
| 321 Map<String, Map<String, Method>> _getMethods | 317 Map<String, Map<String, Method>> _getMethods |
| 322 (Map<String, MethodMirror> mirrorMap, bool includePrivate) { | 318 (Map<String, MethodMirror> mirrorMap, bool includePrivate) { |
| 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, _type(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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 | 403 |
| 408 /** | 404 /** |
| 409 * Returns a map of [Parameter] objects constructed from [mirrorList]. | 405 * Returns a map of [Parameter] objects constructed from [mirrorList]. |
| 410 */ | 406 */ |
| 411 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { | 407 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { |
| 412 var data = {}; | 408 var data = {}; |
| 413 mirrorList.forEach((ParameterMirror mirror) { | 409 mirrorList.forEach((ParameterMirror mirror) { |
| 414 _currentMember = mirror; | 410 _currentMember = mirror; |
| 415 data[mirror.simpleName] = new Parameter(mirror.simpleName, | 411 data[mirror.simpleName] = new Parameter(mirror.simpleName, |
| 416 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, | 412 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, |
| 417 mirror.type.qualifiedName, mirror.defaultValue, | 413 _type(mirror.type), mirror.defaultValue, |
| 418 _getAnnotations(mirror)); | 414 _getAnnotations(mirror)); |
| 419 }); | 415 }); |
| 420 return data; | 416 return data; |
| 421 } | 417 } |
| 422 | 418 |
| 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 [Type] object constructed from the Method.returnType | |
| 430 * Type mirror. | |
| 431 */ | |
| 432 Type _type(TypeMirror mirror) { | |
| 433 return new Type(mirror.qualifiedName, _typeGenerics(mirror)); | |
| 434 } | |
| 435 | |
| 436 /** | |
| 437 * Returns a list of [Type] objects constructed from TypeMirrors. | |
| 438 */ | |
| 439 List<Type> _typeGenerics(TypeMirror mirror) { | |
| 440 if (mirror is ClassMirror && !mirror.isTypedef) { | |
| 441 var innerList = []; | |
| 442 mirror.typeArguments.forEach((e) { | |
| 443 innerList.add(new Type(e.qualifiedName, _typeGenerics(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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 577 } | 594 } |
| 578 | 595 |
| 579 /** | 596 /** |
| 580 * A class containing properties of a Dart variable. | 597 * A class containing properties of a Dart variable. |
| 581 */ | 598 */ |
| 582 class Variable extends Indexable { | 599 class Variable extends Indexable { |
| 583 | 600 |
| 584 bool isFinal; | 601 bool isFinal; |
| 585 bool isStatic; | 602 bool isStatic; |
| 586 bool isConst; | 603 bool isConst; |
| 587 String type; | 604 Type type; |
| 588 | 605 |
| 589 /// List of the meta annotations on the variable. | 606 /// List of the meta annotations on the variable. |
| 590 List<String> annotations; | 607 List<String> annotations; |
| 591 | 608 |
| 592 Variable(String name, this.isFinal, this.isStatic, this.isConst, this.type, | 609 Variable(String name, this.isFinal, this.isStatic, this.isConst, this.type, |
| 593 String comment, this.annotations, String qualifiedName) : super(name, | 610 String comment, this.annotations, String qualifiedName) : super(name, |
| 594 comment, qualifiedName); | 611 comment, qualifiedName); |
| 595 | 612 |
| 596 /// Generates a map describing the [Variable] object. | 613 /// Generates a map describing the [Variable] object. |
| 597 Map toMap() => { | 614 Map toMap() => { |
| 598 'name': name, | 615 'name': name, |
| 599 'qualifiedname': qualifiedName, | 616 'qualifiedname': qualifiedName, |
| 600 'comment': comment, | 617 'comment': comment, |
| 601 'final': isFinal.toString(), | 618 'final': isFinal.toString(), |
| 602 'static': isStatic.toString(), | 619 'static': isStatic.toString(), |
| 603 'constant': isConst.toString(), | 620 'constant': isConst.toString(), |
| 604 'type': type, | 621 'type': new List.filled(1, type.toMap()), |
| 605 'annotations': new List.from(annotations) | 622 'annotations': new List.from(annotations) |
| 606 }; | 623 }; |
| 607 } | 624 } |
| 608 | 625 |
| 609 /** | 626 /** |
| 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 Type 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 |
| 649 String name; | 666 String name; |
| 650 bool isOptional; | 667 bool isOptional; |
| 651 bool isNamed; | 668 bool isNamed; |
| 652 bool hasDefaultValue; | 669 bool hasDefaultValue; |
| 653 String type; | 670 Type type; |
| 654 String defaultValue; | 671 String defaultValue; |
| 655 | 672 |
| 656 /// List of the meta annotations on the parameter. | 673 /// List of the meta annotations on the parameter. |
| 657 List<String> annotations; | 674 List<String> annotations; |
| 658 | 675 |
| 659 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, | 676 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, |
| 660 this.type, this.defaultValue, this.annotations); | 677 this.type, this.defaultValue, this.annotations); |
| 661 | 678 |
| 662 /// Generates a map describing the [Parameter] object. | 679 /// Generates a map describing the [Parameter] object. |
| 663 Map toMap() => { | 680 Map toMap() => { |
| 664 'name': name, | 681 'name': name, |
| 665 'optional': isOptional.toString(), | 682 'optional': isOptional.toString(), |
| 666 'named': isNamed.toString(), | 683 'named': isNamed.toString(), |
| 667 'default': hasDefaultValue.toString(), | 684 'default': hasDefaultValue.toString(), |
| 668 'type': type, | 685 'type': new List.filled(1, type.toMap()), |
| 669 'value': defaultValue, | 686 'value': defaultValue, |
| 670 'annotations': new List.from(annotations) | 687 'annotations': new List.from(annotations) |
| 671 }; | 688 }; |
| 672 } | 689 } |
| 673 | 690 |
| 674 /** | 691 /** |
| 675 * A class containing properties of a Generic. | 692 * A class containing properties of a Generic. |
| 676 */ | 693 */ |
| 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 * Holds the name of a return type, and its generic type parameters. | |
| 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" : | |
|
Emily Fortuna
2013/07/25 17:38:08
can your example show an inner part, too?
janicejl
2013/07/25 21:39:57
Done.
| |
| 724 */ | |
| 725 class Type { | |
| 726 String outer; | |
| 727 List<Type> inner; | |
| 728 | |
| 729 Type(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 |