| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library docgen.package_helpers; | |
| 6 | |
| 7 import 'exports/source_mirrors.dart'; | |
| 8 import 'generator.dart' show pubScript; | |
| 9 | |
| 10 import 'dart:io'; | |
| 11 import 'package:path/path.dart' as path; | |
| 12 import 'package:yaml/yaml.dart'; | |
| 13 | |
| 14 /// Helper accessor to determine the full pathname of the root of the dart | |
| 15 /// checkout if we are running without the --sdk parameter specified. That | |
| 16 /// normally means we are running directly from source, and we expect to | |
| 17 /// find the root as the directory above 'pkg'. The only other time this | |
| 18 /// would happen is running a snapshot directly, rather than from the | |
| 19 /// dartdocgen script, where we look for the dart-sdk directory. If not | |
| 20 /// using the script and not in a normal directory structure, you'll need | |
| 21 /// to pass the --sdk parameter. | |
| 22 String get rootDirectory { | |
| 23 if (_rootDirectoryCache != null) return _rootDirectoryCache; | |
| 24 var scriptDir = path.absolute(path.dirname(Platform.script.toFilePath())); | |
| 25 var root = scriptDir; | |
| 26 var base = path.basename(root); | |
| 27 while (base != 'dart-sdk' && base != 'pkg') { | |
| 28 root = path.dirname(root); | |
| 29 base = path.basename(root); | |
| 30 if (root == base) { | |
| 31 // We have reached the root of the filesystem without finding anything. | |
| 32 throw new FileSystemException("Cannot find SDK directory starting from ", | |
| 33 scriptDir); | |
| 34 } | |
| 35 } | |
| 36 _rootDirectoryCache = path.dirname(root); | |
| 37 return _rootDirectoryCache; | |
| 38 } | |
| 39 String _rootDirectoryCache; | |
| 40 | |
| 41 /// Given a LibraryMirror that is a library, return the name of the directory | |
| 42 /// holding the package information for that library. If the library is not | |
| 43 /// part of a package, return null. | |
| 44 String getPackageDirectory(LibraryMirror mirror) { | |
| 45 var file = mirror.uri.toFilePath(); | |
| 46 // Any file that's in a package will be in a directory of the form | |
| 47 // packagename/lib/.../filename.dart, so we know that a possible | |
| 48 // package directory is at least in the directory above the one containing | |
| 49 // [file] | |
| 50 var directoryAbove = path.dirname(path.dirname(file)); | |
| 51 var possiblePackage = _packageDirectoryFor(directoryAbove); | |
| 52 // We only want components that are somewhere underneath the lib directory. | |
| 53 var subPath = path.relative(file, from: possiblePackage); | |
| 54 var subPathComponents = path.split(subPath); | |
| 55 if (subPathComponents.isNotEmpty && subPathComponents.first == 'lib') { | |
| 56 return possiblePackage; | |
| 57 } else { | |
| 58 return null; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 Map _getPubspec(String directoryName) { | |
| 63 var pubspecName = path.join(directoryName, 'pubspec.yaml'); | |
| 64 File pubspec = new File(pubspecName); | |
| 65 if (!pubspec.existsSync()) return {'name': ''}; | |
| 66 var contents = pubspec.readAsStringSync(); | |
| 67 return loadYaml(contents); | |
| 68 } | |
| 69 | |
| 70 /// Read a pubspec and return the library name, given a directory | |
| 71 String packageNameFor(String directoryName) => | |
| 72 _getPubspec(directoryName)['name']; | |
| 73 | |
| 74 /// Read a pubspec and return the library name, given a directory | |
| 75 String _packageVersionFor(String directoryName) { | |
| 76 var spec = _getPubspec(directoryName); | |
| 77 return '${spec['name']}-${spec['version']}'; | |
| 78 } | |
| 79 | |
| 80 /// Look in the pubspec.lock to determine what version of a package we are | |
| 81 /// documenting (null if not applicable). | |
| 82 String packageVersion(LibraryMirror mirror) { | |
| 83 if (mirror.uri.scheme == 'file') { | |
| 84 String packageDirectory = getPackageDirectory(mirror); | |
| 85 if (packageDirectory == null) return ''; | |
| 86 if (packageDirectory.contains('.pub-cache')) { | |
| 87 return path.basename(packageDirectory); | |
| 88 } | |
| 89 String packageName = packageNameFor(packageDirectory); | |
| 90 return _packageVersionFor(packageDirectory); | |
| 91 } else if (mirror.uri.scheme == 'dart') { | |
| 92 // If this item is from the SDK, use what version of pub we're running to | |
| 93 // ascertain the SDK version. | |
| 94 var pubVersion = Process.runSync(pubScript, ['--version'], | |
| 95 runInShell: true); | |
| 96 if (pubVersion.exitCode != 0) { | |
| 97 print(pubVersion.stderr); | |
| 98 } | |
| 99 return pubVersion.stdout.replaceAll('Pub ', '').trim(); | |
| 100 } | |
| 101 return ''; | |
| 102 } | |
| 103 | |
| 104 String _packageVersionHelper(String packageDirectory, String packageName) { | |
| 105 var publockName = path.join(packageDirectory, 'pubspec.lock'); | |
| 106 File publock = new File(publockName); | |
| 107 if (!publock.existsSync()) return ''; | |
| 108 var contents = publock.readAsStringSync(); | |
| 109 var spec = loadYaml(contents); | |
| 110 return spec['packages'][packageName]; | |
| 111 } | |
| 112 | |
| 113 /// Recursively walk up from directory name looking for a pubspec. Return | |
| 114 /// the directory that contains it, or null if none is found. | |
| 115 String _packageDirectoryFor(String directoryName) { | |
| 116 var dir = directoryName; | |
| 117 while (!_pubspecFor(dir).existsSync()) { | |
| 118 var newDir = path.dirname(dir); | |
| 119 if (newDir == dir) return null; | |
| 120 dir = newDir; | |
| 121 } | |
| 122 return dir; | |
| 123 } | |
| 124 | |
| 125 File _pubspecFor(String directoryName) => | |
| 126 new File(path.join(directoryName, 'pubspec.yaml')); | |
| OLD | NEW |