OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 import 'package:analyzer/dart/element/element.dart'; | |
6 import 'package:analyzer/file_system/file_system.dart'; | |
7 import 'package:analyzer/src/generated/engine.dart'; | |
8 import 'package:analyzer/src/generated/source.dart'; | |
9 import 'package:analyzer/src/generated/source_io.dart'; | |
10 | |
11 class LibraryDependencyCollector { | |
12 final Set<LibraryElement> _visitedLibraries = new Set<LibraryElement>(); | |
13 final Set<String> _dependencies = new Set<String>(); | |
14 | |
15 final Iterable<AnalysisContext> _contexts; | |
16 | |
17 LibraryDependencyCollector(this._contexts); | |
18 | |
19 Map<String, Map<String, List<String>>> calculatePackageMap( | |
20 Map<Folder, AnalysisContext> folderMap) { | |
21 Map<AnalysisContext, Folder> contextMap = _reverse(folderMap); | |
22 Map<String, Map<String, List<String>>> result = | |
23 new Map<String, Map<String, List<String>>>(); | |
24 for (AnalysisContext context in _contexts) { | |
25 Map<String, List<Folder>> packageMap = context.sourceFactory.packageMap; | |
26 if (packageMap != null) { | |
27 Map<String, List<String>> map = new Map<String, List<String>>(); | |
28 packageMap.forEach((String name, List<Folder> folders) => | |
29 map[name] = new List.from(folders.map((Folder f) => f.path))); | |
30 result[contextMap[context].path] = map; | |
31 } | |
32 } | |
33 return result; | |
34 } | |
35 | |
36 Set<String> collectLibraryDependencies() { | |
37 _contexts.forEach((AnalysisContext context) => context.librarySources | |
38 .forEach((Source source) => | |
39 _addDependencies(context.getLibraryElement(source)))); | |
40 return _dependencies; | |
41 } | |
42 | |
43 void _addDependencies(LibraryElement libraryElement) { | |
44 if (libraryElement == null) { | |
45 return; | |
46 } | |
47 if (_visitedLibraries.add(libraryElement)) { | |
48 for (CompilationUnitElement cu in libraryElement.units) { | |
49 String path = cu.source.fullName; | |
50 if (path != null) { | |
51 _dependencies.add(path); | |
52 } | |
53 } | |
54 libraryElement.imports.forEach( | |
55 (ImportElement import) => _addDependencies(import.importedLibrary)); | |
56 libraryElement.exports.forEach( | |
57 (ExportElement export) => _addDependencies(export.exportedLibrary)); | |
58 } | |
59 } | |
60 | |
61 Map<AnalysisContext, Folder> _reverse(Map<Folder, AnalysisContext> map) { | |
62 Map<AnalysisContext, Folder> reverseMap = | |
63 new Map<AnalysisContext, Folder>(); | |
64 map.forEach((Folder f, AnalysisContext c) => reverseMap[c] = f); | |
65 return reverseMap; | |
66 } | |
67 } | |
OLD | NEW |