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 all 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
Loading
Loading