| 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 1401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1412 | 1412 |
| 1413 /// For a library's [mirror], determine the name of the package (if any) we | 1413 /// For a library's [mirror], determine the name of the package (if any) we |
| 1414 /// believe it came from (because of its file URI). | 1414 /// believe it came from (because of its file URI). |
| 1415 /// | 1415 /// |
| 1416 /// If no package could be determined, we return an empty string. | 1416 /// If no package could be determined, we return an empty string. |
| 1417 String _findPackage(LibraryMirror mirror) { | 1417 String _findPackage(LibraryMirror mirror) { |
| 1418 if (mirror == null) return ''; | 1418 if (mirror == null) return ''; |
| 1419 if (_hasBeenCheckedForPackage) return packageName; | 1419 if (_hasBeenCheckedForPackage) return packageName; |
| 1420 _hasBeenCheckedForPackage = true; | 1420 _hasBeenCheckedForPackage = true; |
| 1421 if (mirror.uri.scheme != 'file') return ''; | 1421 if (mirror.uri.scheme != 'file') return ''; |
| 1422 // We assume that we are documenting only libraries under package/lib | |
| 1423 packageName = _packageName(mirror); | 1422 packageName = _packageName(mirror); |
| 1424 // Associate the package readme with all the libraries. This is a bit | 1423 // Associate the package readme with all the libraries. This is a bit |
| 1425 // wasteful, but easier than trying to figure out which partial match | 1424 // wasteful, but easier than trying to figure out which partial match |
| 1426 // is best. | 1425 // is best. |
| 1427 packageIntro = _packageIntro(_getRootdir(mirror)); | 1426 packageIntro = _packageIntro(_getRootdir(mirror)); |
| 1428 return packageName; | 1427 return packageName; |
| 1429 } | 1428 } |
| 1430 | 1429 |
| 1431 String _packageIntro(packageDir) { | 1430 String _packageIntro(packageDir) { |
| 1431 if (packageDir == null) return null; |
| 1432 var dir = new Directory(packageDir); | 1432 var dir = new Directory(packageDir); |
| 1433 var files = dir.listSync(); | 1433 var files = dir.listSync(); |
| 1434 var readmes = files.where((FileSystemEntity each) => (each is File && | 1434 var readmes = files.where((FileSystemEntity each) => (each is File && |
| 1435 each.path.substring(packageDir.length + 1, each.path.length) | 1435 each.path.substring(packageDir.length + 1, each.path.length) |
| 1436 .startsWith('README'))).toList(); | 1436 .startsWith('README'))).toList(); |
| 1437 if (readmes.isEmpty) return ''; | 1437 if (readmes.isEmpty) return ''; |
| 1438 // If there are multiples, pick the shortest name. | 1438 // If there are multiples, pick the shortest name. |
| 1439 readmes.sort((a, b) => a.path.length.compareTo(b.path.length)); | 1439 readmes.sort((a, b) => a.path.length.compareTo(b.path.length)); |
| 1440 var readme = readmes.first; | 1440 var readme = readmes.first; |
| 1441 var linkResolver = (name) => Indexable.globalFixReference(name); | 1441 var linkResolver = (name) => Indexable.globalFixReference(name); |
| 1442 var contents = markdown.markdownToHtml(readme | 1442 var contents = markdown.markdownToHtml(readme |
| 1443 .readAsStringSync(), linkResolver: linkResolver, | 1443 .readAsStringSync(), linkResolver: linkResolver, |
| 1444 inlineSyntaxes: _MARKDOWN_SYNTAXES); | 1444 inlineSyntaxes: _MARKDOWN_SYNTAXES); |
| 1445 return contents; | 1445 return contents; |
| 1446 } | 1446 } |
| 1447 | 1447 |
| 1448 /// Given a LibraryMirror that is a library, return the name of the directory | 1448 /// Given a LibraryMirror that is a library, return the name of the directory |
| 1449 /// holding that library. | 1449 /// holding the package information for that library. If the library is not |
| 1450 static String _getRootdir(LibraryMirror mirror) => | 1450 /// part of a package, return null. |
| 1451 path.dirname(path.dirname(mirror.uri.toFilePath())); | 1451 static String _getRootdir(LibraryMirror mirror) { |
| 1452 var file = mirror.uri.toFilePath(); |
| 1453 // Any file that's in a package will be in a directory of the form |
| 1454 // packagename/lib/.../filename.dart, so we know that a possible |
| 1455 // package directory is at least in the directory above the one containing |
| 1456 // [file] |
| 1457 var directoryAbove = path.dirname(path.dirname(file)); |
| 1458 return _packageDirectoryFor(directoryAbove); |
| 1459 } |
| 1452 | 1460 |
| 1453 /// Read a pubspec and return the library name given a [LibraryMirror]. | 1461 /// Read a pubspec and return the library name given a [LibraryMirror]. |
| 1454 static String _packageName(LibraryMirror mirror) { | 1462 static String _packageName(LibraryMirror mirror) { |
| 1455 if (mirror.uri.scheme != 'file') return ''; | 1463 if (mirror.uri.scheme != 'file') return ''; |
| 1456 var rootdir = _getRootdir(mirror); | 1464 var rootdir = _getRootdir(mirror); |
| 1465 if (rootdir == null) return ''; |
| 1457 return packageNameFor(rootdir); | 1466 return packageNameFor(rootdir); |
| 1458 } | 1467 } |
| 1459 | 1468 |
| 1469 /// Recursively walk up from directory name looking for a pubspec. Return |
| 1470 /// the directory that contains it, or null if none is found. |
| 1471 static String _packageDirectoryFor(String directoryName) { |
| 1472 var dir = directoryName; |
| 1473 while (!_pubspecFor(dir).existsSync()) { |
| 1474 var newDir = path.dirname(dir); |
| 1475 if (newDir == dir) return null; |
| 1476 dir = newDir; |
| 1477 } |
| 1478 return dir; |
| 1479 } |
| 1480 |
| 1481 static File _pubspecFor(String directoryName) => |
| 1482 new File(path.join(directoryName, 'pubspec.yaml')); |
| 1483 |
| 1460 /// Read a pubspec and return the library name, given a directory | 1484 /// Read a pubspec and return the library name, given a directory |
| 1461 static String packageNameFor(String directoryName) { | 1485 static String packageNameFor(String directoryName) { |
| 1462 var pubspecName = path.join(directoryName, 'pubspec.yaml'); | 1486 var pubspecName = path.join(directoryName, 'pubspec.yaml'); |
| 1463 File pubspec = new File(pubspecName); | 1487 File pubspec = new File(pubspecName); |
| 1464 if (!pubspec.existsSync()) return ''; | 1488 if (!pubspec.existsSync()) return ''; |
| 1465 var contents = pubspec.readAsStringSync(); | 1489 var contents = pubspec.readAsStringSync(); |
| 1466 var spec = loadYaml(contents); | 1490 var spec = loadYaml(contents); |
| 1467 return spec["name"]; | 1491 return spec["name"]; |
| 1468 } | 1492 } |
| 1469 | 1493 |
| (...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2229 .map((e) => originalMirror.getField(e.simpleName).reflectee) | 2253 .map((e) => originalMirror.getField(e.simpleName).reflectee) |
| 2230 .where((e) => e != null) | 2254 .where((e) => e != null) |
| 2231 .toList(); | 2255 .toList(); |
| 2232 } | 2256 } |
| 2233 | 2257 |
| 2234 Map toMap() => { | 2258 Map toMap() => { |
| 2235 'name': Indexable.getDocgenObject(mirror, owningLibrary).docName, | 2259 'name': Indexable.getDocgenObject(mirror, owningLibrary).docName, |
| 2236 'parameters': parameters | 2260 'parameters': parameters |
| 2237 }; | 2261 }; |
| 2238 } | 2262 } |
| OLD | NEW |