Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library analyzer.src.generated.bazel; | |
| 6 | |
| 7 import 'dart:core' hide Resource; | |
| 8 | |
| 9 import 'package:analyzer/file_system/file_system.dart'; | |
| 10 import 'package:analyzer/src/generated/source.dart'; | |
| 11 import 'package:analyzer/src/generated/source_io.dart'; | |
| 12 | |
| 13 /** | |
| 14 * Instances of the class `BazelFileUriResolver` resolve `file` URI's by first | |
| 15 * resolving file uri's in the expected way, and then by looking in the | |
| 16 * corresponding generated directories. | |
| 17 */ | |
| 18 class BazelFileUriResolver extends ResourceUriResolver { | |
| 19 /** | |
| 20 * The Bazel workspace directory. | |
| 21 */ | |
| 22 final Folder _workspaceDir; | |
| 23 | |
| 24 /** | |
| 25 * The build directories that relative `file` URI's should use to resolve | |
| 26 * relative URIs. | |
| 27 */ | |
| 28 final List<Folder> _buildDirectories; | |
| 29 | |
| 30 BazelFileUriResolver( | |
| 31 ResourceProvider provider, this._workspaceDir, this._buildDirectories) | |
| 32 : super(provider); | |
| 33 | |
| 34 @override | |
| 35 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | |
| 36 if (!ResourceUriResolver.isFileUri(uri)) { | |
| 37 return null; | |
| 38 } | |
| 39 | |
| 40 Resource resource = provider.getResource(provider.pathContext.fromUri(uri)); | |
| 41 if (resource is File && resource.exists) { | |
| 42 return resource.createSource(actualUri ?? uri); | |
| 43 } | |
| 44 | |
| 45 String relativeFromWorkspaceDir = _getPathFromWorkspaceDir(uri); | |
| 46 if (_buildDirectories.isEmpty || relativeFromWorkspaceDir.isEmpty) { | |
| 47 return null; | |
| 48 } | |
| 49 | |
| 50 for (Folder buildDir in _buildDirectories) { | |
| 51 // We intentionally wait until now to check to see if this directory | |
| 52 // exists as "bazel clean" can be run at any time on the users machine. | |
| 53 if (!buildDir.exists) { | |
|
Brian Wilkerson
2016/06/17 20:53:11
Checking for existence is expensive, and there's n
jwren
2016/06/17 21:19:45
Done.
| |
| 54 continue; | |
| 55 } | |
| 56 | |
| 57 File file = buildDir.getChildAssumingFile(relativeFromWorkspaceDir); | |
| 58 if (file.exists) { | |
| 59 return file.createSource(actualUri ?? uri); | |
| 60 } | |
| 61 } | |
| 62 return null; | |
| 63 } | |
| 64 | |
| 65 String _getPathFromWorkspaceDir(Uri uri) { | |
| 66 String uriPath = uri.path; | |
| 67 String workspacePath = _workspaceDir.path; | |
| 68 | |
| 69 if (uriPath.startsWith(workspacePath) && | |
| 70 workspacePath.length < uriPath.length) { | |
| 71 return uriPath.substring(workspacePath.length + 1); | |
| 72 } | |
| 73 return ''; | |
| 74 } | |
| 75 } | |
| OLD | NEW |