Skip to content

Commit 66d3af5

Browse files
authored
Add early validation for package build and installation requirements (#996)
2 parents 97e337c + 2901d32 commit 66d3af5

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

src/StaticPHP/DI/ApplicationContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function getContainer(): Container
8383
*
8484
* @param class-string<T> $id Service identifier
8585
*
86-
* @return T
86+
* @return null|T
8787
*/
8888
public static function get(string $id): mixed
8989
{

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 -----------

src/StaticPHP/Util/InteractiveTerm.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use StaticPHP\DI\ApplicationContext;
88
use Symfony\Component\Console\Helper\ProgressIndicator;
99
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\ConsoleOutput;
1011
use Symfony\Component\Console\Output\OutputInterface;
1112
use ZM\Logger\ConsoleColor;
1213

@@ -16,8 +17,8 @@ class InteractiveTerm
1617

1718
public static function notice(string $message, bool $indent = false): void
1819
{
19-
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
20-
$output = ApplicationContext::get(OutputInterface::class);
20+
$no_ansi = ApplicationContext::get(InputInterface::class)?->getOption('no-ansi') ?? false;
21+
$output = ApplicationContext::get(OutputInterface::class) ?? new ConsoleOutput();
2122
if ($output->isVerbose()) {
2223
logger()->notice(strip_ansi_colors($message));
2324
} else {
@@ -27,8 +28,8 @@ public static function notice(string $message, bool $indent = false): void
2728

2829
public static function success(string $message, bool $indent = false): void
2930
{
30-
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
31-
$output = ApplicationContext::get(OutputInterface::class);
31+
$no_ansi = ApplicationContext::get(InputInterface::class)?->getOption('no-ansi') ?? false;
32+
$output = ApplicationContext::get(OutputInterface::class) ?? new ConsoleOutput();
3233
if ($output->isVerbose()) {
3334
logger()->info(strip_ansi_colors($message));
3435
} else {
@@ -38,8 +39,8 @@ public static function success(string $message, bool $indent = false): void
3839

3940
public static function plain(string $message): void
4041
{
41-
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
42-
$output = ApplicationContext::get(OutputInterface::class);
42+
$no_ansi = ApplicationContext::get(InputInterface::class)?->getOption('no-ansi') ?? false;
43+
$output = ApplicationContext::get(OutputInterface::class) ?? new ConsoleOutput();
4344
if ($output->isVerbose()) {
4445
logger()->info(strip_ansi_colors($message));
4546
} else {
@@ -49,8 +50,8 @@ public static function plain(string $message): void
4950

5051
public static function info(string $message): void
5152
{
52-
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
53-
$output = ApplicationContext::get(OutputInterface::class);
53+
$no_ansi = ApplicationContext::get(InputInterface::class)?->getOption('no-ansi') ?? false;
54+
$output = ApplicationContext::get(OutputInterface::class) ?? new ConsoleOutput();
5455
if (!$output->isVerbose()) {
5556
$output->writeln(($no_ansi ? 'strip_ansi_colors' : 'strval')(ConsoleColor::green('') . $message));
5657
}
@@ -59,8 +60,8 @@ public static function info(string $message): void
5960

6061
public static function error(string $message, bool $indent = true): void
6162
{
62-
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
63-
$output = ApplicationContext::get(OutputInterface::class);
63+
$no_ansi = ApplicationContext::get(InputInterface::class)?->getOption('no-ansi') ?? false;
64+
$output = ApplicationContext::get(OutputInterface::class) ?? new ConsoleOutput();
6465
if ($output->isVerbose()) {
6566
logger()->error(strip_ansi_colors($message));
6667
} else {
@@ -75,15 +76,15 @@ public static function advance(): void
7576

7677
public static function setMessage(string $message): void
7778
{
78-
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
79+
$no_ansi = ApplicationContext::get(InputInterface::class)?->getOption('no-ansi') ?? false;
7980
self::$indicator?->setMessage(($no_ansi ? 'strip_ansi_colors' : 'strval')($message));
8081
}
8182

8283
public static function finish(string $message, bool $status = true): void
8384
{
84-
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
85+
$no_ansi = ApplicationContext::get(InputInterface::class)?->getOption('no-ansi') ?? false;
8586
$message = $no_ansi ? strip_ansi_colors($message) : $message;
86-
$output = ApplicationContext::get(OutputInterface::class);
87+
$output = ApplicationContext::get(OutputInterface::class) ?? new ConsoleOutput();
8788
if ($output->isVerbose()) {
8889
if ($status) {
8990
logger()->info($message);
@@ -104,8 +105,8 @@ public static function finish(string $message, bool $status = true): void
104105

105106
public static function indicateProgress(string $message): void
106107
{
107-
$no_ansi = ApplicationContext::get(InputInterface::class)->getOption('no-ansi') ?? false;
108-
$output = ApplicationContext::get(OutputInterface::class);
108+
$no_ansi = ApplicationContext::get(InputInterface::class)?->getOption('no-ansi') ?? false;
109+
$output = ApplicationContext::get(OutputInterface::class) ?? new ConsoleOutput();
109110
if ($output->isVerbose()) {
110111
logger()->info(strip_ansi_colors($message));
111112
return;

0 commit comments

Comments
 (0)