Skip to content

Commit 794d92c

Browse files
committed
Add early validation for package build and installation requirements
1 parent 97e337c commit 794d92c

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/StaticPHP/Package/PackageInstaller.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use StaticPHP\DI\ApplicationContext;
1313
use StaticPHP\Exception\WrongUsageException;
1414
use StaticPHP\Registry\PackageLoader;
15+
use StaticPHP\Runtime\SystemTarget;
1516
use StaticPHP\Util\DependencyResolver;
1617
use StaticPHP\Util\FileSystem;
1718
use StaticPHP\Util\InteractiveTerm;
@@ -133,6 +134,9 @@ public function run(bool $interactive = true, bool $disable_delay_msg = false):
133134
echo PHP_EOL;
134135
}
135136

137+
// Early validation: check if packages can be built or installed before downloading
138+
$this->validatePackagesBeforeBuild();
139+
136140
// check download
137141
if ($this->download) {
138142
$downloaderOptions = DownloaderOptions::extractFromConsoleOptions($this->options, 'dl');
@@ -199,8 +203,6 @@ public function run(bool $interactive = true, bool $disable_delay_msg = false):
199203
if ($interactive) {
200204
InteractiveTerm::finish('Built package: ' . ConsoleColor::green($package->getName()) . ($status === SPC_STATUS_ALREADY_BUILT ? ' (already built, skipped)' : ''));
201205
}
202-
} elseif ($package->getType() === 'library') {
203-
throw new WrongUsageException("Package '{$package->getName()}' cannot be installed: no build stage defined and no binary artifact available for current OS.");
204206
}
205207
}
206208
}
@@ -535,6 +537,23 @@ private function printArrayInfo(array $info): void
535537
}
536538
}
537539

540+
private function validatePackagesBeforeBuild(): void
541+
{
542+
foreach ($this->packages as $package) {
543+
if ($package->getType() !== 'library') {
544+
continue;
545+
}
546+
$is_to_build = $this->isBuildPackage($package);
547+
$has_build_stage = $package instanceof LibraryPackage && $package->hasStage('build');
548+
$should_use_binary = $package instanceof LibraryPackage && ($package->getArtifact()?->shouldUseBinary() ?? false);
549+
550+
// Check if package can neither be built nor installed
551+
if (!$is_to_build && !$should_use_binary && !$has_build_stage) {
552+
throw new WrongUsageException("Package '{$package->getName()}' cannot be installed: no build stage defined and no binary artifact available for current OS: " . SystemTarget::getCurrentPlatformString());
553+
}
554+
}
555+
}
556+
538557
private function performAfterInstallActions(Package $package): void
539558
{
540559
// ----------- perform post-install actions from extracted .package.{pkg_name}.postinstall.json -----------

0 commit comments

Comments
 (0)