| 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, String> calculatePackageMap() { |
| 23 Map<String, String> map = new Map<String, String>(); |
| 24 for (AnalysisContext context in _contexts) { |
| 25 Map<String, Folder> packageMap = context.sourceFactory.packageMap; |
| 26 if (packageMap != null) { |
| 27 packageMap.forEach( |
| 28 (String name, Folder folder) => map.putIfAbsent(name, () => folder.p
ath)); |
| 29 } |
| 30 } |
| 31 return map; |
| 32 } |
| 33 |
| 20 Set<String> collectLibraryDependencies() { | 34 Set<String> collectLibraryDependencies() { |
| 21 _contexts.forEach( | 35 _contexts.forEach( |
| 22 (AnalysisContext context) => | 36 (AnalysisContext context) => |
| 23 context.librarySources.forEach( | 37 context.librarySources.forEach( |
| 24 (Source source) => _addDependencies(context.getLibraryElement(so
urce)))); | 38 (Source source) => _addDependencies(context.getLibraryElement(so
urce)))); |
| 25 return _dependencies; | 39 return _dependencies; |
| 26 } | 40 } |
| 27 | 41 |
| 28 void _addDependencies(LibraryElement libraryElement) { | 42 void _addDependencies(LibraryElement libraryElement) { |
| 29 if (libraryElement == null) { | 43 if (libraryElement == null) { |
| 30 return; | 44 return; |
| 31 } | 45 } |
| 32 if (_visitedLibraries.add(libraryElement)) { | 46 if (_visitedLibraries.add(libraryElement)) { |
| 33 for (CompilationUnitElement cu in libraryElement.units) { | 47 for (CompilationUnitElement cu in libraryElement.units) { |
| 34 String path = cu.source.fullName; | 48 String path = cu.source.fullName; |
| 35 if (path != null) { | 49 if (path != null) { |
| 36 _dependencies.add(path); | 50 _dependencies.add(path); |
| 37 } | 51 } |
| 38 } | 52 } |
| 39 libraryElement.imports.forEach( | 53 libraryElement.imports.forEach( |
| 40 (ImportElement import) => _addDependencies(import.importedLibrary)); | 54 (ImportElement import) => _addDependencies(import.importedLibrary)); |
| 41 libraryElement.exports.forEach( | 55 libraryElement.exports.forEach( |
| 42 (ExportElement export) => _addDependencies(export.exportedLibrary)); | 56 (ExportElement export) => _addDependencies(export.exportedLibrary)); |
| 43 } | 57 } |
| 44 } | 58 } |
| 45 } | 59 } |
| OLD | NEW |