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 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 * Returns a map of [Class] objects constructed from inputted mirrors. | 361 * Returns a map of [Class] objects constructed from inputted mirrors. |
| 362 */ | 362 */ |
| 363 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap, | 363 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap, |
| 364 bool includePrivate) { | 364 bool includePrivate) { |
| 365 | 365 |
| 366 var abstractClasses = {}; | 366 var abstractClasses = {}; |
| 367 var classes = {}; | 367 var classes = {}; |
| 368 var typedefs = {}; | 368 var typedefs = {}; |
| 369 var errors = {}; | 369 var errors = {}; |
| 370 | 370 |
| 371 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { | 371 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { |
| 372 if (includePrivate || !mirror.isPrivate) { | 372 if (includePrivate || !mirror.isPrivate) { |
| 373 var superclass = (mirror.superclass != null) ? | 373 var superclass = (mirror.superclass != null) ? |
| 374 mirror.superclass.qualifiedName : ''; | 374 mirror.superclass.qualifiedName : ''; |
| 375 var interfaces = | 375 var interfaces = |
| 376 mirror.superinterfaces.map((interface) => interface.qualifiedName); | 376 mirror.superinterfaces.map((interface) => interface.qualifiedName); |
| 377 var clazz = new Class(mirrorName, superclass, _getComment(mirror), | 377 var clazz = new Class(mirrorName, superclass, _getComment(mirror), |
| 378 interfaces.toList(), _getVariables(mirror.variables, includePrivate), | 378 interfaces.toList(), _getVariables(mirror.variables, includePrivate), |
| 379 _getMethods(mirror.methods, includePrivate), | 379 _getMethods(mirror.methods, includePrivate), |
| 380 _getAnnotations(mirror), mirror.qualifiedName); | 380 _getAnnotations(mirror), _getGenerics(mirror), mirror.qualifiedName); |
| 381 _currentClass = mirror; | 381 _currentClass = mirror; |
| 382 | 382 |
| 383 if (isError(mirror.qualifiedName)) { | 383 if (isError(mirror.qualifiedName)) { |
| 384 errors[mirrorName] = clazz; | 384 errors[mirrorName] = clazz; |
| 385 } else if (mirror.isTypedef) { | 385 } else if (mirror.isTypedef) { |
| 386 typedefs[mirrorName] = clazz; | 386 typedefs[mirrorName] = clazz; |
| 387 } else if (mirror.isAbstract) { | 387 } else if (mirror.isAbstract) { |
| 388 abstractClasses[mirrorName] = clazz; | 388 abstractClasses[mirrorName] = clazz; |
| 389 } else if (mirror.isClass) { | 389 } else if (mirror.isClass) { |
| 390 classes[mirrorName] = clazz; | 390 classes[mirrorName] = clazz; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 410 _currentMember = mirror; | 410 _currentMember = mirror; |
| 411 data[mirror.simpleName] = new Parameter(mirror.simpleName, | 411 data[mirror.simpleName] = new Parameter(mirror.simpleName, |
| 412 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, | 412 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, |
| 413 mirror.type.qualifiedName, mirror.defaultValue, | 413 mirror.type.qualifiedName, mirror.defaultValue, |
| 414 _getAnnotations(mirror)); | 414 _getAnnotations(mirror)); |
| 415 }); | 415 }); |
| 416 return data; | 416 return data; |
| 417 } | 417 } |
| 418 | 418 |
| 419 /** | 419 /** |
| 420 * Returns a map of [Generic] objects constructed from the class mirror. | |
| 421 */ | |
| 422 Map<String, Generic> _getGenerics(ClassMirror mirror) { | |
| 423 var data = {}; | |
|
Alan Knight
2013/07/18 22:20:46
Today I learned... you could write this as
new Map
janicejl
2013/07/19 01:12:22
Done. That's really neat. Thanks for letting me kn
| |
| 424 mirror.typeVariables.forEach((e) { | |
| 425 data[e.toString()] = new Generic(e.toString(), e.upperBound.qualifiedName); | |
| 426 }); | |
| 427 return data; | |
| 428 } | |
| 429 | |
| 430 /** | |
| 420 * Writes text to a file in the 'docs' directory. | 431 * Writes text to a file in the 'docs' directory. |
| 421 */ | 432 */ |
| 422 void _writeToFile(String text, String filename) { | 433 void _writeToFile(String text, String filename) { |
| 423 Directory dir = new Directory('docs'); | 434 Directory dir = new Directory('docs'); |
| 424 if (!dir.existsSync()) { | 435 if (!dir.existsSync()) { |
| 425 dir.createSync(); | 436 dir.createSync(); |
| 426 } | 437 } |
| 427 File file = new File('docs/$filename'); | 438 File file = new File('docs/$filename'); |
| 428 if (!file.existsSync()) { | 439 if (!file.existsSync()) { |
| 429 file.createSync(); | 440 file.createSync(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 508 | 519 |
| 509 /// List of the names of interfaces that this class implements. | 520 /// List of the names of interfaces that this class implements. |
| 510 List<String> interfaces; | 521 List<String> interfaces; |
| 511 | 522 |
| 512 /// Top-level variables in the class. | 523 /// Top-level variables in the class. |
| 513 Map<String, Variable> variables; | 524 Map<String, Variable> variables; |
| 514 | 525 |
| 515 /// Methods in the class. | 526 /// Methods in the class. |
| 516 Map<String, Map<String, Method>> methods; | 527 Map<String, Map<String, Method>> methods; |
| 517 | 528 |
| 529 /// Generic infomation about the class. | |
| 530 Map<String, Generic> generics; | |
| 531 | |
| 518 String name; | 532 String name; |
| 519 String superclass; | 533 String superclass; |
| 520 | 534 |
| 521 /// List of the meta annotations on the class. | 535 /// List of the meta annotations on the class. |
| 522 List<String> annotations; | 536 List<String> annotations; |
| 523 | 537 |
| 524 Class(this.name, this.superclass, this.comment, this.interfaces, | 538 Class(this.name, this.superclass, this.comment, this.interfaces, |
| 525 this.variables, this.methods, this.annotations, | 539 this.variables, this.methods, this.annotations, this.generics, |
| 526 String qualifiedName) : super(qualifiedName) {} | 540 String qualifiedName) : super(qualifiedName) {} |
| 527 | 541 |
| 528 /// Generates a map describing the [Class] object. | 542 /// Generates a map describing the [Class] object. |
| 529 Map toMap() { | 543 Map toMap() { |
| 530 var classMap = {}; | 544 var classMap = {}; |
| 531 classMap['name'] = name; | 545 classMap['name'] = name; |
| 532 classMap['comment'] = comment; | 546 classMap['comment'] = comment; |
| 533 classMap['superclass'] = superclass; | 547 classMap['superclass'] = superclass; |
| 534 classMap['implements'] = new List.from(interfaces); | 548 classMap['implements'] = new List.from(interfaces); |
| 535 classMap['variables'] = recurseMap(variables); | 549 classMap['variables'] = recurseMap(variables); |
| 536 classMap['methods'] = recurseMap(methods); | 550 classMap['methods'] = recurseMap(methods); |
| 537 classMap['annotations'] = new List.from(annotations); | 551 classMap['annotations'] = new List.from(annotations); |
| 552 classMap['generics'] = recurseMap(generics); | |
| 538 return classMap; | 553 return classMap; |
| 539 } | 554 } |
| 540 } | 555 } |
| 541 | 556 |
| 542 /** | 557 /** |
| 543 * A class containing properties of a Dart variable. | 558 * A class containing properties of a Dart variable. |
| 544 */ | 559 */ |
| 545 class Variable extends Indexable { | 560 class Variable extends Indexable { |
| 546 | 561 |
| 547 /// Documentation comment with converted markdown. | 562 /// Documentation comment with converted markdown. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 630 parameterMap['name'] = name; | 645 parameterMap['name'] = name; |
| 631 parameterMap['optional'] = isOptional.toString(); | 646 parameterMap['optional'] = isOptional.toString(); |
| 632 parameterMap['named'] = isNamed.toString(); | 647 parameterMap['named'] = isNamed.toString(); |
| 633 parameterMap['default'] = hasDefaultValue.toString(); | 648 parameterMap['default'] = hasDefaultValue.toString(); |
| 634 parameterMap['type'] = type; | 649 parameterMap['type'] = type; |
| 635 parameterMap['value'] = defaultValue; | 650 parameterMap['value'] = defaultValue; |
| 636 parameterMap['annotations'] = new List.from(annotations); | 651 parameterMap['annotations'] = new List.from(annotations); |
| 637 return parameterMap; | 652 return parameterMap; |
| 638 } | 653 } |
| 639 } | 654 } |
| 655 | |
| 656 /** | |
| 657 * A class containing properties of a Generic. | |
| 658 */ | |
| 659 class Generic { | |
| 660 String name; | |
| 661 String type; | |
| 662 | |
| 663 Generic(this.name, this.type); | |
| 664 | |
| 665 Map toMap() { | |
| 666 var genericMap = {}; | |
| 667 genericMap['name'] = name; | |
|
Alan Knight
2013/07/18 22:20:46
You could write this as
=> {"name" : name, "type"
janicejl
2013/07/19 01:12:22
Done.
| |
| 668 genericMap['type'] = type; | |
| 669 return genericMap; | |
| 670 } | |
| 671 } | |
| OLD | NEW |