| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 sdk_source; | 5 library sdk_source; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'io.dart'; | 8 import 'io.dart'; |
| 9 import 'package.dart'; | 9 import 'package.dart'; |
| 10 import 'pubspec.dart'; | 10 import 'pubspec.dart'; |
| 11 import 'sdk.dart' as sdk; |
| 11 import 'source.dart'; | 12 import 'source.dart'; |
| 12 import 'version.dart'; | 13 import 'version.dart'; |
| 13 | 14 |
| 14 /// A package source that uses libraries from the Dart SDK. | 15 /// A package source that uses libraries from the Dart SDK. |
| 15 class SdkSource extends Source { | 16 class SdkSource extends Source { |
| 16 final String name = "sdk"; | 17 final String name = "sdk"; |
| 17 final bool shouldCache = false; | 18 final bool shouldCache = false; |
| 18 | 19 |
| 19 /// The root directory of the Dart SDK. | |
| 20 final String _rootDir; | |
| 21 | |
| 22 String get rootDir { | |
| 23 if (_rootDir != null) return _rootDir; | |
| 24 throw "Pub can't find the Dart SDK. Please set the DART_SDK environment " | |
| 25 "variable to the Dart SDK directory."; | |
| 26 } | |
| 27 | |
| 28 SdkSource(this._rootDir); | |
| 29 | |
| 30 /// SDK packages are not individually versioned. Instead, their version is | 20 /// SDK packages are not individually versioned. Instead, their version is |
| 31 /// inferred from the revision number of the SDK itself. | 21 /// inferred from the revision number of the SDK itself. |
| 32 Future<Pubspec> describe(PackageId id) { | 22 Future<Pubspec> describe(PackageId id) { |
| 33 var version; | 23 return _getPackagePath(id).then((packageDir) { |
| 34 return readTextFile(join(rootDir, "revision")).then((revision) { | |
| 35 version = new Version.parse("0.0.0-r.${revision.trim()}"); | |
| 36 // Read the pubspec for the package's dependencies. | |
| 37 return _getPackagePath(id); | |
| 38 }).then((packageDir) { | |
| 39 // TODO(rnystrom): What if packageDir is null? | 24 // TODO(rnystrom): What if packageDir is null? |
| 40 return Package.load(id.name, packageDir, systemCache.sources); | 25 return Package.load(id.name, packageDir, systemCache.sources); |
| 41 }).then((package) { | 26 }).then((package) { |
| 42 // Ignore the pubspec's version, and use the SDK's. | 27 // Ignore the pubspec's version, and use the SDK's. |
| 43 return new Pubspec(id.name, version, package.pubspec.dependencies); | 28 return new Pubspec(id.name, sdk.version, package.pubspec.dependencies); |
| 44 }); | 29 }); |
| 45 } | 30 } |
| 46 | 31 |
| 47 /// Since all the SDK files are already available locally, installation just | 32 /// Since all the SDK files are already available locally, installation just |
| 48 /// involves symlinking the SDK library into the packages directory. | 33 /// involves symlinking the SDK library into the packages directory. |
| 49 Future<bool> install(PackageId id, String destPath) { | 34 Future<bool> install(PackageId id, String destPath) { |
| 50 return _getPackagePath(id).then((path) { | 35 return _getPackagePath(id).then((path) { |
| 51 if (path == null) return new Future<bool>.immediate(false); | 36 if (path == null) return new Future<bool>.immediate(false); |
| 52 | 37 |
| 53 return createPackageSymlink(id.name, path, destPath).then( | 38 return createPackageSymlink(id.name, path, destPath).then( |
| 54 (_) => true); | 39 (_) => true); |
| 55 }); | 40 }); |
| 56 } | 41 } |
| 57 | 42 |
| 58 /// Gets the path in the SDK to the directory containing package [id]. Looks | 43 /// Gets the path in the SDK's "pkg" directory to the directory containing |
| 59 /// inside both "pkg" and "lib" in the SDK. Returns `null` if the package | 44 /// package [id]. Returns `null` if the package could not be found. |
| 60 /// could not be found. | |
| 61 Future<String> _getPackagePath(PackageId id) { | 45 Future<String> _getPackagePath(PackageId id) { |
| 62 // Look in "pkg" first. | 46 var pkgPath = join(sdk.rootDirectory, "pkg", id.description); |
| 63 var pkgPath = join(rootDir, "pkg", id.description); | 47 return dirExists(pkgPath).then((found) => found ? pkgPath : null); |
| 64 return exists(pkgPath).then((found) { | |
| 65 if (found) return new Future<String>.immediate(pkgPath); | |
| 66 | |
| 67 // Not in "pkg", so try "lib". | |
| 68 // TODO(rnystrom): Get rid of this when all SDK packages are moved from | |
| 69 // "lib" to "pkg". | |
| 70 var libPath = join(rootDir, "lib", id.description); | |
| 71 return exists(libPath).then((found) => found ? libPath : null); | |
| 72 }); | |
| 73 } | 48 } |
| 74 } | 49 } |
| OLD | NEW |