Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /** | 1 /** |
|
Emily Fortuna
2013/06/13 20:11:44
add copyright notice at the top of this file, plea
Tate Mandel
2013/06/13 20:24:56
Done.
| |
| 2 * The docgen tool takes in a library as input and produces documentation | 2 * The docgen tool takes in a library as input and produces documentation |
| 3 * for the library as well as all libraries it imports and uses. The tool can | 3 * for the library as well as all libraries it imports and uses. The tool can |
| 4 * be run by passing in the path to a .dart file like this: | 4 * be run by passing in the path to a .dart file like this: |
| 5 * | 5 * |
| 6 * ./dart docgen.dart path/to/file.dart | 6 * ./dart docgen.dart path/to/file.dart |
| 7 * | 7 * |
| 8 * This outputs information about all classes, variables, functions, and | 8 * This outputs information about all classes, variables, functions, and |
| 9 * methods defined in the library and its imported libraries. | 9 * methods defined in the library and its imported libraries. |
| 10 */ | 10 */ |
| 11 library docgen; | 11 library docgen; |
| 12 | 12 |
| 13 // TODO(tmandel): Use 'package:' references for imports with relative paths. | |
| 13 import 'dart:io'; | 14 import 'dart:io'; |
| 15 import 'dart:json'; | |
| 14 import 'dart:async'; | 16 import 'dart:async'; |
| 15 import '../lib/dart2yaml.dart'; | 17 import '../lib/dart2yaml.dart'; |
| 16 import '../lib/src/dart2js_mirrors.dart'; | 18 import '../lib/src/dart2js_mirrors.dart'; |
| 17 import 'package:markdown/markdown.dart' as markdown; | 19 import 'package:markdown/markdown.dart' as markdown; |
| 18 import '../../args/lib/args.dart'; | 20 import '../../args/lib/args.dart'; |
| 19 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ; | 21 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ; |
| 20 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. dart'; | 22 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. dart'; |
| 21 | 23 |
| 22 /** | 24 /** |
| 23 * Entry function to create YAML documentation from Dart files. | 25 * Entry function to create YAML documentation from Dart files. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 | 59 |
| 58 /// Current library being documented to be used for comment links. | 60 /// Current library being documented to be used for comment links. |
| 59 LibraryMirror _currentLibrary; | 61 LibraryMirror _currentLibrary; |
| 60 | 62 |
| 61 /// Current class being documented to be used for comment links. | 63 /// Current class being documented to be used for comment links. |
| 62 ClassMirror _currentClass; | 64 ClassMirror _currentClass; |
| 63 | 65 |
| 64 /// Current member being documented to be used for comment links. | 66 /// Current member being documented to be used for comment links. |
| 65 MemberMirror _currentMember; | 67 MemberMirror _currentMember; |
| 66 | 68 |
| 69 /// Should the output file type be JSON? | |
| 70 // TODO(tmandel): Add flag to allow for output to JSON. | |
| 71 bool outputToJson = false; | |
| 72 | |
| 73 /// Resolves reference links | |
| 74 markdown.Resolver linkResolver; | |
| 75 | |
| 76 /** | |
| 77 * Docgen constructor initializes the link resolver for markdown parsing. | |
| 78 */ | |
| 79 Docgen() { | |
| 80 this.linkResolver = (name) => | |
| 81 fixReference(name, _currentLibrary, _currentClass, _currentMember); | |
| 82 } | |
| 83 | |
| 67 /** | 84 /** |
| 68 * Creates documentation for filtered libraries. | 85 * Creates documentation for filtered libraries. |
| 69 */ | 86 */ |
| 70 void documentLibraries() { | 87 void documentLibraries() { |
| 71 //TODO(tmandel): Filter libraries and determine output type using flags. | 88 //TODO(tmandel): Filter libraries and determine output type using flags. |
| 72 _libraries.forEach((library) { | 89 _libraries.forEach((library) { |
| 73 _currentLibrary = library; | 90 _currentLibrary = library; |
| 74 var result = new Library(library.qualifiedName, _getComment(library), | 91 var result = new Library(library.qualifiedName, _getComment(library), |
| 75 _getVariables(library.variables), _getMethods(library.functions), | 92 _getVariables(library.variables), _getMethods(library.functions), |
| 76 _getClasses(library.classes)); | 93 _getClasses(library.classes)); |
| 77 _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); | 94 if (outputToJson) { |
| 95 _writeToFile(stringify(result.toMap()), "${result.name}.json"); | |
| 96 } else { | |
| 97 _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); | |
| 98 } | |
| 78 }); | 99 }); |
| 79 } | 100 } |
| 80 | 101 |
| 81 /** | 102 /** |
| 82 * Returns any documentation comments associated with a mirror with | 103 * Returns any documentation comments associated with a mirror with |
| 83 * simple markdown converted to html. | 104 * simple markdown converted to html. |
| 84 */ | 105 */ |
| 85 String _getComment(DeclarationMirror mirror) { | 106 String _getComment(DeclarationMirror mirror) { |
| 86 String commentText; | 107 String commentText; |
| 87 mirror.metadata.forEach((metadata) { | 108 mirror.metadata.forEach((metadata) { |
| 88 if (metadata is CommentInstanceMirror) { | 109 if (metadata is CommentInstanceMirror) { |
| 89 CommentInstanceMirror comment = metadata; | 110 CommentInstanceMirror comment = metadata; |
| 90 if (comment.isDocComment) { | 111 if (comment.isDocComment) { |
| 91 if (commentText == null) { | 112 if (commentText == null) { |
| 92 commentText = comment.trimmedText; | 113 commentText = comment.trimmedText; |
| 93 } else { | 114 } else { |
| 94 commentText = "$commentText ${comment.trimmedText}"; | 115 commentText = "$commentText ${comment.trimmedText}"; |
| 95 } | 116 } |
| 96 } | 117 } |
| 97 } | 118 } |
| 98 }); | 119 }); |
| 99 // TODO(tmandel): Resolve links to members in markdown using _currentClass, | |
| 100 // _currentMember, and _currentLibrary. | |
| 101 return commentText == null ? "" : | 120 return commentText == null ? "" : |
| 102 markdown.markdownToHtml(commentText.trim()); | 121 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver); |
| 122 } | |
| 123 | |
| 124 /** | |
| 125 * Converts all [_] references in comments to <code>_</code>. | |
| 126 */ | |
| 127 // TODO(tmandel): Create proper links for [_] style markdown based | |
| 128 // on scope once layout of viewer is finished. | |
| 129 markdown.Node fixReference(String name, LibraryMirror currentLibrary, | |
| 130 ClassMirror currentClass, MemberMirror currentMember) { | |
| 131 return new markdown.Element.text('code', name); | |
| 103 } | 132 } |
| 104 | 133 |
| 105 /** | 134 /** |
| 106 * Returns a map of [Variable] objects constructed from inputted mirrors. | 135 * Returns a map of [Variable] objects constructed from inputted mirrors. |
| 107 */ | 136 */ |
| 108 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { | 137 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { |
| 109 var data = {}; | 138 var data = {}; |
| 110 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { | 139 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { |
| 111 _currentMember = mirror; | 140 _currentMember = mirror; |
| 112 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, | 141 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 | 264 |
| 236 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, | 265 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, |
| 237 this.comment, this.interfaces, this.variables, this.methods); | 266 this.comment, this.interfaces, this.variables, this.methods); |
| 238 | 267 |
| 239 /// Generates a map describing the [Class] object. | 268 /// Generates a map describing the [Class] object. |
| 240 Map toMap() { | 269 Map toMap() { |
| 241 var classMap = {}; | 270 var classMap = {}; |
| 242 classMap["name"] = name; | 271 classMap["name"] = name; |
| 243 classMap["comment"] = comment; | 272 classMap["comment"] = comment; |
| 244 classMap["superclass"] = superclass; | 273 classMap["superclass"] = superclass; |
| 245 classMap["abstract"] = isAbstract; | 274 classMap["abstract"] = isAbstract.toString(); |
| 246 classMap["typedef"] = isTypedef; | 275 classMap["typedef"] = isTypedef.toString(); |
| 247 classMap["implements"] = interfaces; | 276 classMap["implements"] = new List.from(interfaces); |
| 248 classMap["variables"] = recurseMap(variables); | 277 classMap["variables"] = recurseMap(variables); |
| 249 classMap["methods"] = recurseMap(methods); | 278 classMap["methods"] = recurseMap(methods); |
| 250 return classMap; | 279 return classMap; |
| 251 } | 280 } |
| 252 } | 281 } |
| 253 | 282 |
| 254 /** | 283 /** |
| 255 * A class containing properties of a Dart variable. | 284 * A class containing properties of a Dart variable. |
| 256 */ | 285 */ |
| 257 class Variable { | 286 class Variable { |
| 258 | 287 |
| 259 /// Documentation comment with converted markdown. | 288 /// Documentation comment with converted markdown. |
| 260 String comment; | 289 String comment; |
| 261 | 290 |
| 262 String name; | 291 String name; |
| 263 bool isFinal; | 292 bool isFinal; |
| 264 bool isStatic; | 293 bool isStatic; |
| 265 String type; | 294 String type; |
| 266 | 295 |
| 267 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment); | 296 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment); |
| 268 | 297 |
| 269 /// Generates a map describing the [Variable] object. | 298 /// Generates a map describing the [Variable] object. |
| 270 Map toMap() { | 299 Map toMap() { |
| 271 var variableMap = {}; | 300 var variableMap = {}; |
| 272 variableMap["name"] = name; | 301 variableMap["name"] = name; |
| 273 variableMap["comment"] = comment; | 302 variableMap["comment"] = comment; |
| 274 variableMap["final"] = isFinal; | 303 variableMap["final"] = isFinal.toString(); |
| 275 variableMap["static"] = isStatic; | 304 variableMap["static"] = isStatic.toString(); |
| 276 variableMap["type"] = type; | 305 variableMap["type"] = type; |
| 277 return variableMap; | 306 return variableMap; |
| 278 } | 307 } |
| 279 } | 308 } |
| 280 | 309 |
| 281 /** | 310 /** |
| 282 * A class containing properties of a Dart method. | 311 * A class containing properties of a Dart method. |
| 283 */ | 312 */ |
| 284 class Method { | 313 class Method { |
| 285 | 314 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 299 | 328 |
| 300 Method(this.name, this.isSetter, this.isGetter, this.isConstructor, | 329 Method(this.name, this.isSetter, this.isGetter, this.isConstructor, |
| 301 this.isOperator, this.isStatic, this.returnType, this.comment, | 330 this.isOperator, this.isStatic, this.returnType, this.comment, |
| 302 this.parameters); | 331 this.parameters); |
| 303 | 332 |
| 304 /// Generates a map describing the [Method] object. | 333 /// Generates a map describing the [Method] object. |
| 305 Map toMap() { | 334 Map toMap() { |
| 306 var methodMap = {}; | 335 var methodMap = {}; |
| 307 methodMap["name"] = name; | 336 methodMap["name"] = name; |
| 308 methodMap["comment"] = comment; | 337 methodMap["comment"] = comment; |
| 309 methodMap["type"] = isSetter ? "Setter" : isGetter ? "Getter" : | 338 methodMap["type"] = isSetter ? "setter" : isGetter ? "getter" : |
| 310 isOperator ? "Operator" : isConstructor ? "Constructor" : "Method"; | 339 isOperator ? "operator" : isConstructor ? "constructor" : "method"; |
| 311 methodMap["static"] = isStatic; | 340 methodMap["static"] = isStatic.toString(); |
| 312 methodMap["return"] = returnType; | 341 methodMap["return"] = returnType; |
| 313 methodMap["parameters"] = recurseMap(parameters); | 342 methodMap["parameters"] = recurseMap(parameters); |
| 314 return methodMap; | 343 return methodMap; |
| 315 } | 344 } |
| 316 } | 345 } |
| 317 | 346 |
| 318 /** | 347 /** |
| 319 * A class containing properties of a Dart method/function parameter. | 348 * A class containing properties of a Dart method/function parameter. |
| 320 */ | 349 */ |
| 321 class Parameter { | 350 class Parameter { |
| 322 | 351 |
| 323 String name; | 352 String name; |
| 324 bool isOptional; | 353 bool isOptional; |
| 325 bool isNamed; | 354 bool isNamed; |
| 326 bool hasDefaultValue; | 355 bool hasDefaultValue; |
| 327 String type; | 356 String type; |
| 328 String defaultValue; | 357 String defaultValue; |
| 329 | 358 |
| 330 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, | 359 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, |
| 331 this.type, this.defaultValue); | 360 this.type, this.defaultValue); |
| 332 | 361 |
| 333 /// Generates a map describing the [Parameter] object. | 362 /// Generates a map describing the [Parameter] object. |
| 334 Map toMap() { | 363 Map toMap() { |
| 335 var parameterMap = {}; | 364 var parameterMap = {}; |
| 336 parameterMap["name"] = name; | 365 parameterMap["name"] = name; |
| 337 parameterMap["optional"] = isOptional; | 366 parameterMap["optional"] = isOptional.toString(); |
| 338 parameterMap["default"] = hasDefaultValue; | 367 parameterMap["named"] = isNamed.toString(); |
| 368 parameterMap["default"] = hasDefaultValue.toString(); | |
| 339 parameterMap["type"] = type; | 369 parameterMap["type"] = type; |
| 340 parameterMap["value"] = defaultValue; | 370 parameterMap["value"] = defaultValue; |
| 341 return parameterMap; | 371 return parameterMap; |
| 342 } | 372 } |
| 343 } | 373 } |
| 344 | 374 |
| 345 /** | 375 /** |
| 346 * Writes text to a file in the 'docs' directory. | 376 * Writes text to a file in the 'docs' directory. |
| 347 */ | 377 */ |
| 348 void _writeToFile(String text, String filename) { | 378 void _writeToFile(String text, String filename) { |
| 349 Directory dir = new Directory('docs'); | 379 Directory dir = new Directory('docs'); |
| 350 if (!dir.existsSync()) { | 380 if (!dir.existsSync()) { |
| 351 dir.createSync(); | 381 dir.createSync(); |
| 352 } | 382 } |
| 353 File file = new File('docs/$filename'); | 383 File file = new File('docs/$filename'); |
| 354 if (!file.exists()) { | 384 if (!file.exists()) { |
| 355 file.createSync(); | 385 file.createSync(); |
| 356 } | 386 } |
| 357 file.openSync(); | 387 file.openSync(); |
| 358 file.writeAsString(text); | 388 file.writeAsString(text); |
| 359 } | 389 } |
| OLD | NEW |