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, destination, id.description["path"], | |
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 dynamic parseDescription(String containingPath, description, | |
51 {bool fromLockFile: false}) { | |
52 // A path coming from a pubspec is a simple string. From a lock file, it's | |
53 // an expanded {"path": ..., "relative": ...} map. | |
nweiz
2013/02/16 01:02:14
This might be a little more helpful if it were in
Bob Nystrom
2013/02/16 01:19:04
Done.
| |
54 if (fromLockFile) { | |
55 if (description is! Map) { | |
56 throw new FormatException("The description must be a map."); | |
57 } | |
58 | |
59 if (description["path"] is! String) { | |
60 throw new FormatException("The 'path' field of the description must " | |
61 "be a string."); | |
62 } | |
63 | |
64 if (description["relative"] is! bool) { | |
65 throw new FormatException("The 'relative' field of the description " | |
66 "must be a boolean."); | |
67 } | |
68 | |
69 return description; | |
70 } | |
71 | |
44 if (description is! String) { | 72 if (description is! String) { |
45 throw new FormatException("The description must be a path string."); | 73 throw new FormatException("The description must be a path string."); |
46 } | 74 } |
75 | |
76 // Resolve the path relative to the containing file path, and remember | |
77 // whether the original path was relative or absolute. | |
78 bool isRelative = path.isRelative(description); | |
79 if (path.isRelative(description)) { | |
80 // Can't handle relative paths coming from pubspecs that are not on the | |
81 // local file system. | |
82 assert(containingPath != null); | |
83 | |
84 description = path.join(path.dirname(containingPath), description); | |
85 } | |
86 | |
87 return { | |
88 "path": description, | |
89 "relative": isRelative | |
90 }; | |
47 } | 91 } |
48 | 92 |
49 /// Ensures that [dir] is a valid path. It must be an absolute path that | 93 /// 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 | 94 /// with a "path" key containing a path that points to an existing directory. |
51 /// is invalid. | 95 /// Throws a [FormatException] if the path is invalid. |
52 void _validatePath(String name, String dir) { | 96 void _validatePath(String name, description) { |
53 // Relative paths are not (currently) allowed because the user would expect | 97 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 | 98 |
68 if (fileExists(dir)) { | 99 if (fileExists(dir)) { |
69 throw new FormatException( | 100 throw new FormatException( |
70 "Path dependency for package '$name' must refer to a " | 101 "Path dependency for package '$name' must refer to a " |
71 "directory, not a file. Was '$dir'."); | 102 "directory, not a file. Was '$dir'."); |
72 } | 103 } |
73 | 104 |
74 if (!dirExists(dir)) { | 105 if (!dirExists(dir)) { |
75 throw new FormatException("Could not find package '$name' at '$dir'."); | 106 throw new FormatException("Could not find package '$name' at '$dir'."); |
76 } | 107 } |
77 } | 108 } |
78 } | 109 } |
OLD | NEW |