|
| 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/context/context_root.dart' as old; |
| 6 | +import 'package:analyzer/context/declared_variables.dart'; |
| 7 | +import 'package:analyzer/dart/analysis/analysis_context.dart'; |
| 8 | +import 'package:analyzer/dart/analysis/context_builder.dart'; |
| 9 | +import 'package:analyzer/dart/analysis/context_root.dart'; |
| 10 | +import 'package:analyzer/file_system/file_system.dart'; |
| 11 | +import 'package:analyzer/file_system/physical_file_system.dart'; |
| 12 | +import 'package:analyzer/src/context/builder.dart' as old |
| 13 | + show ContextBuilder, ContextBuilderOptions; |
| 14 | +import 'package:analyzer/src/dart/analysis/driver.dart' |
| 15 | + show AnalysisDriver, AnalysisDriverScheduler; |
| 16 | +import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart'; |
| 17 | +import 'package:analyzer/src/dart/analysis/file_state.dart' |
| 18 | + show FileContentOverlay; |
| 19 | +import 'package:analyzer/src/dart/sdk/sdk.dart'; |
| 20 | +import 'package:analyzer/src/generated/sdk.dart' show DartSdkManager; |
| 21 | +import 'package:analyzer/src/generated/source.dart' show ContentCache; |
| 22 | +import 'package:front_end/src/base/performance_logger.dart' show PerformanceLog; |
| 23 | +import 'package:front_end/src/byte_store/byte_store.dart' show MemoryByteStore; |
| 24 | +import 'package:meta/meta.dart'; |
| 25 | + |
| 26 | +/** |
| 27 | + * An implementation of a context builder. |
| 28 | + */ |
| 29 | +class ContextBuilderImpl implements ContextBuilder { |
| 30 | + /** |
| 31 | + * The resource provider used to access the file system. |
| 32 | + */ |
| 33 | + final ResourceProvider resourceProvider; |
| 34 | + |
| 35 | + /** |
| 36 | + * Initialize a newly created context builder. If a [resourceProvider] is |
| 37 | + * given, then it will be used to access the file system, otherwise the |
| 38 | + * default resource provider will be used. |
| 39 | + */ |
| 40 | + ContextBuilderImpl({ResourceProvider resourceProvider}) |
| 41 | + : resourceProvider = |
| 42 | + resourceProvider ?? PhysicalResourceProvider.INSTANCE; |
| 43 | + |
| 44 | + /** |
| 45 | + * Return the path to the default location of the SDK, or `null` if the sdk |
| 46 | + * cannot be found. |
| 47 | + */ |
| 48 | + String get _defaultSdkPath => |
| 49 | + FolderBasedDartSdk.defaultSdkDirectory(resourceProvider)?.path; |
| 50 | + |
| 51 | + @override |
| 52 | + AnalysisContext createContext( |
| 53 | + {@required ContextRoot contextRoot, |
| 54 | + DeclaredVariables declaredVariables, |
| 55 | + String sdkPath}) { |
| 56 | + PerformanceLog performanceLog = new PerformanceLog(new StringBuffer()); |
| 57 | + AnalysisDriverScheduler scheduler = |
| 58 | + new AnalysisDriverScheduler(performanceLog); |
| 59 | + sdkPath ??= _defaultSdkPath; |
| 60 | + if (sdkPath == null) { |
| 61 | + throw new ArgumentError('Cannot find path to the SDK'); |
| 62 | + } |
| 63 | + DartSdkManager sdkManager = new DartSdkManager(sdkPath, true); |
| 64 | + scheduler.start(); |
| 65 | + |
| 66 | + // TODO(brianwilkerson) Move the required implementation from the old |
| 67 | + // ContextBuilder to this class and remove the old class. |
| 68 | + old.ContextBuilderOptions options = new old.ContextBuilderOptions(); |
| 69 | + if (declaredVariables != null) { |
| 70 | + options.declaredVariables = _toMap(declaredVariables); |
| 71 | + } |
| 72 | + options.defaultPackageFilePath = contextRoot.packagesFile?.path; |
| 73 | + |
| 74 | + old.ContextBuilder builder = new old.ContextBuilder( |
| 75 | + resourceProvider, sdkManager, new ContentCache(), |
| 76 | + options: options); |
| 77 | + builder.analysisDriverScheduler = scheduler; |
| 78 | + builder.byteStore = new MemoryByteStore(); |
| 79 | + builder.fileContentOverlay = new FileContentOverlay(); |
| 80 | + builder.performanceLog = performanceLog; |
| 81 | + |
| 82 | + old.ContextRoot oldContextRoot = new old.ContextRoot( |
| 83 | + contextRoot.root.path, contextRoot.excludedPaths.toList()); |
| 84 | + AnalysisDriver driver = builder.buildDriver(oldContextRoot); |
| 85 | + DriverBasedAnalysisContext context = |
| 86 | + new DriverBasedAnalysisContext(resourceProvider, contextRoot, driver); |
| 87 | + return context; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Convert the [declaredVariables] into a map for use with the old context |
| 92 | + * builder. |
| 93 | + */ |
| 94 | + Map<String, String> _toMap(DeclaredVariables declaredVariables) { |
| 95 | + Map<String, String> map = <String, String>{}; |
| 96 | + for (String name in declaredVariables.variableNames) { |
| 97 | + map[name] = declaredVariables.get(name); |
| 98 | + } |
| 99 | + return map; |
| 100 | + } |
| 101 | +} |
0 commit comments