| 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 [OPTIONS] [FILE/DIR] |
| 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. | |
| 18 import 'dart:io'; | 17 import 'dart:io'; |
| 19 import 'dart:json'; | 18 import 'dart:json'; |
| 20 import 'dart:async'; | 19 import 'dart:async'; |
| 21 import '../lib/dart2yaml.dart'; | |
| 22 import '../lib/src/dart2js_mirrors.dart'; | |
| 23 import 'package:markdown/markdown.dart' as markdown; | 20 import 'package:markdown/markdown.dart' as markdown; |
| 24 import '../../args/lib/args.dart'; | 21 import 'package:args/args.dart'; |
| 25 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'
; | 22 import 'dart2yaml.dart'; |
| 26 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.
dart'; | 23 import 'package:compiler_unsupported/compiler.dart' as api; |
| 24 import 'package:compiler_unsupported/implementation/filenames.dart'; |
| 25 import 'package:compiler_unsupported/implementation/mirrors/dart2js_mirror.dart' |
| 26 as dart2js; |
| 27 import 'package:compiler_unsupported/implementation/mirrors/mirrors.dart'; |
| 28 import 'package:compiler_unsupported/implementation/mirrors/mirrors_util.dart'; |
| 29 import 'package:compiler_unsupported/implementation/source_file_provider.dart'; |
| 30 import 'package:logging/logging.dart'; |
| 31 |
| 32 /// Logger for Dart Doc Generator. |
| 33 var logger = new Logger("Docgen"); |
| 34 |
| 35 /// Unique ID, will get incremented everytime an ID is requested. |
| 36 int _uid = 0; |
| 37 |
| 38 int getID() => _uid++; |
| 39 |
| 40 const String usage = "Usage: dart docgen.dart [OPTIONS] [FILE/DIR]"; |
| 27 | 41 |
| 28 /** | 42 /** |
| 29 * Entry function to create YAML documentation from Dart files. | 43 * Returns a ArgParser with all the flags and options created. |
| 30 */ | 44 */ |
| 31 void main() { | 45 ArgParser initArgParser() { |
| 32 // TODO(tmandel): Use args library once flags are clear. | 46 var parser = new ArgParser(); |
| 33 Options opts = new Options(); | 47 parser.addFlag("help", abbr: "h", |
| 34 Docgen docgen = new Docgen(); | 48 help: "Prints help and usage information", |
| 49 negatable: false, |
| 50 callback: (help) { |
| 51 if (help) print(parser.getUsage()); |
| 52 }); |
| 53 parser.addFlag("verbose", abbr: "v", |
| 54 help: "Runs docgen with logging. ", |
| 55 defaultsTo: false, negatable: false, |
| 56 callback: (verbose) { |
| 57 if (verbose) logger.onRecord.listen((record) => print(record.message)); |
| 58 }); |
| 59 parser.addFlag("yaml", abbr: "y", |
| 60 help: "Outputs to YAML", |
| 61 defaultsTo: true, negatable: true); |
| 62 parser.addFlag("json", abbr: "j", |
| 63 help: "Outputs to JSON", |
| 64 defaultsTo: false, negatable: true); |
| 65 parser.addFlag("hide-private", |
| 66 help: "Hides private declarations" , |
| 67 defaultsTo: false, negatable: false); |
| 68 parser.addFlag("sdk", |
| 69 help: "Flag to parse SDK Library files", |
| 70 defaultsTo: true, negatable: true); |
| 35 | 71 |
| 36 if (opts.arguments.length > 0) { | 72 return parser; |
| 37 List<Path> libraries = [new Path(opts.arguments[0])]; | 73 } |
| 38 Path sdkDirectory = new Path("../../../sdk/"); | 74 |
| 39 var workingMirrors = analyze(libraries, sdkDirectory, | 75 List<Path> listLibraries(List<String> args) { |
| 40 options: ['--preserve-comments', '--categories=Client,Server']); | 76 if (args.length != 1) { |
| 41 workingMirrors.then( (MirrorSystem mirrorSystem) { | 77 throw new UnsupportedError(usage); |
| 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 } | 78 } |
| 79 var libraries = new List<Path>(); |
| 80 var type = FileSystemEntity.typeSync(args[0]); |
| 81 |
| 82 if (type == FileSystemEntityType.NOT_FOUND) { |
| 83 throw new UnsupportedError("File does not exist. $usage"); |
| 84 } else if (type == FileSystemEntityType.LINK) { |
| 85 libraries.addAll(listLibrariesFromDir(new Link(args[0]).targetSync())); |
| 86 } else if (type == FileSystemEntityType.FILE) { |
| 87 libraries.add(new Path(args[0])); |
| 88 logger.info("Added to libraries: ${libraries.last.toString()}"); |
| 89 } else if (type == FileSystemEntityType.DIRECTORY) { |
| 90 libraries.addAll(listLibrariesFromDir(args[0])); |
| 91 } |
| 92 return libraries; |
| 93 } |
| 94 |
| 95 List<Path> listLibrariesFromDir(String path) { |
| 96 var libraries = new List<Path>(); |
| 97 new Directory(path).listSync(recursive: true, |
| 98 followLinks: true).forEach((file) { |
| 99 if (new Path(file.path).extension == "dart") { |
| 100 if (!file.path.contains("/packages/")) { |
| 101 libraries.add(new Path(file.path)); |
| 102 logger.info("Added to libraries: ${libraries.last.toString()}"); |
| 103 } |
| 104 } |
| 105 }); |
| 106 return libraries; |
| 51 } | 107 } |
| 52 | 108 |
| 53 /** | 109 /** |
| 54 * This class documents a list of libraries. | 110 * This class documents a list of libraries. |
| 55 */ | 111 */ |
| 56 class Docgen { | 112 class Docgen { |
| 57 | 113 |
| 58 /// Libraries to be documented. | 114 /// Libraries to be documented. |
| 59 List<LibraryMirror> _libraries; | 115 List<LibraryMirror> _libraries; |
| 60 | 116 |
| 61 /// Saves list of libraries for Docgen object. | 117 /// Saves list of libraries for Docgen object. |
| 62 void set libraries(value) => _libraries = value; | 118 void set libraries(value) { |
| 119 _libraries = value; |
| 120 } |
| 63 | 121 |
| 64 /// Current library being documented to be used for comment links. | 122 /// Current library being documented to be used for comment links. |
| 65 LibraryMirror _currentLibrary; | 123 LibraryMirror _currentLibrary; |
| 66 | 124 |
| 67 /// Current class being documented to be used for comment links. | 125 /// Current class being documented to be used for comment links. |
| 68 ClassMirror _currentClass; | 126 ClassMirror _currentClass; |
| 69 | 127 |
| 70 /// Current member being documented to be used for comment links. | 128 /// Current member being documented to be used for comment links. |
| 71 MemberMirror _currentMember; | 129 MemberMirror _currentMember; |
| 72 | 130 |
| 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 | 131 /// Resolves reference links |
| 78 markdown.Resolver linkResolver; | 132 markdown.Resolver linkResolver; |
| 79 | 133 |
| 134 /// Should the output file type be YAML? |
| 135 bool outputToYaml; |
| 136 /// Should the output file type be JSON? |
| 137 bool outputToJson; |
| 138 /// Should the output file hide private declarations? |
| 139 bool hidePrivate; |
| 140 /// Should the output include SDK libraries? |
| 141 bool sdk; |
| 142 |
| 80 /** | 143 /** |
| 81 * Docgen constructor initializes the link resolver for markdown parsing. | 144 * Docgen constructor initializes the link resolver for markdown parsing. |
| 145 * Also initializes the command line arguments. |
| 82 */ | 146 */ |
| 83 Docgen() { | 147 Docgen({ArgResults argResults}) { |
| 148 outputToYaml = argResults["yaml"]; |
| 149 outputToJson = argResults["json"]; |
| 150 hidePrivate = argResults["hide-private"]; |
| 151 sdk = argResults["sdk"]; |
| 152 |
| 84 this.linkResolver = (name) => | 153 this.linkResolver = (name) => |
| 85 fixReference(name, _currentLibrary, _currentClass, _currentMember); | 154 fixReference(name, _currentLibrary, _currentClass, _currentMember); |
| 86 } | 155 } |
| 87 | 156 |
| 88 /** | 157 /** |
| 158 * Analyzes set of libraries by getting a mirror system and triggers the |
| 159 * documentation of the libraries. |
| 160 */ |
| 161 void analyze(List<Path> libraries) { |
| 162 /// Assuming the dart executable is from the Dart SDK folder. |
| 163 var sdkRoot = new Path(new Options().executable).directoryPath |
| 164 .directoryPath; |
| 165 logger.info("SDK Root: ${sdkRoot.toString()}"); |
| 166 Path packageDir = libraries.last.directoryPath.append("packages"); |
| 167 logger.info("Package Root: ${packageDir.toString()}"); |
| 168 getMirrorSystem(libraries, sdkRoot, |
| 169 packageRoot: packageDir).then((MirrorSystem mirrorSystem) { |
| 170 if (mirrorSystem.libraries.values.isEmpty) { |
| 171 throw new UnsupportedError("No Library Mirrors."); |
| 172 } |
| 173 this.libraries = mirrorSystem.libraries.values; |
| 174 documentLibraries(); |
| 175 }); |
| 176 } |
| 177 |
| 178 /** |
| 179 * Analyzes set of libraries and provides a mirror system which can be used |
| 180 * for static inspection of the source code. |
| 181 */ |
| 182 Future<MirrorSystem> getMirrorSystem(List<Path> libraries, |
| 183 Path libraryRoot, {Path packageRoot}) { |
| 184 SourceFileProvider provider = new SourceFileProvider(); |
| 185 api.DiagnosticHandler diagnosticHandler = |
| 186 new FormattingDiagnosticHandler(provider).diagnosticHandler; |
| 187 Uri libraryUri = currentDirectory.resolve(appendSlash('$libraryRoot')); |
| 188 Uri packageUri = null; |
| 189 if (packageRoot != null) { |
| 190 packageUri = currentDirectory.resolve(appendSlash('$packageRoot')); |
| 191 } |
| 192 List<Uri> librariesUri = <Uri>[]; |
| 193 libraries.forEach((library) { |
| 194 librariesUri.add(currentDirectory.resolve(library.toString())); |
| 195 }); |
| 196 return dart2js.analyze(librariesUri, libraryUri, packageUri, |
| 197 provider.readStringFromUri, diagnosticHandler, |
| 198 ['--preserve-comments', '--categories=Client,Server']); |
| 199 } |
| 200 |
| 201 /** |
| 89 * Creates documentation for filtered libraries. | 202 * Creates documentation for filtered libraries. |
| 90 */ | 203 */ |
| 91 void documentLibraries() { | 204 void documentLibraries() { |
| 92 //TODO(tmandel): Filter libraries and determine output type using flags. | |
| 93 _libraries.forEach((library) { | 205 _libraries.forEach((library) { |
| 94 _currentLibrary = library; | 206 // Files belonging to the SDK have a uri that begins with "dart:". |
| 95 var result = new Library(library.qualifiedName, _getComment(library), | 207 if (sdk || !library.uri.toString().startsWith("dart:")) { |
| 96 _getVariables(library.variables), _getMethods(library.functions), | 208 _currentLibrary = library; |
| 97 _getClasses(library.classes)); | 209 var result = new Library(library.qualifiedName, _getComment(library), |
| 98 if (outputToJson) { | 210 _getVariables(library.variables), _getMethods(library.functions), |
| 99 _writeToFile(stringify(result.toMap()), "${result.name}.json"); | 211 _getClasses(library.classes), getID()); |
| 100 } else { | 212 if (outputToJson) { |
| 101 _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); | 213 _writeToFile(stringify(result.toMap()), "${result.name}.json"); |
| 102 } | 214 } |
| 215 if (outputToYaml) { |
| 216 _writeToFile(getYamlString(result.toMap()), "${result.name}.yaml"); |
| 217 } |
| 218 } |
| 103 }); | 219 }); |
| 104 } | 220 } |
| 105 | 221 |
| 106 /** | 222 /** |
| 107 * Returns any documentation comments associated with a mirror with | 223 * Returns any documentation comments associated with a mirror with |
| 108 * simple markdown converted to html. | 224 * simple markdown converted to html. |
| 109 */ | 225 */ |
| 110 String _getComment(DeclarationMirror mirror) { | 226 String _getComment(DeclarationMirror mirror) { |
| 111 String commentText; | 227 String commentText; |
| 112 mirror.metadata.forEach((metadata) { | 228 mirror.metadata.forEach((metadata) { |
| 113 if (metadata is CommentInstanceMirror) { | 229 if (metadata is CommentInstanceMirror) { |
| 114 CommentInstanceMirror comment = metadata; | 230 CommentInstanceMirror comment = metadata; |
| 115 if (comment.isDocComment) { | 231 if (comment.isDocComment) { |
| 116 if (commentText == null) { | 232 if (commentText == null) { |
| 117 commentText = comment.trimmedText; | 233 commentText = comment.trimmedText; |
| 118 } else { | 234 } else { |
| 119 commentText = "$commentText ${comment.trimmedText}"; | 235 commentText = "$commentText ${comment.trimmedText}"; |
| 120 } | 236 } |
| 121 } | 237 } |
| 122 } | 238 } |
| 123 }); | 239 }); |
| 124 return commentText == null ? "" : | 240 commentText = commentText == null ? "" : |
| 125 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver); | 241 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver) |
| 242 .replaceAll("\n", ""); |
| 243 return commentText; |
| 126 } | 244 } |
| 127 | 245 |
| 128 /** | 246 /** |
| 129 * Converts all [_] references in comments to <code>_</code>. | 247 * Converts all [_] references in comments to <code>_</code>. |
| 130 */ | 248 */ |
| 131 // TODO(tmandel): Create proper links for [_] style markdown based | 249 // TODO(tmandel): Create proper links for [_] style markdown based |
| 132 // on scope once layout of viewer is finished. | 250 // on scope once layout of viewer is finished. |
| 133 markdown.Node fixReference(String name, LibraryMirror currentLibrary, | 251 markdown.Node fixReference(String name, LibraryMirror currentLibrary, |
| 134 ClassMirror currentClass, MemberMirror currentMember) { | 252 ClassMirror currentClass, MemberMirror currentMember) { |
| 135 return new markdown.Element.text('code', name); | 253 return new markdown.Element.text('code', name); |
| 136 } | 254 } |
| 137 | 255 |
| 138 /** | 256 /** |
| 139 * Returns a map of [Variable] objects constructed from inputted mirrors. | 257 * Returns a map of [Variable] objects constructed from inputted mirrors. |
| 140 */ | 258 */ |
| 141 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { | 259 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { |
| 142 var data = {}; | 260 var data = {}; |
| 143 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { | 261 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { |
| 144 _currentMember = mirror; | 262 if (!hidePrivate || !mirror.isPrivate) { |
| 145 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, | 263 _currentMember = mirror; |
| 146 mirror.isStatic, mirror.type.toString(), _getComment(mirror)); | 264 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, |
| 265 mirror.isStatic, mirror.type.toString(), _getComment(mirror), |
| 266 getID()); |
| 267 } |
| 147 }); | 268 }); |
| 148 return data; | 269 return data; |
| 149 } | 270 } |
| 150 | 271 |
| 151 /** | 272 /** |
| 152 * Returns a map of [Method] objects constructed from inputted mirrors. | 273 * Returns a map of [Method] objects constructed from inputted mirrors. |
| 153 */ | 274 */ |
| 154 Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { | 275 Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { |
| 155 var data = {}; | 276 var data = {}; |
| 156 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { | 277 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { |
| 157 _currentMember = mirror; | 278 if (!hidePrivate || !mirror.isPrivate) { |
| 158 data[mirrorName] = new Method(mirrorName, mirror.isSetter, | 279 _currentMember = mirror; |
| 159 mirror.isGetter, mirror.isConstructor, mirror.isOperator, | 280 data[mirrorName] = new Method(mirrorName, mirror.isSetter, |
| 160 mirror.isStatic, mirror.returnType.toString(), _getComment(mirror), | 281 mirror.isGetter, mirror.isConstructor, mirror.isOperator, |
| 161 _getParameters(mirror.parameters)); | 282 mirror.isStatic, mirror.returnType.toString(), _getComment(mirror), |
| 283 _getParameters(mirror.parameters), getID()); |
| 284 } |
| 162 }); | 285 }); |
| 163 return data; | 286 return data; |
| 164 } | 287 } |
| 165 | 288 |
| 166 /** | 289 /** |
| 167 * Returns a map of [Class] objects constructed from inputted mirrors. | 290 * Returns a map of [Class] objects constructed from inputted mirrors. |
| 168 */ | 291 */ |
| 169 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { | 292 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { |
| 170 var data = {}; | 293 var data = {}; |
| 171 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { | 294 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { |
| 172 _currentClass = mirror; | 295 if (!hidePrivate || !mirror.isPrivate) { |
| 173 var superclass; | 296 _currentClass = mirror; |
| 174 if (mirror.superclass != null) { | 297 var superclass = (mirror.superclass != null) ? |
| 175 superclass = mirror.superclass.qualifiedName; | 298 mirror.superclass.qualifiedName : ""; |
| 299 var interfaces = |
| 300 mirror.superinterfaces.map((interface) => interface.qualifiedName); |
| 301 data[mirrorName] = new Class(mirrorName, superclass, mirror.isAbstract, |
| 302 mirror.isTypedef, _getComment(mirror), interfaces.toList(), |
| 303 _getVariables(mirror.variables), _getMethods(mirror.methods), |
| 304 getID()); |
| 176 } | 305 } |
| 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 }); | 306 }); |
| 183 return data; | 307 return data; |
| 184 } | 308 } |
| 185 | 309 |
| 186 /** | 310 /** |
| 187 * Returns a map of [Parameter] objects constructed from inputted mirrors. | 311 * Returns a map of [Parameter] objects constructed from inputted mirrors. |
| 188 */ | 312 */ |
| 189 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { | 313 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { |
| 190 var data = {}; | 314 var data = {}; |
| 191 mirrorList.forEach((ParameterMirror mirror) { | 315 mirrorList.forEach((ParameterMirror mirror) { |
| 192 _currentMember = mirror; | 316 _currentMember = mirror; |
| 193 data[mirror.simpleName] = new Parameter(mirror.simpleName, | 317 data[mirror.simpleName] = new Parameter(mirror.simpleName, |
| 194 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, | 318 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, |
| 195 mirror.type.toString(), mirror.defaultValue); | 319 mirror.type.toString(), mirror.defaultValue, getID()); |
| 196 }); | 320 }); |
| 197 return data; | 321 return data; |
| 198 } | 322 } |
| 199 } | 323 } |
| 200 | 324 |
| 201 /** | 325 /** |
| 202 * Transforms the map by calling toMap on each value in it. | 326 * Transforms the map by calling toMap on each value in it. |
| 203 */ | 327 */ |
| 204 Map recurseMap(Map inputMap) { | 328 Map recurseMap(Map inputMap) { |
| 205 var outputMap = {}; | 329 var outputMap = {}; |
| 206 inputMap.forEach((key, value) { | 330 inputMap.forEach((key, value) { |
| 207 outputMap[key] = value.toMap(); | 331 outputMap[key] = value.toMap(); |
| 208 }); | 332 }); |
| 209 return outputMap; | 333 return outputMap; |
| 210 } | 334 } |
| 211 | 335 |
| 212 /** | 336 /** |
| 213 * A class containing contents of a Dart library. | 337 * A class containing contents of a Dart library. |
| 214 */ | 338 */ |
| 215 class Library { | 339 class Library { |
| 216 | 340 |
| 341 /// Unique ID number for resolving links. |
| 342 int id; |
| 343 |
| 217 /// Documentation comment with converted markdown. | 344 /// Documentation comment with converted markdown. |
| 218 String comment; | 345 String comment; |
| 219 | 346 |
| 220 /// Top-level variables in the library. | 347 /// Top-level variables in the library. |
| 221 Map<String, Variable> variables; | 348 Map<String, Variable> variables; |
| 222 | 349 |
| 223 /// Top-level functions in the library. | 350 /// Top-level functions in the library. |
| 224 Map<String, Method> functions; | 351 Map<String, Method> functions; |
| 225 | 352 |
| 226 /// Classes defined within the library | 353 /// Classes defined within the library |
| 227 Map<String, Class> classes; | 354 Map<String, Class> classes; |
| 228 | 355 |
| 229 String name; | 356 String name; |
| 230 | 357 |
| 231 Library(this.name, this.comment, this.variables, | 358 Library(this.name, this.comment, this.variables, |
| 232 this.functions, this.classes); | 359 this.functions, this.classes, this.id); |
| 233 | 360 |
| 234 /// Generates a map describing the [Library] object. | 361 /// Generates a map describing the [Library] object. |
| 235 Map toMap() { | 362 Map toMap() { |
| 236 var libraryMap = {}; | 363 var libraryMap = {}; |
| 364 libraryMap["id"] = id; |
| 237 libraryMap["name"] = name; | 365 libraryMap["name"] = name; |
| 238 libraryMap["comment"] = comment; | 366 libraryMap["comment"] = comment; |
| 239 libraryMap["variables"] = recurseMap(variables); | 367 libraryMap["variables"] = recurseMap(variables); |
| 240 libraryMap["functions"] = recurseMap(functions); | 368 libraryMap["functions"] = recurseMap(functions); |
| 241 libraryMap["classes"] = recurseMap(classes); | 369 libraryMap["classes"] = recurseMap(classes); |
| 242 return libraryMap; | 370 return libraryMap; |
| 243 } | 371 } |
| 244 } | 372 } |
| 245 | 373 |
| 246 /** | 374 /** |
| 247 * A class containing contents of a Dart class. | 375 * A class containing contents of a Dart class. |
| 248 */ | 376 */ |
| 249 // TODO(tmandel): Figure out how to do typedefs (what is needed) | 377 // TODO(tmandel): Figure out how to do typedefs (what is needed) |
| 250 class Class { | 378 class Class { |
| 251 | 379 |
| 380 /// Unique ID number for resolving links. |
| 381 int id; |
| 382 |
| 252 /// Documentation comment with converted markdown. | 383 /// Documentation comment with converted markdown. |
| 253 String comment; | 384 String comment; |
| 254 | 385 |
| 255 /// List of the names of interfaces that this class implements. | 386 /// List of the names of interfaces that this class implements. |
| 256 List<String> interfaces; | 387 List<String> interfaces; |
| 257 | 388 |
| 258 /// Top-level variables in the class. | 389 /// Top-level variables in the class. |
| 259 Map<String, Variable> variables; | 390 Map<String, Variable> variables; |
| 260 | 391 |
| 261 /// Methods in the class. | 392 /// Methods in the class. |
| 262 Map<String, Method> methods; | 393 Map<String, Method> methods; |
| 263 | 394 |
| 264 String name; | 395 String name; |
| 265 String superclass; | 396 String superclass; |
| 266 bool isAbstract; | 397 bool isAbstract; |
| 267 bool isTypedef; | 398 bool isTypedef; |
| 268 | 399 |
| 269 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, | 400 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, |
| 270 this.comment, this.interfaces, this.variables, this.methods); | 401 this.comment, this.interfaces, this.variables, this.methods, this.id); |
| 271 | 402 |
| 272 /// Generates a map describing the [Class] object. | 403 /// Generates a map describing the [Class] object. |
| 273 Map toMap() { | 404 Map toMap() { |
| 274 var classMap = {}; | 405 var classMap = {}; |
| 406 classMap["id"] = id; |
| 275 classMap["name"] = name; | 407 classMap["name"] = name; |
| 276 classMap["comment"] = comment; | 408 classMap["comment"] = comment; |
| 277 classMap["superclass"] = superclass; | 409 classMap["superclass"] = superclass; |
| 278 classMap["abstract"] = isAbstract.toString(); | 410 classMap["abstract"] = isAbstract.toString(); |
| 279 classMap["typedef"] = isTypedef.toString(); | 411 classMap["typedef"] = isTypedef.toString(); |
| 280 classMap["implements"] = new List.from(interfaces); | 412 classMap["implements"] = new List.from(interfaces); |
| 281 classMap["variables"] = recurseMap(variables); | 413 classMap["variables"] = recurseMap(variables); |
| 282 classMap["methods"] = recurseMap(methods); | 414 classMap["methods"] = recurseMap(methods); |
| 283 return classMap; | 415 return classMap; |
| 284 } | 416 } |
| 285 } | 417 } |
| 286 | 418 |
| 287 /** | 419 /** |
| 288 * A class containing properties of a Dart variable. | 420 * A class containing properties of a Dart variable. |
| 289 */ | 421 */ |
| 290 class Variable { | 422 class Variable { |
| 291 | 423 |
| 424 /// Unique ID number for resolving links. |
| 425 int id; |
| 426 |
| 292 /// Documentation comment with converted markdown. | 427 /// Documentation comment with converted markdown. |
| 293 String comment; | 428 String comment; |
| 294 | 429 |
| 295 String name; | 430 String name; |
| 296 bool isFinal; | 431 bool isFinal; |
| 297 bool isStatic; | 432 bool isStatic; |
| 298 String type; | 433 String type; |
| 299 | 434 |
| 300 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment); | 435 Variable(this.name, this.isFinal, this.isStatic, this.type, |
| 436 this.comment, this.id); |
| 301 | 437 |
| 302 /// Generates a map describing the [Variable] object. | 438 /// Generates a map describing the [Variable] object. |
| 303 Map toMap() { | 439 Map toMap() { |
| 304 var variableMap = {}; | 440 var variableMap = {}; |
| 441 variableMap["id"] = id; |
| 305 variableMap["name"] = name; | 442 variableMap["name"] = name; |
| 306 variableMap["comment"] = comment; | 443 variableMap["comment"] = comment; |
| 307 variableMap["final"] = isFinal.toString(); | 444 variableMap["final"] = isFinal.toString(); |
| 308 variableMap["static"] = isStatic.toString(); | 445 variableMap["static"] = isStatic.toString(); |
| 309 variableMap["type"] = type; | 446 variableMap["type"] = type; |
| 310 return variableMap; | 447 return variableMap; |
| 311 } | 448 } |
| 312 } | 449 } |
| 313 | 450 |
| 314 /** | 451 /** |
| 315 * A class containing properties of a Dart method. | 452 * A class containing properties of a Dart method. |
| 316 */ | 453 */ |
| 317 class Method { | 454 class Method { |
| 318 | 455 |
| 456 /// Unique ID number for resolving links. |
| 457 int id; |
| 458 |
| 319 /// Documentation comment with converted markdown. | 459 /// Documentation comment with converted markdown. |
| 320 String comment; | 460 String comment; |
| 321 | 461 |
| 322 /// Parameters for this method. | 462 /// Parameters for this method. |
| 323 Map<String, Parameter> parameters; | 463 Map<String, Parameter> parameters; |
| 324 | 464 |
| 325 String name; | 465 String name; |
| 326 bool isSetter; | 466 bool isSetter; |
| 327 bool isGetter; | 467 bool isGetter; |
| 328 bool isConstructor; | 468 bool isConstructor; |
| 329 bool isOperator; | 469 bool isOperator; |
| 330 bool isStatic; | 470 bool isStatic; |
| 331 String returnType; | 471 String returnType; |
| 332 | 472 |
| 333 Method(this.name, this.isSetter, this.isGetter, this.isConstructor, | 473 Method(this.name, this.isSetter, this.isGetter, this.isConstructor, |
| 334 this.isOperator, this.isStatic, this.returnType, this.comment, | 474 this.isOperator, this.isStatic, this.returnType, this.comment, |
| 335 this.parameters); | 475 this.parameters, this.id); |
| 336 | 476 |
| 337 /// Generates a map describing the [Method] object. | 477 /// Generates a map describing the [Method] object. |
| 338 Map toMap() { | 478 Map toMap() { |
| 339 var methodMap = {}; | 479 var methodMap = {}; |
| 480 methodMap["id"] = id; |
| 340 methodMap["name"] = name; | 481 methodMap["name"] = name; |
| 341 methodMap["comment"] = comment; | 482 methodMap["comment"] = comment; |
| 342 methodMap["type"] = isSetter ? "setter" : isGetter ? "getter" : | 483 methodMap["type"] = isSetter ? "setter" : isGetter ? "getter" : |
| 343 isOperator ? "operator" : isConstructor ? "constructor" : "method"; | 484 isOperator ? "operator" : isConstructor ? "constructor" : "method"; |
| 344 methodMap["static"] = isStatic.toString(); | 485 methodMap["static"] = isStatic.toString(); |
| 345 methodMap["return"] = returnType; | 486 methodMap["return"] = returnType; |
| 346 methodMap["parameters"] = recurseMap(parameters); | 487 methodMap["parameters"] = recurseMap(parameters); |
| 347 return methodMap; | 488 return methodMap; |
| 348 } | 489 } |
| 349 } | 490 } |
| 350 | 491 |
| 351 /** | 492 /** |
| 352 * A class containing properties of a Dart method/function parameter. | 493 * A class containing properties of a Dart method/function parameter. |
| 353 */ | 494 */ |
| 354 class Parameter { | 495 class Parameter { |
| 355 | 496 |
| 497 /// Unique ID number for resolving links. |
| 498 int id; |
| 499 |
| 356 String name; | 500 String name; |
| 357 bool isOptional; | 501 bool isOptional; |
| 358 bool isNamed; | 502 bool isNamed; |
| 359 bool hasDefaultValue; | 503 bool hasDefaultValue; |
| 360 String type; | 504 String type; |
| 361 String defaultValue; | 505 String defaultValue; |
| 362 | 506 |
| 363 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, | 507 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, |
| 364 this.type, this.defaultValue); | 508 this.type, this.defaultValue, this.id); |
| 365 | 509 |
| 366 /// Generates a map describing the [Parameter] object. | 510 /// Generates a map describing the [Parameter] object. |
| 367 Map toMap() { | 511 Map toMap() { |
| 368 var parameterMap = {}; | 512 var parameterMap = {}; |
| 513 parameterMap["id"] = id; |
| 369 parameterMap["name"] = name; | 514 parameterMap["name"] = name; |
| 370 parameterMap["optional"] = isOptional.toString(); | 515 parameterMap["optional"] = isOptional.toString(); |
| 371 parameterMap["named"] = isNamed.toString(); | 516 parameterMap["named"] = isNamed.toString(); |
| 372 parameterMap["default"] = hasDefaultValue.toString(); | 517 parameterMap["default"] = hasDefaultValue.toString(); |
| 373 parameterMap["type"] = type; | 518 parameterMap["type"] = type; |
| 374 parameterMap["value"] = defaultValue; | 519 parameterMap["value"] = defaultValue; |
| 375 return parameterMap; | 520 return parameterMap; |
| 376 } | 521 } |
| 377 } | 522 } |
| 378 | 523 |
| 379 /** | 524 /** |
| 380 * Writes text to a file in the 'docs' directory. | 525 * Writes text to a file in the 'docs' directory. |
| 381 */ | 526 */ |
| 382 void _writeToFile(String text, String filename) { | 527 void _writeToFile(String text, String filename) { |
| 383 Directory dir = new Directory('docs'); | 528 Directory dir = new Directory('docs'); |
| 384 if (!dir.existsSync()) { | 529 if (!dir.existsSync()) { |
| 385 dir.createSync(); | 530 dir.createSync(); |
| 386 } | 531 } |
| 387 File file = new File('docs/$filename'); | 532 File file = new File('docs/$filename'); |
| 388 if (!file.exists()) { | 533 if (!file.existsSync()) { |
| 389 file.createSync(); | 534 file.createSync(); |
| 390 } | 535 } |
| 391 file.openSync(); | 536 file.openSync(); |
| 392 file.writeAsString(text); | 537 file.writeAsString(text); |
| 393 } | 538 } |
| OLD | NEW |