| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library docgen.package_helpers; | 5 library docgen.package_helpers; |
| 6 | 6 |
| 7 import 'exports/source_mirrors.dart'; | 7 import 'exports/source_mirrors.dart'; |
| 8 import 'generator.dart' show pubScript; | 8 import 'generator.dart' show pubScript; |
| 9 | 9 |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| 11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
| 12 import 'package:yaml/yaml.dart'; | 12 import 'package:yaml/yaml.dart'; |
| 13 | 13 |
| 14 /// Helper accessor to determine the full pathname of the root of the dart | 14 /// Helper accessor to determine the full pathname of the root of the dart |
| 15 /// checkout. We can be in one of three situations: | 15 /// checkout. We can be in one of three situations: |
| 16 /// 1) Running from pkg/docgen/bin/docgen.dart | 16 /// 1) Running from pkg/docgen/bin/docgen.dart |
| 17 /// 2) Running from a snapshot in a build, | 17 /// 2) Running from a snapshot in a build, |
| 18 /// e.g. xcodebuild/ReleaseIA32/dart-sdk/bin | 18 /// e.g. xcodebuild/ReleaseIA32/dart-sdk/bin |
| 19 /// 3) Running from a built distribution, | 19 /// 3) Running from a built distribution, |
| 20 /// e.g. ...somename/dart-sdk/bin/snapshots | 20 /// e.g. ...somename/dart-sdk/bin/snapshots |
| 21 String get rootDirectory { | 21 String get rootDirectory { |
| 22 if (_rootDirectoryCache != null) return _rootDirectoryCache; | 22 if (_rootDirectoryCache != null) return _rootDirectoryCache; |
| 23 var scriptDir = path.absolute(path.dirname(Platform.script.toFilePath())); | 23 var scriptDir = path.absolute(path.dirname(Platform.script.toFilePath())); |
| 24 var root = scriptDir; | 24 var root = scriptDir; |
| 25 var base = path.basename(root); | 25 var base = path.basename(root); |
| 26 var found = false; | 26 // When we find dart-sdk or sdk we are one level below the root. |
| 27 // When we find a version file or sdk we are one level below the root. | 27 while (base != 'dart-sdk' && base != 'sdk' && base != 'pkg') { |
| 28 while (!found && base != 'sdk' && base != 'pkg') { | |
| 29 root = path.dirname(root); | 28 root = path.dirname(root); |
| 30 base = path.basename(root); | 29 base = path.basename(root); |
| 31 // Look for something that looks like the dart-sdk directory, which we | |
| 32 // expect to be in the path above us and to have a 'version' file containing | |
| 33 // the same version as we are running. | |
| 34 if (hasMatchingVersionFile(root)) { | |
| 35 found = true; | |
| 36 } | |
| 37 if (root == base) { | 30 if (root == base) { |
| 38 // We have reached the root of the filesystem without finding anything. | 31 // We have reached the root of the filesystem without finding anything. |
| 39 throw new FileSystemException("Cannot find SDK directory starting from ", | 32 throw new FileSystemException("Cannot find SDK directory starting from ", |
| 40 scriptDir); | 33 scriptDir); |
| 41 } | 34 } |
| 42 } | 35 } |
| 43 _rootDirectoryCache = path.dirname(root); | 36 _rootDirectoryCache = path.dirname(root); |
| 44 return _rootDirectoryCache; | 37 return _rootDirectoryCache; |
| 45 } | 38 } |
| 46 String _rootDirectoryCache; | 39 String _rootDirectoryCache; |
| 47 | 40 |
| 48 | |
| 49 /// Does this directory contain a version file that has the same version as | |
| 50 /// we do. | |
| 51 bool hasMatchingVersionFile(String root) { | |
| 52 var versionFile = new File(path.join(root, 'version')); | |
| 53 if (!versionFile.existsSync()) return false; | |
| 54 var version = versionFile.readAsStringSync().trim(); | |
| 55 var vmVersion = Platform.version; | |
| 56 return vmVersion.startsWith(version); | |
| 57 } | |
| 58 | |
| 59 /// Given a LibraryMirror that is a library, return the name of the directory | 41 /// Given a LibraryMirror that is a library, return the name of the directory |
| 60 /// holding the package information for that library. If the library is not | 42 /// holding the package information for that library. If the library is not |
| 61 /// part of a package, return null. | 43 /// part of a package, return null. |
| 62 String getPackageDirectory(LibraryMirror mirror) { | 44 String getPackageDirectory(LibraryMirror mirror) { |
| 63 var file = mirror.uri.toFilePath(); | 45 var file = mirror.uri.toFilePath(); |
| 64 // Any file that's in a package will be in a directory of the form | 46 // Any file that's in a package will be in a directory of the form |
| 65 // packagename/lib/.../filename.dart, so we know that a possible | 47 // packagename/lib/.../filename.dart, so we know that a possible |
| 66 // package directory is at least in the directory above the one containing | 48 // package directory is at least in the directory above the one containing |
| 67 // [file] | 49 // [file] |
| 68 var directoryAbove = path.dirname(path.dirname(file)); | 50 var directoryAbove = path.dirname(path.dirname(file)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 while (!_pubspecFor(dir).existsSync()) { | 117 while (!_pubspecFor(dir).existsSync()) { |
| 136 var newDir = path.dirname(dir); | 118 var newDir = path.dirname(dir); |
| 137 if (newDir == dir) return null; | 119 if (newDir == dir) return null; |
| 138 dir = newDir; | 120 dir = newDir; |
| 139 } | 121 } |
| 140 return dir; | 122 return dir; |
| 141 } | 123 } |
| 142 | 124 |
| 143 File _pubspecFor(String directoryName) => | 125 File _pubspecFor(String directoryName) => |
| 144 new File(path.join(directoryName, 'pubspec.yaml')); | 126 new File(path.join(directoryName, 'pubspec.yaml')); |
| OLD | NEW |