|
| 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 | +import 'dart:typed_data'; |
| 6 | + |
| 7 | +import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; |
| 8 | +import 'package:analyzer/dart/analysis/session.dart'; |
| 9 | +import 'package:analyzer/file_system/file_system.dart'; |
| 10 | +import 'package:analyzer/src/context/packages.dart'; |
| 11 | +import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 12 | +import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 13 | +import 'package:analyzer/src/dart/analysis/performance_logger.dart'; |
| 14 | +import 'package:analyzer/src/generated/engine.dart'; |
| 15 | +import 'package:analyzer/src/generated/source.dart'; |
| 16 | +import 'package:analyzer/src/summary/package_bundle_reader.dart'; |
| 17 | +import 'package:analyzer/src/summary/summary_sdk.dart'; |
| 18 | +import 'package:analyzer/src/summary2/package_bundle_format.dart'; |
| 19 | + |
| 20 | +export 'package:analyzer/src/context/packages.dart' show Packages, Package; |
| 21 | +export 'package:analyzer/src/dart/analysis/experiments.dart' |
| 22 | + show ExperimentStatus; |
| 23 | +export 'package:analyzer/src/generated/engine.dart' |
| 24 | + show AnalysisOptions, AnalysisOptionsImpl; |
| 25 | +export 'package:analyzer/src/generated/source.dart' show Source, UriResolver; |
| 26 | + |
| 27 | +/// A somewhat low level API to create [AnalysisSession]. |
| 28 | +/// |
| 29 | +/// Ideally we want clients to use [AnalysisContextCollection], which |
| 30 | +/// encapsulates any internals and is driven by `package_config.json` and |
| 31 | +/// `analysis_options.yaml` files. But so far it looks that `build_resolvers` |
| 32 | +/// wants to provide [UriResolver], and push [Packages] created by other means |
| 33 | +/// than parsing `package_config.json`. |
| 34 | +AnalysisDriverForPackageBuild createAnalysisDriver({ |
| 35 | + required ResourceProvider resourceProvider, |
| 36 | + required Uint8List sdkSummaryBytes, |
| 37 | + required AnalysisOptions analysisOptions, |
| 38 | + required List<UriResolver> uriResolvers, |
| 39 | + required Packages packages, |
| 40 | +}) { |
| 41 | + var sdkBundle = PackageBundleReader(sdkSummaryBytes); |
| 42 | + var sdk = SummaryBasedDartSdk.forBundle(sdkBundle); |
| 43 | + |
| 44 | + var sourceFactory = SourceFactory([ |
| 45 | + DartUriResolver(sdk), |
| 46 | + ...uriResolvers, |
| 47 | + ]); |
| 48 | + |
| 49 | + var dataStore = SummaryDataStore([]); |
| 50 | + dataStore.addBundle('', sdkBundle); |
| 51 | + |
| 52 | + var logger = PerformanceLog(null); |
| 53 | + var scheduler = AnalysisDriverScheduler(logger); |
| 54 | + var driver = AnalysisDriver.tmp1( |
| 55 | + scheduler: scheduler, |
| 56 | + logger: logger, |
| 57 | + resourceProvider: resourceProvider, |
| 58 | + byteStore: MemoryByteStore(), |
| 59 | + sourceFactory: sourceFactory, |
| 60 | + analysisOptions: analysisOptions as AnalysisOptionsImpl, |
| 61 | + externalSummaries: dataStore, |
| 62 | + packages: packages, |
| 63 | + ); |
| 64 | + |
| 65 | + scheduler.start(); |
| 66 | + |
| 67 | + return AnalysisDriverForPackageBuild._(driver); |
| 68 | +} |
| 69 | + |
| 70 | +/// [AnalysisSession] plus a tiny bit more. |
| 71 | +class AnalysisDriverForPackageBuild { |
| 72 | + final AnalysisDriver _driver; |
| 73 | + |
| 74 | + AnalysisDriverForPackageBuild._(this._driver); |
| 75 | + |
| 76 | + AnalysisSession get currentSession { |
| 77 | + return _driver.currentSession; |
| 78 | + } |
| 79 | + |
| 80 | + /// The file with the given [path] might have changed - updated, added or |
| 81 | + /// removed. Or not, we don't know. Or it might have, but then changed back. |
| 82 | + /// |
| 83 | + /// The [path] must be absolute and normalized. |
| 84 | + /// |
| 85 | + /// The [currentSession] most probably will be invalidated. |
| 86 | + /// Note, is does NOT at the time of writing this comment. |
| 87 | + /// But we are going to fix this. |
| 88 | + void changeFile(String path) { |
| 89 | + _driver.changeFile(path); |
| 90 | + } |
| 91 | + |
| 92 | + /// Return `true` if the [uri] can be resolved to an existing file. |
| 93 | + bool isUriOfExistingFile(Uri uri) { |
| 94 | + var source = _driver.sourceFactory.forUri2(uri); |
| 95 | + return source != null && source.exists(); |
| 96 | + } |
| 97 | +} |
0 commit comments