| 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 19 matching lines...) Expand all Loading... |
| 30 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart'; | 30 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart'; |
| 31 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro
r.dart' | 31 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro
r.dart' |
| 32 as dart2js; | 32 as dart2js; |
| 33 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'
; | 33 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'
; |
| 34 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.
dart'; | 34 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.
dart'; |
| 35 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider.
dart'; | 35 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider.
dart'; |
| 36 import '../../../sdk/lib/_internal/libraries.dart'; | 36 import '../../../sdk/lib/_internal/libraries.dart'; |
| 37 | 37 |
| 38 var logger = new Logger('Docgen'); | 38 var logger = new Logger('Docgen'); |
| 39 | 39 |
| 40 const String usage = 'Usage: dart docgen.dart [OPTIONS] [fooDir/barFile]'; | 40 const String USAGE = 'Usage: dart docgen.dart [OPTIONS] [fooDir/barFile]'; |
| 41 | 41 |
| 42 /** | 42 /// Current library being documented to be used for comment links. |
| 43 * This class documents a list of libraries. | 43 LibraryMirror _currentLibrary; |
| 44 */ | 44 |
| 45 class Docgen { | 45 /// Current class being documented to be used for comment links. |
| 46 | 46 ClassMirror _currentClass; |
| 47 /// Libraries to be documented. | 47 |
| 48 List<LibraryMirror> _libraries; | 48 /// Current member being documented to be used for comment links. |
| 49 | 49 MemberMirror _currentMember; |
| 50 /// Current library being documented to be used for comment links. | 50 |
| 51 LibraryMirror _currentLibrary; | 51 /// Resolves reference links in doc comments. |
| 52 | 52 markdown.Resolver linkResolver; |
| 53 /// Current class being documented to be used for comment links. | 53 |
| 54 ClassMirror _currentClass; | 54 /// Package directory of directory being analyzed. |
| 55 | 55 String packageDir; |
| 56 /// Current member being documented to be used for comment links. | 56 |
| 57 MemberMirror _currentMember; | 57 bool outputToYaml; |
| 58 | 58 bool outputToJson; |
| 59 /// Resolves reference links in doc comments. | 59 bool includePrivate; |
| 60 markdown.Resolver linkResolver; | 60 /// State for whether imported SDK libraries should also be outputted. |
| 61 | 61 bool includeSdk; |
| 62 /// Package directory of directory being analyzed. | 62 /// State for whether all SDK libraries should be outputted. |
| 63 String packageDir; | 63 bool parseSdk; |
| 64 | 64 |
| 65 bool outputToYaml; | 65 /** |
| 66 bool outputToJson; | 66 * Docgen constructor initializes the link resolver for markdown parsing. |
| 67 bool includePrivate; | 67 * Also initializes the command line arguments. |
| 68 /// State for whether imported SDK libraries should also be outputted. | 68 */ |
| 69 bool includeSdk; | 69 void docgen(ArgResults argResults) { |
| 70 /// State for whether all SDK libraries should be outputted. | 70 _setCommandLineArguments(argResults); |
| 71 bool parseSdk; | 71 |
| 72 | 72 linkResolver = (name) => |
| 73 /** | 73 fixReference(name, _currentLibrary, _currentClass, _currentMember); |
| 74 * Docgen constructor initializes the link resolver for markdown parsing. | 74 |
| 75 * Also initializes the command line arguments. | 75 getMirrorSystem(argResults.rest).then((MirrorSystem mirrorSystem) { |
| 76 */ | |
| 77 Docgen(ArgResults argResults) { | |
| 78 setCommandLineArguments(argResults); | |
| 79 | |
| 80 this.linkResolver = (name) => | |
| 81 fixReference(name, _currentLibrary, _currentClass, _currentMember); | |
| 82 | |
| 83 getMirrorSystem(argResults.rest).then(setLibraries); | |
| 84 } | |
| 85 | |
| 86 void setCommandLineArguments(ArgResults argResults) { | |
| 87 outputToYaml = argResults['yaml'] || argResults['output-format'] == 'yaml'; | |
| 88 outputToJson = argResults['json'] || argResults['output-format'] == 'json'; | |
| 89 if (outputToYaml && outputToJson) { | |
| 90 throw new ArgumentError('Cannot have contradictory output flags.'); | |
| 91 } | |
| 92 outputToYaml = outputToYaml || !outputToJson; | |
| 93 includePrivate = argResults['include-private']; | |
| 94 parseSdk = argResults['parse-sdk']; | |
| 95 includeSdk = parseSdk || argResults['include-sdk']; | |
| 96 } | |
| 97 | |
| 98 List<String> listLibraries(List<String> args) { | |
| 99 // TODO(janicejl): At the moment, only have support to have either one file, | |
| 100 // or one directory. This is because there can only be one package directory | |
| 101 // since only one docgen is created per run. | |
| 102 if (args.length != 1) throw new UnsupportedError(usage); | |
| 103 var libraries = new List<String>(); | |
| 104 var type = FileSystemEntity.typeSync(args[0]); | |
| 105 | |
| 106 if (type == FileSystemEntityType.FILE) { | |
| 107 libraries.add(path.absolute(args[0])); | |
| 108 logger.info('Added to libraries: ${libraries.last}'); | |
| 109 } else { | |
| 110 libraries.addAll(listDartFromDir(args[0])); | |
| 111 } | |
| 112 logger.info('Package Directory: $packageDir'); | |
| 113 return libraries; | |
| 114 } | |
| 115 | |
| 116 List<String> listDartFromDir(String args) { | |
| 117 var files = listDir(args, recursive: true); | |
| 118 packageDir = files.firstWhere((f) => | |
| 119 f.endsWith('/pubspec.yaml'), orElse: () => ''); | |
| 120 if (packageDir != '') packageDir = path.dirname(packageDir) + '/packages'; | |
| 121 return files.where((f) => | |
| 122 f.endsWith('.dart') && !f.contains('/packages')).toList() | |
| 123 ..forEach((lib) => logger.info('Added to libraries: $lib')); | |
| 124 } | |
| 125 | |
| 126 List<String> listSdk() { | |
| 127 var sdk = new List<String>(); | |
| 128 LIBRARIES.forEach((String name, LibraryInfo info) { | |
| 129 if (info.documented) { | |
| 130 sdk.add('dart:$name'); | |
| 131 logger.info('Add to SDK: ${sdk.last}'); | |
| 132 } | |
| 133 }); | |
| 134 return sdk; | |
| 135 } | |
| 136 | |
| 137 void setLibraries(MirrorSystem mirrorSystem) { | |
| 138 if (mirrorSystem.libraries.values.isEmpty) { | 76 if (mirrorSystem.libraries.values.isEmpty) { |
| 139 throw new UnsupportedError('No Library Mirrors.'); | 77 throw new StateError('No Library Mirrors.'); |
| 140 } | 78 } |
| 141 this.libraries = mirrorSystem.libraries.values; | 79 _documentLibraries(mirrorSystem.libraries.values); |
| 142 documentLibraries(); | 80 }); |
| 143 } | 81 } |
| 144 | 82 |
| 145 /** | 83 void _setCommandLineArguments(ArgResults argResults) { |
| 146 * Analyzes set of libraries by getting a mirror system and triggers the | 84 outputToYaml = argResults['yaml'] || argResults['output-format'] == 'yaml'; |
| 147 * documentation of the libraries. | 85 outputToJson = argResults['json'] || argResults['output-format'] == 'json'; |
| 148 */ | 86 if (outputToYaml && outputToJson) { |
| 149 Future<MirrorSystem> getMirrorSystem(List<String> args) { | 87 throw new ArgumentError('Cannot have contradictory output flags.'); |
| 150 var libraries = !parseSdk ? listLibraries(args) : listSdk(); | 88 } |
| 151 if (libraries.isEmpty) throw new StateError('No Libraries.'); | 89 outputToYaml = outputToYaml || !outputToJson; |
| 152 // DART_SDK should be set to the root of the SDK library. | 90 includePrivate = argResults['include-private']; |
| 153 var sdkRoot = Platform.environment['DART_SDK']; | 91 parseSdk = argResults['parse-sdk']; |
| 154 if (sdkRoot != null) { | 92 includeSdk = parseSdk || argResults['include-sdk']; |
| 155 logger.info('Using DART_SDK to find SDK at $sdkRoot'); | 93 } |
| 156 } else { | 94 |
| 157 // If DART_SDK is not defined in the environment, | 95 List<String> _listLibraries(List<String> args) { |
| 158 // assuming the dart executable is from the Dart SDK folder inside bin. | 96 // TODO(janicejl): At the moment, only have support to have either one file, |
| 159 sdkRoot = path.dirname(path.dirname(new Options().executable)); | 97 // or one directory. This is because there can only be one package directory |
| 160 logger.info('SDK Root: ${sdkRoot}'); | 98 // since only one docgen is created per run. |
| 161 } | 99 if (args.length != 1) throw new UnsupportedError(USAGE); |
| 162 | 100 var libraries = new List<String>(); |
| 163 return getMirrorSystemHelper(libraries, sdkRoot, packageRoot: packageDir); | 101 var type = FileSystemEntity.typeSync(args[0]); |
| 164 } | 102 |
| 165 | 103 if (type == FileSystemEntityType.FILE) { |
| 166 /** | 104 libraries.add(path.absolute(args[0])); |
| 167 * Analyzes set of libraries and provides a mirror system which can be used | 105 logger.info('Added to libraries: ${libraries.last}'); |
| 168 * for static inspection of the source code. | 106 } else { |
| 169 */ | 107 libraries.addAll(_listDartFromDir(args[0])); |
| 170 Future<MirrorSystem> getMirrorSystemHelper(List<String> libraries, | |
| 171 String libraryRoot, {String packageRoot}) { | |
| 172 SourceFileProvider provider = new SourceFileProvider(); | |
| 173 api.DiagnosticHandler diagnosticHandler = | |
| 174 new FormattingDiagnosticHandler(provider).diagnosticHandler; | |
| 175 Uri libraryUri = currentDirectory.resolve(appendSlash('$libraryRoot')); | |
| 176 Uri packageUri = null; | |
| 177 if (packageRoot != null) { | |
| 178 packageUri = currentDirectory.resolve(appendSlash('$packageRoot')); | |
| 179 } | |
| 180 List<Uri> librariesUri = <Uri>[]; | |
| 181 libraries.forEach((library) { | |
| 182 librariesUri.add(currentDirectory.resolve(library)); | |
| 183 }); | |
| 184 return dart2js.analyze(librariesUri, libraryUri, packageUri, | |
| 185 provider.readStringFromUri, diagnosticHandler, | |
| 186 ['--preserve-comments', '--categories=Client,Server']); | |
| 187 } | |
| 188 | |
| 189 /** | |
| 190 * Creates documentation for filtered libraries. | |
| 191 */ | |
| 192 void documentLibraries() { | |
| 193 _libraries.forEach((lib) { | |
| 194 // Files belonging to the SDK have a uri that begins with 'dart:'. | |
| 195 if (includeSdk || !lib.uri.toString().startsWith('dart:')) { | |
| 196 var library = generateLibrary(lib); | |
| 197 outputLibrary(library); | |
| 198 } | |
| 199 }); | |
| 200 // Outputs a text file with a list of files available after creating all | |
| 201 // the libraries. This will help the viewer know what files are available | |
| 202 // to read in. | |
| 203 _writeToFile(listDir("docs").join('\n'), 'library_list.txt'); | |
| 204 } | |
| 205 | |
| 206 Library generateLibrary(dart2js.Dart2JsLibraryMirror library) { | |
| 207 _currentLibrary = library; | |
| 208 var result = new Library(library.qualifiedName, _getComment(library), | |
| 209 _getVariables(library.variables), _getMethods(library.functions), | |
| 210 _getClasses(library.classes)); | |
| 211 logger.fine('Generated library for ${result.name}'); | |
| 212 return result; | |
| 213 } | |
| 214 | |
| 215 void outputLibrary(Library result) { | |
| 216 if (outputToJson) { | |
| 217 _writeToFile(stringify(result.toMap()), '${result.name}.json'); | |
| 218 } | |
| 219 if (outputToYaml) { | |
| 220 _writeToFile(getYamlString(result.toMap()), '${result.name}.yaml'); | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 /// Saves list of libraries for Docgen object. | |
| 225 void set libraries(value){ | |
| 226 _libraries = value; | |
| 227 } | |
| 228 | |
| 229 /** | |
| 230 * Returns any documentation comments associated with a mirror with | |
| 231 * simple markdown converted to html. | |
| 232 */ | |
| 233 String _getComment(DeclarationMirror mirror) { | |
| 234 String commentText; | |
| 235 mirror.metadata.forEach((metadata) { | |
| 236 if (metadata is CommentInstanceMirror) { | |
| 237 CommentInstanceMirror comment = metadata; | |
| 238 if (comment.isDocComment) { | |
| 239 if (commentText == null) { | |
| 240 commentText = comment.trimmedText; | |
| 241 } else { | |
| 242 commentText = '$commentText ${comment.trimmedText}'; | |
| 243 } | |
| 244 } | |
| 245 } | |
| 246 }); | |
| 247 commentText = commentText == null ? '' : | |
| 248 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver) | |
| 249 .replaceAll('\n', ''); | |
| 250 return commentText; | |
| 251 } | |
| 252 | |
| 253 /** | |
| 254 * Converts all [_] references in comments to <code>_</code>. | |
| 255 */ | |
| 256 // TODO(tmandel): Create proper links for [_] style markdown based | |
| 257 // on scope once layout of viewer is finished. | |
| 258 markdown.Node fixReference(String name, LibraryMirror currentLibrary, | |
| 259 ClassMirror currentClass, MemberMirror currentMember) { | |
| 260 return new markdown.Element.text('code', name); | |
| 261 } | |
| 262 | |
| 263 /** | |
| 264 * Returns a map of [Variable] objects constructed from inputted mirrors. | |
| 265 */ | |
| 266 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { | |
| 267 var data = {}; | |
| 268 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { | |
| 269 if (includePrivate || !mirror.isPrivate) { | |
| 270 _currentMember = mirror; | |
| 271 data[mirrorName] = new Variable(mirrorName, mirror.qualifiedName, | |
| 272 mirror.isFinal, mirror.isStatic, mirror.type.qualifiedName, | |
| 273 _getComment(mirror)); | |
| 274 } | |
| 275 }); | |
| 276 return data; | |
| 277 } | |
| 278 | |
| 279 /** | |
| 280 * Returns a map of [Method] objects constructed from inputted mirrors. | |
| 281 */ | |
| 282 Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { | |
| 283 var data = {}; | |
| 284 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { | |
| 285 if (includePrivate || !mirror.isPrivate) { | |
| 286 _currentMember = mirror; | |
| 287 data[mirrorName] = new Method(mirrorName, mirror.qualifiedName, | |
| 288 mirror.isSetter, mirror.isGetter, mirror.isConstructor, | |
| 289 mirror.isOperator, mirror.isStatic, mirror.returnType.qualifiedName,
| |
| 290 _getComment(mirror), _getParameters(mirror.parameters)); | |
| 291 } | |
| 292 }); | |
| 293 return data; | |
| 294 } | 108 } |
| 295 | 109 logger.info('Package Directory: $packageDir'); |
| 296 /** | 110 return libraries; |
| 297 * Returns a map of [Class] objects constructed from inputted mirrors. | 111 } |
| 298 */ | 112 |
| 299 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { | 113 List<String> _listDartFromDir(String args) { |
| 300 var data = {}; | 114 var files = listDir(args, recursive: true); |
| 301 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { | 115 packageDir = files.firstWhere((f) => |
| 302 if (includePrivate || !mirror.isPrivate) { | 116 f.endsWith('/pubspec.yaml'), orElse: () => ''); |
| 303 _currentClass = mirror; | 117 if (packageDir != '') packageDir = path.dirname(packageDir) + '/packages'; |
| 304 var superclass = (mirror.superclass != null) ? | 118 return files.where((f) => |
| 305 mirror.superclass.qualifiedName : ''; | 119 f.endsWith('.dart') && !f.contains('/packages')).toList() |
| 306 var interfaces = | 120 ..forEach((lib) => logger.info('Added to libraries: $lib')); |
| 307 mirror.superinterfaces.map((interface) => interface.qualifiedName); | 121 } |
| 308 data[mirrorName] = new Class(mirrorName, mirror.qualifiedName, | 122 |
| 309 superclass, mirror.isAbstract, mirror.isTypedef, | 123 List<String> _listSdk() { |
| 310 _getComment(mirror), interfaces.toList(), | 124 var sdk = new List<String>(); |
| 311 _getVariables(mirror.variables), _getMethods(mirror.methods)); | 125 LIBRARIES.forEach((String name, LibraryInfo info) { |
| 312 } | 126 if (info.documented) { |
| 313 }); | 127 sdk.add('dart:$name'); |
| 314 return data; | 128 logger.info('Add to SDK: ${sdk.last}'); |
| 315 } | 129 } |
| 316 | 130 }); |
| 317 /** | 131 return sdk; |
| 318 * Returns a map of [Parameter] objects constructed from inputted mirrors. | 132 } |
| 319 */ | 133 |
| 320 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { | 134 /** |
| 321 var data = {}; | 135 * Analyzes set of libraries by getting a mirror system and triggers the |
| 322 mirrorList.forEach((ParameterMirror mirror) { | 136 * documentation of the libraries. |
| 137 */ |
| 138 Future<MirrorSystem> getMirrorSystem(List<String> args) { |
| 139 var libraries = !parseSdk ? _listLibraries(args) : _listSdk(); |
| 140 if (libraries.isEmpty) throw new StateError('No Libraries.'); |
| 141 // DART_SDK should be set to the root of the SDK library. |
| 142 var sdkRoot = Platform.environment['DART_SDK']; |
| 143 if (sdkRoot != null) { |
| 144 logger.info('Using DART_SDK to find SDK at $sdkRoot'); |
| 145 } else { |
| 146 // If DART_SDK is not defined in the environment, |
| 147 // assuming the dart executable is from the Dart SDK folder inside bin. |
| 148 sdkRoot = path.dirname(path.dirname(new Options().executable)); |
| 149 logger.info('SDK Root: ${sdkRoot}'); |
| 150 } |
| 151 |
| 152 return _getMirrorSystemHelper(libraries, sdkRoot, packageRoot: packageDir); |
| 153 } |
| 154 |
| 155 /** |
| 156 * Analyzes set of libraries and provides a mirror system which can be used |
| 157 * for static inspection of the source code. |
| 158 */ |
| 159 Future<MirrorSystem> _getMirrorSystemHelper(List<String> libraries, |
| 160 String libraryRoot, {String packageRoot}) { |
| 161 SourceFileProvider provider = new SourceFileProvider(); |
| 162 api.DiagnosticHandler diagnosticHandler = |
| 163 new FormattingDiagnosticHandler(provider).diagnosticHandler; |
| 164 Uri libraryUri = currentDirectory.resolve(appendSlash('$libraryRoot')); |
| 165 Uri packageUri = null; |
| 166 if (packageRoot != null) { |
| 167 packageUri = currentDirectory.resolve(appendSlash('$packageRoot')); |
| 168 } |
| 169 List<Uri> librariesUri = <Uri>[]; |
| 170 libraries.forEach((library) { |
| 171 librariesUri.add(currentDirectory.resolve(library)); |
| 172 }); |
| 173 return dart2js.analyze(librariesUri, libraryUri, packageUri, |
| 174 provider.readStringFromUri, diagnosticHandler, |
| 175 ['--preserve-comments', '--categories=Client,Server']); |
| 176 } |
| 177 |
| 178 /** |
| 179 * Creates documentation for filtered libraries. |
| 180 */ |
| 181 void _documentLibraries(List<LibraryMirror> libraries) { |
| 182 libraries.forEach((lib) { |
| 183 // Files belonging to the SDK have a uri that begins with 'dart:'. |
| 184 if (includeSdk || !lib.uri.toString().startsWith('dart:')) { |
| 185 var library = generateLibrary(lib); |
| 186 _outputLibrary(library); |
| 187 } |
| 188 }); |
| 189 // Outputs a text file with a list of files available after creating all |
| 190 // the libraries. This will help the viewer know what files are available |
| 191 // to read in. |
| 192 _writeToFile(listDir("docs").join('\n'), 'library_list.txt'); |
| 193 } |
| 194 |
| 195 Library generateLibrary(dart2js.Dart2JsLibraryMirror library) { |
| 196 _currentLibrary = library; |
| 197 var result = new Library(library.qualifiedName, _getComment(library), |
| 198 _getVariables(library.variables), _getMethods(library.functions), |
| 199 _getClasses(library.classes)); |
| 200 logger.fine('Generated library for ${result.name}'); |
| 201 return result; |
| 202 } |
| 203 |
| 204 void _outputLibrary(Library result) { |
| 205 if (outputToJson) { |
| 206 _writeToFile(stringify(result.toMap()), '${result.name}.json'); |
| 207 } |
| 208 if (outputToYaml) { |
| 209 _writeToFile(getYamlString(result.toMap()), '${result.name}.yaml'); |
| 210 } |
| 211 } |
| 212 |
| 213 /** |
| 214 * Returns any documentation comments associated with a mirror with |
| 215 * simple markdown converted to html. |
| 216 */ |
| 217 String _getComment(DeclarationMirror mirror) { |
| 218 String commentText; |
| 219 mirror.metadata.forEach((metadata) { |
| 220 if (metadata is CommentInstanceMirror) { |
| 221 CommentInstanceMirror comment = metadata; |
| 222 if (comment.isDocComment) { |
| 223 if (commentText == null) { |
| 224 commentText = comment.trimmedText; |
| 225 } else { |
| 226 commentText = '$commentText ${comment.trimmedText}'; |
| 227 } |
| 228 } |
| 229 } |
| 230 }); |
| 231 commentText = commentText == null ? '' : |
| 232 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver) |
| 233 .replaceAll('\n', ''); |
| 234 return commentText; |
| 235 } |
| 236 |
| 237 /** |
| 238 * Converts all [_] references in comments to <code>_</code>. |
| 239 */ |
| 240 // TODO(tmandel): Create proper links for [_] style markdown based |
| 241 // on scope once layout of viewer is finished. |
| 242 markdown.Node fixReference(String name, LibraryMirror currentLibrary, |
| 243 ClassMirror currentClass, MemberMirror currentMember) { |
| 244 return new markdown.Element.text('code', name); |
| 245 } |
| 246 |
| 247 /** |
| 248 * Returns a map of [Variable] objects constructed from inputted mirrors. |
| 249 */ |
| 250 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap) { |
| 251 var data = {}; |
| 252 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { |
| 253 if (includePrivate || !mirror.isPrivate) { |
| 323 _currentMember = mirror; | 254 _currentMember = mirror; |
| 324 data[mirror.simpleName] = new Parameter(mirror.simpleName, | 255 data[mirrorName] = new Variable(mirrorName, mirror.qualifiedName, |
| 325 mirror.qualifiedName, mirror.isOptional, mirror.isNamed, | 256 mirror.isFinal, mirror.isStatic, mirror.type.qualifiedName, |
| 326 mirror.hasDefaultValue, mirror.type.qualifiedName, | 257 _getComment(mirror)); |
| 327 mirror.defaultValue); | 258 } |
| 328 }); | 259 }); |
| 329 return data; | 260 return data; |
| 330 } | 261 } |
| 331 } | 262 |
| 332 | 263 /** |
| 333 /** | 264 * Returns a map of [Method] objects constructed from inputted mirrors. |
| 265 */ |
| 266 Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap) { |
| 267 var data = {}; |
| 268 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { |
| 269 if (includePrivate || !mirror.isPrivate) { |
| 270 _currentMember = mirror; |
| 271 data[mirrorName] = new Method(mirrorName, mirror.qualifiedName, |
| 272 mirror.isSetter, mirror.isGetter, mirror.isConstructor, |
| 273 mirror.isOperator, mirror.isStatic, mirror.returnType.qualifiedName, |
| 274 _getComment(mirror), _getParameters(mirror.parameters)); |
| 275 } |
| 276 }); |
| 277 return data; |
| 278 } |
| 279 |
| 280 /** |
| 281 * Returns a map of [Class] objects constructed from inputted mirrors. |
| 282 */ |
| 283 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap) { |
| 284 var data = {}; |
| 285 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { |
| 286 if (includePrivate || !mirror.isPrivate) { |
| 287 _currentClass = mirror; |
| 288 var superclass = (mirror.superclass != null) ? |
| 289 mirror.superclass.qualifiedName : ''; |
| 290 var interfaces = |
| 291 mirror.superinterfaces.map((interface) => interface.qualifiedName); |
| 292 data[mirrorName] = new Class(mirrorName, mirror.qualifiedName, |
| 293 superclass, mirror.isAbstract, mirror.isTypedef, |
| 294 _getComment(mirror), interfaces.toList(), |
| 295 _getVariables(mirror.variables), _getMethods(mirror.methods)); |
| 296 } |
| 297 }); |
| 298 return data; |
| 299 } |
| 300 |
| 301 /** |
| 302 * Returns a map of [Parameter] objects constructed from inputted mirrors. |
| 303 */ |
| 304 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { |
| 305 var data = {}; |
| 306 mirrorList.forEach((ParameterMirror mirror) { |
| 307 _currentMember = mirror; |
| 308 data[mirror.simpleName] = new Parameter(mirror.simpleName, |
| 309 mirror.qualifiedName, mirror.isOptional, mirror.isNamed, |
| 310 mirror.hasDefaultValue, mirror.type.qualifiedName, |
| 311 mirror.defaultValue); |
| 312 }); |
| 313 return data; |
| 314 } |
| 315 |
| 316 /** |
| 317 * Writes text to a file in the 'docs' directory. |
| 318 */ |
| 319 void _writeToFile(String text, String filename) { |
| 320 Directory dir = new Directory('docs'); |
| 321 if (!dir.existsSync()) { |
| 322 dir.createSync(); |
| 323 } |
| 324 File file = new File('docs/$filename'); |
| 325 if (!file.existsSync()) { |
| 326 file.createSync(); |
| 327 } |
| 328 file.openSync(); |
| 329 file.writeAsString(text); |
| 330 } |
| 331 |
| 332 /** |
| 334 * Transforms the map by calling toMap on each value in it. | 333 * Transforms the map by calling toMap on each value in it. |
| 335 */ | 334 */ |
| 336 Map recurseMap(Map inputMap) { | 335 Map recurseMap(Map inputMap) { |
| 337 var outputMap = {}; | 336 var outputMap = {}; |
| 338 inputMap.forEach((key, value) { | 337 inputMap.forEach((key, value) { |
| 339 outputMap[key] = value.toMap(); | 338 outputMap[key] = value.toMap(); |
| 340 }); | 339 }); |
| 341 return outputMap; | 340 return outputMap; |
| 342 } | 341 } |
| 343 | 342 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 | 391 |
| 393 /// Methods in the class. | 392 /// Methods in the class. |
| 394 Map<String, Method> methods; | 393 Map<String, Method> methods; |
| 395 | 394 |
| 396 String name; | 395 String name; |
| 397 String qualifiedName; | 396 String qualifiedName; |
| 398 String superclass; | 397 String superclass; |
| 399 bool isAbstract; | 398 bool isAbstract; |
| 400 bool isTypedef; | 399 bool isTypedef; |
| 401 | 400 |
| 402 Class(this.name, this.qualifiedName, this.superclass, this.isAbstract, this.is
Typedef, | 401 Class(this.name, this.qualifiedName, this.superclass, this.isAbstract, |
| 403 this.comment, this.interfaces, this.variables, this.methods); | 402 this.isTypedef, this.comment, this.interfaces, this.variables, |
| 403 this.methods); |
| 404 | 404 |
| 405 /// Generates a map describing the [Class] object. | 405 /// Generates a map describing the [Class] object. |
| 406 Map toMap() { | 406 Map toMap() { |
| 407 var classMap = {}; | 407 var classMap = {}; |
| 408 classMap['name'] = name; | 408 classMap['name'] = name; |
| 409 classMap['qualifiedname'] = qualifiedName; | 409 classMap['qualifiedname'] = qualifiedName; |
| 410 classMap['comment'] = comment; | 410 classMap['comment'] = comment; |
| 411 classMap['superclass'] = superclass; | 411 classMap['superclass'] = superclass; |
| 412 classMap['abstract'] = isAbstract.toString(); | 412 classMap['abstract'] = isAbstract.toString(); |
| 413 classMap['typedef'] = isTypedef.toString(); | 413 classMap['typedef'] = isTypedef.toString(); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 class Parameter { | 493 class Parameter { |
| 494 | 494 |
| 495 String name; | 495 String name; |
| 496 String qualifiedName; | 496 String qualifiedName; |
| 497 bool isOptional; | 497 bool isOptional; |
| 498 bool isNamed; | 498 bool isNamed; |
| 499 bool hasDefaultValue; | 499 bool hasDefaultValue; |
| 500 String type; | 500 String type; |
| 501 String defaultValue; | 501 String defaultValue; |
| 502 | 502 |
| 503 Parameter(this.name, this.qualifiedName, this.isOptional, this.isNamed, this.h
asDefaultValue, | 503 Parameter(this.name, this.qualifiedName, this.isOptional, this.isNamed, |
| 504 this.type, this.defaultValue); | 504 this.hasDefaultValue, this.type, this.defaultValue); |
| 505 | 505 |
| 506 /// Generates a map describing the [Parameter] object. | 506 /// Generates a map describing the [Parameter] object. |
| 507 Map toMap() { | 507 Map toMap() { |
| 508 var parameterMap = {}; | 508 var parameterMap = {}; |
| 509 parameterMap['name'] = name; | 509 parameterMap['name'] = name; |
| 510 parameterMap['qualifiedname'] = qualifiedName; | 510 parameterMap['qualifiedname'] = qualifiedName; |
| 511 parameterMap['optional'] = isOptional.toString(); | 511 parameterMap['optional'] = isOptional.toString(); |
| 512 parameterMap['named'] = isNamed.toString(); | 512 parameterMap['named'] = isNamed.toString(); |
| 513 parameterMap['default'] = hasDefaultValue.toString(); | 513 parameterMap['default'] = hasDefaultValue.toString(); |
| 514 parameterMap['type'] = type; | 514 parameterMap['type'] = type; |
| 515 parameterMap['value'] = defaultValue; | 515 parameterMap['value'] = defaultValue; |
| 516 return parameterMap; | 516 return parameterMap; |
| 517 } | 517 } |
| 518 } | 518 } |
| 519 | |
| 520 /** | |
| 521 * Writes text to a file in the 'docs' directory. | |
| 522 */ | |
| 523 void _writeToFile(String text, String filename) { | |
| 524 Directory dir = new Directory('docs'); | |
| 525 if (!dir.existsSync()) { | |
| 526 dir.createSync(); | |
| 527 } | |
| 528 File file = new File('docs/$filename'); | |
| 529 if (!file.existsSync()) { | |
| 530 file.createSync(); | |
| 531 } | |
| 532 file.openSync(); | |
| 533 file.writeAsString(text); | |
| 534 } | |
| OLD | NEW |