| OLD | NEW |
| 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 Loading... |
| 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 final Context _context; |
| 47 |
| 48 /** |
| 49 * The cache of absolute [Uri]s to [Source]s mappings. |
| 50 */ |
| 51 final Map<Uri, Source> _sourceCache = new HashMap<Uri, Source>(); |
| 52 |
| 53 BazelPackageUriResolver(BazelWorkspace workspace) |
| 54 : _workspace = workspace, |
| 55 _context = workspace.provider.pathContext; |
| 56 |
| 57 @override |
| 58 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 59 return _sourceCache.putIfAbsent(uri, () { |
| 60 if (uri.scheme != 'package') { |
| 61 return null; |
| 62 } |
| 63 String uriPath = uri.path; |
| 64 int slash = uriPath.indexOf('/'); |
| 65 |
| 66 // If the path either starts with a slash or has no slash, it is invalid. |
| 67 if (slash < 1) { |
| 68 return null; |
| 69 } |
| 70 |
| 71 String packageName = uriPath.substring(0, slash); |
| 72 String fileUriPart = uriPath.substring(slash + 1); |
| 73 String filePath = fileUriPart.replaceAll('/', _context.separator); |
| 74 |
| 75 if (packageName.indexOf('.') == -1) { |
| 76 String path = |
| 77 _context.join('third_party', 'dart', packageName, 'lib', filePath); |
| 78 File file = _workspace.getFile(path); |
| 79 return file?.createSource(uri); |
| 80 } else { |
| 81 String packagePath = packageName.replaceAll('.', _context.separator); |
| 82 String path = |
| 83 _context.join(_workspace.root, packagePath, 'lib', filePath); |
| 84 File file = _workspace.findFile(path); |
| 85 return file?.createSource(uri); |
| 86 } |
| 87 }); |
| 88 } |
| 89 } |
| 90 |
| 91 /** |
| 41 * Information about a Bazel workspace. | 92 * Information about a Bazel workspace. |
| 42 */ | 93 */ |
| 43 class BazelWorkspace { | 94 class BazelWorkspace { |
| 44 static const String _WORKSPACE = 'WORKSPACE'; | 95 static const String _WORKSPACE = 'WORKSPACE'; |
| 45 static const String _READONLY = 'READONLY'; | 96 static const String _READONLY = 'READONLY'; |
| 46 | 97 |
| 47 final ResourceProvider provider; | 98 final ResourceProvider provider; |
| 48 | 99 |
| 49 /** | 100 /** |
| 50 * The absolute workspace root path. | 101 * The absolute workspace root path. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 return new BazelWorkspace._(provider, path, null, null, null); | 173 return new BazelWorkspace._(provider, path, null, null, null); |
| 123 } | 174 } |
| 124 } | 175 } |
| 125 } | 176 } |
| 126 | 177 |
| 127 BazelWorkspace._( | 178 BazelWorkspace._( |
| 128 this.provider, this.root, this.readonly, this.bin, this.genfiles); | 179 this.provider, this.root, this.readonly, this.bin, this.genfiles); |
| 129 | 180 |
| 130 /** | 181 /** |
| 131 * Return the file with the given [absolutePath], looking first into | 182 * Return the file with the given [absolutePath], looking first into |
| 132 * directories for generated files: `bazel-bin` and `bazel-genfiles`, and | 183 * directories for generated files: `bazel-genfiles` and `bazel-bin`, and |
| 133 * then into the workspace root. The file in the workspace root is returned | 184 * then into the workspace root. The file in the workspace root is returned |
| 134 * even if it does not exist. Return `null` if the given [absolutePath] is | 185 * even if it does not exist. Return `null` if the given [absolutePath] is |
| 135 * not in the workspace [root]. | 186 * not in the workspace [root]. |
| 136 */ | 187 */ |
| 137 File findFile(String absolutePath) { | 188 File findFile(String absolutePath) { |
| 138 Context context = provider.pathContext; | 189 Context context = provider.pathContext; |
| 139 try { | 190 try { |
| 140 String relative = context.relative(absolutePath, from: root); | 191 String relative = context.relative(absolutePath, from: root); |
| 141 // genfiles | 192 // genfiles |
| 142 if (genfiles != null) { | 193 if (genfiles != null) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 158 if (file.exists) { | 209 if (file.exists) { |
| 159 return file; | 210 return file; |
| 160 } | 211 } |
| 161 } | 212 } |
| 162 // Not generated, return the default one. | 213 // Not generated, return the default one. |
| 163 return provider.getFile(absolutePath); | 214 return provider.getFile(absolutePath); |
| 164 } catch (_) { | 215 } catch (_) { |
| 165 return null; | 216 return null; |
| 166 } | 217 } |
| 167 } | 218 } |
| 219 |
| 220 /** |
| 221 * Return the file for the given [pathInWorkspace]. The file is returned even |
| 222 * if it does not exist. |
| 223 */ |
| 224 File getFile(String pathInWorkspace) { |
| 225 return provider.getFile(provider.pathContext.join(root, pathInWorkspace)); |
| 226 } |
| 168 } | 227 } |
| OLD | NEW |