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["path"], | 28 return new Pubspec.load(id.name, id.description, systemCache.sources); |
29 systemCache.sources); | |
30 }); | 29 }); |
31 } | 30 } |
32 | 31 |
33 Future<bool> install(PackageId id, String destination) { | 32 Future<bool> install(PackageId id, String path) { |
34 return defer(() { | 33 return defer(() { |
35 try { | 34 try { |
36 _validatePath(id.name, id.description); | 35 _validatePath(id.name, id.description); |
37 } on FormatException catch(err) { | 36 } on FormatException catch(err) { |
38 return false; | 37 return false; |
39 } | 38 } |
40 | 39 return createPackageSymlink(id.name, id.description, path); |
41 return createPackageSymlink(id.name, destination, id.description["path"], | |
42 relative: id.description["relative"]); | |
43 }).then((_) => true); | 40 }).then((_) => true); |
44 } | 41 } |
45 | 42 |
46 /// Parses a path dependency. This takes in a path string and returns a map. | 43 void validateDescription(description, {bool fromLockFile: false}) { |
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 | |
73 if (description is! String) { | 44 if (description is! String) { |
74 throw new FormatException("The description must be a path string."); | 45 throw new FormatException("The description must be a path string."); |
75 } | 46 } |
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 }; | |
92 } | 47 } |
93 | 48 |
94 /// Ensures that [description] is a valid path description. It must be a map, | 49 /// Ensures that [dir] is a valid path. It must be an absolute path that |
95 /// with a "path" key containing a path that points to an existing directory. | 50 /// points to an existing directory. Throws a [FormatException] if the path |
96 /// Throws a [FormatException] if the path is invalid. | 51 /// is invalid. |
97 void _validatePath(String name, description) { | 52 void _validatePath(String name, String dir) { |
98 var dir = description["path"]; | 53 // Relative paths are not (currently) allowed because the user would expect |
| 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 } |
99 | 67 |
100 if (fileExists(dir)) { | 68 if (fileExists(dir)) { |
101 throw new FormatException( | 69 throw new FormatException( |
102 "Path dependency for package '$name' must refer to a " | 70 "Path dependency for package '$name' must refer to a " |
103 "directory, not a file. Was '$dir'."); | 71 "directory, not a file. Was '$dir'."); |
104 } | 72 } |
105 | 73 |
106 if (!dirExists(dir)) { | 74 if (!dirExists(dir)) { |
107 throw new FormatException("Could not find package '$name' at '$dir'."); | 75 throw new FormatException("Could not find package '$name' at '$dir'."); |
108 } | 76 } |
109 } | 77 } |
110 } | 78 } |
OLD | NEW |