| 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 /// **docgen** is a tool for creating machine readable representations of Dart | 5 /// **docgen** is a tool for creating machine readable representations of Dart |
| 6 /// code metadata, including: classes, members, comments and annotations. | 6 /// code metadata, including: classes, members, comments and annotations. |
| 7 /// | 7 /// |
| 8 /// docgen is run on a `.dart` file or a directory containing `.dart` files. | 8 /// docgen is run on a `.dart` file or a directory containing `.dart` files. |
| 9 /// | 9 /// |
| 10 /// $ dart docgen.dart [OPTIONS] [FILE/DIR] | 10 /// $ dart docgen.dart [OPTIONS] [FILE/DIR] |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 import 'package:path/path.dart' as path; | 22 import 'package:path/path.dart' as path; |
| 23 import 'package:yaml/yaml.dart'; | 23 import 'package:yaml/yaml.dart'; |
| 24 | 24 |
| 25 import 'dart2yaml.dart'; | 25 import 'dart2yaml.dart'; |
| 26 import 'src/io.dart'; | 26 import 'src/io.dart'; |
| 27 import '../../../sdk/lib/_internal/compiler/compiler.dart' as api; | 27 import '../../../sdk/lib/_internal/compiler/compiler.dart' as api; |
| 28 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart'; | 28 import '../../../sdk/lib/_internal/compiler/implementation/filenames.dart'; |
| 29 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro
r.dart' | 29 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro
r.dart' |
| 30 as dart2js; | 30 as dart2js; |
| 31 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'
; | 31 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'
; |
| 32 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.
dart' |
| 33 as dart2js_util; |
| 32 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider.
dart'; | 34 import '../../../sdk/lib/_internal/compiler/implementation/source_file_provider.
dart'; |
| 33 import '../../../sdk/lib/_internal/libraries.dart'; | 35 import '../../../sdk/lib/_internal/libraries.dart'; |
| 34 | 36 |
| 35 var logger = new Logger('Docgen'); | 37 var logger = new Logger('Docgen'); |
| 36 | 38 |
| 37 const String USAGE = 'Usage: dart docgen.dart [OPTIONS] fooDir/barFile'; | 39 const String USAGE = 'Usage: dart docgen.dart [OPTIONS] fooDir/barFile'; |
| 38 | 40 |
| 39 | 41 |
| 40 List<String> skippedAnnotations = const [ | 42 List<String> skippedAnnotations = const [ |
| 41 'metadata.DocsEditable', '_js_helper.JSName', '_js_helper.Creates', | 43 'metadata.DocsEditable', '_js_helper.JSName', '_js_helper.Creates', |
| 42 '_js_helper.Returns', 'observe-src-metadata.Reflectable']; | 44 '_js_helper.Returns', 'observe-src-metadata.Reflectable']; |
| 43 | 45 |
| 46 /// Set of libraries declared in the SDK, so libraries that can be accessed |
| 47 /// when running dart by default. |
| 48 Iterable<LibraryMirror> _sdkLibraries; |
| 49 |
| 50 /// The dart:core library, which contains all types that are always available |
| 51 /// without import. |
| 52 LibraryMirror _coreLibrary; |
| 53 |
| 44 /// Current library being documented to be used for comment links. | 54 /// Current library being documented to be used for comment links. |
| 45 LibraryMirror _currentLibrary; | 55 LibraryMirror _currentLibrary; |
| 46 | 56 |
| 47 /// Current class being documented to be used for comment links. | 57 /// Current class being documented to be used for comment links. |
| 48 ClassMirror _currentClass; | 58 ClassMirror _currentClass; |
| 49 | 59 |
| 50 /// Current member being documented to be used for comment links. | 60 /// Current member being documented to be used for comment links. |
| 51 MemberMirror _currentMember; | 61 MemberMirror _currentMember; |
| 52 | 62 |
| 53 /// Support for [:foo:]-style code comments to the markdown parser. | 63 /// Support for [:foo:]-style code comments to the markdown parser. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 fixReference(name, _currentLibrary, _currentClass, _currentMember); | 112 fixReference(name, _currentLibrary, _currentClass, _currentMember); |
| 103 | 113 |
| 104 return getMirrorSystem(files, packageRoot: packageRoot, parseSdk: parseSdk) | 114 return getMirrorSystem(files, packageRoot: packageRoot, parseSdk: parseSdk) |
| 105 .then((MirrorSystem mirrorSystem) { | 115 .then((MirrorSystem mirrorSystem) { |
| 106 if (mirrorSystem.libraries.isEmpty) { | 116 if (mirrorSystem.libraries.isEmpty) { |
| 107 throw new StateError('No library mirrors were created.'); | 117 throw new StateError('No library mirrors were created.'); |
| 108 } | 118 } |
| 109 var librariesWeAskedFor = _listLibraries(files); | 119 var librariesWeAskedFor = _listLibraries(files); |
| 110 var librariesWeGot = mirrorSystem.libraries.values.where( | 120 var librariesWeGot = mirrorSystem.libraries.values.where( |
| 111 (each) => each.uri.scheme == 'file'); | 121 (each) => each.uri.scheme == 'file'); |
| 112 var sdkLibraries = mirrorSystem.libraries.values.where( | 122 _sdkLibraries = mirrorSystem.libraries.values.where( |
| 113 (each) => each.uri.scheme == 'dart'); | 123 (each) => each.uri.scheme == 'dart'); |
| 124 _coreLibrary = _sdkLibraries.singleWhere((lib) => |
| 125 lib.uri.toString().startsWith('dart:core')); |
| 114 var librariesWeGotByPath = new Map.fromIterables( | 126 var librariesWeGotByPath = new Map.fromIterables( |
| 115 librariesWeGot.map((each) => each.uri.toFilePath()), | 127 librariesWeGot.map((each) => each.uri.toFilePath()), |
| 116 librariesWeGot); | 128 librariesWeGot); |
| 117 var librariesToDocument = librariesWeAskedFor.map( | 129 var librariesToDocument = librariesWeAskedFor.map( |
| 118 (each) => librariesWeGotByPath.putIfAbsent(each, | 130 (each) => librariesWeGotByPath.putIfAbsent(each, |
| 119 () => throw "Missing library $each")).toList(); | 131 () => throw "Missing library $each")).toList(); |
| 120 librariesToDocument.addAll((includeSdk || parseSdk) ? sdkLibraries : []); | 132 librariesToDocument.addAll((includeSdk || parseSdk) ? _sdkLibraries : []); |
| 121 _documentLibraries(librariesToDocument, includeSdk: includeSdk, | 133 _documentLibraries(librariesToDocument, includeSdk: includeSdk, |
| 122 outputToYaml: outputToYaml, append: append, parseSdk: parseSdk, | 134 outputToYaml: outputToYaml, append: append, parseSdk: parseSdk, |
| 123 introduction: introduction); | 135 introduction: introduction); |
| 124 return true; | 136 return true; |
| 125 }); | 137 }); |
| 126 } | 138 } |
| 127 | 139 |
| 128 /// For a [library] and its corresponding [mirror] that we believe come | 140 /// For a [library] and its corresponding [mirror] that we believe come |
| 129 /// from a package (because it has a file | 141 /// from a package (because it has a file |
| 130 /// URI) look for the package name and set it on [library]. | 142 /// URI) look for the package name and set it on [library]. |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 | 518 |
| 507 String _htmlMdn(String content, String url) { | 519 String _htmlMdn(String content, String url) { |
| 508 return '<div class="mdn">' + content.trim() + '<p class="mdn-note">' | 520 return '<div class="mdn">' + content.trim() + '<p class="mdn-note">' |
| 509 '<a href="' + url.trim() + '">from Mdn</a></p></div>'; | 521 '<a href="' + url.trim() + '">from Mdn</a></p></div>'; |
| 510 } | 522 } |
| 511 | 523 |
| 512 /// Look for the specified name starting with the current member, and | 524 /// Look for the specified name starting with the current member, and |
| 513 /// progressively working outward to the current library scope. | 525 /// progressively working outward to the current library scope. |
| 514 String findElementInScope(String name, LibraryMirror currentLibrary, | 526 String findElementInScope(String name, LibraryMirror currentLibrary, |
| 515 ClassMirror currentClass, MemberMirror currentMember) { | 527 ClassMirror currentClass, MemberMirror currentMember) { |
| 528 determineLookupFunc(name) => name.contains('.') ? |
| 529 dart2js_util.lookupQualifiedInScope : |
| 530 (mirror, name) => mirror.lookupInScope(name); |
| 531 var lookupFunc = determineLookupFunc(name); |
| 532 |
| 516 var memberScope = currentMember == null ? | 533 var memberScope = currentMember == null ? |
| 517 null : currentMember.lookupInScope(name); | 534 null : lookupFunc(currentMember, name); |
| 518 if (memberScope != null) { | 535 if (memberScope != null) return docName(memberScope); |
| 519 return docName(memberScope); | 536 |
| 520 } else { | 537 var classScope = currentClass == null ? |
| 521 var classScope = currentClass == null ? | 538 null : lookupFunc(currentClass, name); |
| 522 null : currentClass.lookupInScope(name); | 539 if (classScope != null) return docName(classScope); |
| 523 if (classScope != null) { | 540 |
| 524 return docName(classScope); | 541 var libraryScope = currentLibrary == null ? |
| 525 } else { | 542 null : lookupFunc(currentLibrary, name); |
| 526 var libraryScope = currentLibrary == null ? | 543 if (libraryScope != null) return docName(libraryScope); |
| 527 null : currentLibrary.lookupInScope(name); | 544 |
| 528 if (libraryScope != null) { | 545 // Look in the dart core library scope. |
| 529 return docName(libraryScope); | 546 var coreScope = lookupFunc(_coreLibrary, name); |
| 530 } | 547 if (coreScope != null) return docName(_coreLibrary); |
| 548 |
| 549 // If it's a reference that starts with a another library name, then it |
| 550 // looks for a match of that library name in the other sdk libraries. |
| 551 if(name.contains('.')) { |
| 552 var index = name.indexOf('.'); |
| 553 var libraryName = name.substring(0, index); |
| 554 var remainingName = name.substring(index + 1); |
| 555 foundLibraryName(library) => library.uri.pathSegments[0] == libraryName; |
| 556 |
| 557 if (_sdkLibraries.any(foundLibraryName)) { |
| 558 var library = _sdkLibraries.singleWhere(foundLibraryName); |
| 559 // Look to see if it's a fully qualified library name. |
| 560 var scope = determineLookupFunc(remainingName)(library, remainingName); |
| 561 if (scope != null) return docName(scope); |
| 531 } | 562 } |
| 532 } | 563 } |
| 533 return null; | 564 return null; |
| 534 } | 565 } |
| 535 | 566 |
| 536 // HTML escaped version of '<' character. | 567 // HTML escaped version of '<' character. |
| 537 final _LESS_THAN = '<'; | 568 final _LESS_THAN = '<'; |
| 538 | 569 |
| 539 /// Chunk the provided name into individual parts to be resolved. We take a | 570 /// Chunk the provided name into individual parts to be resolved. We take a |
| 540 /// simplistic approach to chunking, though, we break at " ", ",", "<" | 571 /// simplistic approach to chunking, though, we break at " ", ",", "<" |
| (...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1325 String docName(DeclarationMirror m) { | 1356 String docName(DeclarationMirror m) { |
| 1326 if (m is LibraryMirror) { | 1357 if (m is LibraryMirror) { |
| 1327 return (m as LibraryMirror).qualifiedName.replaceAll('.','-'); | 1358 return (m as LibraryMirror).qualifiedName.replaceAll('.','-'); |
| 1328 } | 1359 } |
| 1329 var owner = m.owner; | 1360 var owner = m.owner; |
| 1330 if (owner == null) return m.qualifiedName; | 1361 if (owner == null) return m.qualifiedName; |
| 1331 // For the unnamed constructor we just return the class name. | 1362 // For the unnamed constructor we just return the class name. |
| 1332 if (m.simpleName == '') return docName(owner); | 1363 if (m.simpleName == '') return docName(owner); |
| 1333 return docName(owner) + '.' + m.simpleName; | 1364 return docName(owner) + '.' + m.simpleName; |
| 1334 } | 1365 } |
| OLD | NEW |