-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
file_tracker.dart
234 lines (198 loc) · 8.19 KB
/
file_tracker.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2017, 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.
import 'package:analyzer/src/dart/analysis/file_state.dart';
import 'package:analyzer/src/dart/analysis/performance_logger.dart';
/// Maintains the file system state needed by the analysis driver, as well as
/// information about files that have changed and the impact of those changes.
///
/// Three related sets of files are tracked: "added files" is the set of files
/// for which the client would like analysis. "changed files" is the set of
/// files which is known to have changed, but for which we have not yet measured
/// the impact of the change. "pending files" is the subset of "added files"
/// which might have been impacted by a change, and thus need analysis.
///
/// Provides methods for updating the file system state in response to changes.
class FileTracker {
/// The logger to write performed operations and performance to.
final PerformanceLog _logger;
/// The current file system state.
final FileSystemState _fsState;
final StoredFileContentStrategy _fileContentStrategy;
/// The set of added files.
final addedFiles = <String>{};
/// The set of files were reported as changed through [changeFile] and not
/// checked for actual changes yet.
final _changedFiles = <String>{};
/// The set of files that are currently scheduled for analysis, which were
/// reported as changed through [changeFile].
var _pendingChangedFiles = <String>{};
/// The set of files that are currently scheduled for analysis, which directly
/// import a changed file.
var _pendingImportFiles = <String>{};
/// The set of files that are currently scheduled for analysis, which have an
/// error or a warning, which might be fixed by a changed file.
var _pendingErrorFiles = <String>{};
/// The set of files that are currently scheduled for analysis, and don't
/// have any special relation with changed files.
var _pendingFiles = <String>{};
FileTracker(this._logger, this._fsState, this._fileContentStrategy);
/// Returns the path to exactly one that needs analysis.
String? get anyPendingFile {
return _pendingChangedFiles.firstOrNull ??
_pendingImportFiles.firstOrNull ??
_pendingErrorFiles.firstOrNull ??
_pendingFiles.firstOrNull;
}
/// Returns a boolean indicating whether there are any files that have
/// changed, but for which the impact of the changes hasn't been measured.
bool get hasChangedFiles => _changedFiles.isNotEmpty;
/// Return `true` if there are changed files that need analysis.
bool get hasPendingChangedFiles => _pendingChangedFiles.isNotEmpty;
/// Return `true` if there are files that have an error or warning, and that
/// need analysis.
bool get hasPendingErrorFiles => _pendingErrorFiles.isNotEmpty;
/// Returns a boolean indicating whether there are any files that need
/// analysis.
bool get hasPendingFiles {
return hasPendingChangedFiles ||
hasPendingImportFiles ||
hasPendingErrorFiles ||
_pendingFiles.isNotEmpty;
}
/// Return `true` if there are files that directly import a changed file that
/// need analysis.
bool get hasPendingImportFiles => _pendingImportFiles.isNotEmpty;
/// Returns a count of how many files need analysis.
int get numberOfPendingFiles {
return _pendingChangedFiles.length +
_pendingImportFiles.length +
_pendingErrorFiles.length +
_pendingFiles.length;
}
/// Adds the given [path] to the set of "added files".
void addFile(String path) {
addedFiles.add(path);
changeFile(path);
}
/// Adds the given [paths] to the set of "added files".
void addFiles(Iterable<String> paths) {
addedFiles.addAll(paths);
_pendingFiles.addAll(paths);
}
/// Adds the given [path] to the set of "changed files".
void changeFile(String path) {
_fileContentStrategy.markFileForReading(path);
_changedFiles.add(path);
if (addedFiles.contains(path)) {
_pendingChangedFiles.add(path);
}
}
/// Removes the given [path] from the set of "pending files".
///
/// Should be called after the client has analyzed a file.
void fileWasAnalyzed(String path) {
_pendingChangedFiles.remove(path);
_pendingImportFiles.remove(path);
_pendingErrorFiles.remove(path);
_pendingFiles.remove(path);
}
/// Returns a boolean indicating whether the given [path] points to a file
/// that requires analysis.
bool isFilePending(String path) {
return _pendingChangedFiles.contains(path) ||
_pendingImportFiles.contains(path) ||
_pendingErrorFiles.contains(path) ||
_pendingFiles.contains(path);
}
/// Removes the given [path] from the set of "added files".
void removeFile(String path) {
_fileContentStrategy.markFileForReading(path);
addedFiles.remove(path);
_pendingChangedFiles.remove(path);
_pendingImportFiles.remove(path);
_pendingErrorFiles.remove(path);
_pendingFiles.remove(path);
// TODO(paulberry): removing the path from [fsState] and re-analyzing all
// files seems extreme.
_fsState.removeFile(path);
_pendingFiles.addAll(addedFiles);
}
/// Verify the API signature for the file with the given [path], and decide
/// which linked libraries should be invalidated, and files reanalyzed.
void verifyApiSignature(String path) {
return _logger.run('Verify API signature of $path', () {
_logger.writeln('Work in ${_fsState.contextName}');
var file = _fsState.getFileForPath(path);
var changeKind = file.refresh();
switch (changeKind) {
case FileStateRefreshResult.nothing:
return;
case FileStateRefreshResult.contentChanged:
if (file.mightBeExecutedByMacroClass) {
break;
}
return;
case FileStateRefreshResult.apiChanged:
break;
}
_logger.writeln('API signatures mismatch found.');
// TODO(scheglov): schedule analysis of only affected files
var pendingChangedFiles = <String>{};
var pendingImportFiles = <String>{};
var pendingErrorFiles = <String>{};
var pendingFiles = <String>{};
// Add the changed file.
if (addedFiles.contains(path)) {
pendingChangedFiles.add(path);
}
// Add files that directly import the changed file.
for (var addedPath in addedFiles) {
var addedFile = _fsState.getFileForPath(addedPath);
var addedKind = addedFile.kind;
if (addedKind is LibraryOrAugmentationFileKind) {
if (addedKind.importsFile(file)) {
pendingImportFiles.add(addedPath);
}
}
}
// Add files with errors or warnings that might be fixed.
for (String addedPath in addedFiles) {
FileState addedFile = _fsState.getFileForPath(addedPath);
if (addedFile.hasErrorOrWarning) {
pendingErrorFiles.add(addedPath);
}
}
// Add all previous pending files.
pendingChangedFiles.addAll(_pendingChangedFiles);
pendingImportFiles.addAll(_pendingImportFiles);
pendingErrorFiles.addAll(_pendingErrorFiles);
pendingFiles.addAll(_pendingFiles);
// Add all the rest.
pendingFiles.addAll(addedFiles);
// Replace pending files.
_pendingChangedFiles = pendingChangedFiles;
_pendingImportFiles = pendingImportFiles;
_pendingErrorFiles = pendingErrorFiles;
_pendingFiles = pendingFiles;
});
}
/// If at least one file is in the "changed files" set, determines the impact
/// of the change, updates the set of pending files, and returns `true`.
///
/// If no files are in the "changed files" set, returns `false`.
bool verifyChangedFilesIfNeeded() {
// Verify all changed files one at a time.
if (_changedFiles.isNotEmpty) {
String path = _changedFiles.first;
_changedFiles.remove(path);
// If the file has not been accessed yet, we either will eventually read
// it later while analyzing one of the added files, or don't need it.
if (_fsState.knownFilePaths.contains(path)) {
verifyApiSignature(path);
}
return true;
}
return false;
}
}