-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
dependency_tracking.ts
164 lines (142 loc) Β· 5.08 KB
/
dependency_tracking.ts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {AbsoluteFsPath} from '../../file_system';
import {DependencyTracker} from '../api';
/**
* An implementation of the `DependencyTracker` dependency graph API.
*
* The `FileDependencyGraph`'s primary job is to determine whether a given file has "logically"
* changed, given the set of physical changes (direct changes to files on disk).
*
* A file is logically changed if at least one of three conditions is met:
*
* 1. The file itself has physically changed.
* 2. One of its dependencies has physically changed.
* 3. One of its resource dependencies has physically changed.
*/
export class FileDependencyGraph<T extends {fileName: string} = ts.SourceFile> implements
DependencyTracker<T> {
private nodes = new Map<T, FileNode>();
addDependency(from: T, on: T): void {
this.nodeFor(from).dependsOn.add(on.fileName);
}
addResourceDependency(from: T, resource: AbsoluteFsPath): void {
this.nodeFor(from).usesResources.add(resource);
}
addTransitiveDependency(from: T, on: T): void {
const nodeFrom = this.nodeFor(from);
nodeFrom.dependsOn.add(on.fileName);
const nodeOn = this.nodeFor(on);
for (const dep of nodeOn.dependsOn) {
nodeFrom.dependsOn.add(dep);
}
}
addTransitiveResources(from: T, resourcesOf: T): void {
const nodeFrom = this.nodeFor(from);
const nodeOn = this.nodeFor(resourcesOf);
for (const dep of nodeOn.usesResources) {
nodeFrom.usesResources.add(dep);
}
}
recordDependencyAnalysisFailure(file: T): void {
this.nodeFor(file).failedAnalysis = true;
}
getResourceDependencies(from: T): AbsoluteFsPath[] {
const node = this.nodes.get(from);
return node ? [...node.usesResources] : [];
}
isStale(sf: T, changedTsPaths: Set<string>, changedResources: Set<AbsoluteFsPath>): boolean {
return isLogicallyChanged(sf, this.nodeFor(sf), changedTsPaths, EMPTY_SET, changedResources);
}
/**
* Update the current dependency graph from a previous one, incorporating a set of physical
* changes.
*
* This method performs two tasks:
*
* 1. For files which have not logically changed, their dependencies from `previous` are added to
* `this` graph.
* 2. For files which have logically changed, they're added to a set of logically changed files
* which is eventually returned.
*
* In essence, for build `n`, this method performs:
*
* G(n) + L(n) = G(n - 1) + P(n)
*
* where:
*
* G(n) = the dependency graph of build `n`
* L(n) = the logically changed files from build n - 1 to build n.
* P(n) = the physically changed files from build n - 1 to build n.
*/
updateWithPhysicalChanges(
previous: FileDependencyGraph<T>, changedTsPaths: Set<string>, deletedTsPaths: Set<string>,
changedResources: Set<AbsoluteFsPath>): Set<string> {
const logicallyChanged = new Set<string>();
for (const sf of previous.nodes.keys()) {
const node = previous.nodeFor(sf);
if (isLogicallyChanged(sf, node, changedTsPaths, deletedTsPaths, changedResources)) {
logicallyChanged.add(sf.fileName);
} else if (!deletedTsPaths.has(sf.fileName)) {
this.nodes.set(sf, {
dependsOn: new Set(node.dependsOn),
usesResources: new Set(node.usesResources),
failedAnalysis: false,
});
}
}
return logicallyChanged;
}
private nodeFor(sf: T): FileNode {
if (!this.nodes.has(sf)) {
this.nodes.set(sf, {
dependsOn: new Set<string>(),
usesResources: new Set<AbsoluteFsPath>(),
failedAnalysis: false,
});
}
return this.nodes.get(sf)!;
}
}
/**
* Determine whether `sf` has logically changed, given its dependencies and the set of physically
* changed files and resources.
*/
function isLogicallyChanged<T extends {fileName: string}>(
sf: T, node: FileNode, changedTsPaths: ReadonlySet<string>, deletedTsPaths: ReadonlySet<string>,
changedResources: ReadonlySet<AbsoluteFsPath>): boolean {
// A file is assumed to have logically changed if its dependencies could not be determined
// accurately.
if (node.failedAnalysis) {
return true;
}
// A file is logically changed if it has physically changed itself (including being deleted).
if (changedTsPaths.has(sf.fileName) || deletedTsPaths.has(sf.fileName)) {
return true;
}
// A file is logically changed if one of its dependencies has physically changed.
for (const dep of node.dependsOn) {
if (changedTsPaths.has(dep) || deletedTsPaths.has(dep)) {
return true;
}
}
// A file is logically changed if one of its resources has physically changed.
for (const dep of node.usesResources) {
if (changedResources.has(dep)) {
return true;
}
}
return false;
}
interface FileNode {
dependsOn: Set<string>;
usesResources: Set<AbsoluteFsPath>;
failedAnalysis: boolean;
}
const EMPTY_SET: ReadonlySet<any> = new Set<any>();