| 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 * The docgen tool takes in a library as input and produces documentation | 6 * The docgen tool takes in a library as input and produces documentation |
| 7 * for the library as well as all libraries it imports and uses. The tool can | 7 * for the library as well as all libraries it imports and uses. The tool can |
| 8 * be run by passing in the path to a .dart file like this: | 8 * be run by passing in the path to a .dart file like this: |
| 9 * | 9 * |
| 10 * ./dart docgen.dart path/to/file.dart | 10 * ./dart docgen.dart path/to/file.dart |
| 11 * | 11 * |
| 12 * This outputs information about all classes, variables, functions, and | 12 * This outputs information about all classes, variables, functions, and |
| 13 * methods defined in the library and its imported libraries. | 13 * methods defined in the library and its imported libraries. |
| 14 */ | 14 */ |
| 15 library docgen; | 15 library docgen; |
| 16 | 16 |
| 17 // TODO(tmandel): Use 'package:' references for imports with relative paths. | 17 // TODO(tmandel): Use 'package:' references for imports with relative paths. |
| 18 import 'dart:io'; | 18 import 'dart:io'; |
| 19 import 'dart:json'; | 19 import 'dart:json'; |
| 20 import 'dart:async'; | 20 import 'dart:async'; |
| 21 import '../lib/dart2yaml.dart'; | |
| 22 import '../lib/src/dart2js_mirrors.dart'; | |
| 23 import 'package:markdown/markdown.dart' as markdown; | 21 import 'package:markdown/markdown.dart' as markdown; |
| 24 import '../../args/lib/args.dart'; | 22 import 'package:args/args.dart'; |
| 25 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'
; | 23 import 'dart2yaml.dart'; |
| 26 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.
dart'; | 24 import 'package:compiler_unsupported/implementation/mirrors/dart2js_mirror.dart'
; |
| 25 import 'package:compiler_unsupported/implementation/mirrors/mirrors.dart'; |
| 26 import 'package:compiler_unsupported/implementation/mirrors/mirrors_util.dart'; |
| 27 | 27 |
| 28 /** | 28 /// Unique ID, will get incremented everytime an ID is requested. |
| 29 * Entry function to create YAML documentation from Dart files. | 29 int _uid = 0; |
| 30 */ | 30 |
| 31 void main() { | 31 int getID() => _uid++; |
| 32 // TODO(tmandel): Use args library once flags are clear. | |
| 33 Options opts = new Options(); | |
| 34 Docgen docgen = new Docgen(); | |
| 35 | |
| 36 if (opts.arguments.length > 0) { | |
| 37 List<Path> libraries = [new Path(opts.arguments[0])]; | |
| 38 Path sdkDirectory = new Path("../../../sdk/"); | |
| 39 var workingMirrors = analyze(libraries, sdkDirectory, | |
| 40 options: ['--preserve-comments', '--categories=Client,Server']); | |
| 41 workingMirrors.then( (MirrorSystem mirrorSystem) { | |
| 42 var mirrors = mirrorSystem.libraries.values; | |
| 43 if (mirrors.isEmpty) { | |
| 44 print("no LibraryMirrors"); | |
| 45 } else { | |
| 46 docgen.libraries = mirrors; | |
| 47 docgen.documentLibraries(); | |
| 48 } | |
| 49 }); | |
| 50 } | |
| 51 } | |
| 52 | 32 |
| 53 /** | 33 /** |
| 54 * This class documents a list of libraries. | 34 * This class documents a list of libraries. |
| 55 */ | 35 */ |
| 56 class Docgen { | 36 class Docgen { |
| 57 | 37 |
| 58 /// Libraries to be documented. | 38 /// Libraries to be documented. |
| 59 List<LibraryMirror> _libraries; | 39 List<LibraryMirror> _libraries; |
| 60 | 40 |
| 61 /// Saves list of libraries for Docgen object. | 41 /// Saves list of libraries for Docgen object. |
| 62 void set libraries(value) => _libraries = value; | 42 void set libraries(value) { |
| 43 _libraries = value; |
| 44 } |
| 63 | 45 |
| 64 /// Current library being documented to be used for comment links. | 46 /// Current library being documented to be used for comment links. |
| 65 LibraryMirror _currentLibrary; | 47 LibraryMirror _currentLibrary; |
| 66 | 48 |
| 67 /// Current class being documented to be used for comment links. | 49 /// Current class being documented to be used for comment links. |
| 68 ClassMirror _currentClass; | 50 ClassMirror _currentClass; |
| 69 | 51 |
| 70 /// Current member being documented to be used for comment links. | 52 /// Current member being documented to be used for comment links. |
| 71 MemberMirror _currentMember; | 53 MemberMirror _currentMember; |
| 72 | 54 |
| 73 /// Should the output file type be JSON? | |
| 74 // TODO(tmandel): Add flag to allow for output to JSON. | |
| 75 bool outputToJson = false; | |
| 76 | |
| 77 /// Resolves reference links | 55 /// Resolves reference links |
| 78 markdown.Resolver linkResolver; | 56 markdown.Resolver linkResolver; |
| 79 | 57 |
| 80 /** | 58 /** |
| 81 * Docgen constructor initializes the link resolver for markdown parsing. | 59 * Docgen constructor initializes the link resolver for markdown parsing. |
| 82 */ | 60 */ |
| 83 Docgen() { | 61 Docgen({Path sdkRoot, ArgResults argResults}) { |
| 62 this.sdkRoot = sdkRoot; |
| 63 outputToYaml = argResults["yaml"]; |
| 64 outputToJson = argResults["json"]; |
| 65 hidePrivate = argResults["hide-private"]; |
| 66 sdk = argResults["sdk"]; |
| 67 |
| 84 this.linkResolver = (name) => | 68 this.linkResolver = (name) => |
| 85 fixReference(name, _currentLibrary, _currentClass, _currentMember); | 69 fixReference(name, _currentLibrary, _currentClass, _currentMember); |
| 86 } | 70 } |
| 87 | 71 |
| 72 /// Path to the SDK Libraries. |
| 73 Path sdkRoot; |
| 74 |
| 75 /// Should the output file type be YAML? |
| 76 bool outputToYaml; |
| 77 /// Should the output file type be JSON? |
| 78 bool outputToJson; |
| 79 /// Should the output file hide private declarations? |
| 80 bool hidePrivate; |
| 81 /// Should the output include SDK libraries? |
| 82 bool sdk; |
| 83 |
| 88 /** | 84 /** |
| 89 * Creates documentation for filtered libraries. | 85 * Creates documentation for filtered libraries. |
| 90 */ | 86 */ |
| 91 void documentLibraries() { | 87 void documentLibraries() { |
| 92 //TODO(tmandel): Filter libraries and determine output type using flags. | |
| 93 _libraries.forEach((library) { | 88 _libraries.forEach((library) { |
| 94 _currentLibrary = library; | 89 // Files belong to the SDK have a uri that begins with "dart:". |
| 95 var result = new Library(library.qualifiedName, _getComment(library), | 90 if (sdk || !library.uri.toString().startsWith("dart:")) { |
| 96 _getVariables(library.variables), _getMethods(library.functions), | 91 _currentLibrary = library; |
| 97 _getClasses(library.classes)); | 92 var result = new Library(library.qualifiedName, _getComment(library), |
| 98 if (outputToJson) { | 93 _getVariables(library.variables), _getMethods(library.functions), |
| 99 _writeToFile(stringify(result.toMap()), "${result.name}.json"); | 94 _getClasses(library.classes), getID()); |
| 100 } else { | 95 if (outputToJson) { |
| 101 _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); | 96 _writeToFile(stringify(result.toMap()), "${result.name}.json"); |
| 102 } | 97 } |
| 98 if (outputToYaml) { |
| 99 _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); |
| 100 } |
| 101 } |
| 103 }); | 102 }); |
| 104 } | 103 } |
| 105 | 104 |
| 106 /** | 105 /** |
| 107 * Returns any documentation comments associated with a mirror with | 106 * Returns any documentation comments associated with a mirror with |
| 108 * simple markdown converted to html. | 107 * simple markdown converted to html. |
| 109 */ | 108 */ |
| 110 String _getComment(DeclarationMirror mirror) { | 109 String _getComment(DeclarationMirror mirror) { |
| 111 String commentText; | 110 String commentText; |
| 112 mirror.metadata.forEach((metadata) { | 111 mirror.metadata.forEach((metadata) { |
| 113 if (metadata is CommentInstanceMirror) { | 112 if (metadata is CommentInstanceMirror) { |
| 114 CommentInstanceMirror comment = metadata; | 113 CommentInstanceMirror comment = metadata; |
| 115 if (comment.isDocComment) { | 114 if (comment.isDocComment) { |
| 116 if (commentText == null) { | 115 if (commentText == null) { |
| 117 commentText = comment.trimmedText; | 116 commentText = comment.trimmedText; |
| 118 } else { | 117 } else { |
| 119 commentText = "$commentText ${comment.trimmedText}"; | 118 commentText = "$commentText ${comment.trimmedText}"; |
| 120 } | 119 } |
| 121 } | 120 } |
| 122 } | 121 } |
| 123 }); | 122 }); |
| 124 return commentText == null ? "" : | 123 commentText = commentText == null ? "" : |
| 125 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver); | 124 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver); |
| 125 return commentText.replaceAll("\n", "<br/>"); |
| 126 } | 126 } |
| 127 | 127 |
| 128 /** | 128 /** |
| 129 * Converts all [_] references in comments to <code>_</code>. | 129 * Converts all [_] references in comments to <code>_</code>. |
| 130 */ | 130 */ |
| 131 // TODO(tmandel): Create proper links for [_] style markdown based | 131 // TODO(tmandel): Create proper links for [_] style markdown based |
| 132 // on scope once layout of viewer is finished. | 132 // on scope once layout of viewer is finished. |
| 133 markdown.Node fixReference(String name, LibraryMirror currentLibrary, | 133 markdown.Node fixReference(String name, LibraryMirror currentLibrary, |
| 134 ClassMirror currentClass, MemberMirror currentMember) { | 134 ClassMirror currentClass, MemberMirror currentMember) { |
| 135 return new markdown.Element.text('code', name); | 135 return new markdown.Element.text('code', name); |
| 136 } | 136 } |
| 137 | 137 |
| 138 /** | 138 /** |
| 139 * Returns a map of [Variable] objects constructed from inputted mirrors. | 139 * Returns a map of [Variable] objects constructed from inputted mirrors. |
| 140 */ | 140 */ |
| 141 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { | 141 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { |
| 142 var data = {}; | 142 var data = {}; |
| 143 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { | 143 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { |
| 144 _currentMember = mirror; | 144 if (!hidePrivate || !mirror.isPrivate) { |
| 145 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, | 145 _currentMember = mirror; |
| 146 mirror.isStatic, mirror.type.toString(), _getComment(mirror)); | 146 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, |
| 147 mirror.isStatic, mirror.type.toString(), _getComment(mirror), |
| 148 getID()); |
| 149 } |
| 147 }); | 150 }); |
| 148 return data; | 151 return data; |
| 149 } | 152 } |
| 150 | 153 |
| 151 /** | 154 /** |
| 152 * Returns a map of [Method] objects constructed from inputted mirrors. | 155 * Returns a map of [Method] objects constructed from inputted mirrors. |
| 153 */ | 156 */ |
| 154 Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { | 157 Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { |
| 155 var data = {}; | 158 var data = {}; |
| 156 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { | 159 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { |
| 157 _currentMember = mirror; | 160 if (!hidePrivate || !mirror.isPrivate) { |
| 158 data[mirrorName] = new Method(mirrorName, mirror.isSetter, | 161 _currentMember = mirror; |
| 159 mirror.isGetter, mirror.isConstructor, mirror.isOperator, | 162 data[mirrorName] = new Method(mirrorName, mirror.isSetter, |
| 160 mirror.isStatic, mirror.returnType.toString(), _getComment(mirror), | 163 mirror.isGetter, mirror.isConstructor, mirror.isOperator, |
| 161 _getParameters(mirror.parameters)); | 164 mirror.isStatic, mirror.returnType.toString(), _getComment(mirror), |
| 165 _getParameters(mirror.parameters), getID()); |
| 166 } |
| 162 }); | 167 }); |
| 163 return data; | 168 return data; |
| 164 } | 169 } |
| 165 | 170 |
| 166 /** | 171 /** |
| 167 * Returns a map of [Class] objects constructed from inputted mirrors. | 172 * Returns a map of [Class] objects constructed from inputted mirrors. |
| 168 */ | 173 */ |
| 169 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { | 174 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { |
| 170 var data = {}; | 175 var data = {}; |
| 171 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { | 176 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { |
| 172 _currentClass = mirror; | 177 if (!hidePrivate || !mirror.isPrivate) { |
| 173 var superclass; | 178 _currentClass = mirror; |
| 174 if (mirror.superclass != null) { | 179 var superclass = (mirror.superclass != null) ? |
| 175 superclass = mirror.superclass.qualifiedName; | 180 mirror.superclass.qualifiedName : ""; |
| 181 var interfaces = |
| 182 mirror.superinterfaces.map((interface) => interface.qualifiedName); |
| 183 data[mirrorName] = new Class(mirrorName, superclass, mirror.isAbstract, |
| 184 mirror.isTypedef, _getComment(mirror), interfaces.toList(), |
| 185 _getVariables(mirror.variables), _getMethods(mirror.methods), |
| 186 getID()); |
| 176 } | 187 } |
| 177 var interfaces = | |
| 178 mirror.superinterfaces.map((interface) => interface.qualifiedName); | |
| 179 data[mirrorName] = new Class(mirrorName, superclass, mirror.isAbstract, | |
| 180 mirror.isTypedef, _getComment(mirror), interfaces, | |
| 181 _getVariables(mirror.variables), _getMethods(mirror.methods)); | |
| 182 }); | 188 }); |
| 183 return data; | 189 return data; |
| 184 } | 190 } |
| 185 | 191 |
| 186 /** | 192 /** |
| 187 * Returns a map of [Parameter] objects constructed from inputted mirrors. | 193 * Returns a map of [Parameter] objects constructed from inputted mirrors. |
| 188 */ | 194 */ |
| 189 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { | 195 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { |
| 190 var data = {}; | 196 var data = {}; |
| 191 mirrorList.forEach((ParameterMirror mirror) { | 197 mirrorList.forEach((ParameterMirror mirror) { |
| 192 _currentMember = mirror; | 198 _currentMember = mirror; |
| 193 data[mirror.simpleName] = new Parameter(mirror.simpleName, | 199 data[mirror.simpleName] = new Parameter(mirror.simpleName, |
| 194 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, | 200 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, |
| 195 mirror.type.toString(), mirror.defaultValue); | 201 mirror.type.toString(), mirror.defaultValue, getID()); |
| 196 }); | 202 }); |
| 197 return data; | 203 return data; |
| 198 } | 204 } |
| 199 } | 205 } |
| 200 | 206 |
| 201 /** | 207 /** |
| 202 * Transforms the map by calling toMap on each value in it. | 208 * Transforms the map by calling toMap on each value in it. |
| 203 */ | 209 */ |
| 204 Map recurseMap(Map inputMap) { | 210 Map recurseMap(Map inputMap) { |
| 205 var outputMap = {}; | 211 var outputMap = {}; |
| 206 inputMap.forEach((key, value) { | 212 inputMap.forEach((key, value) { |
| 207 outputMap[key] = value.toMap(); | 213 outputMap[key] = value.toMap(); |
| 208 }); | 214 }); |
| 209 return outputMap; | 215 return outputMap; |
| 210 } | 216 } |
| 211 | 217 |
| 212 /** | 218 /** |
| 213 * A class containing contents of a Dart library. | 219 * A class containing contents of a Dart library. |
| 214 */ | 220 */ |
| 215 class Library { | 221 class Library { |
| 216 | 222 |
| 223 /// Unique ID number for resolving links. |
| 224 int id; |
| 225 |
| 217 /// Documentation comment with converted markdown. | 226 /// Documentation comment with converted markdown. |
| 218 String comment; | 227 String comment; |
| 219 | 228 |
| 220 /// Top-level variables in the library. | 229 /// Top-level variables in the library. |
| 221 Map<String, Variable> variables; | 230 Map<String, Variable> variables; |
| 222 | 231 |
| 223 /// Top-level functions in the library. | 232 /// Top-level functions in the library. |
| 224 Map<String, Method> functions; | 233 Map<String, Method> functions; |
| 225 | 234 |
| 226 /// Classes defined within the library | 235 /// Classes defined within the library |
| 227 Map<String, Class> classes; | 236 Map<String, Class> classes; |
| 228 | 237 |
| 229 String name; | 238 String name; |
| 230 | 239 |
| 231 Library(this.name, this.comment, this.variables, | 240 Library(this.name, this.comment, this.variables, |
| 232 this.functions, this.classes); | 241 this.functions, this.classes, this.id); |
| 233 | 242 |
| 234 /// Generates a map describing the [Library] object. | 243 /// Generates a map describing the [Library] object. |
| 235 Map toMap() { | 244 Map toMap() { |
| 236 var libraryMap = {}; | 245 var libraryMap = {}; |
| 246 libraryMap["id"] = id; |
| 237 libraryMap["name"] = name; | 247 libraryMap["name"] = name; |
| 238 libraryMap["comment"] = comment; | 248 libraryMap["comment"] = comment; |
| 239 libraryMap["variables"] = recurseMap(variables); | 249 libraryMap["variables"] = recurseMap(variables); |
| 240 libraryMap["functions"] = recurseMap(functions); | 250 libraryMap["functions"] = recurseMap(functions); |
| 241 libraryMap["classes"] = recurseMap(classes); | 251 libraryMap["classes"] = recurseMap(classes); |
| 242 return libraryMap; | 252 return libraryMap; |
| 243 } | 253 } |
| 244 } | 254 } |
| 245 | 255 |
| 246 /** | 256 /** |
| 247 * A class containing contents of a Dart class. | 257 * A class containing contents of a Dart class. |
| 248 */ | 258 */ |
| 249 // TODO(tmandel): Figure out how to do typedefs (what is needed) | 259 // TODO(tmandel): Figure out how to do typedefs (what is needed) |
| 250 class Class { | 260 class Class { |
| 251 | 261 |
| 262 /// Unique ID number for resolving links. |
| 263 int id; |
| 264 |
| 252 /// Documentation comment with converted markdown. | 265 /// Documentation comment with converted markdown. |
| 253 String comment; | 266 String comment; |
| 254 | 267 |
| 255 /// List of the names of interfaces that this class implements. | 268 /// List of the names of interfaces that this class implements. |
| 256 List<String> interfaces; | 269 List<String> interfaces; |
| 257 | 270 |
| 258 /// Top-level variables in the class. | 271 /// Top-level variables in the class. |
| 259 Map<String, Variable> variables; | 272 Map<String, Variable> variables; |
| 260 | 273 |
| 261 /// Methods in the class. | 274 /// Methods in the class. |
| 262 Map<String, Method> methods; | 275 Map<String, Method> methods; |
| 263 | 276 |
| 264 String name; | 277 String name; |
| 265 String superclass; | 278 String superclass; |
| 266 bool isAbstract; | 279 bool isAbstract; |
| 267 bool isTypedef; | 280 bool isTypedef; |
| 268 | 281 |
| 269 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, | 282 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, |
| 270 this.comment, this.interfaces, this.variables, this.methods); | 283 this.comment, this.interfaces, this.variables, this.methods, this.id); |
| 271 | 284 |
| 272 /// Generates a map describing the [Class] object. | 285 /// Generates a map describing the [Class] object. |
| 273 Map toMap() { | 286 Map toMap() { |
| 274 var classMap = {}; | 287 var classMap = {}; |
| 288 classMap["id"] = id; |
| 275 classMap["name"] = name; | 289 classMap["name"] = name; |
| 276 classMap["comment"] = comment; | 290 classMap["comment"] = comment; |
| 277 classMap["superclass"] = superclass; | 291 classMap["superclass"] = superclass; |
| 278 classMap["abstract"] = isAbstract.toString(); | 292 classMap["abstract"] = isAbstract.toString(); |
| 279 classMap["typedef"] = isTypedef.toString(); | 293 classMap["typedef"] = isTypedef.toString(); |
| 280 classMap["implements"] = new List.from(interfaces); | 294 classMap["implements"] = new List.from(interfaces); |
| 281 classMap["variables"] = recurseMap(variables); | 295 classMap["variables"] = recurseMap(variables); |
| 282 classMap["methods"] = recurseMap(methods); | 296 classMap["methods"] = recurseMap(methods); |
| 283 return classMap; | 297 return classMap; |
| 284 } | 298 } |
| 285 } | 299 } |
| 286 | 300 |
| 287 /** | 301 /** |
| 288 * A class containing properties of a Dart variable. | 302 * A class containing properties of a Dart variable. |
| 289 */ | 303 */ |
| 290 class Variable { | 304 class Variable { |
| 291 | 305 |
| 306 /// Unique ID number for resolving links. |
| 307 int id; |
| 308 |
| 292 /// Documentation comment with converted markdown. | 309 /// Documentation comment with converted markdown. |
| 293 String comment; | 310 String comment; |
| 294 | 311 |
| 295 String name; | 312 String name; |
| 296 bool isFinal; | 313 bool isFinal; |
| 297 bool isStatic; | 314 bool isStatic; |
| 298 String type; | 315 String type; |
| 299 | 316 |
| 300 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment); | 317 Variable(this.name, this.isFinal, this.isStatic, this.type, |
| 318 this.comment, this.id); |
| 301 | 319 |
| 302 /// Generates a map describing the [Variable] object. | 320 /// Generates a map describing the [Variable] object. |
| 303 Map toMap() { | 321 Map toMap() { |
| 304 var variableMap = {}; | 322 var variableMap = {}; |
| 323 variableMap["id"] = id; |
| 305 variableMap["name"] = name; | 324 variableMap["name"] = name; |
| 306 variableMap["comment"] = comment; | 325 variableMap["comment"] = comment; |
| 307 variableMap["final"] = isFinal.toString(); | 326 variableMap["final"] = isFinal.toString(); |
| 308 variableMap["static"] = isStatic.toString(); | 327 variableMap["static"] = isStatic.toString(); |
| 309 variableMap["type"] = type; | 328 variableMap["type"] = type; |
| 310 return variableMap; | 329 return variableMap; |
| 311 } | 330 } |
| 312 } | 331 } |
| 313 | 332 |
| 314 /** | 333 /** |
| 315 * A class containing properties of a Dart method. | 334 * A class containing properties of a Dart method. |
| 316 */ | 335 */ |
| 317 class Method { | 336 class Method { |
| 318 | 337 |
| 338 /// Unique ID number for resolving links. |
| 339 int id; |
| 340 |
| 319 /// Documentation comment with converted markdown. | 341 /// Documentation comment with converted markdown. |
| 320 String comment; | 342 String comment; |
| 321 | 343 |
| 322 /// Parameters for this method. | 344 /// Parameters for this method. |
| 323 Map<String, Parameter> parameters; | 345 Map<String, Parameter> parameters; |
| 324 | 346 |
| 325 String name; | 347 String name; |
| 326 bool isSetter; | 348 bool isSetter; |
| 327 bool isGetter; | 349 bool isGetter; |
| 328 bool isConstructor; | 350 bool isConstructor; |
| 329 bool isOperator; | 351 bool isOperator; |
| 330 bool isStatic; | 352 bool isStatic; |
| 331 String returnType; | 353 String returnType; |
| 332 | 354 |
| 333 Method(this.name, this.isSetter, this.isGetter, this.isConstructor, | 355 Method(this.name, this.isSetter, this.isGetter, this.isConstructor, |
| 334 this.isOperator, this.isStatic, this.returnType, this.comment, | 356 this.isOperator, this.isStatic, this.returnType, this.comment, |
| 335 this.parameters); | 357 this.parameters, this.id); |
| 336 | 358 |
| 337 /// Generates a map describing the [Method] object. | 359 /// Generates a map describing the [Method] object. |
| 338 Map toMap() { | 360 Map toMap() { |
| 339 var methodMap = {}; | 361 var methodMap = {}; |
| 362 methodMap["id"] = id; |
| 340 methodMap["name"] = name; | 363 methodMap["name"] = name; |
| 341 methodMap["comment"] = comment; | 364 methodMap["comment"] = comment; |
| 342 methodMap["type"] = isSetter ? "setter" : isGetter ? "getter" : | 365 methodMap["type"] = isSetter ? "setter" : isGetter ? "getter" : |
| 343 isOperator ? "operator" : isConstructor ? "constructor" : "method"; | 366 isOperator ? "operator" : isConstructor ? "constructor" : "method"; |
| 344 methodMap["static"] = isStatic.toString(); | 367 methodMap["static"] = isStatic.toString(); |
| 345 methodMap["return"] = returnType; | 368 methodMap["return"] = returnType; |
| 346 methodMap["parameters"] = recurseMap(parameters); | 369 methodMap["parameters"] = recurseMap(parameters); |
| 347 return methodMap; | 370 return methodMap; |
| 348 } | 371 } |
| 349 } | 372 } |
| 350 | 373 |
| 351 /** | 374 /** |
| 352 * A class containing properties of a Dart method/function parameter. | 375 * A class containing properties of a Dart method/function parameter. |
| 353 */ | 376 */ |
| 354 class Parameter { | 377 class Parameter { |
| 355 | 378 |
| 379 /// Unique ID number for resolving links. |
| 380 int id; |
| 381 |
| 356 String name; | 382 String name; |
| 357 bool isOptional; | 383 bool isOptional; |
| 358 bool isNamed; | 384 bool isNamed; |
| 359 bool hasDefaultValue; | 385 bool hasDefaultValue; |
| 360 String type; | 386 String type; |
| 361 String defaultValue; | 387 String defaultValue; |
| 362 | 388 |
| 363 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, | 389 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, |
| 364 this.type, this.defaultValue); | 390 this.type, this.defaultValue, this.id); |
| 365 | 391 |
| 366 /// Generates a map describing the [Parameter] object. | 392 /// Generates a map describing the [Parameter] object. |
| 367 Map toMap() { | 393 Map toMap() { |
| 368 var parameterMap = {}; | 394 var parameterMap = {}; |
| 395 parameterMap["id"] = id; |
| 369 parameterMap["name"] = name; | 396 parameterMap["name"] = name; |
| 370 parameterMap["optional"] = isOptional.toString(); | 397 parameterMap["optional"] = isOptional.toString(); |
| 371 parameterMap["named"] = isNamed.toString(); | 398 parameterMap["named"] = isNamed.toString(); |
| 372 parameterMap["default"] = hasDefaultValue.toString(); | 399 parameterMap["default"] = hasDefaultValue.toString(); |
| 373 parameterMap["type"] = type; | 400 parameterMap["type"] = type; |
| 374 parameterMap["value"] = defaultValue; | 401 parameterMap["value"] = defaultValue; |
| 375 return parameterMap; | 402 return parameterMap; |
| 376 } | 403 } |
| 377 } | 404 } |
| 378 | 405 |
| 379 /** | 406 /** |
| 380 * Writes text to a file in the 'docs' directory. | 407 * Writes text to a file in the 'docs' directory. |
| 381 */ | 408 */ |
| 382 void _writeToFile(String text, String filename) { | 409 void _writeToFile(String text, String filename) { |
| 383 Directory dir = new Directory('docs'); | 410 Directory dir = new Directory('docs'); |
| 384 if (!dir.existsSync()) { | 411 if (!dir.existsSync()) { |
| 385 dir.createSync(); | 412 dir.createSync(); |
| 386 } | 413 } |
| 387 File file = new File('docs/$filename'); | 414 File file = new File('docs/$filename'); |
| 388 if (!file.exists()) { | 415 if (!file.existsSync()) { |
| 389 file.createSync(); | 416 file.createSync(); |
| 390 } | 417 } |
| 391 file.openSync(); | 418 file.openSync(); |
| 392 file.writeAsString(text); | 419 file.writeAsString(text); |
| 393 } | 420 } |
| OLD | NEW |