| 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 // When we find dart-sdk or sdk we are one level below the root. | 26 var found = false; |
| 27 while (base != 'dart-sdk' && base != 'sdk' && base != 'pkg') { | 27 // When we find a version file or sdk we are one level below the root. |
| 28 while (!found && base != 'sdk' && base != 'pkg') { |
| 28 root = path.dirname(root); | 29 root = path.dirname(root); |
| 29 base = path.basename(root); | 30 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 } |
| 30 if (root == base) { | 37 if (root == base) { |
| 31 // We have reached the root of the filesystem without finding anything. | 38 // We have reached the root of the filesystem without finding anything. |
| 32 throw new FileSystemException("Cannot find SDK directory starting from ", | 39 throw new FileSystemException("Cannot find SDK directory starting from ", |
| 33 scriptDir); | 40 scriptDir); |
| 34 } | 41 } |
| 35 } | 42 } |
| 36 _rootDirectoryCache = path.dirname(root); | 43 _rootDirectoryCache = path.dirname(root); |
| 37 return _rootDirectoryCache; | 44 return _rootDirectoryCache; |
| 38 } | 45 } |
| 39 String _rootDirectoryCache; | 46 String _rootDirectoryCache; |
| 40 | 47 |
| 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 |
| 41 /// Given a LibraryMirror that is a library, return the name of the directory | 59 /// 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 | 60 /// holding the package information for that library. If the library is not |
| 43 /// part of a package, return null. | 61 /// part of a package, return null. |
| 44 String getPackageDirectory(LibraryMirror mirror) { | 62 String getPackageDirectory(LibraryMirror mirror) { |
| 45 var file = mirror.uri.toFilePath(); | 63 var file = mirror.uri.toFilePath(); |
| 46 // Any file that's in a package will be in a directory of the form | 64 // 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 | 65 // packagename/lib/.../filename.dart, so we know that a possible |
| 48 // package directory is at least in the directory above the one containing | 66 // package directory is at least in the directory above the one containing |
| 49 // [file] | 67 // [file] |
| 50 var directoryAbove = path.dirname(path.dirname(file)); | 68 var directoryAbove = path.dirname(path.dirname(file)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 while (!_pubspecFor(dir).existsSync()) { | 135 while (!_pubspecFor(dir).existsSync()) { |
| 118 var newDir = path.dirname(dir); | 136 var newDir = path.dirname(dir); |
| 119 if (newDir == dir) return null; | 137 if (newDir == dir) return null; |
| 120 dir = newDir; | 138 dir = newDir; |
| 121 } | 139 } |
| 122 return dir; | 140 return dir; |
| 123 } | 141 } |
| 124 | 142 |
| 125 File _pubspecFor(String directoryName) => | 143 File _pubspecFor(String directoryName) => |
| 126 new File(path.join(directoryName, 'pubspec.yaml')); | 144 new File(path.join(directoryName, 'pubspec.yaml')); |
| OLD | NEW |