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