OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 path_source; | 5 library path_source; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import '../../pkg/path/lib/path.dart' as path; | 10 import '../../pkg/path/lib/path.dart' as path; |
11 | 11 |
12 import 'io.dart'; | 12 import 'io.dart'; |
13 import 'package.dart'; | 13 import 'package.dart'; |
14 import 'pubspec.dart'; | 14 import 'pubspec.dart'; |
15 import 'version.dart'; | 15 import 'version.dart'; |
16 import 'source.dart'; | 16 import 'source.dart'; |
17 import 'utils.dart'; | 17 import 'utils.dart'; |
18 | 18 |
19 // TODO(rnystrom): Support relative paths. (See comment in _validatePath().) | 19 // TODO(rnystrom): Support relative paths. (See comment in _validatePath().) |
20 /// A package [Source] that installs packages from a given local file path. | 20 /// A package [Source] that installs packages from a given local file path. |
21 class PathSource extends Source { | 21 class PathSource extends Source { |
22 final name = 'path'; | 22 final name = 'path'; |
23 final shouldCache = false; | 23 final shouldCache = false; |
24 | 24 |
25 Future<Pubspec> describe(PackageId id) { | 25 Future<Pubspec> describe(PackageId id) { |
26 return defer(() { | 26 return defer(() { |
27 _validatePath(id.name, id.description); | 27 _validatePath(id.name, id.description); |
28 return new Pubspec.load(id.name, id.description, systemCache.sources); | 28 return new Pubspec.load(id.name, id.description["path"], |
| 29 systemCache.sources); |
29 }); | 30 }); |
30 } | 31 } |
31 | 32 |
32 Future<bool> install(PackageId id, String path) { | 33 Future<bool> install(PackageId id, String destination) { |
33 return defer(() { | 34 return defer(() { |
34 try { | 35 try { |
35 _validatePath(id.name, id.description); | 36 _validatePath(id.name, id.description); |
36 } on FormatException catch(err) { | 37 } on FormatException catch(err) { |
37 return false; | 38 return false; |
38 } | 39 } |
39 return createPackageSymlink(id.name, id.description, path); | 40 |
| 41 return createPackageSymlink(id.name, id.description["path"], destination, |
| 42 relative: id.description["relative"]); |
40 }).then((_) => true); | 43 }).then((_) => true); |
41 } | 44 } |
42 | 45 |
43 void validateDescription(description, {bool fromLockFile: false}) { | 46 /// Parses a path dependency. This takes in a path string and returns a map. |
| 47 /// The "path" key will be the original path but resolves relative to the |
| 48 /// containing path. The "relative" key will be `true` if the original path |
| 49 /// was relative. |
| 50 /// |
| 51 /// A path coming from a pubspec is a simple string. From a lock file, it's |
| 52 /// an expanded {"path": ..., "relative": ...} map. |
| 53 dynamic parseDescription(String containingPath, description, |
| 54 {bool fromLockFile: false}) { |
| 55 if (fromLockFile) { |
| 56 if (description is! Map) { |
| 57 throw new FormatException("The description must be a map."); |
| 58 } |
| 59 |
| 60 if (description["path"] is! String) { |
| 61 throw new FormatException("The 'path' field of the description must " |
| 62 "be a string."); |
| 63 } |
| 64 |
| 65 if (description["relative"] is! bool) { |
| 66 throw new FormatException("The 'relative' field of the description " |
| 67 "must be a boolean."); |
| 68 } |
| 69 |
| 70 return description; |
| 71 } |
| 72 |
44 if (description is! String) { | 73 if (description is! String) { |
45 throw new FormatException("The description must be a path string."); | 74 throw new FormatException("The description must be a path string."); |
46 } | 75 } |
| 76 |
| 77 // Resolve the path relative to the containing file path, and remember |
| 78 // whether the original path was relative or absolute. |
| 79 bool isRelative = path.isRelative(description); |
| 80 if (path.isRelative(description)) { |
| 81 // Can't handle relative paths coming from pubspecs that are not on the |
| 82 // local file system. |
| 83 assert(containingPath != null); |
| 84 |
| 85 description = path.join(path.dirname(containingPath), description); |
| 86 } |
| 87 |
| 88 return { |
| 89 "path": description, |
| 90 "relative": isRelative |
| 91 }; |
47 } | 92 } |
48 | 93 |
49 /// Ensures that [dir] is a valid path. It must be an absolute path that | 94 /// Ensures that [description] is a valid path description. It must be a map, |
50 /// points to an existing directory. Throws a [FormatException] if the path | 95 /// with a "path" key containing a path that points to an existing directory. |
51 /// is invalid. | 96 /// Throws a [FormatException] if the path is invalid. |
52 void _validatePath(String name, String dir) { | 97 void _validatePath(String name, description) { |
53 // Relative paths are not (currently) allowed because the user would expect | 98 var dir = description["path"]; |
54 // them to be relative to the pubspec where the dependency appears. That in | |
55 // turn means that two pubspecs in different locations with the same | |
56 // relative path dependency could refer to two different packages. That | |
57 // violates pub's rule that a description should uniquely identify a | |
58 // package. | |
59 // | |
60 // At some point, we may want to loosen this, but it will mean tracking | |
61 // where a given PackageId appeared. | |
62 if (!path.isAbsolute(dir)) { | |
63 throw new FormatException( | |
64 "Path dependency for package '$name' must be an absolute path. " | |
65 "Was '$dir'."); | |
66 } | |
67 | 99 |
68 if (fileExists(dir)) { | 100 if (fileExists(dir)) { |
69 throw new FormatException( | 101 throw new FormatException( |
70 "Path dependency for package '$name' must refer to a " | 102 "Path dependency for package '$name' must refer to a " |
71 "directory, not a file. Was '$dir'."); | 103 "directory, not a file. Was '$dir'."); |
72 } | 104 } |
73 | 105 |
74 if (!dirExists(dir)) { | 106 if (!dirExists(dir)) { |
75 throw new FormatException("Could not find package '$name' at '$dir'."); | 107 throw new FormatException("Could not find package '$name' at '$dir'."); |
76 } | 108 } |
77 } | 109 } |
78 } | 110 } |
OLD | NEW |