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