-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: split
BuildAssetUriResolver
into AnalysisDriverModel
an…
…dd `AnalysisDriverModelUriResolver`. (#3813) * Refactor: split `BuildAssetUriResolver` into `AnalysisDriverModel` and `AnalysisDriverModelUriResolver`. `AnalysisDriverModel` is `build_runner`'s state related to build steps that use analysis, including an in-memory filesystem for use by the analyzer. `UriResolver` is the analyzer's view of that state. * Address review comments.
- Loading branch information
1 parent
4c76b74
commit 72b4f5b
Showing
6 changed files
with
170 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:analyzer/file_system/memory_file_system.dart'; | ||
// ignore: implementation_imports | ||
import 'package:analyzer/src/clients/build_resolvers/build_resolvers.dart'; | ||
import 'package:build/build.dart'; | ||
|
||
/// Manages analysis driver and related build state. | ||
/// | ||
/// - Tracks the import graph of all sources needed for analysis. | ||
/// - Given a set of entrypoints, adds them to known sources, optionally with | ||
/// their transitive imports. | ||
/// - Given a set of entrypoints, informs a `BuildStep` which inputs it now | ||
/// depends on because of analysis. | ||
/// - Maintains an in-memory filesystem that is the analyzer's view of the | ||
/// build. | ||
/// - Notifies the analyzer of changes to that in-memory filesystem. | ||
abstract class AnalysisDriverModel { | ||
/// In-memory filesystem for the analyzer. | ||
abstract final MemoryResourceProvider resourceProvider; | ||
|
||
/// Notifies that [step] has completed. | ||
/// | ||
/// All build steps must complete before [reset] is called. | ||
void notifyComplete(BuildStep step); | ||
|
||
/// Clear cached information specific to an individual build. | ||
void reset(); | ||
|
||
/// Attempts to parse [uri] into an [AssetId] and returns it if it is cached. | ||
/// | ||
/// Handles 'package:' or 'asset:' URIs, as well as 'file:' URIs of the form | ||
/// `/$packageName/$assetPath`. | ||
/// | ||
/// Returns null if the Uri cannot be parsed or is not cached. | ||
AssetId? lookupCachedAsset(Uri uri); | ||
|
||
/// Updates [resourceProvider] and the analysis driver given by | ||
/// `withDriverResource` with updated versions of [entryPoints]. | ||
/// | ||
/// If [transitive], then all the transitive imports from [entryPoints] are | ||
/// also updated. | ||
Future<void> performResolve( | ||
BuildStep buildStep, | ||
List<AssetId> entryPoints, | ||
Future<void> Function( | ||
FutureOr<void> Function(AnalysisDriverForPackageBuild)) | ||
withDriverResource, | ||
{required bool transitive}); | ||
} |
72 changes: 72 additions & 0 deletions
72
build_resolvers/lib/src/analysis_driver_model_uri_resolver.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analyzer/source/file_source.dart'; | ||
// ignore: implementation_imports | ||
import 'package:analyzer/src/clients/build_resolvers/build_resolvers.dart'; | ||
import 'package:build/build.dart'; | ||
import 'package:path/path.dart' as p; | ||
|
||
import 'analysis_driver_model.dart'; | ||
|
||
const _ignoredSchemes = ['dart', 'dart-ext']; | ||
|
||
/// A [UriResolver] on top of [AnalysisDriverModel]'s in-memory filesystem | ||
/// | ||
/// This is the analyzer's view of the current build: the files that | ||
/// `build_runner` wants the analyzer to analyze. | ||
/// | ||
/// The in-memory filesystem uses POSIX-style paths with `package:foo/bar' | ||
/// mapping to `/foo/bar`. | ||
class AnalysisDriverModelUriResolver implements UriResolver { | ||
final AnalysisDriverModel analysisDriverModel; | ||
AnalysisDriverModelUriResolver(this.analysisDriverModel); | ||
|
||
@override | ||
Source? resolveAbsolute(Uri uri, [Uri? actualUri]) { | ||
final assetId = parseAsset(uri); | ||
if (assetId == null) return null; | ||
|
||
var file = analysisDriverModel.resourceProvider.getFile(assetPath(assetId)); | ||
return FileSource(file, assetId.uri); | ||
} | ||
|
||
@override | ||
Uri pathToUri(String path) { | ||
var pathSegments = p.posix.split(path); | ||
var packageName = pathSegments[1]; | ||
if (pathSegments[2] == 'lib') { | ||
return Uri( | ||
scheme: 'package', | ||
pathSegments: [packageName].followedBy(pathSegments.skip(3)), | ||
); | ||
} else { | ||
return Uri( | ||
scheme: 'asset', | ||
pathSegments: [packageName].followedBy(pathSegments.skip(2)), | ||
); | ||
} | ||
} | ||
|
||
/// Attempts to parse [uri] into an [AssetId]. | ||
/// | ||
/// Handles 'package:' or 'asset:' URIs, as well as 'file:' URIs that have the | ||
/// same pattern used by [assetPath]. | ||
/// | ||
/// Returns null if the Uri cannot be parsed. | ||
static AssetId? parseAsset(Uri uri) { | ||
if (_ignoredSchemes.any(uri.isScheme)) return null; | ||
if (uri.isScheme('package') || uri.isScheme('asset')) { | ||
return AssetId.resolve(uri); | ||
} | ||
if (uri.isScheme('file')) { | ||
final parts = p.split(uri.path); | ||
return AssetId(parts[1], p.posix.joinAll(parts.skip(2))); | ||
} | ||
return null; | ||
} | ||
|
||
static String assetPath(AssetId assetId) => | ||
p.posix.join('/${assetId.package}', assetId.path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.