Skip to content

Commit

Permalink
Add the --dry-run option
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Dec 3, 2024
1 parent c6f0f4b commit d84dfd2
Show file tree
Hide file tree
Showing 21 changed files with 247 additions and 24 deletions.
11 changes: 11 additions & 0 deletions src/BinaryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ final class BinaryFile
{
private const HASH_TYPE_SHA256 = 'sha256';

private const DRY_RUN_FAKE_HASH = 'dry_run_fake_hash';

/**
* @param non-empty-string $filePath
* @param non-empty-string $checksum
Expand All @@ -33,4 +35,13 @@ public static function fromFileWithSha256Checksum(string $filePath): self
hash_file(self::HASH_TYPE_SHA256, $filePath),
);
}

/** @param non-empty-string $filePath */
public static function nonExistentForDryRun(string $filePath): self
{
return new self(
'dry-run::' . $filePath,
self::DRY_RUN_FAKE_HASH,
);
}
}
1 change: 1 addition & 0 deletions src/Building/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public function __invoke(
array $configureOptions,
OutputInterface $output,
PhpizePath|null $phpizePath,
bool $dryRun,
): BinaryFile;
}
39 changes: 36 additions & 3 deletions src/Building/UnixBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __invoke(
array $configureOptions,
OutputInterface $output,
PhpizePath|null $phpizePath,
bool $dryRun,
): BinaryFile {
$outputCallback = null;
if ($output->isVerbose()) {
Expand Down Expand Up @@ -60,6 +61,7 @@ public function __invoke(
$downloadedPackage,
$output,
$outputCallback,
$dryRun,
);

$output->writeln('<info>phpize complete</info>.');
Expand All @@ -69,16 +71,28 @@ public function __invoke(
$configureOptions[] = '--with-php-config=' . $phpConfigPath;
}

$this->configure($downloadedPackage, $configureOptions, $output, $outputCallback);
$this->configure(
$downloadedPackage,
$configureOptions,
$output,
$outputCallback,
$dryRun,
);

$optionsOutput = count($configureOptions) ? ' with options: ' . implode(' ', $configureOptions) : '.';
$output->writeln('<info>Configure complete</info>' . $optionsOutput);

$this->make($targetPlatform, $downloadedPackage, $output, $outputCallback);
$this->make(
$targetPlatform,
$downloadedPackage,
$output,
$outputCallback,
$dryRun,
);

$expectedSoFile = $downloadedPackage->extractedSourcePath . '/modules/' . $downloadedPackage->package->extensionName->name() . '.so';

if (! file_exists($expectedSoFile)) {
if (! $dryRun && ! file_exists($expectedSoFile)) {
throw ExtensionBinaryNotFound::fromExpectedBinary($expectedSoFile);
}

Expand All @@ -87,6 +101,10 @@ public function __invoke(
$expectedSoFile,
));

if ($dryRun) {
return BinaryFile::nonExistentForDryRun($expectedSoFile);
}

return BinaryFile::fromFileWithSha256Checksum($expectedSoFile);
}

Expand All @@ -96,13 +114,18 @@ private function phpize(
DownloadedPackage $downloadedPackage,
OutputInterface $output,
callable|null $outputCallback,
bool $dryRun,
): void {
$phpizeCommand = [$phpize->phpizeBinaryPath];

if ($output->isVerbose()) {
$output->writeln('<comment>Running phpize step using: ' . implode(' ', $phpizeCommand) . '</comment>');
}

if ($dryRun) {
return;
}

Process::run(
$phpizeCommand,
$downloadedPackage->extractedSourcePath,
Expand All @@ -120,13 +143,18 @@ private function configure(
array $configureOptions,
OutputInterface $output,
callable|null $outputCallback,
bool $dryRun,
): void {
$configureCommand = ['./configure', ...$configureOptions];

if ($output->isVerbose()) {
$output->writeln('<comment>Running configure step with: ' . implode(' ', $configureCommand) . '</comment>');
}

if ($dryRun) {
return;
}

Process::run(
$configureCommand,
$downloadedPackage->extractedSourcePath,
Expand All @@ -141,6 +169,7 @@ private function make(
DownloadedPackage $downloadedPackage,
OutputInterface $output,
callable|null $outputCallback,
bool $dryRun,
): void {
$makeCommand = ['make'];

Expand All @@ -154,6 +183,10 @@ private function make(
$output->writeln('<comment>Running make step with: ' . implode(' ', $makeCommand) . '</comment>');
}

if ($dryRun) {
return;
}

Process::run(
$makeCommand,
$downloadedPackage->extractedSourcePath,
Expand Down
1 change: 1 addition & 0 deletions src/Building/WindowsBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __invoke(
array $configureOptions,
OutputInterface $output,
PhpizePath|null $phpizePath,
bool $dryRun,
): BinaryFile {
$prebuiltDll = WindowsExtensionAssetName::determineDllName($targetPlatform, $downloadedPackage);

Expand Down
3 changes: 3 additions & 0 deletions src/Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function configure(): void
parent::configure();

CommandHelper::configureDownloadBuildInstallOptions($this);
CommandHelper::configureDryRunOption($this);
}

public function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -54,6 +55,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
PieOperation::Resolve,
[], // Configure options are not needed for resolve only
null,
CommandHelper::determineDryRunFromInputs($input),
),
);

Expand All @@ -74,6 +76,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
PieOperation::Build,
$configureOptionsValues,
CommandHelper::determinePhpizePathFromInputs($input),
false,
),
);

Expand Down
16 changes: 16 additions & 0 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class CommandHelper
private const OPTION_WITH_PHP_PATH = 'with-php-path';
private const OPTION_WITH_PHPIZE_PATH = 'with-phpize-path';
private const OPTION_MAKE_PARALLEL_JOBS = 'make-parallel-jobs';
private const OPTION_DRY_RUN = 'dry-run';

/** @psalm-suppress UnusedConstructor */
private function __construct()
Expand Down Expand Up @@ -89,6 +90,16 @@ public static function configureDownloadBuildInstallOptions(Command $command): v
$command->ignoreValidationErrors();
}

public static function configureDryRunOption(Command $command): void
{
$command->addOption(
self::OPTION_DRY_RUN,
null,
InputOption::VALUE_NONE,
'Do not actually build or install the extension, just show the steps that would be run.',
);
}

public static function validateInput(InputInterface $input, Command $command): void
{
$input->bind($command->getDefinition());
Expand Down Expand Up @@ -154,6 +165,11 @@ public static function determineTargetPlatformFromInputs(InputInterface $input,
return $targetPlatform;
}

public static function determineDryRunFromInputs(InputInterface $input): bool
{
return $input->hasOption(self::OPTION_DRY_RUN) && $input->getOption(self::OPTION_DRY_RUN);
}

public static function determinePhpizePathFromInputs(InputInterface $input): PhpizePath|null
{
if ($input->hasOption(self::OPTION_WITH_PHPIZE_PATH)) {
Expand Down
1 change: 1 addition & 0 deletions src/Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
PieOperation::Download,
[], // Configure options are not needed for download only
null,
CommandHelper::determineDryRunFromInputs($input),
),
);

Expand Down
1 change: 1 addition & 0 deletions src/Command/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
PieOperation::Resolve,
[], // Configure options are not needed for resolve only
null,
CommandHelper::determineDryRunFromInputs($input),
),
);

Expand Down
3 changes: 3 additions & 0 deletions src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function configure(): void
parent::configure();

CommandHelper::configureDownloadBuildInstallOptions($this);
CommandHelper::configureDryRunOption($this);
}

