| 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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 return true; | 136 return true; |
| 137 }); | 137 }); |
| 138 } | 138 } |
| 139 | 139 |
| 140 /// For a library's [mirror], determine the name of the package (if any) we | 140 /// For a library's [mirror], determine the name of the package (if any) we |
| 141 /// believe it came from (because of its file URI). | 141 /// believe it came from (because of its file URI). |
| 142 /// | 142 /// |
| 143 /// If [library] is specified, we set the packageName field. If no package could | 143 /// If [library] is specified, we set the packageName field. If no package could |
| 144 /// be determined, we return an empty string. | 144 /// be determined, we return an empty string. |
| 145 String _findPackage(LibraryMirror mirror, [Library library]) { | 145 String _findPackage(LibraryMirror mirror, [Library library]) { |
| 146 if (library == null) { |
| 147 library = entityMap[mirror.simpleName]; |
| 148 } |
| 149 if (library != null) { |
| 150 if (library.hasBeenCheckedForPackage) return library.packageName; |
| 151 library.hasBeenCheckedForPackage = true; |
| 152 } |
| 146 if (mirror.uri.scheme != 'file') return ''; | 153 if (mirror.uri.scheme != 'file') return ''; |
| 147 var filePath = mirror.uri.toFilePath(); | 154 var filePath = mirror.uri.toFilePath(); |
| 148 // We assume that we are documenting only libraries under package/lib | 155 // We assume that we are documenting only libraries under package/lib |
| 149 var rootdir = path.dirname((path.dirname(filePath))); | 156 var rootdir = path.dirname((path.dirname(filePath))); |
| 150 var pubspec = path.join(rootdir, 'pubspec.yaml'); | 157 var pubspec = path.join(rootdir, 'pubspec.yaml'); |
| 151 var packageName = _packageName(pubspec); | 158 var packageName = _packageName(pubspec); |
| 152 if (library != null) { | 159 if (library != null) { |
| 153 library.packageName = packageName; | 160 library.packageName = packageName; |
| 154 // If we are the main library in a package, associate the package readme | 161 // Associate the package readme with all the libraries. This is a bit |
| 155 // with us. | 162 // wasteful, but easier than trying to figure out which partial match |
| 156 // TODO(alanknight): We can't really rely on all packages having a library | 163 // is best. |
| 157 // that matches the package name. Need a better way to store this. | 164 library.packageIntro = _packageIntro(rootdir); |
| 158 if (library.packageName == library.name) { | |
| 159 library.packageIntro = _packageIntro(rootdir); | |
| 160 } | |
| 161 } | 165 } |
| 162 return packageName; | 166 return packageName; |
| 163 } | 167 } |
| 164 | 168 |
| 165 String _packageIntro(packageDir) { | 169 String _packageIntro(packageDir) { |
| 166 var dir = new Directory(packageDir); | 170 var dir = new Directory(packageDir); |
| 167 var files = dir.listSync(); | 171 var files = dir.listSync(); |
| 168 var readmes = files.where((FileSystemEntity each) => (each is File && | 172 var readmes = files.where((FileSystemEntity each) => (each is File && |
| 169 each.path.substring(packageDir.length + 1, each.path.length) | 173 each.path.substring(packageDir.length + 1, each.path.length) |
| 170 .startsWith('README'))).toList(); | 174 .startsWith('README'))).toList(); |
| 171 if (readmes.isEmpty) return ''; | 175 if (readmes.isEmpty) return ''; |
| 172 // If there are multiples, pick the shortest name. | 176 // If there are multiples, pick the shortest name. |
| 173 readmes.sort((a, b) => a.length.compareTo(b.length)); | 177 readmes.sort((a, b) => a.length.compareTo(b.length)); |
| 174 var readme = readmes.first; | 178 var readme = readmes.first; |
| 175 var contents = markdown.markdownToHtml(readme | 179 var contents = markdown.markdownToHtml(readme |
| 176 .readAsStringSync(), linkResolver: linkResolver, | 180 .readAsStringSync(), linkResolver: linkResolver, |
| 177 inlineSyntaxes: markdownSyntaxes); | 181 inlineSyntaxes: markdownSyntaxes); |
| 178 return contents; | 182 return contents; |
| 179 } | 183 } |
| 180 | 184 |
| 181 | |
| 182 List<String> _listLibraries(List<String> args) { | 185 List<String> _listLibraries(List<String> args) { |
| 183 var libraries = new List<String>(); | 186 var libraries = new List<String>(); |
| 184 for (var arg in args) { | 187 for (var arg in args) { |
| 185 var type = FileSystemEntity.typeSync(arg); | 188 var type = FileSystemEntity.typeSync(arg); |
| 186 | 189 |
| 187 if (type == FileSystemEntityType.FILE) { | 190 if (type == FileSystemEntityType.FILE) { |
| 188 if (arg.endsWith('.dart')) { | 191 if (arg.endsWith('.dart')) { |
| 189 libraries.add(path.absolute(arg)); | 192 libraries.add(path.absolute(arg)); |
| 190 logger.info('Added to libraries: ${libraries.last}'); | 193 logger.info('Added to libraries: ${libraries.last}'); |
| 191 } | 194 } |
| (...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 861 /// Top-level variables in the library. | 864 /// Top-level variables in the library. |
| 862 Map<String, Variable> variables; | 865 Map<String, Variable> variables; |
| 863 | 866 |
| 864 /// Top-level functions in the library. | 867 /// Top-level functions in the library. |
| 865 MethodGroup functions; | 868 MethodGroup functions; |
| 866 | 869 |
| 867 /// Classes defined within the library | 870 /// Classes defined within the library |
| 868 ClassGroup classes; | 871 ClassGroup classes; |
| 869 | 872 |
| 870 String packageName = ''; | 873 String packageName = ''; |
| 874 bool hasBeenCheckedForPackage = false; |
| 871 | 875 |
| 872 String get packagePrefix => packageName == null || packageName.isEmpty | 876 String get packagePrefix => packageName == null || packageName.isEmpty |
| 873 ? '' | 877 ? '' |
| 874 : '$packageName/'; | 878 : '$packageName/'; |
| 875 | 879 |
| 876 String packageIntro; | 880 String packageIntro; |
| 877 | 881 |
| 878 Map get previewMap { | 882 Map get previewMap { |
| 879 var basic = super.previewMap; | 883 var basic = super.previewMap; |
| 880 basic['packageName'] = packageName; | 884 basic['packageName'] = packageName; |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1396 /// Remove statics from the map of inherited items before adding them. | 1400 /// Remove statics from the map of inherited items before adding them. |
| 1397 Map _filterStatics(Map items) { | 1401 Map _filterStatics(Map items) { |
| 1398 var result = {}; | 1402 var result = {}; |
| 1399 items.forEach((name, item) { | 1403 items.forEach((name, item) { |
| 1400 if (!item.isStatic) { | 1404 if (!item.isStatic) { |
| 1401 result[name] = item; | 1405 result[name] = item; |
| 1402 } | 1406 } |
| 1403 }); | 1407 }); |
| 1404 return result; | 1408 return result; |
| 1405 } | 1409 } |
| OLD | NEW |