Files
sdk/pkg/analyzer/lib/utilities/package_config_file_builder.dart
T
Konstantin Shcheglov e20704c3b0 CQ. Move PackageConfigFileBuilder to analyzer_testing.
Move PackageConfigFileBuilder into the analyzer_testing public API and
deprecate the copy exposed from package:analyzer. The builder is only
used by test infrastructure, so keeping it in analyzer_testing makes the
ownership clearer and avoids exposing test-only utilities from analyzer.

Update the builder API to accept a rootFolder instead of a rootPath.
This lets callers pass the resource-provider folder directly, so the
generated rootUri is derived from the same file-system abstraction that
created the test files. This avoids accidentally passing POSIX paths
where resource provider paths are required, such as on Windows.

Update existing test utilities and callers to import the new library and
pass Folder objects. Remove the production analysis server dependency on
the builder by emitting the temporary plugin package config JSON
directly.

Change-Id: I46b14710626e0d6d5884afcdc5a05b23077acfc9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/499081
Reviewed-by: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2026-04-29 13:18:35 -07:00

113 lines
3.3 KiB
Dart

// Copyright (c) 2020, 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.
@Deprecated('Use package:analyzer_testing/package_config_file_builder.dart.')
library;
import 'package:path/path.dart' as path;
/// Helper for building `.dart_tool/package_config.json` files.
///
/// See accepted/future-releases/language-versioning/package-config-file-v2.md
/// in https://github.com/dart-lang/language/.
///
/// Use the [add] method to add package configurations. These configurations
/// will accumulate into one package config file with the [toContent] method.
@Deprecated('Use PackageConfigFileBuilder from analyzer_testing')
class PackageConfigFileBuilder {
final List<_PackageDescription> _packages = [];
/// The [rootPath] will be given to `toUriStr` of [toContent] to produce
/// the corresponding `file://` URI, normally a POSIX path.
///
/// The [packageUri] is optional (defaults to `'lib/'`), a relative path
/// resolved against the file URI of the [rootPath]. The result must be inside
/// the [rootPath].
///
/// The [languageVersion] specifies the package's Dart language version, in
/// the form of 'X.Y', such as '3.9'.
void add({
required String name,
required String rootPath,
String packageUri = 'lib/',
String? languageVersion,
}) {
if (_packages.any((e) => e.name == name)) {
throw StateError('Already added: $name');
}
_packages.add(
_PackageDescription(
name: name,
rootPath: rootPath,
packageUri: packageUri,
languageVersion: languageVersion,
),
);
}
/// Copies this [PackageConfigFileBuilder] into a new instance.
PackageConfigFileBuilder copy() {
var copy = PackageConfigFileBuilder();
copy._packages.addAll(_packages);
return copy;
}
/// Returns the contents of the built package config file.
String toContent({required path.Context pathContext}) {
var buffer = StringBuffer();
buffer.writeln('{');
var prefix = ' ' * 2;
buffer.writeln('$prefix"configVersion": 2,');
buffer.writeln('$prefix"packages": [');
for (var i = 0; i < _packages.length; i++) {
var package = _packages[i];
var prefix = ' ' * 4;
buffer.writeln('$prefix{');
prefix = ' ' * 6;
buffer.writeln('$prefix"name": "${package.name}",');
var rootUri = pathContext.toUri(package.rootPath).toString();
buffer.write('$prefix"rootUri": "$rootUri"');
buffer.writeln(',');
buffer.write('$prefix"packageUri": "${package.packageUri}"');
if (package.languageVersion != null) {
buffer.writeln(',');
buffer.write('$prefix"languageVersion": "${package.languageVersion}"');
}
buffer.writeln();
prefix = ' ' * 4;
buffer.write(prefix);
buffer.writeln(i < _packages.length - 1 ? '},' : '}');
}
buffer.writeln(' ]');
buffer.writeln('}');
return buffer.toString();
}
}
class _PackageDescription {
final String name;
final String rootPath;
final String packageUri;
final String? languageVersion;
_PackageDescription({
required this.name,
required this.rootPath,
required this.packageUri,
required this.languageVersion,
});
}