| 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'; |
| 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 File uriFile = provider.getFile(provider.pathContext.fromUri(uri)); |
| 41 if (uriFile.exists) { |
| 42 return uriFile.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 File file = buildDir.getChildAssumingFile(relativeFromWorkspaceDir); |
| 52 if (file.exists) { |
| 53 return file.createSource(actualUri ?? uri); |
| 54 } |
| 55 } |
| 56 return null; |
| 57 } |
| 58 |
| 59 String _getPathFromWorkspaceDir(Uri uri) { |
| 60 try { |
| 61 return provider.pathContext.relative(provider.pathContext.fromUri(uri), |
| 62 from: _workspaceDir.path); |
| 63 } on Exception { |
| 64 return ''; |
| 65 } |
| 66 } |
| 67 } |
| OLD | NEW |