Skip to content

Commit

Permalink
[tool] Support third_party for --current-package (#7967)
Browse files Browse the repository at this point in the history
Fixes `--current-package` so that when run on a package in third_party/packages/ in works as expected, rather than failing with an error message saying that it must be run from inside a package.
  • Loading branch information
stuartmorgan authored Oct 30, 2024
1 parent 030dd4e commit cd7a810
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
22 changes: 15 additions & 7 deletions script/tool/lib/src/common/package_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -662,22 +662,30 @@ abstract class PackageCommand extends Command<void> {
}

String? _getCurrentDirectoryPackageName() {
// Ensure that the current directory is within the packages directory.
final Directory absolutePackagesDir = packagesDir.absolute;
final Set<Directory> absolutePackagesDirs = <Directory>{
packagesDir.absolute,
thirdPartyPackagesDir.absolute,
};
bool isATopLevelPackagesDir(Directory directory) =>
absolutePackagesDirs.any((Directory d) => d.path == directory.path);

Directory currentDir = packagesDir.fileSystem.currentDirectory.absolute;
if (!currentDir.path.startsWith(absolutePackagesDir.path) ||
currentDir.path == packagesDir.path) {
// Ensure that the current directory is within one of the top-level packages
// directories.
if (isATopLevelPackagesDir(currentDir) ||
!absolutePackagesDirs
.any((Directory d) => currentDir.path.startsWith(d.path))) {
return null;
}
// If the current directory is a direct subdirectory of the packages
// If the current directory is a direct subdirectory of a packages
// directory, then that's the target.
if (currentDir.parent.path == absolutePackagesDir.path) {
if (isATopLevelPackagesDir(currentDir.parent)) {
return currentDir.basename;
}
// Otherwise, walk up until a package is found...
while (!isPackage(currentDir)) {
currentDir = currentDir.parent;
if (currentDir.path == absolutePackagesDir.path) {
if (isATopLevelPackagesDir(currentDir)) {
return null;
}
}
Expand Down
13 changes: 13 additions & 0 deletions script/tool/test/common/package_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,19 @@ packages/plugin1/plugin1/plugin1.dart
expect(command.plugins, unorderedEquals(<String>[package.path]));
});

test('runs on a package when run from the third_party/packages directory',
() async {
final RepositoryPackage package =
createFakePlugin('a_package', thirdPartyPackagesDir);
createFakePlugin('another_package', thirdPartyPackagesDir);
fileSystem.currentDirectory = package.directory;

await runCapturingPrint(
runner, <String>['sample', '--current-package']);

expect(command.plugins, unorderedEquals(<String>[package.path]));
});

test('runs only app-facing package of a federated plugin', () async {
const String pluginName = 'foo';
final Directory groupDir = packagesDir.childDirectory(pluginName);
Expand Down

0 comments on commit cd7a810

Please sign in to comment.