OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 part of vmservice_io; | 5 part of vmservice_io; |
6 | 6 |
7 _sanitizeWindowsPath(path) { | 7 _sanitizeWindowsPath(path) { |
8 // For Windows we need to massage the paths a bit according to | 8 // For Windows we need to massage the paths a bit according to |
9 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx | 9 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx |
10 // | 10 // |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // The directory to look in to resolve "package:" scheme URIs. By default it | 109 // The directory to look in to resolve "package:" scheme URIs. By default it |
110 // is the 'packages' directory right next to the script. | 110 // is the 'packages' directory right next to the script. |
111 Uri _packageRoot = null; | 111 Uri _packageRoot = null; |
112 | 112 |
113 // The map describing how certain package names are mapped to Uris. | 113 // The map describing how certain package names are mapped to Uris. |
114 Uri _packageConfig = null; | 114 Uri _packageConfig = null; |
115 Map<String, Uri> _packageMap = null; | 115 Map<String, Uri> _packageMap = null; |
116 | 116 |
117 _setPackageRoot(String packageRoot) { | 117 _setPackageRoot(String packageRoot) { |
118 packageRoot = _sanitizeWindowsPath(packageRoot); | 118 packageRoot = _sanitizeWindowsPath(packageRoot); |
119 packageRoot = _enforceTrailingSlash(packageRoot); | |
120 if (packageRoot.startsWith('file:') || | 119 if (packageRoot.startsWith('file:') || |
121 packageRoot.startsWith('http:') || | 120 packageRoot.startsWith('http:') || |
122 packageRoot.startsWith('https:')) { | 121 packageRoot.startsWith('https:')) { |
| 122 packageRoot = _enforceTrailingSlash(packageRoot); |
123 _packageRoot = _workingDirectory.resolve(packageRoot); | 123 _packageRoot = _workingDirectory.resolve(packageRoot); |
124 } else { | 124 } else { |
125 packageRoot = _sanitizeWindowsPath(packageRoot); | 125 packageRoot = _sanitizeWindowsPath(packageRoot); |
126 packageRoot = _trimWindowsPath(packageRoot); | 126 packageRoot = _trimWindowsPath(packageRoot); |
127 _packageRoot = _workingDirectory.resolveUri(new Uri.file(packageRoot)); | 127 _packageRoot = |
| 128 _workingDirectory.resolveUri(new Uri.directory(packageRoot)); |
128 } | 129 } |
129 } | 130 } |
130 | 131 |
131 _setPackagesConfig(String packagesParam) { | 132 _setPackagesConfig(String packagesParam) { |
132 var packagesName = _sanitizeWindowsPath(packagesParam); | 133 var packagesName = _sanitizeWindowsPath(packagesParam); |
133 var packagesUri = Uri.parse(packagesName); | 134 var packagesUri = Uri.parse(packagesName); |
134 if (packagesUri.scheme == '') { | 135 if (packagesUri.scheme == '') { |
135 // Script does not have a scheme, assume that it is a path, | 136 // Script does not have a scheme, assume that it is a path, |
136 // resolve it against the working directory. | 137 // resolve it against the working directory. |
137 packagesUri = _workingDirectory.resolveUri(packagesUri); | 138 packagesUri = _workingDirectory.resolveUri(packagesUri); |
(...skipping 18 matching lines...) Expand all Loading... |
156 // Register the action for when the package resolution is ready. | 157 // Register the action for when the package resolution is ready. |
157 _pendingPackageLoads.add(action); | 158 _pendingPackageLoads.add(action); |
158 } | 159 } |
159 } | 160 } |
160 | 161 |
161 // A list of callbacks which should be invoked after the package map has been | 162 // A list of callbacks which should be invoked after the package map has been |
162 // loaded. | 163 // loaded. |
163 List<Function> _pendingPackageLoads = []; | 164 List<Function> _pendingPackageLoads = []; |
164 | 165 |
165 // Given a uri with a 'package' scheme, return a Uri that is prefixed with | 166 // Given a uri with a 'package' scheme, return a Uri that is prefixed with |
166 // the package root. | 167 // the package root or resolved relative to the package configuration. |
167 Uri _resolvePackageUri(Uri uri) { | 168 Uri _resolvePackageUri(Uri uri) { |
168 assert(uri.scheme == "package"); | 169 assert(uri.scheme == "package"); |
169 assert(_packagesReady); | 170 assert(_packagesReady); |
170 | 171 |
171 if (!uri.host.isEmpty) { | 172 if (uri.host.isNotEmpty) { |
172 var path = '${uri.host}${uri.path}'; | 173 var path = '${uri.host}${uri.path}'; |
173 var right = 'package:$path'; | 174 var right = 'package:$path'; |
174 var wrong = 'package://$path'; | 175 var wrong = 'package://$path'; |
175 | 176 |
176 throw "URIs using the 'package:' scheme should look like " | 177 throw "URIs using the 'package:' scheme should look like " |
177 "'$right', not '$wrong'."; | 178 "'$right', not '$wrong'."; |
178 } | 179 } |
179 | 180 |
| 181 var packageNameEnd = uri.path.indexOf('/'); |
| 182 if (packageNameEnd == 0) { |
| 183 // Package URIs must have a non-empty package name (not start with "/"). |
| 184 throw "URIS using the 'package:' scheme should look like " |
| 185 "'package:packageName${uri.path}', not 'package:${uri.path}'"; |
| 186 } |
180 if (_traceLoading) { | 187 if (_traceLoading) { |
181 _log('Resolving package with uri path: ${uri.path}'); | 188 _log('Resolving package with uri path: ${uri.path}'); |
182 } | 189 } |
183 var resolvedUri; | 190 var resolvedUri; |
184 if (_packageError != null) { | 191 if (_packageError != null) { |
185 if (_traceLoading) { | 192 if (_traceLoading) { |
186 _log("Resolving package with pending resolution error: $_packageError"); | 193 _log("Resolving package with pending resolution error: $_packageError"); |
187 } | 194 } |
188 throw _packageError; | 195 throw _packageError; |
189 } else if (_packageRoot != null) { | 196 } else if (_packageRoot != null) { |
190 resolvedUri = _packageRoot.resolve(uri.path); | 197 resolvedUri = _packageRoot.resolve(uri.path); |
191 } else { | 198 } else { |
192 var packageName = uri.pathSegments[0]; | 199 if (packageNameEnd < 0) { |
| 200 // Package URIs must have a path after the package name, even if it's |
| 201 // just "/". |
| 202 throw "URIS using the 'package:' scheme should look like " |
| 203 "'package:${uri.path}/', not 'package:${uri.path}'"; |
| 204 } |
| 205 var packageName = uri.path.substring(0, packageNameEnd); |
193 var mapping = _packageMap[packageName]; | 206 var mapping = _packageMap[packageName]; |
194 if (_traceLoading) { | 207 if (_traceLoading) { |
195 _log("Mapped '$packageName' package to '$mapping'"); | 208 _log("Mapped '$packageName' package to '$mapping'"); |
196 } | 209 } |
197 if (mapping == null) { | 210 if (mapping == null) { |
198 throw "No mapping for '$packageName' package when resolving '$uri'."; | 211 throw "No mapping for '$packageName' package when resolving '$uri'."; |
199 } | 212 } |
200 var path; | 213 var path; |
201 if (uri.path.length > packageName.length) { | 214 assert(uri.path.length > packageName.length); |
202 path = uri.path.substring(packageName.length + 1); | 215 path = uri.path.substring(packageName.length + 1); |
203 } else { | |
204 // Handle naked package resolution to the default package name: | |
205 // package:foo is equivalent to package:foo/foo.dart | |
206 assert(uri.path.length == packageName.length); | |
207 path = "$packageName.dart"; | |
208 } | |
209 if (_traceLoading) { | 216 if (_traceLoading) { |
210 _log("Path to be resolved in package: $path"); | 217 _log("Path to be resolved in package: $path"); |
211 } | 218 } |
212 resolvedUri = mapping.resolve(path); | 219 resolvedUri = mapping.resolve(path); |
213 } | 220 } |
214 if (_traceLoading) { | 221 if (_traceLoading) { |
215 _log("Resolved '$uri' to '$resolvedUri'."); | 222 _log("Resolved '$uri' to '$resolvedUri'."); |
216 } | 223 } |
217 return resolvedUri; | 224 return resolvedUri; |
218 } | 225 } |
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
980 } | 987 } |
981 resolvedUri = null; | 988 resolvedUri = null; |
982 } | 989 } |
983 sp.send(resolvedUri); | 990 sp.send(resolvedUri); |
984 }); | 991 }); |
985 break; | 992 break; |
986 default: | 993 default: |
987 _log('Unknown loader request tag=$tag from $isolateId'); | 994 _log('Unknown loader request tag=$tag from $isolateId'); |
988 } | 995 } |
989 } | 996 } |
OLD | NEW |