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); |
119 if (packageRoot.startsWith('file:') || | 120 if (packageRoot.startsWith('file:') || |
120 packageRoot.startsWith('http:') || | 121 packageRoot.startsWith('http:') || |
121 packageRoot.startsWith('https:')) { | 122 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 = | 127 _packageRoot = _workingDirectory.resolveUri(new Uri.file(packageRoot)); |
128 _workingDirectory.resolveUri(new Uri.directory(packageRoot)); | |
129 } | 128 } |
130 } | 129 } |
131 | 130 |
132 _setPackagesConfig(String packagesParam) { | 131 _setPackagesConfig(String packagesParam) { |
133 var packagesName = _sanitizeWindowsPath(packagesParam); | 132 var packagesName = _sanitizeWindowsPath(packagesParam); |
134 var packagesUri = Uri.parse(packagesName); | 133 var packagesUri = Uri.parse(packagesName); |
135 if (packagesUri.scheme == '') { | 134 if (packagesUri.scheme == '') { |
136 // Script does not have a scheme, assume that it is a path, | 135 // Script does not have a scheme, assume that it is a path, |
137 // resolve it against the working directory. | 136 // resolve it against the working directory. |
138 packagesUri = _workingDirectory.resolveUri(packagesUri); | 137 packagesUri = _workingDirectory.resolveUri(packagesUri); |
(...skipping 18 matching lines...) Expand all Loading... |
157 // Register the action for when the package resolution is ready. | 156 // Register the action for when the package resolution is ready. |
158 _pendingPackageLoads.add(action); | 157 _pendingPackageLoads.add(action); |
159 } | 158 } |
160 } | 159 } |
161 | 160 |
162 // A list of callbacks which should be invoked after the package map has been | 161 // A list of callbacks which should be invoked after the package map has been |
163 // loaded. | 162 // loaded. |
164 List<Function> _pendingPackageLoads = []; | 163 List<Function> _pendingPackageLoads = []; |
165 | 164 |
166 // Given a uri with a 'package' scheme, return a Uri that is prefixed with | 165 // Given a uri with a 'package' scheme, return a Uri that is prefixed with |
167 // the package root or resolved relative to the package configuration. | 166 // the package root. |
168 Uri _resolvePackageUri(Uri uri) { | 167 Uri _resolvePackageUri(Uri uri) { |
169 assert(uri.scheme == "package"); | 168 assert(uri.scheme == "package"); |
170 assert(_packagesReady); | 169 assert(_packagesReady); |
171 | 170 |
172 if (uri.host.isNotEmpty) { | 171 if (!uri.host.isEmpty) { |
173 var path = '${uri.host}${uri.path}'; | 172 var path = '${uri.host}${uri.path}'; |
174 var right = 'package:$path'; | 173 var right = 'package:$path'; |
175 var wrong = 'package://$path'; | 174 var wrong = 'package://$path'; |
176 | 175 |
177 throw "URIs using the 'package:' scheme should look like " | 176 throw "URIs using the 'package:' scheme should look like " |
178 "'$right', not '$wrong'."; | 177 "'$right', not '$wrong'."; |
179 } | 178 } |
180 | 179 |
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 } | |
187 if (packageNameEnd < 0) { | |
188 // Package URIs must have a path after the package name, even if it's | |
189 // just "/". | |
190 throw "URIS using the 'package:' scheme should look like " | |
191 "'package:${uri.path}/', not 'package:${uri.path}'"; | |
192 } | |
193 if (_traceLoading) { | 180 if (_traceLoading) { |
194 _log('Resolving package with uri path: ${uri.path}'); | 181 _log('Resolving package with uri path: ${uri.path}'); |
195 } | 182 } |
196 var resolvedUri; | 183 var resolvedUri; |
197 if (_packageError != null) { | 184 if (_packageError != null) { |
198 if (_traceLoading) { | 185 if (_traceLoading) { |
199 _log("Resolving package with pending resolution error: $_packageError"); | 186 _log("Resolving package with pending resolution error: $_packageError"); |
200 } | 187 } |
201 throw _packageError; | 188 throw _packageError; |
202 } else if (_packageRoot != null) { | 189 } else if (_packageRoot != null) { |
203 resolvedUri = _packageRoot.resolve(uri.path); | 190 resolvedUri = _packageRoot.resolve(uri.path); |
204 } else { | 191 } else { |
205 var packageName = uri.path.substring(0, packageNameEnd); | 192 var packageName = uri.pathSegments[0]; |
206 var mapping = _packageMap[packageName]; | 193 var mapping = _packageMap[packageName]; |
207 if (_traceLoading) { | 194 if (_traceLoading) { |
208 _log("Mapped '$packageName' package to '$mapping'"); | 195 _log("Mapped '$packageName' package to '$mapping'"); |
209 } | 196 } |
210 if (mapping == null) { | 197 if (mapping == null) { |
211 throw "No mapping for '$packageName' package when resolving '$uri'."; | 198 throw "No mapping for '$packageName' package when resolving '$uri'."; |
212 } | 199 } |
213 var path; | 200 var path; |
214 assert(uri.path.length > packageName.length); | 201 if (uri.path.length > packageName.length) { |
215 path = uri.path.substring(packageName.length + 1); | 202 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 } |
216 if (_traceLoading) { | 209 if (_traceLoading) { |
217 _log("Path to be resolved in package: $path"); | 210 _log("Path to be resolved in package: $path"); |
218 } | 211 } |
219 resolvedUri = mapping.resolve(path); | 212 resolvedUri = mapping.resolve(path); |
220 } | 213 } |
221 if (_traceLoading) { | 214 if (_traceLoading) { |
222 _log("Resolved '$uri' to '$resolvedUri'."); | 215 _log("Resolved '$uri' to '$resolvedUri'."); |
223 } | 216 } |
224 return resolvedUri; | 217 return resolvedUri; |
225 } | 218 } |
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 } | 980 } |
988 resolvedUri = null; | 981 resolvedUri = null; |
989 } | 982 } |
990 sp.send(resolvedUri); | 983 sp.send(resolvedUri); |
991 }); | 984 }); |
992 break; | 985 break; |
993 default: | 986 default: |
994 _log('Unknown loader request tag=$tag from $isolateId'); | 987 _log('Unknown loader request tag=$tag from $isolateId'); |
995 } | 988 } |
996 } | 989 } |
OLD | NEW |