Skip to content

[package_config] Update language version and reformat #2040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/package_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
sdk: [3.4, dev]
sdk: [3.7, dev]
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
Expand Down
73 changes: 48 additions & 25 deletions pkgs/package_config/lib/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export 'package_config_types.dart';
/// The returned package configuration is a *best effort* attempt to create
/// a valid configuration from the invalid configuration file.
/// If no [onError] is provided, errors are thrown immediately.
Future<PackageConfig> loadPackageConfig(File file,
{bool preferNewest = true, void Function(Object error)? onError}) =>
readAnyConfigFile(file, preferNewest, onError ?? throwError);
Future<PackageConfig> loadPackageConfig(
File file, {
bool preferNewest = true,
void Function(Object error)? onError,
}) => readAnyConfigFile(file, preferNewest, onError ?? throwError);

/// Reads a specific package configuration URI.
///
Expand Down Expand Up @@ -86,11 +88,12 @@ Future<PackageConfig> loadPackageConfig(File file,
/// The returned package configuration is a *best effort* attempt to create
/// a valid configuration from the invalid configuration file.
/// If no [onError] is provided, errors are thrown immediately.
Future<PackageConfig> loadPackageConfigUri(Uri file,
{Future<Uint8List?> Function(Uri uri)? loader,
bool preferNewest = true,
void Function(Object error)? onError}) =>
readAnyConfigFileUri(file, loader, onError ?? throwError, preferNewest);
Future<PackageConfig> loadPackageConfigUri(
Uri file, {
Future<Uint8List?> Function(Uri uri)? loader,
bool preferNewest = true,
void Function(Object error)? onError,
}) => readAnyConfigFileUri(file, loader, onError ?? throwError, preferNewest);

/// Finds a package configuration relative to [directory].
///
Expand All @@ -115,16 +118,25 @@ Future<PackageConfig> loadPackageConfigUri(Uri file,
/// any lower-version configuration files are ignored in the search.
///
/// Returns `null` if no configuration file is found.
Future<PackageConfig?> findPackageConfig(Directory directory,
{bool recurse = true,
void Function(Object error)? onError,
int minVersion = 1}) {
Future<PackageConfig?> findPackageConfig(
Directory directory, {
bool recurse = true,
void Function(Object error)? onError,
int minVersion = 1,
}) {
if (minVersion > PackageConfig.maxVersion) {
throw ArgumentError.value(minVersion, 'minVersion',
'Maximum known version is ${PackageConfig.maxVersion}');
throw ArgumentError.value(
minVersion,
'minVersion',
'Maximum known version is ${PackageConfig.maxVersion}',
);
}
return discover.findPackageConfig(
directory, minVersion, recurse, onError ?? throwError);
directory,
minVersion,
recurse,
onError ?? throwError,
);
}

/// Finds a package configuration relative to [location].
Expand Down Expand Up @@ -170,17 +182,27 @@ Future<PackageConfig?> findPackageConfig(Directory directory,
/// any lower-version configuration files are ignored in the search.
///
/// Returns `null` if no configuration file is found.
Future<PackageConfig?> findPackageConfigUri(Uri location,
{bool recurse = true,
int minVersion = 1,
Future<Uint8List?> Function(Uri uri)? loader,
void Function(Object error)? onError}) {
Future<PackageConfig?> findPackageConfigUri(
Uri location, {
bool recurse = true,
int minVersion = 1,
Future<Uint8List?> Function(Uri uri)? loader,
void Function(Object error)? onError,
}) {
if (minVersion > PackageConfig.maxVersion) {
throw ArgumentError.value(minVersion, 'minVersion',
'Maximum known version is ${PackageConfig.maxVersion}');
throw ArgumentError.value(
minVersion,
'minVersion',
'Maximum known version is ${PackageConfig.maxVersion}',
);
}
return discover.findPackageConfigUri(
location, minVersion, loader, onError ?? throwError, recurse);
location,
minVersion,
loader,
onError ?? throwError,
recurse,
);
}

/// Writes a package configuration to the provided directory.
Expand All @@ -195,5 +217,6 @@ Future<PackageConfig?> findPackageConfigUri(Uri location,
/// A comment is generated if `[PackageConfig.extraData]` contains a
/// `"generator"` entry.
Future<void> savePackageConfig(
PackageConfig configuration, Directory directory) =>
writePackageConfigJsonFile(configuration, directory);
PackageConfig configuration,
Directory directory,
) => writePackageConfigJsonFile(configuration, directory);
56 changes: 37 additions & 19 deletions pkgs/package_config/lib/src/discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ final Uri parentPath = Uri(path: '..');
/// If [minVersion] is greater than 1, `.packages` files are ignored.
/// If [minVersion] is greater than the version read from the
/// `package_config.json` file, it too is ignored.
Future<PackageConfig?> findPackageConfig(Directory baseDirectory,
int minVersion, bool recursive, void Function(Object error) onError) async {
Future<PackageConfig?> findPackageConfig(
Directory baseDirectory,
int minVersion,
bool recursive,
void Function(Object error) onError,
) async {
var directory = baseDirectory;
if (!directory.isAbsolute) directory = directory.absolute;
if (!await directory.exists()) {
return null;
}
do {
// Check for $cwd/.packages
var packageConfig =
await findPackageConfigInDirectory(directory, minVersion, onError);
var packageConfig = await findPackageConfigInDirectory(
directory,
minVersion,
onError,
);
if (packageConfig != null) return packageConfig;
if (!recursive) break;
// Check in parent directories.
Expand All @@ -59,23 +66,30 @@ Future<PackageConfig?> findPackageConfig(Directory baseDirectory,

/// Similar to [findPackageConfig] but based on a URI.
Future<PackageConfig?> findPackageConfigUri(
Uri location,
int minVersion,
Future<Uint8List?> Function(Uri uri)? loader,
void Function(Object error) onError,
bool recursive) async {
Uri location,
int minVersion,
Future<Uint8List?> Function(Uri uri)? loader,
void Function(Object error) onError,
bool recursive,
) async {
if (location.isScheme('package')) {
onError(PackageConfigArgumentError(
location, 'location', 'Must not be a package: URI'));
onError(
PackageConfigArgumentError(
location,
'location',
'Must not be a package: URI',
),
);
return null;
}
if (loader == null) {
if (location.isScheme('file')) {
return findPackageConfig(
Directory.fromUri(location.resolveUri(currentPath)),
minVersion,
recursive,
onError);
Directory.fromUri(location.resolveUri(currentPath)),
minVersion,
recursive,
onError,
);
}
loader = defaultLoader;
}
Expand Down Expand Up @@ -116,8 +130,11 @@ Future<PackageConfig?> findPackageConfigUri(
/// If [minVersion] is greater than 1, `.packages` files are ignored.
/// If [minVersion] is greater than the version read from the
/// `package_config.json` file, it too is ignored.
Future<PackageConfig?> findPackageConfigInDirectory(Directory directory,
int minVersion, void Function(Object error) onError) async {
Future<PackageConfig?> findPackageConfigInDirectory(
Directory directory,
int minVersion,
void Function(Object error) onError,
) async {
var packageConfigFile = await checkForPackageConfigJsonFile(directory);
if (packageConfigFile != null) {
var config = await readPackageConfigJsonFile(packageConfigFile, onError);
Expand All @@ -135,8 +152,9 @@ Future<PackageConfig?> findPackageConfigInDirectory(Directory directory,

Future<File?> checkForPackageConfigJsonFile(Directory directory) async {
assert(directory.isAbsolute);
var file =
File(pathJoin(directory.path, '.dart_tool', 'package_config.json'));
var file = File(
pathJoin(directory.path, '.dart_tool', 'package_config.json'),
);
if (await file.exists()) return file;
return null;
}
Expand Down
17 changes: 11 additions & 6 deletions pkgs/package_config/lib/src/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ abstract class PackageConfigError {
class PackageConfigArgumentError extends ArgumentError
implements PackageConfigError {
PackageConfigArgumentError(
Object? super.value, String super.name, String super.message)
: super.value();
Object? super.value,
String super.name,
String super.message,
) : super.value();

PackageConfigArgumentError.from(ArgumentError error)
: super.value(error.invalidValue, error.name, error.message);
: super.value(error.invalidValue, error.name, error.message);
}

class PackageConfigFormatException extends FormatException
implements PackageConfigError {
PackageConfigFormatException(super.message, Object? super.source,
[super.offset]);
PackageConfigFormatException(
super.message,
Object? super.source, [
super.offset,
]);

PackageConfigFormatException.from(FormatException exception)
: super(exception.message, exception.source, exception.offset);
: super(exception.message, exception.source, exception.offset);
}

/// The default `onError` handler.
Expand Down
82 changes: 57 additions & 25 deletions pkgs/package_config/lib/src/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ abstract class PackageConfig {
/// despite the error. The input must still be valid JSON.
/// The result may be [PackageConfig.empty] if there is no way to
/// extract useful information from the bytes.
static PackageConfig parseBytes(Uint8List bytes, Uri baseUri,
{void Function(Object error)? onError}) =>
static PackageConfig parseBytes(
Uint8List bytes,
Uri baseUri, {
void Function(Object error)? onError,
}) =>
parsePackageConfigBytes(bytes, baseUri, onError ?? throwError);

/// Parses a package configuration file.
Expand All @@ -89,8 +92,11 @@ abstract class PackageConfig {
/// despite the error. The input must still be valid JSON.
/// The result may be [PackageConfig.empty] if there is no way to
/// extract useful information from the bytes.
static PackageConfig parseString(String configuration, Uri baseUri,
{void Function(Object error)? onError}) =>
static PackageConfig parseString(
String configuration,
Uri baseUri, {
void Function(Object error)? onError,
}) =>
parsePackageConfigString(configuration, baseUri, onError ?? throwError);

/// Parses the JSON data of a package configuration file.
Expand All @@ -110,34 +116,45 @@ abstract class PackageConfig {
/// despite the error. The input must still be valid JSON.
/// The result may be [PackageConfig.empty] if there is no way to
/// extract useful information from the bytes.
static PackageConfig parseJson(Object? jsonData, Uri baseUri,
{void Function(Object error)? onError}) =>
static PackageConfig parseJson(
Object? jsonData,
Uri baseUri, {
void Function(Object error)? onError,
}) =>
parsePackageConfigJson(jsonData, baseUri, onError ?? throwError);

/// Writes a configuration file for this configuration on [output].
///
/// If [baseUri] is provided, URI references in the generated file
/// will be made relative to [baseUri] where possible.
static void writeBytes(PackageConfig configuration, Sink<Uint8List> output,
[Uri? baseUri]) {
static void writeBytes(
PackageConfig configuration,
Sink<Uint8List> output, [
Uri? baseUri,
]) {
writePackageConfigJsonUtf8(configuration, baseUri, output);
}

/// Writes a configuration JSON text for this configuration on [output].
///
/// If [baseUri] is provided, URI references in the generated file
/// will be made relative to [baseUri] where possible.
static void writeString(PackageConfig configuration, StringSink output,
[Uri? baseUri]) {
static void writeString(
PackageConfig configuration,
StringSink output, [
Uri? baseUri,
]) {
writePackageConfigJsonString(configuration, baseUri, output);
}

/// Converts a configuration to a JSON-like data structure.
///
/// If [baseUri] is provided, URI references in the generated data
/// will be made relative to [baseUri] where possible.
static Map<String, Object?> toJson(PackageConfig configuration,
[Uri? baseUri]) =>
static Map<String, Object?> toJson(
PackageConfig configuration, [
Uri? baseUri,
]) =>
packageConfigToJson(configuration, baseUri);

/// The configuration version number.
Expand Down Expand Up @@ -231,13 +248,23 @@ abstract class Package {
///
/// If [extraData] is supplied, it will be available as the
/// [Package.extraData] of the created package.
factory Package(String name, Uri root,
{Uri? packageUriRoot,
LanguageVersion? languageVersion,
Object? extraData,
bool relativeRoot = true}) =>
SimplePackage.validate(name, root, packageUriRoot, languageVersion,
extraData, relativeRoot, throwError)!;
factory Package(
String name,
Uri root, {
Uri? packageUriRoot,
LanguageVersion? languageVersion,
Object? extraData,
bool relativeRoot = true,
}) =>
SimplePackage.validate(
name,
root,
packageUriRoot,
languageVersion,
extraData,
relativeRoot,
throwError,
)!;

/// The package-name of the package.
String get name;
Expand Down Expand Up @@ -330,8 +357,10 @@ abstract class LanguageVersion implements Comparable<LanguageVersion> {
/// If [onError] is not supplied, it defaults to throwing the exception.
/// If the call does not throw, then an [InvalidLanguageVersion] is returned
/// containing the original [source].
static LanguageVersion parse(String source,
{void Function(Object error)? onError}) =>
static LanguageVersion parse(
String source, {
void Function(Object error)? onError,
}) =>
parseLanguageVersion(source, onError ?? throwError);

/// The major language version.
Expand Down Expand Up @@ -486,10 +515,13 @@ extension LanguageVersionRelationalOperators on LanguageVersion {
}

static Never _throwThisInvalid() => throw StateError(
'Can\'t compare an invalid language version to another language version. '
'Verify language versions are valid after parsing.');
'Can\'t compare an invalid language version '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: switch to " here? I had slash escapes for quotes.

'to another language version. '
'Verify language versions are valid after parsing.',
);

static Never _throwOtherInvalid() => throw StateError(
'Can\'t compare a language version to an invalid language version. '
'Verify language versions are valid after parsing.');
'Can\'t compare a language version to an invalid language version. '
'Verify language versions are valid after parsing.',
);
}
Loading
Loading