public function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -59,6 +60,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
PieOperation::Resolve,
[], // Configure options are not needed for resolve only
null,
CommandHelper::determineDryRunFromInputs($input),
),
);

Expand All @@ -79,6 +81,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
PieOperation::Install,
$configureOptionsValues,
CommandHelper::determinePhpizePathFromInputs($input),
false,
),
);

Expand Down
2 changes: 2 additions & 0 deletions src/ComposerIntegration/InstallAndBuildProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function __invoke(
$composerRequest->configureOptions,
$output,
$composerRequest->phpizePath,
$composerRequest->dryRun,
);

$this->installedJsonMetadata->addBuildMetadata(
Expand All @@ -77,6 +78,7 @@ public function __invoke(
$downloadedPackage,
$composerRequest->targetPlatform,
$output,
$composerRequest->dryRun,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions src/ComposerIntegration/PieComposerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(
public readonly PieOperation $operation,
public readonly array $configureOptions,
public readonly PhpizePath|null $phpizePath,
public readonly bool $dryRun,
) {
}
}
1 change: 1 addition & 0 deletions src/Installing/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public function __invoke(
DownloadedPackage $downloadedPackage,
TargetPlatform $targetPlatform,
OutputInterface $output,
bool $dryRun,
): BinaryFile;
}
28 changes: 17 additions & 11 deletions src/Installing/UnixInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class UnixInstall implements Install
{
private const MAKE_INSTALL_TIMEOUT_SECS = 60; // 1 minute

public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $targetPlatform, OutputInterface $output): BinaryFile
public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $targetPlatform, OutputInterface $output, bool $dryRun): BinaryFile
{
$targetExtensionPath = $targetPlatform->phpBinaryPath->extensionPath();

Expand All @@ -47,18 +47,20 @@ public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $t
array_unshift($makeInstallCommand, 'sudo');
}

$makeInstallOutput = Process::run(
$makeInstallCommand,
$downloadedPackage->extractedSourcePath,
self::MAKE_INSTALL_TIMEOUT_SECS,
);
if (! $dryRun) {
$makeInstallOutput = Process::run(
$makeInstallCommand,
$downloadedPackage->extractedSourcePath,
self::MAKE_INSTALL_TIMEOUT_SECS,
);

if ($output->isVeryVerbose()) {
$output->writeln($makeInstallOutput);
}
if ($output->isVeryVerbose()) {
$output->writeln($makeInstallOutput);
}

if (! file_exists($expectedSharedObjectLocation)) {
throw new RuntimeException('Install failed, ' . $expectedSharedObjectLocation . ' was not installed.');
if (! file_exists($expectedSharedObjectLocation)) {
throw new RuntimeException('Install failed, ' . $expectedSharedObjectLocation . ' was not installed.');
}
}

$output->writeln('<info>Install complete:</info> ' . $expectedSharedObjectLocation);
Expand All @@ -74,6 +76,10 @@ public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $t
$downloadedPackage->package->extensionName->name(),
));

if ($dryRun) {
return BinaryFile::nonExistentForDryRun($expectedSharedObjectLocation);
}

return BinaryFile::fromFileWithSha256Checksum($expectedSharedObjectLocation);
}
}
Loading

0 comments on commit d84dfd2

Please sign in to comment.