/
stylesheet_graph.dart
297 lines (257 loc) · 10.8 KB
/
stylesheet_graph.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'dart:collection';
import 'package:collection/collection.dart';
import 'package:tuple/tuple.dart';
import 'ast/sass.dart';
import 'import_cache.dart';
import 'importer.dart';
import 'visitor/find_dependencies.dart';
/// A graph of the import relationships between stylesheets, available via
/// [nodes].
class StylesheetGraph {
/// A map from canonical URLs to the stylesheet nodes for those URLs.
Map<Uri, StylesheetNode> get nodes => UnmodifiableMapView(_nodes);
final _nodes = <Uri, StylesheetNode>{};
/// The import cache used to load stylesheets.
final ImportCache importCache;
/// A map from canonical URLs to the time the corresponding stylesheet or any
/// of the stylesheets it transitively imports was modified.
final _transitiveModificationTimes = <Uri, DateTime>{};
StylesheetGraph(this.importCache);
/// Returns whether the stylesheet at [url] or any of the stylesheets it
/// imports were modified since [since].
///
/// If [baseImporter] is non-`null`, this first tries to use [baseImporter] to
/// import [url] (resolved relative to [baseUrl] if it's passed).
///
/// Returns `true` if the import cache can't find a stylesheet at [url].
bool modifiedSince(Uri url, DateTime since,
[Importer baseImporter, Uri baseUrl]) {
DateTime transitiveModificationTime(StylesheetNode node) {
return _transitiveModificationTimes.putIfAbsent(node.canonicalUrl, () {
var latest = node.importer.modificationTime(node.canonicalUrl);
for (var upstream in node.upstream.values) {
// If an import is missing, always recompile so we show the user the
// error.
var upstreamTime = upstream == null
? DateTime.now()
: transitiveModificationTime(upstream);
if (upstreamTime.isAfter(latest)) latest = upstreamTime;
}
return latest;
});
}
var node = _add(url, baseImporter, baseUrl);
if (node == null) return true;
return transitiveModificationTime(node).isAfter(since);
}
/// Adds the stylesheet at [url] and all the stylesheets it imports to this
/// graph and returns its node.
///
/// If [baseImporter] is non-`null`, this first tries to use [baseImporter] to
/// import [url] (resolved relative to [baseUrl] if it's passed).
///
/// Returns `null` if the import cache can't find a stylesheet at [url].
StylesheetNode _add(Uri url, [Importer baseImporter, Uri baseUrl]) {
var tuple = _ignoreErrors(() => importCache.canonicalize(url,
baseImporter: baseImporter, baseUrl: baseUrl));
if (tuple == null) return null;
return addCanonical(tuple.item1, tuple.item2, tuple.item3);
}
/// Adds the stylesheet at the canonicalized [canonicalUrl] and all the
/// stylesheets it imports to this graph and returns its node.
///
/// Returns `null` if [importer] can't import [canonicalUrl].
///
/// If passed, the [originalUrl] represents the URL that was canonicalized
/// into [canonicalUrl]. It's used as the URL for the parsed stylesheet, which
/// is in turn used in error reporting.
StylesheetNode addCanonical(Importer importer, Uri canonicalUrl,
[Uri originalUrl]) {
var stylesheet = _ignoreErrors(
() => importCache.importCanonical(importer, canonicalUrl, originalUrl));
if (stylesheet == null) return null;
return _nodes.putIfAbsent(
canonicalUrl,
() => StylesheetNode._(stylesheet, importer, canonicalUrl,
_upstreamNodes(stylesheet, importer, canonicalUrl)));
}
/// Returns a map from non-canonicalized imported URLs in [stylesheet], which
/// appears within [baseUrl] imported by [baseImporter].
Map<Tuple2<Uri, bool>, StylesheetNode> _upstreamNodes(
Stylesheet stylesheet, Importer baseImporter, Uri baseUrl) {
var active = {baseUrl};
var tuple = findDependencies(stylesheet);
var importUrls = tuple.item2.map((import) => Uri.parse(import.url));
return {
for (var url in tuple.item1)
Tuple2(url, false): _nodeFor(url, baseImporter, baseUrl, active),
for (var url in importUrls)
Tuple2(url, true):
_nodeFor(url, baseImporter, baseUrl, active, forImport: true)
};
}
/// Re-parses the stylesheet at [canonicalUrl] and updates the dependency graph
/// accordingly.
///
/// Throws a [StateError] if [canonicalUrl] isn't already in the dependency graph.
///
/// Removes the stylesheet from the graph entirely and returns `null` if the
/// stylesheet's importer can no longer import it.
StylesheetNode reload(Uri canonicalUrl) {
var node = _nodes[canonicalUrl];
if (node == null) {
throw StateError("$canonicalUrl is not in the dependency graph.");
}
// Rather than spending time computing exactly which modification times
// should be updated, just clear the cache and let it be computed again
// later.
_transitiveModificationTimes.clear();
importCache.clearImport(canonicalUrl);
var stylesheet = _ignoreErrors(
() => importCache.importCanonical(node.importer, canonicalUrl));
if (stylesheet == null) {
remove(canonicalUrl);
return null;
}
node._stylesheet = stylesheet;
node._stylesheet = stylesheet;
node._replaceUpstream(
_upstreamNodes(stylesheet, node.importer, canonicalUrl));
return node;
}
/// Removes the stylesheet at [canonicalUrl] from the stylesheet graph.
///
/// Throws a [StateError] if [canonicalUrl] isn't already in the dependency graph.
void remove(Uri canonicalUrl) {
var node = _nodes.remove(canonicalUrl);
if (node == null) {
throw StateError("$canonicalUrl is not in the dependency graph.");
}
// Rather than spending time computing exactly which modification times
// should be updated, just clear the cache and let it be computed again
// later.
_transitiveModificationTimes.clear();
importCache.clearImport(canonicalUrl);
node._remove();
}
/// Returns the [StylesheetNode] for the stylesheet at the given [url], which
/// appears within [baseUrl] imported by [baseImporter].
///
/// The [active] set should contain the canonical URLs that are currently
/// being imported. It's used to detect circular imports.
StylesheetNode _nodeFor(
Uri url, Importer baseImporter, Uri baseUrl, Set<Uri> active,
{bool forImport = false}) {
var tuple = _ignoreErrors(() => importCache.canonicalize(url,
baseImporter: baseImporter, baseUrl: baseUrl, forImport: forImport));
// If an import fails, let the evaluator surface that error rather than
// surfacing it here.
if (tuple == null) return null;
var importer = tuple.item1;
var canonicalUrl = tuple.item2;
var resolvedUrl = tuple.item3;
// Don't use [putIfAbsent] here because we want to avoid adding an entry if
// the import fails.
if (_nodes.containsKey(canonicalUrl)) return _nodes[canonicalUrl];
/// If we detect a circular import, act as though it doesn't exist. A better
/// error will be produced during compilation.
if (active.contains(canonicalUrl)) return null;
var stylesheet = _ignoreErrors(
() => importCache.importCanonical(importer, canonicalUrl, resolvedUrl));
if (stylesheet == null) return null;
active.add(canonicalUrl);
var node = StylesheetNode._(stylesheet, importer, canonicalUrl,
_upstreamNodes(stylesheet, importer, canonicalUrl));
active.remove(canonicalUrl);
_nodes[canonicalUrl] = node;
return node;
}
/// Clears the cached canonical version of the given [url] in [importCache].
///
/// Also resets the cached modification times for stylesheets in the graph.
void clearCanonicalize(Uri url) {
// Rather than spending time computing exactly which modification times
// should be updated, just clear the cache and let it be computed again
// later.
_transitiveModificationTimes.clear();
importCache.clearCanonicalize(url);
}
/// Runs [callback] and returns its result.
///
/// If [callback] throws any errors, ignores them and returns `null`. This is
/// used to wrap calls to the import cache, since importer errors should be
/// surfaced by the compilation process rather than the graph.
T _ignoreErrors<T>(T callback()) {
try {
return callback();
} catch (_) {
return null;
}
}
}
/// A node in a [StylesheetGraph] that tracks a single stylesheet and all the
/// upstream stylesheets it imports and the downstream stylesheets that import
/// it.
///
/// A [StylesheetNode] is immutable except for its downstream nodes. When the
/// stylesheet itself changes, a new node should be generated.
class StylesheetNode {
/// The parsed stylesheet.
Stylesheet get stylesheet => _stylesheet;
Stylesheet _stylesheet;
/// The importer that was used to load this stylesheet.
final Importer importer;
/// The canonical URL of [stylesheet].
final Uri canonicalUrl;
/// A map from non-canonicalized import URLs in [stylesheet] to the
/// stylesheets those imports refer to.
///
/// This may have `null` values, which indicate failed imports.
Map<Tuple2<Uri, bool>, StylesheetNode> get upstream =>
UnmodifiableMapView(_upstream);
Map<Tuple2<Uri, bool>, StylesheetNode> _upstream;
/// The stylesheets that import [stylesheet].
Set<StylesheetNode> get downstream => UnmodifiableSetView(_downstream);
final _downstream = <StylesheetNode>{};
StylesheetNode._(
this._stylesheet, this.importer, this.canonicalUrl, this._upstream) {
for (var node in upstream.values) {
if (node != null) node._downstream.add(this);
}
}
/// Sets [newUpstream] as the new value of [upstream] and adjusts upstream
/// nodes' [downstream] fields accordingly.
void _replaceUpstream(Map<Tuple2<Uri, bool>, StylesheetNode> newUpstream) {
var oldUpstream = Set.of(upstream.values)..remove(null);
var newUpstreamSet = Set.of(newUpstream.values)..remove(null);
for (var removed in oldUpstream.difference(newUpstreamSet)) {
var wasRemoved = removed._downstream.remove(this);
assert(wasRemoved);
}
for (var added in newUpstreamSet.difference(oldUpstream)) {
var wasAdded = added._downstream.add(this);
assert(wasAdded);
}
_upstream = newUpstream;
}
/// Removes [this] as an upstream and downstream node from all the nodes that
/// import it and that it imports.
void _remove() {
for (var node in upstream.values) {
if (node == null) continue;
var wasRemoved = node._downstream.remove(this);
assert(wasRemoved);
}
for (var node in downstream) {
for (var url in node._upstream.keys.toList()) {
if (node._upstream[url] == this) {
node._upstream[url] = null;
break;
}
}
}
}
}