| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 context.directory.manager; | 5 library context.directory.manager; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:core' hide Resource; | 9 import 'dart:core' hide Resource; |
| 10 | 10 |
| 11 import 'package:analysis_server/src/analysis_server.dart'; | 11 import 'package:analysis_server/src/analysis_server.dart'; |
| 12 import 'package:analysis_server/src/source/optimizing_pub_package_map_provider.d
art'; | 12 import 'package:analysis_server/src/source/optimizing_pub_package_map_provider.d
art'; |
| 13 import 'package:analysis_server/uri/resolver_provider.dart'; | 13 import 'package:analysis_server/uri/resolver_provider.dart'; |
| 14 import 'package:analyzer/file_system/file_system.dart'; | 14 import 'package:analyzer/file_system/file_system.dart'; |
| 15 import 'package:analyzer/instrumentation/instrumentation.dart'; | 15 import 'package:analyzer/instrumentation/instrumentation.dart'; |
| 16 import 'package:analyzer/source/package_map_resolver.dart'; | 16 import 'package:analyzer/source/package_map_resolver.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; | 17 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/java_io.dart'; | 18 import 'package:analyzer/src/generated/java_io.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| 20 import 'package:analyzer/src/generated/source_io.dart'; | 20 import 'package:analyzer/src/generated/source_io.dart'; |
| 21 import 'package:path/path.dart' as pathos; | 21 import 'package:path/path.dart' as pathos; |
| 22 import 'package:watcher/watcher.dart'; | 22 import 'package:watcher/watcher.dart'; |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * The name of `packages` folders. | |
| 26 */ | |
| 27 const String PACKAGES_NAME = 'packages'; | |
| 28 | |
| 29 /** | |
| 30 * File name of pubspec files. | |
| 31 */ | |
| 32 const String PUBSPEC_NAME = 'pubspec.yaml'; | |
| 33 | |
| 34 /** | |
| 35 * Class that maintains a mapping from included/excluded paths to a set of | 25 * Class that maintains a mapping from included/excluded paths to a set of |
| 36 * folders that should correspond to analysis contexts. | 26 * folders that should correspond to analysis contexts. |
| 37 */ | 27 */ |
| 38 abstract class ContextManager { | 28 abstract class AbstractContextManager implements ContextManager { |
| 39 /** | 29 /** |
| 40 * The name of the `lib` directory. | 30 * The name of the `lib` directory. |
| 41 */ | 31 */ |
| 42 static const String LIB_DIR_NAME = 'lib'; | 32 static const String LIB_DIR_NAME = 'lib'; |
| 43 | 33 |
| 44 /** | 34 /** |
| 35 * The name of `packages` folders. |
| 36 */ |
| 37 static const String PACKAGES_NAME = 'packages'; |
| 38 |
| 39 /** |
| 40 * File name of pubspec files. |
| 41 */ |
| 42 static const String PUBSPEC_NAME = 'pubspec.yaml'; |
| 43 |
| 44 /** |
| 45 * [_ContextInfo] object for each included directory in the most | 45 * [_ContextInfo] object for each included directory in the most |
| 46 * recent successful call to [setRoots]. | 46 * recent successful call to [setRoots]. |
| 47 */ | 47 */ |
| 48 Map<Folder, _ContextInfo> _contexts = new HashMap<Folder, _ContextInfo>(); | 48 Map<Folder, _ContextInfo> _contexts = new HashMap<Folder, _ContextInfo>(); |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * The [ResourceProvider] using which paths are converted into [Resource]s. | 51 * The [ResourceProvider] using which paths are converted into [Resource]s. |
| 52 */ | 52 */ |
| 53 final ResourceProvider resourceProvider; | 53 final ResourceProvider resourceProvider; |
| 54 | 54 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 * Provider which is used to determine the mapping from package name to | 91 * Provider which is used to determine the mapping from package name to |
| 92 * package folder. | 92 * package folder. |
| 93 */ | 93 */ |
| 94 final OptimizingPubPackageMapProvider _packageMapProvider; | 94 final OptimizingPubPackageMapProvider _packageMapProvider; |
| 95 | 95 |
| 96 /** | 96 /** |
| 97 * The instrumentation service used to report instrumentation data. | 97 * The instrumentation service used to report instrumentation data. |
| 98 */ | 98 */ |
| 99 final InstrumentationService _instrumentationService; | 99 final InstrumentationService _instrumentationService; |
| 100 | 100 |
| 101 ContextManager(this.resourceProvider, this.packageResolverProvider, | 101 AbstractContextManager(this.resourceProvider, this.packageResolverProvider, |
| 102 this._packageMapProvider, this._instrumentationService) { | 102 this._packageMapProvider, this._instrumentationService) { |
| 103 pathContext = resourceProvider.pathContext; | 103 pathContext = resourceProvider.pathContext; |
| 104 } | 104 } |
| 105 | 105 |
| 106 /** | 106 /** |
| 107 * Create and return a new analysis context. | 107 * Create and return a new analysis context. |
| 108 */ | 108 */ |
| 109 AnalysisContext addContext(Folder folder, UriResolver packageUriResolver); | 109 AnalysisContext addContext(Folder folder, UriResolver packageUriResolver); |
| 110 | 110 |
| 111 /** | 111 /** |
| (...skipping 26 matching lines...) Expand all Loading... |
| 138 AnalysisContext contextN = contextInfo.context; | 138 AnalysisContext contextN = contextInfo.context; |
| 139 if (context != contextN) { | 139 if (context != contextN) { |
| 140 for (Source source in contextN.sources) { | 140 for (Source source in contextN.sources) { |
| 141 flushedFiles.remove(source.fullName); | 141 flushedFiles.remove(source.fullName); |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 return flushedFiles.toList(growable: false); | 145 return flushedFiles.toList(growable: false); |
| 146 } | 146 } |
| 147 | 147 |
| 148 /** | 148 @override |
| 149 * Return a list containing all of the contexts contained in the given | |
| 150 * [analysisRoot]. | |
| 151 */ | |
| 152 List<AnalysisContext> contextsInAnalysisRoot(Folder analysisRoot) { | 149 List<AnalysisContext> contextsInAnalysisRoot(Folder analysisRoot) { |
| 153 List<AnalysisContext> contexts = <AnalysisContext>[]; | 150 List<AnalysisContext> contexts = <AnalysisContext>[]; |
| 154 _contexts.forEach((Folder contextFolder, _ContextInfo info) { | 151 _contexts.forEach((Folder contextFolder, _ContextInfo info) { |
| 155 if (analysisRoot.isOrContains(contextFolder.path)) { | 152 if (analysisRoot.isOrContains(contextFolder.path)) { |
| 156 contexts.add(info.context); | 153 contexts.add(info.context); |
| 157 } | 154 } |
| 158 }); | 155 }); |
| 159 return contexts; | 156 return contexts; |
| 160 } | 157 } |
| 161 | 158 |
| 162 /** | 159 /** |
| 163 * We have finished computing the package map. | 160 * We have finished computing the package map. |
| 164 */ | 161 */ |
| 165 void endComputePackageMap() { | 162 void endComputePackageMap() { |
| 166 // Do nothing. | 163 // Do nothing. |
| 167 } | 164 } |
| 168 | 165 |
| 169 /** | 166 @override |
| 170 * Returns `true` if the given absolute [path] is in one of the current | |
| 171 * root folders and is not excluded. | |
| 172 */ | |
| 173 bool isInAnalysisRoot(String path) { | 167 bool isInAnalysisRoot(String path) { |
| 174 // check if excluded | 168 // check if excluded |
| 175 if (_isExcluded(path)) { | 169 if (_isExcluded(path)) { |
| 176 return false; | 170 return false; |
| 177 } | 171 } |
| 178 // check if in of the roots | 172 // check if in of the roots |
| 179 for (Folder root in _contexts.keys) { | 173 for (Folder root in _contexts.keys) { |
| 180 if (root.contains(path)) { | 174 if (root.contains(path)) { |
| 181 return true; | 175 return true; |
| 182 } | 176 } |
| 183 } | 177 } |
| 184 // no | 178 // no |
| 185 return false; | 179 return false; |
| 186 } | 180 } |
| 187 | 181 |
| 188 /** | 182 @override |
| 189 * Rebuild the set of contexts from scratch based on the data last sent to | |
| 190 * setRoots(). Only contexts contained in the given list of analysis [roots] | |
| 191 * will be rebuilt, unless the list is `null`, in which case every context | |
| 192 * will be rebuilt. | |
| 193 */ | |
| 194 void refresh(List<Resource> roots) { | 183 void refresh(List<Resource> roots) { |
| 195 // Destroy old contexts | 184 // Destroy old contexts |
| 196 List<Folder> contextFolders = _contexts.keys.toList(); | 185 List<Folder> contextFolders = _contexts.keys.toList(); |
| 197 if (roots == null) { | 186 if (roots == null) { |
| 198 contextFolders.forEach(_destroyContext); | 187 contextFolders.forEach(_destroyContext); |
| 199 } else { | 188 } else { |
| 200 roots.forEach((Resource resource) { | 189 roots.forEach((Resource resource) { |
| 201 contextFolders.forEach((Folder contextFolder) { | 190 contextFolders.forEach((Folder contextFolder) { |
| 202 if (resource is Folder && resource.isOrContains(contextFolder.path)) { | 191 if (resource is Folder && resource.isOrContains(contextFolder.path)) { |
| 203 _destroyContext(contextFolder); | 192 _destroyContext(contextFolder); |
| 204 } | 193 } |
| 205 }); | 194 }); |
| 206 }); | 195 }); |
| 207 } | 196 } |
| 208 | 197 |
| 209 // Rebuild contexts based on the data last sent to setRoots(). | 198 // Rebuild contexts based on the data last sent to setRoots(). |
| 210 setRoots(includedPaths, excludedPaths, packageRoots); | 199 setRoots(includedPaths, excludedPaths, packageRoots); |
| 211 } | 200 } |
| 212 | 201 |
| 213 /** | 202 /** |
| 214 * Remove the context associated with the given [folder]. | 203 * Remove the context associated with the given [folder]. |
| 215 */ | 204 */ |
| 216 void removeContext(Folder folder); | 205 void removeContext(Folder folder); |
| 217 | 206 |
| 218 /** | 207 @override |
| 219 * Change the set of paths which should be used as starting points to | |
| 220 * determine the context directories. | |
| 221 */ | |
| 222 void setRoots(List<String> includedPaths, List<String> excludedPaths, | 208 void setRoots(List<String> includedPaths, List<String> excludedPaths, |
| 223 Map<String, String> packageRoots) { | 209 Map<String, String> packageRoots) { |
| 224 this.packageRoots = packageRoots; | 210 this.packageRoots = packageRoots; |
| 225 | 211 |
| 226 // Normalize all package root sources by mapping them to folders on the | 212 // Normalize all package root sources by mapping them to folders on the |
| 227 // filesystem. Ignore any package root sources that aren't folders. | 213 // filesystem. Ignore any package root sources that aren't folders. |
| 228 normalizedPackageRoots = <String, String>{}; | 214 normalizedPackageRoots = <String, String>{}; |
| 229 packageRoots.forEach((String sourcePath, String targetPath) { | 215 packageRoots.forEach((String sourcePath, String targetPath) { |
| 230 Resource resource = resourceProvider.getResource(sourcePath); | 216 Resource resource = resourceProvider.getResource(sourcePath); |
| 231 if (resource is Folder) { | 217 if (resource is Folder) { |
| (...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 Source source = file.createSource(); | 745 Source source = file.createSource(); |
| 760 if (context == null) { | 746 if (context == null) { |
| 761 return source; | 747 return source; |
| 762 } | 748 } |
| 763 Uri uri = context.sourceFactory.restoreUri(source); | 749 Uri uri = context.sourceFactory.restoreUri(source); |
| 764 return file.createSource(uri); | 750 return file.createSource(uri); |
| 765 } | 751 } |
| 766 } | 752 } |
| 767 | 753 |
| 768 /** | 754 /** |
| 755 * Class that maintains a mapping from included/excluded paths to a set of |
| 756 * folders that should correspond to analysis contexts. |
| 757 */ |
| 758 abstract class ContextManager { |
| 759 // TODO(brianwilkerson) Support: |
| 760 // setting the default analysis options |
| 761 // setting the default content cache |
| 762 // setting the default SDK |
| 763 // maintaining AnalysisContext.folderMap (or remove it) |
| 764 // telling server when a context has been added or removed (see onContextsCh
anged) |
| 765 // telling server when a context needs to be re-analyzed |
| 766 // notifying the client when results should be flushed |
| 767 // using analyzeFileFunctions to determine which files to analyze |
| 768 // |
| 769 // TODO(brianwilkerson) Move this class to a public library. |
| 770 |
| 771 // /** |
| 772 // * The default options used to create new analysis contexts. |
| 773 // */ |
| 774 // AnalysisOptionsImpl get defaultOptions; |
| 775 |
| 776 /** |
| 777 * Return the list of excluded paths (folders and files) most recently passed |
| 778 * to [setRoots]. |
| 779 */ |
| 780 List<String> get excludedPaths; |
| 781 |
| 782 /** |
| 783 * Return the list of included paths (folders and files) most recently passed |
| 784 * to [setRoots]. |
| 785 */ |
| 786 List<String> get includedPaths; |
| 787 |
| 788 // /** |
| 789 // * A stream that is notified when contexts are added or removed. |
| 790 // */ |
| 791 // Stream<ContextsChangedEvent> get onContextsChanged; |
| 792 |
| 793 /** |
| 794 * Return a list containing all of the contexts contained in the given |
| 795 * [analysisRoot]. |
| 796 */ |
| 797 List<AnalysisContext> contextsInAnalysisRoot(Folder analysisRoot); |
| 798 |
| 799 /** |
| 800 * Return `true` if the given absolute [path] is in one of the current |
| 801 * root folders and is not excluded. |
| 802 */ |
| 803 bool isInAnalysisRoot(String path); |
| 804 |
| 805 /** |
| 806 * Rebuild the set of contexts from scratch based on the data last sent to |
| 807 * [setRoots]. Only contexts contained in the given list of analysis [roots] |
| 808 * will be rebuilt, unless the list is `null`, in which case every context |
| 809 * will be rebuilt. |
| 810 */ |
| 811 void refresh(List<Resource> roots); |
| 812 |
| 813 /** |
| 814 * Change the set of paths which should be used as starting points to |
| 815 * determine the context directories. |
| 816 */ |
| 817 void setRoots(List<String> includedPaths, List<String> excludedPaths, |
| 818 Map<String, String> packageRoots); |
| 819 } |
| 820 |
| 821 /** |
| 822 * An indication that one or more contexts were added, changed, or removed. |
| 823 * |
| 824 * The lists of [added], [changed] and [removed] contexts will not contain |
| 825 * duplications (that is, a single context will not be in any list multiple |
| 826 * times), nor will there be any overlap between the lists (that is, a single |
| 827 * context will not be in more than one list). |
| 828 */ |
| 829 abstract class ContextsChangedEvent { |
| 830 /** |
| 831 * Return the contexts that were added to the server. |
| 832 */ |
| 833 List<AnalysisContext> get added; |
| 834 |
| 835 /** |
| 836 * Return the contexts that were changed. |
| 837 */ |
| 838 List<AnalysisContext> get changed; |
| 839 |
| 840 /** |
| 841 * Return the contexts that were removed from the server. |
| 842 */ |
| 843 List<AnalysisContext> get removed; |
| 844 } |
| 845 |
| 846 /** |
| 769 * Information tracked by the [ContextManager] for each context. | 847 * Information tracked by the [ContextManager] for each context. |
| 770 */ | 848 */ |
| 771 class _ContextInfo { | 849 class _ContextInfo { |
| 772 /** | 850 /** |
| 773 * The [Folder] for which this information object is created. | 851 * The [Folder] for which this information object is created. |
| 774 */ | 852 */ |
| 775 final Folder folder; | 853 final Folder folder; |
| 776 | 854 |
| 777 /** | 855 /** |
| 778 * The enclosed pubspec-based contexts. | 856 * The enclosed pubspec-based contexts. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 return excludes(resource.path); | 931 return excludes(resource.path); |
| 854 } | 932 } |
| 855 | 933 |
| 856 /** | 934 /** |
| 857 * Returns `true` if [path] is the pubspec file of this context. | 935 * Returns `true` if [path] is the pubspec file of this context. |
| 858 */ | 936 */ |
| 859 bool isPubspec(String path) { | 937 bool isPubspec(String path) { |
| 860 return path == pubspecPath; | 938 return path == pubspecPath; |
| 861 } | 939 } |
| 862 } | 940 } |
| OLD | NEW |