Skip to content

Commit

Permalink
Support optional packageUri (#148)
Browse files Browse the repository at this point in the history
* Support optional packageUri

* Fix changelog and pubspec.yaml version
  • Loading branch information
jonasfj authored Aug 22, 2023
1 parent 1c46390 commit e5ce42a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
5 changes: 2 additions & 3 deletions pkgs/extension_discovery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## 1.0.2-wip
- Improve error messaging for `package_config.json` parsing.

## 1.0.1-wip

- Support optional `packageUri`.
- Improve error messaging for `package_config.json` parsing.
- Update the package description.

## 1.0.0
Expand Down
7 changes: 7 additions & 0 deletions pkgs/extension_discovery/lib/src/expect_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ extension ExpectJson on Map<String, Object?> {
}
}

Uri? optionalUri(String key) {
if (containsKey(key)) {
return expectUri(key);
}
return null;
}

List<Object?> expectList(String key) {
if (this[key] case List<Object?> v) return v;
throw FormatException('The value at "$key" must be a list:\n$toString()');
Expand Down
16 changes: 8 additions & 8 deletions pkgs/extension_discovery/lib/src/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ Future<PackageConfig> loadPackageConfig(
if (packageConfig.expectNumber('configVersion') != 2) {
throw FormatException('"configVersion" must be 2');
}
return packageConfig
.expectListObjects('packages')
.map((p) => (
name: p.expectString('name'),
rootUri: p.expectUri('rootUri').asDirectory(),
packageUri: p.expectUri('packageUri').asDirectory(),
))
.toList();
return packageConfig.expectListObjects('packages').map((p) {
final rootUri = p.expectUri('rootUri').asDirectory();
return (
name: p.expectString('name'),
rootUri: rootUri,
packageUri: p.optionalUri('packageUri')?.asDirectory() ?? rootUri,
);
}).toList();
} on IOException catch (e) {
if (!packageConfigFile.existsSync()) {
throw packageConfigNotFound(packageConfigFile.uri);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/extension_discovery/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: extension_discovery
description: >-
A convention and utilities for package extension discovery.
version: 1.0.2-wip
version: 1.0.1-wip
repository: https://github.com/dart-lang/tools/tree/main/pkgs/extension_discovery

dev_dependencies:
Expand Down
50 changes: 47 additions & 3 deletions pkgs/extension_discovery/test/package_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,53 @@ void main() {

// ################################## Cannot read version 99

final pkgcfg = jsonDecode(packageConfigFile.readAsStringSync()) as Map;
pkgcfg['configVersion'] = 99;
packageConfigFile.writeAsStringSync(jsonEncode(pkgcfg));
{
final pkgcfg = jsonDecode(packageConfigFile.readAsStringSync()) as Map;
pkgcfg['configVersion'] = 99;
packageConfigFile.writeAsStringSync(jsonEncode(pkgcfg));
}

// Loading before `dart pub get` throws PackageConfigException
expect(
loadPackageConfig(packageConfigFile),
throwsA(isA<PackageConfigException>()),
);

// ################################## packageUri is optional

// Get dependencies
await d.dartPubGet(d.path('myapp'));

{
final pkgcfg = jsonDecode(packageConfigFile.readAsStringSync()) as Map;
for (final p in pkgcfg['packages'] as List) {
(p as Map).remove('packageUri');
}
packageConfigFile.writeAsStringSync(jsonEncode(pkgcfg));
}

// Parse package_config
{
final packages = await loadPackageConfig(packageConfigFile);
expect(packages, isNotEmpty);
expect(packages.any((p) => p.name == 'myapp'), isTrue);
expect(packages.any((p) => p.name == 'foo'), isTrue);
expect(packages.any((p) => p.name == 'bar'), isTrue);
expect(packages.every((p) => p.rootUri == p.packageUri), isTrue);
}

// ################################## packageUri is optional

// Get dependencies
await d.dartPubGet(d.path('myapp'));

{
final pkgcfg = jsonDecode(packageConfigFile.readAsStringSync()) as Map;
for (final p in pkgcfg['packages'] as List) {
(p as Map).remove('rootUri');
}
packageConfigFile.writeAsStringSync(jsonEncode(pkgcfg));
}

// Loading before `dart pub get` throws PackageConfigException
expect(
Expand Down

0 comments on commit e5ce42a

Please sign in to comment.