|
| 1 | +// Copyright (c) 2018, 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/analysis/analysis_context_collection.dart'; |
| 6 | +import 'package:analyzer/dart/analysis/context_locator.dart'; |
| 7 | +import 'package:analyzer/dart/analysis/context_root.dart'; |
| 8 | +import 'package:analyzer/dart/analysis/declared_variables.dart'; |
| 9 | +import 'package:analyzer/file_system/file_system.dart'; |
| 10 | +import 'package:analyzer/file_system/physical_file_system.dart'; |
| 11 | +import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 12 | +import 'package:analyzer/src/dart/analysis/context_builder.dart'; |
| 13 | +import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 14 | +import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart'; |
| 15 | +import 'package:analyzer/src/dart/analysis/file_content_cache.dart'; |
| 16 | +import 'package:analyzer/src/dart/analysis/info_declaration_store.dart'; |
| 17 | +import 'package:analyzer/src/dart/analysis/performance_logger.dart'; |
| 18 | +import 'package:analyzer/src/dart/analysis/unlinked_unit_store.dart'; |
| 19 | +import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl; |
| 20 | +import 'package:analyzer/src/generated/sdk.dart'; |
| 21 | +import 'package:analyzer/src/summary2/kernel_compilation_service.dart'; |
| 22 | +import 'package:analyzer/src/summary2/macro.dart'; |
| 23 | +import 'package:analyzer/src/util/sdk.dart'; |
| 24 | + |
| 25 | +/// An implementation of [AnalysisContextCollection]. |
| 26 | +class AnalysisContextCollectionImpl2 implements AnalysisContextCollection { |
| 27 | + /// The resource provider used to access the file system. |
| 28 | + final ResourceProvider resourceProvider; |
| 29 | + |
| 30 | + /// The support for executing macros. |
| 31 | + late final MacroSupport macroSupport; |
| 32 | + |
| 33 | + /// The shared container into which drivers record files ownership. |
| 34 | + final OwnedFiles ownedFiles = OwnedFiles(); |
| 35 | + |
| 36 | + /// The list of analysis contexts. |
| 37 | + @override |
| 38 | + final List<DriverBasedAnalysisContext> contexts = []; |
| 39 | + |
| 40 | + /// Initialize a newly created analysis context manager. |
| 41 | + AnalysisContextCollectionImpl2({ |
| 42 | + ByteStore? byteStore, |
| 43 | + Map<String, String>? declaredVariables, |
| 44 | + bool drainStreams = true, |
| 45 | + bool enableIndex = false, |
| 46 | + required List<String> includedPaths, |
| 47 | + List<String>? excludedPaths, |
| 48 | + List<String>? librarySummaryPaths, |
| 49 | + String? optionsFile, |
| 50 | + String? packagesFile, |
| 51 | + PerformanceLog? performanceLog, |
| 52 | + ResourceProvider? resourceProvider, |
| 53 | + bool retainDataForTesting = false, |
| 54 | + String? sdkPath, |
| 55 | + String? sdkSummaryPath, |
| 56 | + AnalysisDriverScheduler? scheduler, |
| 57 | + FileContentCache? fileContentCache, |
| 58 | + UnlinkedUnitStore? unlinkedUnitStore, |
| 59 | + InfoDeclarationStore? infoDeclarationStore, |
| 60 | + @Deprecated('Use updateAnalysisOptions2, which must be a function that ' |
| 61 | + 'accepts a second parameter') |
| 62 | + void Function(AnalysisOptionsImpl)? updateAnalysisOptions, |
| 63 | + void Function({ |
| 64 | + required AnalysisOptionsImpl analysisOptions, |
| 65 | + required ContextRoot contextRoot, |
| 66 | + required DartSdk sdk, |
| 67 | + })? updateAnalysisOptions2, |
| 68 | + MacroSupport? macroSupport, |
| 69 | + }) : resourceProvider = |
| 70 | + resourceProvider ?? PhysicalResourceProvider.INSTANCE { |
| 71 | + sdkPath ??= getSdkPath(); |
| 72 | + |
| 73 | + _throwIfAnyNotAbsoluteNormalizedPath(includedPaths); |
| 74 | + _throwIfNotAbsoluteNormalizedPath(sdkPath); |
| 75 | + |
| 76 | + if (updateAnalysisOptions != null && updateAnalysisOptions2 != null) { |
| 77 | + throw ArgumentError( |
| 78 | + 'Either updateAnalysisOptions or updateAnalysisOptions2 must be ' |
| 79 | + 'given, but not both.'); |
| 80 | + } |
| 81 | + |
| 82 | + this.macroSupport = macroSupport ??= KernelMacroSupport(); |
| 83 | + |
| 84 | + var contextLocator = ContextLocator( |
| 85 | + resourceProvider: this.resourceProvider, |
| 86 | + ); |
| 87 | + var roots = contextLocator.locateRoots( |
| 88 | + includedPaths: includedPaths, |
| 89 | + excludedPaths: excludedPaths, |
| 90 | + optionsFile: optionsFile, |
| 91 | + packagesFile: packagesFile, |
| 92 | + ); |
| 93 | + for (var root in roots) { |
| 94 | + var contextBuilder = ContextBuilderImpl( |
| 95 | + resourceProvider: this.resourceProvider, |
| 96 | + ); |
| 97 | + var context = contextBuilder.createContext( |
| 98 | + byteStore: byteStore, |
| 99 | + contextRoot: root, |
| 100 | + declaredVariables: DeclaredVariables.fromMap(declaredVariables ?? {}), |
| 101 | + drainStreams: drainStreams, |
| 102 | + enableIndex: enableIndex, |
| 103 | + librarySummaryPaths: librarySummaryPaths, |
| 104 | + performanceLog: performanceLog, |
| 105 | + retainDataForTesting: retainDataForTesting, |
| 106 | + sdkPath: sdkPath, |
| 107 | + sdkSummaryPath: sdkSummaryPath, |
| 108 | + scheduler: scheduler, |
| 109 | + // ignore: deprecated_member_use_from_same_package |
| 110 | + updateAnalysisOptions: updateAnalysisOptions, |
| 111 | + updateAnalysisOptions2: updateAnalysisOptions2, |
| 112 | + fileContentCache: fileContentCache, |
| 113 | + unlinkedUnitStore: unlinkedUnitStore ?? UnlinkedUnitStoreImpl(), |
| 114 | + infoDeclarationStore: infoDeclarationStore, |
| 115 | + macroSupport: macroSupport, |
| 116 | + ownedFiles: ownedFiles, |
| 117 | + ); |
| 118 | + contexts.add(context); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /// Return `true` if the read state of configuration files is consistent |
| 123 | + /// with their current state on the file system. We use this as a work |
| 124 | + /// around an issue with watching for file system changes. |
| 125 | + bool get areWorkspacesConsistent { |
| 126 | + for (var analysisContext in contexts) { |
| 127 | + var contextRoot = analysisContext.contextRoot; |
| 128 | + var workspace = contextRoot.workspace; |
| 129 | + if (!workspace.isConsistentWithFileSystem) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + } |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + @override |
| 137 | + DriverBasedAnalysisContext contextFor(String path) { |
| 138 | + _throwIfNotAbsoluteNormalizedPath(path); |
| 139 | + |
| 140 | + for (var context in contexts) { |
| 141 | + if (context.contextRoot.isAnalyzed(path)) { |
| 142 | + return context; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + throw StateError('Unable to find the context to $path'); |
| 147 | + } |
| 148 | + |
| 149 | + Future<void> dispose({ |
| 150 | + bool forTesting = false, |
| 151 | + }) async { |
| 152 | + for (final analysisContext in contexts) { |
| 153 | + await analysisContext.driver.dispose2(); |
| 154 | + } |
| 155 | + await macroSupport.dispose(); |
| 156 | + // If there are other collections, they will have to start it again. |
| 157 | + if (!forTesting) { |
| 158 | + await KernelCompilationService.dispose(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /// Check every element with [_throwIfNotAbsoluteNormalizedPath]. |
| 163 | + void _throwIfAnyNotAbsoluteNormalizedPath(List<String> paths) { |
| 164 | + for (var path in paths) { |
| 165 | + _throwIfNotAbsoluteNormalizedPath(path); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /// The driver supports only absolute normalized paths, this method is used |
| 170 | + /// to validate any input paths to prevent errors later. |
| 171 | + void _throwIfNotAbsoluteNormalizedPath(String path) { |
| 172 | + var pathContext = resourceProvider.pathContext; |
| 173 | + if (!pathContext.isAbsolute(path) || pathContext.normalize(path) != path) { |
| 174 | + throw ArgumentError( |
| 175 | + 'Only absolute normalized paths are supported: $path'); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments