| 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 '../../../../sdk/lib/_internal/compiler/implementation/mirrors/source_mir
rors.dart'; |
| 8 |
| 9 import 'dart:io'; |
| 10 import 'package:path/path.dart' as path; |
| 11 import 'package:yaml/yaml.dart'; |
| 12 |
| 13 /// Helper accessor to determine the full pathname of the root of the dart |
| 14 /// checkout. We can be in one of three situations: |
| 15 /// 1) Running from pkg/docgen/bin/docgen.dart |
| 16 /// 2) Running from a snapshot in a build, |
| 17 /// e.g. xcodebuild/ReleaseIA32/dart-sdk/bin |
| 18 /// 3) Running from a built distribution, |
| 19 /// e.g. ...somename/dart-sdk/bin/snapshots |
| 20 String get rootDirectory { |
| 21 if (_rootDirectoryCache != null) return _rootDirectoryCache; |
| 22 var scriptDir = path.absolute(path.dirname(Platform.script.toFilePath())); |
| 23 var root = scriptDir; |
| 24 var base = path.basename(root); |
| 25 // When we find dart-sdk or sdk we are one level below the root. |
| 26 while (base != 'dart-sdk' && base != 'sdk' && base != 'pkg') { |
| 27 root = path.dirname(root); |
| 28 base = path.basename(root); |
| 29 if (root == base) { |
| 30 // We have reached the root of the filesystem without finding anything. |
| 31 throw new FileSystemException("Cannot find SDK directory starting from ", |
| 32 scriptDir); |
| 33 } |
| 34 } |
| 35 _rootDirectoryCache = path.dirname(root); |
| 36 return _rootDirectoryCache; |
| 37 } |
| 38 String _rootDirectoryCache; |
| 39 |
| 40 /// Given a LibraryMirror that is a library, return the name of the directory |
| 41 /// holding the package information for that library. If the library is not |
| 42 /// part of a package, return null. |
| 43 String getPackageDirectory(LibraryMirror mirror) { |
| 44 var file = mirror.uri.toFilePath(); |
| 45 // Any file that's in a package will be in a directory of the form |
| 46 // packagename/lib/.../filename.dart, so we know that a possible |
| 47 // package directory is at least in the directory above the one containing |
| 48 // [file] |
| 49 var directoryAbove = path.dirname(path.dirname(file)); |
| 50 var possiblePackage = _packageDirectoryFor(directoryAbove); |
| 51 // We only want components that are somewhere underneath the lib directory. |
| 52 var subPath = path.relative(file, from: possiblePackage); |
| 53 var subPathComponents = path.split(subPath); |
| 54 if (subPathComponents.isNotEmpty && subPathComponents.first == 'lib') { |
| 55 return possiblePackage; |
| 56 } else { |
| 57 return null; |
| 58 } |
| 59 } |
| 60 |
| 61 /// Read a pubspec and return the library name, given a directory |
| 62 String packageNameFor(String directoryName) { |
| 63 var pubspecName = path.join(directoryName, 'pubspec.yaml'); |
| 64 File pubspec = new File(pubspecName); |
| 65 if (!pubspec.existsSync()) return ''; |
| 66 var contents = pubspec.readAsStringSync(); |
| 67 var spec = loadYaml(contents); |
| 68 return spec["name"]; |
| 69 } |
| 70 |
| 71 /// Recursively walk up from directory name looking for a pubspec. Return |
| 72 /// the directory that contains it, or null if none is found. |
| 73 String _packageDirectoryFor(String directoryName) { |
| 74 var dir = directoryName; |
| 75 while (!_pubspecFor(dir).existsSync()) { |
| 76 var newDir = path.dirname(dir); |
| 77 if (newDir == dir) return null; |
| 78 dir = newDir; |
| 79 } |
| 80 return dir; |
| 81 } |
| 82 |
| 83 File _pubspecFor(String directoryName) => |
| 84 new File(path.join(directoryName, 'pubspec.yaml')); |
| OLD | NEW |