OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library dev_compiler.src.transformer.asset_universe; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:analyzer/analyzer.dart' show UriBasedDirective, parseDirectives; | |
10 import 'package:barback/barback.dart' show Asset, AssetId; | |
11 | |
12 import 'asset_source.dart'; | |
13 import 'uri_resolver.dart' show assetIdToUri, resolveAssetId; | |
14 | |
15 /// Set of assets sources available for analysis / compilation. | |
16 class AssetUniverse { | |
17 final _assetCache = <AssetId, AssetSource>{}; | |
18 | |
19 Iterable<AssetId> get assetIds => _assetCache.keys; | |
20 | |
21 AssetSource getAssetSource(AssetId id) { | |
22 var source = _assetCache[id]; | |
23 if (source == null) { | |
24 throw new ArgumentError(id.toString()); | |
25 } | |
26 return source; | |
27 } | |
28 | |
29 /// Recursively loads the asset with [id] and all its transitive dependencies. | |
30 Future scanSources(AssetId id, Future<Asset> getInput(AssetId id)) async { | |
31 if (_assetCache.containsKey(id)) return; | |
32 | |
33 var asset = await getInput(id); | |
34 var contents = await asset.readAsString(); | |
35 _assetCache[id] = | |
36 new AssetSource(Uri.parse(assetIdToUri(id)), asset, contents); | |
37 | |
38 var deps = _getDependentAssetIds(id, contents); | |
39 await Future.wait(deps.map((depId) => scanSources(depId, getInput))); | |
40 } | |
41 | |
42 Iterable<AssetId> _getDependentAssetIds(AssetId id, String contents) sync* { | |
43 var directives = parseDirectives(contents, suppressErrors: true).directives; | |
44 for (var directive in directives) { | |
45 if (directive is UriBasedDirective) { | |
46 var uri = directive.uri.stringValue; | |
47 var assetId = resolveAssetId(Uri.parse(uri), fromAssetId: id); | |
48 if (assetId != null) yield assetId; | |
49 } | |
50 } | |
51 } | |
52 } | |
OLD | NEW |