diff --git a/src/SelfManage/BuildTools/CheckAllBuildTools.php b/src/SelfManage/BuildTools/CheckAllBuildTools.php index edcee5a..f628b26 100644 --- a/src/SelfManage/BuildTools/CheckAllBuildTools.php +++ b/src/SelfManage/BuildTools/CheckAllBuildTools.php @@ -7,6 +7,8 @@ namespace Php\Pie\SelfManage\BuildTools; use Composer\IO\IOInterface; use function array_unique; +use function array_values; +use function count; use function implode; /** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ @@ -76,9 +78,10 @@ class CheckAllBuildTools public function check(IOInterface $io, bool $forceInstall): void { $io->write('Checking if all build tools are installed.', verbosity: IOInterface::VERBOSE); - $packageManager = PackageManager::detect(); - $missingTools = []; + /** @var list $packagesToInstall */ $packagesToInstall = []; + $missingTools = []; + $packageManager = PackageManager::detect(); $allFound = true; foreach ($this->buildTools as $buildTool) { @@ -89,10 +92,15 @@ class CheckAllBuildTools $allFound = false; $missingTools[] = $buildTool->tool; - $packageName = $buildTool->packageNameFor($packageManager); + + if ($packageManager === null) { + continue; + } + + $packageName = $buildTool->packageNameFor($packageManager); if ($packageName === null) { - $io->writeError('Could not find package name for build tool ' . $buildTool->tool . '.'); + $io->writeError('Could not find package name for build tool ' . $buildTool->tool . '.', verbosity: IOInterface::VERBOSE); continue; } @@ -107,15 +115,27 @@ class CheckAllBuildTools $io->write('The following build tools are missing: ' . implode(', ', $missingTools) . ''); - if (! $io->isInteractive() && ! $forceInstall) { - $io->writeError('You are not running in interactive mode, and --force was not specified. You may need to install the following build tools: ' . implode(' ', $packagesToInstall) . ''); + if ($packageManager === null) { + $io->write('Could not find a package manager to install the missing build tools.'); return; } - $packagesToInstall = array_unique($packagesToInstall); + if (! count($packagesToInstall)) { + $io->write('Could not determine packages to install.'); - $io->write('The following command will be run: ' . implode(' ', $packageManager->installCommand($packagesToInstall)), verbosity: IOInterface::VERBOSE); + return; + } + + $proposedInstallCommand = implode(' ', $packageManager->installCommand(array_values(array_unique($packagesToInstall)))); + + if (! $io->isInteractive() && ! $forceInstall) { + $io->writeError('You are not running in interactive mode. You may need to run: ' . $proposedInstallCommand . ''); + + return; + } + + $io->write('The following command will be run: ' . $proposedInstallCommand, verbosity: IOInterface::VERY_VERBOSE); if ($io->isInteractive() && ! $forceInstall) { if (! $io->askConfirmation('Would you like to install them now?', false)) { @@ -125,7 +145,7 @@ class CheckAllBuildTools } } - $packageManager->install($packagesToInstall); - $io->write('Build tools installed.'); + $packageManager->install(array_values(array_unique($packagesToInstall))); + $io->write('Missing build tools have been installed.'); } }