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

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

Issue 2393813003: Add BazelWorkspace and move BazelFileUriResolver to it. (Closed)
Patch Set: 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
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:core'; 7 import 'dart:core';
8 8
9 import 'package:analyzer/file_system/file_system.dart'; 9 import 'package:analyzer/file_system/file_system.dart';
10 import 'package:analyzer/src/generated/source.dart'; 10 import 'package:analyzer/src/generated/source.dart';
11 import 'package:analyzer/src/generated/source_io.dart'; 11 import 'package:analyzer/src/generated/source_io.dart';
12 import 'package:path/path.dart';
12 13
13 /** 14 /**
14 * Instances of the class `BazelFileUriResolver` resolve `file` URI's by first 15 * 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 * resolving file uri's in the expected way, and then by looking in the
16 * corresponding generated directories. 17 * corresponding generated directories.
17 */ 18 */
18 class BazelFileUriResolver extends ResourceUriResolver { 19 class BazelFileUriResolver extends ResourceUriResolver {
19 /** 20 final BazelWorkspace workspace;
20 * The Bazel workspace directory.
21 */
22 final Folder _workspaceDir;
23 21
24 /** 22 BazelFileUriResolver(BazelWorkspace workspace)
25 * The build directories that relative `file` URI's should use to resolve 23 : workspace = workspace,
26 * relative URIs. 24 super(workspace.provider);
27 */
28 final List<Folder> _buildDirectories;
29
30 BazelFileUriResolver(
31 ResourceProvider provider, this._workspaceDir, this._buildDirectories)
32 : super(provider);
33 25
34 @override 26 @override
35 Source resolveAbsolute(Uri uri, [Uri actualUri]) { 27 Source resolveAbsolute(Uri uri, [Uri actualUri]) {
36 if (!ResourceUriResolver.isFileUri(uri)) { 28 if (!ResourceUriResolver.isFileUri(uri)) {
37 return null; 29 return null;
38 } 30 }
39 31 String path = provider.pathContext.fromUri(uri);
40 File uriFile = provider.getFile(provider.pathContext.fromUri(uri)); 32 File file = workspace.findFile(path);
41 if (uriFile.exists) { 33 if (file != null) {
42 return uriFile.createSource(actualUri ?? uri); 34 return file.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 } 35 }
56 return null; 36 return null;
57 } 37 }
38 }
58 39
59 String _getPathFromWorkspaceDir(Uri uri) { 40 /**
41 * Information about a Bazel workspace.
42 */
43 class BazelWorkspace {
44 static const String _WORKSPACE = 'WORKSPACE';
45 static const String _READONLY = 'READONLY';
46
47 final ResourceProvider provider;
48
49 /**
50 * The absolute workspace root path.
51 *
52 * It contains the `WORKSPACE` file or its parent contains the `READONLY`
53 * folder.
54 */
55 final String root;
56
57 /**
58 * The absolute path to the optional `READONLY` folder if a git-based
59 * workspace, or `null`.
60 */
61 final String readonly;
62
63 /**
64 * The absolute path to the `bazel-bin` folder.
65 */
66 final String bin;
67
68 /**
69 * The absolute path to the `bazel-genfiles` folder.
70 */
71 final String genfiles;
72
73 /**
74 * Create a new Bazel workspace that contains the given [path].
75 *
76 * The [symlinkPrefix] is the prefix for names of symlinks like `bazel-bin`,
77 * `bazel-genfiles`, etc.
78 */
79 factory BazelWorkspace(ResourceProvider provider, String path,
80 {String symlinkPrefix: 'bazel', String readonlySuffix}) {
81 Context context = provider.pathContext;
82
83 // Ensure that the path is absolute and normalized.
84 if (!context.isAbsolute(path)) {
85 throw new ArgumentError('not absolute: $path');
86 }
87 path = context.normalize(path);
88
89 Folder folder = provider.getFolder(path);
90 while (true) {
91 Folder parent = folder.parent;
92
93 // Found the READONLY folder, must be a git-based workspace.
94 if (readonlySuffix != null) {
95 Folder readonlyFolder = parent.getChildAssumingFolder(_READONLY);
96 if (parent != null && readonlyFolder.exists) {
97 String root = folder.path;
98 String readonly = readonlyFolder.path;
99 return new BazelWorkspace._(
100 provider,
101 root,
102 context.join(readonly, readonlySuffix),
103 context.join(root, '$symlinkPrefix-bin'),
104 context.join(root, '$symlinkPrefix-genfiles'));
105 }
106 }
107
108 // Found the WORKSPACE file, must be a non-git workspace.
109 if (folder.getChildAssumingFile(_WORKSPACE).exists) {
110 String root = folder.path;
111 return new BazelWorkspace._(
112 provider,
113 root,
114 null,
115 context.join(root, '$symlinkPrefix-bin'),
116 context.join(root, '$symlinkPrefix-genfiles'));
117 }
118
119 // Go up the folder.
120 folder = parent;
121 if (folder == null) {
122 return null;
123 }
124 }
125 }
126
127 BazelWorkspace._(
128 this.provider, this.root, this.readonly, this.bin, this.genfiles);
129
130 /**
131 * Return the file with the given [absolutePath], looking first into
132 * directories for generated files: `bazel-bin` and `bazel-genfiles`, and
133 * 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
135 * not in the workspace [root].
136 */
137 File findFile(String absolutePath) {
138 Context context = provider.pathContext;
60 try { 139 try {
61 return provider.pathContext.relative(provider.pathContext.fromUri(uri), 140 String relative = context.relative(absolutePath, from: root);
62 from: _workspaceDir.path); 141 // genfiles
63 } on Exception { 142 {
64 return ''; 143 File file = provider.getFile(context.join(genfiles, relative));
144 if (file.exists) {
145 return file;
146 }
147 }
148 // bin
149 {
150 File file = provider.getFile(context.join(bin, relative));
151 if (file.exists) {
152 return file;
153 }
154 }
155 // READONLY
156 if (readonly != null) {
157 File file = provider.getFile(context.join(readonly, relative));
158 if (file.exists) {
159 return file;
160 }
161 }
162 // Not generated, return the default one.
163 return provider.getFile(absolutePath);
164 } catch (_) {
165 return null;
65 } 166 }
66 } 167 }
67 } 168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698