Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: pkg/analyzer/lib/src/generated/bazel.dart

Issue 2393963004: Add BazelPackageUriResolver with resolveAbsolute(). (Closed)
Patch Set: Drop extract prefixes for field names in _BazelPackageUri. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/bazel_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 analyzer.src.generated.bazel; 5 library analyzer.src.generated.bazel;
6 6
7 import 'dart:collection';
7 import 'dart:core'; 8 import 'dart:core';
8 9
9 import 'package:analyzer/file_system/file_system.dart'; 10 import 'package:analyzer/file_system/file_system.dart';
10 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
11 import 'package:analyzer/src/generated/source_io.dart'; 12 import 'package:analyzer/src/generated/source_io.dart';
12 import 'package:path/path.dart'; 13 import 'package:path/path.dart';
13 14
14 /** 15 /**
15 * Instances of the class `BazelFileUriResolver` resolve `file` URI's by first 16 * Instances of the class `BazelFileUriResolver` resolve `file` URI's by first
16 * resolving file uri's in the expected way, and then by looking in the 17 * resolving file uri's in the expected way, and then by looking in the
(...skipping 14 matching lines...) Expand all
31 String path = provider.pathContext.fromUri(uri); 32 String path = provider.pathContext.fromUri(uri);
32 File file = workspace.findFile(path); 33 File file = workspace.findFile(path);
33 if (file != null) { 34 if (file != null) {
34 return file.createSource(actualUri ?? uri); 35 return file.createSource(actualUri ?? uri);
35 } 36 }
36 return null; 37 return null;
37 } 38 }
38 } 39 }
39 40
40 /** 41 /**
42 * The [UriResolver] that can resolve `package` URIs in [BazelWorkspace].
43 */
44 class BazelPackageUriResolver extends UriResolver {
45 final BazelWorkspace workspace;
46
47 /**
48 * The cache of absolute [Uri]s to [Source]s mappings.
49 */
50 final Map<Uri, Source> _sourceCache = new HashMap<Uri, Source>();
51
52 BazelPackageUriResolver(this.workspace);
53
54 @override
55 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
56 return _sourceCache.putIfAbsent(uri, () {
57 _BazelPackageUri bazelUri = _BazelPackageUri.parse(uri);
58 if (bazelUri == null) {
59 return null;
60 }
61 if (bazelUri.package.indexOf('.') == -1) {
62 String path = '${workspace.root}/third_party/dart/'
Paul Berry 2016/10/07 15:30:57 This won't work on Windows because it uses hardcod
scheglov 2016/10/07 16:02:43 Thanks. I tested it now manually with Windows styl
63 '${bazelUri.package}/lib/${bazelUri.path}';
64 File file = workspace.getFile(path);
65 return file?.createSource(uri);
66 } else {
67 String packagePath = bazelUri.package
68 .replaceAll('.', workspace.provider.pathContext.separator);
69 String path = '${workspace.root}/$packagePath/lib/${bazelUri.path}';
Paul Berry 2016/10/07 15:30:57 Similar problem here.
scheglov 2016/10/07 16:02:43 Acknowledged.
70 File file = workspace.findFile(path);
71 return file?.createSource(uri);
72 }
73 });
74 }
75 }
76
77 /**
41 * Information about a Bazel workspace. 78 * Information about a Bazel workspace.
42 */ 79 */
43 class BazelWorkspace { 80 class BazelWorkspace {
44 static const String _WORKSPACE = 'WORKSPACE'; 81 static const String _WORKSPACE = 'WORKSPACE';
45 static const String _READONLY = 'READONLY'; 82 static const String _READONLY = 'READONLY';
46 83
47 final ResourceProvider provider; 84 final ResourceProvider provider;
48 85
49 /** 86 /**
50 * The absolute workspace root path. 87 * The absolute workspace root path.
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 if (file.exists) { 195 if (file.exists) {
159 return file; 196 return file;
160 } 197 }
161 } 198 }
162 // Not generated, return the default one. 199 // Not generated, return the default one.
163 return provider.getFile(absolutePath); 200 return provider.getFile(absolutePath);
164 } catch (_) { 201 } catch (_) {
165 return null; 202 return null;
166 } 203 }
167 } 204 }
205
206 /**
207 * Return the file with the given [absolutePath] in the workspace [root].
208 * The file is returned even if it does not exist. Return `null` if the given
209 * [absolutePath] is not in the workspace [root].
Paul Berry 2016/10/07 15:30:57 Since the caller always provides a path within the
scheglov 2016/10/07 16:02:43 Done.
210 */
211 File getFile(String absolutePath) {
212 if (provider.pathContext.isWithin(root, absolutePath)) {
213 return provider.getFile(absolutePath);
214 }
215 return null;
216 }
168 } 217 }
218
219 /**
220 * A `package` URI that has been parsed according to Bazel rules.
221 *
222 * For example, "package:dart.tools.analyzer/src/foo.dart" will be split
223 * into "dart.tools.analyzer" (the package) and "src/foo.dart" (the suffix).
224 */
225 class _BazelPackageUri {
226 /**
227 * The name of the package being imported or exported.
228 *
229 * For example, if the URI is "package:foo.bar/baz.dart" then the package
230 * name is "foo.bar".
231 */
232 final String package;
233
234 /**
235 * The path from the `lib` folder for the package to the file.
236 *
237 * For example, if the URI is "package:foo/bar/baz.dart" then the path is
238 * "bar/baz.dart".
239 */
240 final String path;
241
242 _BazelPackageUri(this.package, this.path);
243
244 @override
245 String toString() => toUri().toString();
246
247 /**
248 * Return the reconstituted `package` URI.
249 */
250 Uri toUri() {
251 return new Uri(scheme: 'package', path: '$package/$path');
252 }
253
254 /**
255 * Parses a Dart `package` URI according to Bazel rules.
256 *
257 * Returns `null` if it's not a valid Bazel `package` URI.
258 */
259 static _BazelPackageUri parse(Uri uri) {
260 if (uri.scheme != 'package') {
261 return null;
262 }
263 String path = uri.path;
264 int slash = path.indexOf('/');
265
266 // If the path either starts with a slash or has no slash, it is invalid.
267 if (slash < 1) {
268 return null;
269 }
270
271 String packageName = path.substring(0, slash);
272 String suffix = path.substring(slash + 1);
273 return new _BazelPackageUri(packageName, suffix);
274 }
275 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/bazel_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698