Chromium Code Reviews| Index: pkg/analyzer/lib/src/generated/bazel.dart |
| diff --git a/pkg/analyzer/lib/src/generated/bazel.dart b/pkg/analyzer/lib/src/generated/bazel.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2f78c390774b67ef12766c8112d7a3d7240ce7a5 |
| --- /dev/null |
| +++ b/pkg/analyzer/lib/src/generated/bazel.dart |
| @@ -0,0 +1,75 @@ |
| +// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library analyzer.src.generated.bazel; |
| + |
| +import 'dart:core' hide Resource; |
| + |
| +import 'package:analyzer/file_system/file_system.dart'; |
| +import 'package:analyzer/src/generated/source.dart'; |
| +import 'package:analyzer/src/generated/source_io.dart'; |
| + |
| +/** |
| + * Instances of the class `BazelFileUriResolver` resolve `file` URI's by first |
| + * resolving file uri's in the expected way, and then by looking in the |
| + * corresponding generated directories. |
| + */ |
| +class BazelFileUriResolver extends ResourceUriResolver { |
| + /** |
| + * The Bazel workspace directory. |
| + */ |
| + final Folder _workspaceDir; |
| + |
| + /** |
| + * The build directories that relative `file` URI's should use to resolve |
| + * relative URIs. |
| + */ |
| + final List<Folder> _buildDirectories; |
| + |
| + BazelFileUriResolver( |
| + ResourceProvider provider, this._workspaceDir, this._buildDirectories) |
| + : super(provider); |
| + |
| + @override |
| + Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| + if (!ResourceUriResolver.isFileUri(uri)) { |
| + return null; |
| + } |
| + |
| + Resource resource = provider.getResource(provider.pathContext.fromUri(uri)); |
| + if (resource is File && resource.exists) { |
| + return resource.createSource(actualUri ?? uri); |
| + } |
| + |
| + String relativeFromWorkspaceDir = _getPathFromWorkspaceDir(uri); |
| + if (_buildDirectories.isEmpty || relativeFromWorkspaceDir.isEmpty) { |
| + return null; |
| + } |
| + |
| + for (Folder buildDir in _buildDirectories) { |
| + // We intentionally wait until now to check to see if this directory |
| + // exists as "bazel clean" can be run at any time on the users machine. |
| + 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.
|
| + continue; |
| + } |
| + |
| + File file = buildDir.getChildAssumingFile(relativeFromWorkspaceDir); |
| + if (file.exists) { |
| + return file.createSource(actualUri ?? uri); |
| + } |
| + } |
| + return null; |
| + } |
| + |
| + String _getPathFromWorkspaceDir(Uri uri) { |
| + String uriPath = uri.path; |
| + String workspacePath = _workspaceDir.path; |
| + |
| + if (uriPath.startsWith(workspacePath) && |
| + workspacePath.length < uriPath.length) { |
| + return uriPath.substring(workspacePath.length + 1); |
| + } |
| + return ''; |
| + } |
| +} |