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