Files
sdk/pkg/analyzer_testing/lib/package_config_file_builder.dart
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

108 lines
3.0 KiB
Dart

// Copyright (c) 2026, 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/file_system/file_system.dart';
/// 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.
class PackageConfigFileBuilder {
final List<_PackageDescription> _packages = [];
/// Adds a package configuration.
///
/// The [rootFolder] is used to produce the package root URI.
///
/// The [packageUriStr] is optional (defaults to `'lib/'`), a relative path
/// resolved against the root URI. The result must be inside the root URI.
///
/// 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 Folder rootFolder,
String packageUriStr = 'lib/',
String? languageVersion,
}) {
if (_packages.any((e) => e.name == name)) {
throw StateError('Already added: $name');
}
_packages.add(
_PackageDescription(
name: name,
rootFolder: rootFolder,
packageUriStr: packageUriStr,
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() {
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}",');
buffer.write('$prefix"rootUri": "${package.rootFolder.toUri()}"');
buffer.writeln(',');
buffer.write('$prefix"packageUri": "${package.packageUriStr}"');
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 Folder rootFolder;
final String packageUriStr;
final String? languageVersion;
_PackageDescription({
required this.name,
required this.rootFolder,
required this.packageUriStr,
required this.languageVersion,
});
}