Skip to content

Commit

Permalink
findPackageConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm committed Sep 27, 2024
1 parent 3de2d4e commit b311c55
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 57 deletions.
2 changes: 2 additions & 0 deletions pkgs/extension_discovery/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Require Dart 3.4.
- Update to the latest version of `package:dart_flutter_team_lints`.
- Add `findPackageConfig()` allowing for automatic discovery of the
packageConfig based on a package directory.

## 2.0.0

Expand Down
2 changes: 1 addition & 1 deletion pkgs/extension_discovery/lib/extension_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'src/package_config.dart';
import 'src/registry.dart';
import 'src/yaml_config_format.dart';

export 'src/package_config.dart' show PackageConfigException;
export 'src/package_config.dart' show PackageConfigException, findPackageConfig;

/// Information about an extension for target package.
final class Extension {
Expand Down
26 changes: 26 additions & 0 deletions pkgs/extension_discovery/lib/src/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ import 'expect_json.dart';
typedef PackageConfigEntry = ({String name, Uri rootUri, Uri packageUri});
typedef PackageConfig = List<PackageConfigEntry>;

/// Searches in [packageDir] and all directories above for a
/// `.dart_tool/package_config.json` file. Returns the Uri of that file if
/// found.
///
/// Returns `null` if no file was found.
Uri? findPackageConfig(Uri packageDir) {
if (!packageDir.isScheme('file')) {
throw ArgumentError(
'Expected [packageDir] to be a file URI, got $packageDir',
);
}
if (!packageDir.path.endsWith('/')) {
packageDir = packageDir.replace(path: '${packageDir.path}/');
}
while (true) {
final packageConfigCandidate =
packageDir.resolve('.dart_tool/package_config.json');
if (File.fromUri(packageConfigCandidate).existsSync()) {
return packageConfigCandidate;
}
final next = packageDir.resolve('..');
if (next == packageDir) return null;
packageDir = next;
}
}

/// Load list of packages and associated URIs from the
/// `.dart_tool/package_config.json` file in [packageConfigFile].
Future<PackageConfig> loadPackageConfig(
Expand Down
56 changes: 0 additions & 56 deletions pkgs/extension_discovery/test/find_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,60 +347,4 @@ writtenAsYaml: true
);
}
});

test('finds extensions in a workspace with `packageDir`', () async {
final pkgLibDir = await Isolate.resolvePackageUri(
Uri.parse('package:extension_discovery/'),
);
final pkgDir = pkgLibDir!.resolve('..');

await d.dir('workspace', [
d.pubspec({
'name': '_',
'environment': {'sdk': '^3.5.0'},
'workspace': ['myapp/'],
}),
d.dir('myapp', [
d.pubspec({
'name': 'myapp',
'dependencies': {
'extension_discovery': {'path': pkgDir.toFilePath()},
'foo': {'path': '../../foo'},
},
'environment': {'sdk': '^3.5.0'},
'resolution': 'workspace'
})
])
]).create();

await d.dir('foo', [
d.pubspec({
'name': 'foo',
'environment': {'sdk': '^3.0.0'},
}),
// It has a config.yaml for myapp
d.dir('extension/myapp', [
d.json('config.yaml', {'fromFoo': true}),
]),
]).create();

// Get dependencies
await d.dartPubGet(d.path('workspace'));
final [extension] = await findExtensions(
'myapp',
packageRootDir: d.fileUri('workspace/myapp'),
);
expect(extension.config, {'fromFoo': true});
expect(extension.package, 'foo');
expect(extension.rootUri, d.fileUri('foo/'));
expect(extension.packageUri, Uri.parse('lib/'));

// Check that there is a myapp.json cache file
await d
.file(
'workspace/.dart_tool/extension_discovery/myapp.json',
isNotEmpty,
)
.validate();
});
}
31 changes: 31 additions & 0 deletions pkgs/extension_discovery/test/package_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert' show jsonDecode, jsonEncode;
import 'dart:math';

import 'package:extension_discovery/src/package_config.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -159,4 +160,34 @@ void main() {
throwsA(isA<PackageConfigException>()),
);
});

test('`findPackageConfig()`', () async {
await d.dir('workspace', [
d.dir('.dart_tool', [
d.file('package_config.json'),
]),
d.dir('myapp', [])
]).create();

expect(
findPackageConfig(d.fileUri('workspace')),
d.fileUri('workspace/.dart_tool/package_config.json'),
);
expect(
findPackageConfig(d.fileUri('workspace/')),
d.fileUri('workspace/.dart_tool/package_config.json'),
);
expect(
findPackageConfig(d.fileUri('workspace/myapp')),
d.fileUri('workspace/.dart_tool/package_config.json'),
);
expect(
findPackageConfig(d.fileUri('.')),
isNull,
);
expect(
findPackageConfig(d.fileUri('foo')),
isNull,
);
});
}

0 comments on commit b311c55

Please sign in to comment.