Chromium Code Reviews| Index: pkg/analysis_server/lib/src/services/dependencies/library_dependencies.dart |
| =================================================================== |
| --- pkg/analysis_server/lib/src/services/dependencies/library_dependencies.dart (revision 0) |
| +++ pkg/analysis_server/lib/src/services/dependencies/library_dependencies.dart (revision 0) |
| @@ -0,0 +1,57 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'dart:math' as math; |
| + |
| +import 'package:analyzer/src/generated/element.dart'; |
| +import 'package:analyzer/src/generated/engine.dart'; |
| +import 'package:analyzer/src/generated/source.dart'; |
| + |
| +String _getParentPath(String path) { |
|
scheglov
2015/01/21 15:10:06
Use "dirname" from "package:path" instead.
pquitslund
2015/01/22 19:22:15
Good idea. I'm changing the API to return librari
|
| + if (path.length == 0) { |
| + return ''; |
| + } |
| + int end = math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); |
| + if (end == path.length - 1) { |
| + end = |
| + math.max(path.lastIndexOf('/', end - 1), path.lastIndexOf('\\', end - 1)); |
| + } |
| + return end == -1 ? '' : path.substring(0, end); |
| +} |
| + |
| +class LibraryDependencyCollector { |
| + |
| + final Set<LibraryElement> myVisitedLibraries = new Set<LibraryElement>(); |
| + final Set<String> _dependencies = new Set<String>(); |
| + |
| + final List<AnalysisContext> _contexts; |
| + |
| + LibraryDependencyCollector(this._contexts); |
| + |
| + Set<String> collectFolderDependencies() { |
| + _contexts.forEach( |
| + (AnalysisContext context) => |
| + context.librarySources.forEach( |
| + (Source source) => _addDependencies(context.getLibraryElement(source)))); |
| + return _dependencies; |
| + } |
| + |
| + void _addDependencies(LibraryElement libraryElement) { |
| + if (libraryElement == null) { |
| + return; |
| + } |
| + if (myVisitedLibraries.add(libraryElement)) { |
| + for (CompilationUnitElement cu in libraryElement.units) { |
| + final String path = cu.source.fullName; |
| + if (path != null) { |
| + _dependencies.add(_getParentPath(path)); |
| + } |
| + } |
| + libraryElement.imports.forEach( |
| + (ImportElement import) => _addDependencies(import.importedLibrary)); |
| + libraryElement.exports.forEach( |
| + (ExportElement export) => _addDependencies(export.exportedLibrary)); |
| + } |
| + } |
| +} |