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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:path/path.dart' as p; | 7 import 'package:path/path.dart' as p; |
8 import 'package:pub_semver/pub_semver.dart'; | 8 import 'package:pub_semver/pub_semver.dart'; |
9 | 9 |
10 import '../exceptions.dart'; | 10 import '../exceptions.dart'; |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 /// Given a valid path reference description, returns the file path it | 25 /// Given a valid path reference description, returns the file path it |
26 /// describes. | 26 /// describes. |
27 /// | 27 /// |
28 /// This returned path may be relative or absolute and it is up to the caller | 28 /// This returned path may be relative or absolute and it is up to the caller |
29 /// to know how to interpret a relative path. | 29 /// to know how to interpret a relative path. |
30 String pathFromDescription(description) => description["path"]; | 30 String pathFromDescription(description) => description["path"]; |
31 | 31 |
32 /// Returns a reference to a path package named [name] at [path]. | 32 /// Returns a reference to a path package named [name] at [path]. |
33 PackageRef refFor(String name, String path) { | 33 PackageRef refFor(String name, String path) { |
34 return new PackageRef(name, 'path', { | 34 return new PackageRef(name, this, { |
35 "path": path, | 35 "path": path, |
36 "relative": p.isRelative(path) | 36 "relative": p.isRelative(path) |
37 }); | 37 }); |
38 } | 38 } |
39 | 39 |
40 /// Returns an ID for a path package with the given [name] and [version] at | 40 /// Returns an ID for a path package with the given [name] and [version] at |
41 /// [path]. | 41 /// [path]. |
42 PackageId idFor(String name, Version version, String path) { | 42 PackageId idFor(String name, Version version, String path) { |
43 return new PackageId(name, 'path', version, { | 43 return new PackageId(name, this, version, { |
44 "path": path, | 44 "path": path, |
45 "relative": p.isRelative(path) | 45 "relative": p.isRelative(path) |
46 }); | 46 }); |
47 } | 47 } |
48 | 48 |
49 bool descriptionsEqual(description1, description2) { | 49 bool descriptionsEqual(description1, description2) { |
50 // Compare real paths after normalizing and resolving symlinks. | 50 // Compare real paths after normalizing and resolving symlinks. |
51 var path1 = canonicalize(description1["path"]); | 51 var path1 = canonicalize(description1["path"]); |
52 var path2 = canonicalize(description2["path"]); | 52 var path2 = canonicalize(description2["path"]); |
53 return path1 == path2; | 53 return path1 == path2; |
54 } | 54 } |
55 | 55 |
| 56 int hashDescription(description) => |
| 57 canonicalize(description["path"]).hashCode; |
| 58 |
56 /// Parses a path dependency. | 59 /// Parses a path dependency. |
57 /// | 60 /// |
58 /// This takes in a path string and returns a map. The "path" key will be the | 61 /// This takes in a path string and returns a map. The "path" key will be the |
59 /// original path but resolved relative to the containing path. The | 62 /// original path but resolved relative to the containing path. The |
60 /// "relative" key will be `true` if the original path was relative. | 63 /// "relative" key will be `true` if the original path was relative. |
61 PackageRef parseRef(String name, description, {String containingPath}) { | 64 PackageRef parseRef(String name, description, {String containingPath}) { |
62 if (description is! String) { | 65 if (description is! String) { |
63 throw new FormatException("The description must be a path string."); | 66 throw new FormatException("The description must be a path string."); |
64 } | 67 } |
65 | 68 |
66 // Resolve the path relative to the containing file path, and remember | 69 // Resolve the path relative to the containing file path, and remember |
67 // whether the original path was relative or absolute. | 70 // whether the original path was relative or absolute. |
68 var isRelative = p.isRelative(description); | 71 var isRelative = p.isRelative(description); |
69 if (isRelative) { | 72 if (isRelative) { |
70 // Relative paths coming from pubspecs that are not on the local file | 73 // Relative paths coming from pubspecs that are not on the local file |
71 // system aren't allowed. This can happen if a hosted or git dependency | 74 // system aren't allowed. This can happen if a hosted or git dependency |
72 // has a path dependency. | 75 // has a path dependency. |
73 if (containingPath == null) { | 76 if (containingPath == null) { |
74 throw new FormatException('"$description" is a relative path, but this ' | 77 throw new FormatException('"$description" is a relative path, but this ' |
75 'isn\'t a local pubspec.'); | 78 'isn\'t a local pubspec.'); |
76 } | 79 } |
77 | 80 |
78 description = p.normalize( | 81 description = p.normalize( |
79 p.join(p.dirname(containingPath), description)); | 82 p.join(p.dirname(containingPath), description)); |
80 } | 83 } |
81 | 84 |
82 return new PackageRef(name, this.name, { | 85 return new PackageRef(name, this, { |
83 "path": description, | 86 "path": description, |
84 "relative": isRelative | 87 "relative": isRelative |
85 }); | 88 }); |
86 } | 89 } |
87 | 90 |
88 PackageId parseId(String name, Version version, description) { | 91 PackageId parseId(String name, Version version, description) { |
89 if (description is! Map) { | 92 if (description is! Map) { |
90 throw new FormatException("The description must be a map."); | 93 throw new FormatException("The description must be a map."); |
91 } | 94 } |
92 | 95 |
93 if (description["path"] is! String) { | 96 if (description["path"] is! String) { |
94 throw new FormatException("The 'path' field of the description must " | 97 throw new FormatException("The 'path' field of the description must " |
95 "be a string."); | 98 "be a string."); |
96 } | 99 } |
97 | 100 |
98 if (description["relative"] is! bool) { | 101 if (description["relative"] is! bool) { |
99 throw new FormatException("The 'relative' field of the description " | 102 throw new FormatException("The 'relative' field of the description " |
100 "must be a boolean."); | 103 "must be a boolean."); |
101 } | 104 } |
102 | 105 |
103 return new PackageId(name, this.name, version, description); | 106 return new PackageId(name, this, version, description); |
104 } | 107 } |
105 | 108 |
106 /// Serializes path dependency's [description]. | 109 /// Serializes path dependency's [description]. |
107 /// | 110 /// |
108 /// For the descriptions where `relative` attribute is `true`, tries to make | 111 /// For the descriptions where `relative` attribute is `true`, tries to make |
109 /// `path` relative to the specified [containingPath]. | 112 /// `path` relative to the specified [containingPath]. |
110 dynamic serializeDescription(String containingPath, description) { | 113 dynamic serializeDescription(String containingPath, description) { |
111 if (description["relative"]) { | 114 if (description["relative"]) { |
112 return { | 115 return { |
113 "path": p.relative(description['path'], from: containingPath), | 116 "path": p.relative(description['path'], from: containingPath), |
(...skipping 19 matching lines...) Expand all Loading... |
133 final PathSource source; | 136 final PathSource source; |
134 | 137 |
135 final SystemCache systemCache; | 138 final SystemCache systemCache; |
136 | 139 |
137 BoundPathSource(this.source, this.systemCache); | 140 BoundPathSource(this.source, this.systemCache); |
138 | 141 |
139 Future<List<PackageId>> doGetVersions(PackageRef ref) async { | 142 Future<List<PackageId>> doGetVersions(PackageRef ref) async { |
140 // There's only one package ID for a given path. We just need to find the | 143 // There's only one package ID for a given path. We just need to find the |
141 // version. | 144 // version. |
142 var pubspec = _loadPubspec(ref); | 145 var pubspec = _loadPubspec(ref); |
143 var id = new PackageId( | 146 var id = new PackageId(ref.name, source, pubspec.version, ref.description); |
144 ref.name, source.name, pubspec.version, ref.description); | |
145 memoizePubspec(id, pubspec); | 147 memoizePubspec(id, pubspec); |
146 return [id]; | 148 return [id]; |
147 } | 149 } |
148 | 150 |
149 Future<Pubspec> doDescribe(PackageId id) async => _loadPubspec(id.toRef()); | 151 Future<Pubspec> doDescribe(PackageId id) async => _loadPubspec(id.toRef()); |
150 | 152 |
151 Pubspec _loadPubspec(PackageRef ref) { | 153 Pubspec _loadPubspec(PackageRef ref) { |
152 var dir = _validatePath(ref.name, ref.description); | 154 var dir = _validatePath(ref.name, ref.description); |
153 return new Pubspec.load(dir, systemCache.sources, expectedName: ref.name); | 155 return new Pubspec.load(dir, systemCache.sources, expectedName: ref.name); |
154 } | 156 } |
(...skipping 21 matching lines...) Expand all Loading... |
176 | 178 |
177 if (fileExists(dir)) { | 179 if (fileExists(dir)) { |
178 fail('Path dependency for package $name must refer to a directory, ' | 180 fail('Path dependency for package $name must refer to a directory, ' |
179 'not a file. Was "$dir".'); | 181 'not a file. Was "$dir".'); |
180 } | 182 } |
181 | 183 |
182 throw new PackageNotFoundException( | 184 throw new PackageNotFoundException( |
183 'Could not find package $name at "$dir".'); | 185 'Could not find package $name at "$dir".'); |
184 } | 186 } |
185 } | 187 } |
OLD | NEW |