|
| 1 | +// Copyright (c) 2021, 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 | +// ignore_for_file: implementation_imports |
| 6 | + |
| 7 | +import 'dart:io'; |
| 8 | +import 'dart:typed_data'; |
| 9 | + |
| 10 | +import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; |
| 11 | +import 'package:analyzer/dart/analysis/analysis_options.dart'; |
| 12 | +import 'package:analyzer/dart/analysis/session.dart'; |
| 13 | +import 'package:analyzer/file_system/file_system.dart'; |
| 14 | +import 'package:analyzer/src/context/packages.dart'; |
| 15 | +import 'package:analyzer/src/dart/analysis/analysis_options.dart'; |
| 16 | +import 'package:analyzer/src/dart/analysis/analysis_options_map.dart'; |
| 17 | +import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 18 | +import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 19 | +import 'package:analyzer/src/dart/analysis/file_content_cache.dart'; |
| 20 | +import 'package:analyzer/src/dart/analysis/performance_logger.dart'; |
| 21 | +import 'package:analyzer/src/dart/sdk/sdk.dart'; |
| 22 | +import 'package:analyzer/src/generated/source.dart'; |
| 23 | +import 'package:analyzer/src/summary/summary_sdk.dart'; |
| 24 | +import 'package:analyzer/src/summary2/package_bundle_format.dart'; |
| 25 | +import 'package:path/path.dart' as p; |
| 26 | + |
| 27 | +export 'package:analyzer/dart/analysis/analysis_options.dart' |
| 28 | + show AnalysisOptions; |
| 29 | +export 'package:analyzer/source/source.dart' show Source; |
| 30 | +export 'package:analyzer/src/context/packages.dart' show Package, Packages; |
| 31 | +export 'package:analyzer/src/dart/analysis/analysis_options.dart' |
| 32 | + show AnalysisOptionsImpl; |
| 33 | +export 'package:analyzer/src/dart/analysis/byte_store.dart' show ByteStore; |
| 34 | +export 'package:analyzer/src/dart/analysis/experiments.dart' |
| 35 | + show ExperimentStatus; |
| 36 | +export 'package:analyzer/src/generated/source.dart' show UriResolver; |
| 37 | + |
| 38 | +/// A somewhat low level API to create [AnalysisSession]. |
| 39 | +/// |
| 40 | +/// Ideally we want clients to use [AnalysisContextCollection], which |
| 41 | +/// encapsulates any internals and is driven by `package_config.json` and |
| 42 | +/// `analysis_options.yaml` files. But so far it looks that `build_resolvers` |
| 43 | +/// wants to provide [UriResolver], and push [Packages] created by other means |
| 44 | +/// than parsing `package_config.json`. |
| 45 | +AnalysisDriverForPackageBuild createAnalysisDriver({ |
| 46 | + required ResourceProvider resourceProvider, |
| 47 | + required Uint8List sdkSummaryBytes, |
| 48 | + required AnalysisOptions analysisOptions, |
| 49 | + FileContentCache? fileContentCache, |
| 50 | + required List<UriResolver> uriResolvers, |
| 51 | + required Packages packages, |
| 52 | + ByteStore? byteStore, |
| 53 | +}) { |
| 54 | + final sdkBundle = PackageBundleReader(sdkSummaryBytes); |
| 55 | + final bundleSdk = SummaryBasedDartSdk.forBundle(sdkBundle); |
| 56 | + |
| 57 | + final runningDarkSdkPath = p.dirname(p.dirname(Platform.resolvedExecutable)); |
| 58 | + final sdk = FolderBasedDartSdk( |
| 59 | + resourceProvider, |
| 60 | + resourceProvider.getFolder(runningDarkSdkPath), |
| 61 | + ); |
| 62 | + |
| 63 | + //final sourceFactory = SourceFactory([DartUriResolver(sdk), ...uriResolvers]); |
| 64 | + final sourceFactory = SourceFactory([DartUriResolver(sdk), ...uriResolvers]); |
| 65 | + |
| 66 | + //final dataStore = SummaryDataStore(); |
| 67 | + //dataStore.addBundle('', sdkBundle); |
| 68 | + |
| 69 | + final logger = PerformanceLog(null); |
| 70 | + byteStore ??= MemoryByteStore(); |
| 71 | + |
| 72 | + final scheduler = AnalysisDriverScheduler(logger); |
| 73 | + scheduler.events.drain<void>().ignore(); |
| 74 | + |
| 75 | + final sharedOptions = analysisOptions as AnalysisOptionsImpl; |
| 76 | + final optionsMap = AnalysisOptionsMap.forSharedOptions(sharedOptions); |
| 77 | + final driver = AnalysisDriver( |
| 78 | + scheduler: scheduler, |
| 79 | + logger: logger, |
| 80 | + resourceProvider: resourceProvider, |
| 81 | + byteStore: byteStore, |
| 82 | + sourceFactory: sourceFactory, |
| 83 | + analysisOptionsMap: optionsMap, |
| 84 | + fileContentCache: fileContentCache, |
| 85 | + // externalSummaries: dataStore, |
| 86 | + packages: packages, |
| 87 | + withFineDependencies: true, |
| 88 | + shouldReportInconsistentAnalysisException: false, |
| 89 | + ); |
| 90 | + |
| 91 | + scheduler.start(); |
| 92 | + |
| 93 | + return AnalysisDriverForPackageBuild._(bundleSdk.libraryUris, driver); |
| 94 | +} |
| 95 | + |
| 96 | +/// [AnalysisSession] plus a tiny bit more. |
| 97 | +class AnalysisDriverForPackageBuild { |
| 98 | + final List<Uri> _sdkLibraryUris; |
| 99 | + final AnalysisDriver _driver; |
| 100 | + |
| 101 | + AnalysisDriverForPackageBuild._(this._sdkLibraryUris, this._driver); |
| 102 | + |
| 103 | + AnalysisSession get currentSession { |
| 104 | + return _driver.currentSession; |
| 105 | + } |
| 106 | + |
| 107 | + /// Returns URIs of libraries in the given SDK. |
| 108 | + List<Uri> get sdkLibraryUris { |
| 109 | + return _sdkLibraryUris; |
| 110 | + } |
| 111 | + |
| 112 | + /// Return a [Future] that completes after pending file changes are applied, |
| 113 | + /// so that [currentSession] can be used to compute results. |
| 114 | + Future<void> applyPendingFileChanges() { |
| 115 | + return _driver.applyPendingFileChanges(); |
| 116 | + } |
| 117 | + |
| 118 | + /// The file with the given [path] might have changed - updated, added or |
| 119 | + /// removed. Or not, we don't know. Or it might have, but then changed back. |
| 120 | + /// |
| 121 | + /// The [path] must be absolute and normalized. |
| 122 | + /// |
| 123 | + /// The [currentSession] most probably will be invalidated. |
| 124 | + /// Note, is does NOT at the time of writing this comment. |
| 125 | + /// But we are going to fix this. |
| 126 | + void changeFile(String path) { |
| 127 | + _driver.changeFile(path); |
| 128 | + } |
| 129 | + |
| 130 | + /// Return `true` if the [uri] can be resolved to an existing file. |
| 131 | + bool isUriOfExistingFile(Uri uri) { |
| 132 | + final source = _driver.sourceFactory.forUri2(uri); |
| 133 | + return source != null && source.exists(); |
| 134 | + } |
| 135 | +} |
0 commit comments