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 Nov 26, 2024
1 parent 3abe8f5 commit 6dce19e
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 25 deletions.
43 changes: 39 additions & 4 deletions src/Building/UnixBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class UnixBuild implements Build
private const PHPIZE_TIMEOUT_SECS = 60; // 1 minute
private const CONFIGURE_TIMEOUT_SECS = 120; // 2 minutes
private const MAKE_TIMEOUT_SECS = null; // unlimited
private const EMPTY_STRING_SHA256 = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';

/** {@inheritDoc} */
public function __invoke(
Expand Down Expand Up @@ -60,6 +61,7 @@ public function __invoke(
$downloadedPackage,
$output,
$outputCallback,
$targetPlatform->dryRun,
);

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

$this->configure($downloadedPackage, $configureOptions, $output, $outputCallback);
$this->configure(
$downloadedPackage,
$configureOptions,
$output,
$outputCallback,
$targetPlatform->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,
$targetPlatform->dryRun,
);

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

if (! file_exists($expectedSoFile)) {
throw ExtensionBinaryNotFound::fromExpectedBinary($expectedSoFile);
if (! $targetPlatform->dryRun) {
if (! file_exists($expectedSoFile)) {
throw ExtensionBinaryNotFound::fromExpectedBinary($expectedSoFile);
}
}

$output->writeln(sprintf(
'<info>Build complete:</info> %s',
$expectedSoFile,
));

if ($targetPlatform->dryRun) {
return new BinaryFile($expectedSoFile, self::EMPTY_STRING_SHA256); // sha256 of empty string
}

return BinaryFile::fromFileWithSha256Checksum($expectedSoFile);
}

Expand All @@ -96,13 +116,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 +145,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 +171,7 @@ private function make(
DownloadedPackage $downloadedPackage,
OutputInterface $output,
callable|null $outputCallback,
bool $dryRun,
): void {
$makeCommand = ['make'];

Expand All @@ -154,6 +185,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/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 Down
18 changes: 17 additions & 1 deletion 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 @@ -138,7 +149,12 @@ public static function determineTargetPlatformFromInputs(InputInterface $input,
}
}

$targetPlatform = TargetPlatform::fromPhpBinaryPath($phpBinaryPath, $makeParallelJobs);
$dryRun = false;
if ($input->hasOption(self::OPTION_DRY_RUN)) {
$dryRun = (bool) $input->getOption(self::OPTION_DRY_RUN);
}

$targetPlatform = TargetPlatform::fromPhpBinaryPath($phpBinaryPath, $makeParallelJobs, $dryRun);

$output->writeln(sprintf('<info>You are running PHP %s</info>', PHP_VERSION));
$output->writeln(sprintf(
Expand Down
1 change: 1 addition & 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 Down
27 changes: 17 additions & 10 deletions src/Installing/UnixInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
final class UnixInstall implements Install
{
private const MAKE_INSTALL_TIMEOUT_SECS = 60; // 1 minute
private const EMPTY_STRING_SHA256 = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';

public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $targetPlatform, OutputInterface $output): BinaryFile
{
Expand All @@ -47,18 +48,20 @@ public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $t
array_unshift($makeInstallCommand, 'sudo');
}

$makeInstallOutput = Process::run(
$makeInstallCommand,
$downloadedPackage->extractedSourcePath,
self::MAKE_INSTALL_TIMEOUT_SECS,
);
if (! $targetPlatform->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 +77,10 @@ public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $t
$downloadedPackage->package->extensionName->name(),
));

if ($targetPlatform->dryRun) {
return new BinaryFile($expectedSharedObjectLocation, self::EMPTY_STRING_SHA256);
}

return BinaryFile::fromFileWithSha256Checksum($expectedSharedObjectLocation);
}
}
22 changes: 22 additions & 0 deletions src/Installing/WindowsInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
final class WindowsInstall implements Install
{
private const EMPTY_STRING_SHA256 = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';

public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $targetPlatform, OutputInterface $output): BinaryFile
{
$extractedSourcePath = $downloadedPackage->extractedSourcePath;
Expand Down Expand Up @@ -82,6 +84,10 @@ public function __invoke(DownloadedPackage $downloadedPackage, TargetPlatform $t
$downloadedPackage->package->extensionName->name(),
));

if ($targetPlatform->dryRun) {
return new BinaryFile($destinationDllName, self::EMPTY_STRING_SHA256);
}

return BinaryFile::fromFileWithSha256Checksum($destinationDllName);
}

Expand All @@ -106,6 +112,10 @@ private function copyExtensionDll(TargetPlatform $targetPlatform, DownloadedPack
$destinationDllName = $targetPlatform->phpBinaryPath->extensionPath() . DIRECTORY_SEPARATOR
. 'php_' . $downloadedPackage->package->extensionName->name() . '.dll';

if ($targetPlatform->dryRun) {
return $destinationDllName;
}

if (! copy($sourceDllName, $destinationDllName) || ! file_exists($destinationDllName) && ! is_file($destinationDllName)) {
throw new RuntimeException('Failed to install DLL to ' . $destinationDllName);
}
Expand Down Expand Up @@ -133,6 +143,10 @@ private function copyExtensionPdb(TargetPlatform $targetPlatform, DownloadedPack
$destinationPdbName = str_replace('.dll', '.pdb', $destinationDllName);
assert($destinationPdbName !== '');

if ($targetPlatform->dryRun) {
return $destinationPdbName;
}

if (! copy($sourcePdbName, $destinationPdbName) || ! file_exists($destinationPdbName) && ! is_file($destinationPdbName)) {
throw new RuntimeException('Failed to install PDB to ' . $destinationPdbName);
}
Expand All @@ -156,6 +170,10 @@ private function copyDependencyDll(TargetPlatform $targetPlatform, SplFileInfo $

$destinationExtraDll = dirname($targetPlatform->phpBinaryPath->phpBinaryPath) . DIRECTORY_SEPARATOR . $file->getFilename();

if ($targetPlatform->dryRun) {
return $destinationExtraDll;
}

if (! copy($file->getPathname(), $destinationExtraDll) || ! file_exists($destinationExtraDll) && ! is_file($destinationExtraDll)) {
throw new RuntimeException('Failed to copy to ' . $destinationExtraDll);
}
Expand All @@ -177,6 +195,10 @@ private function copyExtraFile(TargetPlatform $targetPlatform, DownloadedPackage

$destinationPath = dirname($destinationFullFilename);

if ($targetPlatform->dryRun) {
return $destinationFullFilename;
}

if (! file_exists($destinationPath)) {
mkdir($destinationPath, 0777, true);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Platform/TargetPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(
public readonly ThreadSafetyMode $threadSafety,
public readonly int $makeParallelJobs,
public readonly WindowsCompiler|null $windowsCompiler,
public readonly bool $dryRun,
) {
}

Expand All @@ -36,7 +37,7 @@ public static function isRunningAsRoot(): bool
return function_exists('posix_getuid') && posix_getuid() === 0;
}

public static function fromPhpBinaryPath(PhpBinaryPath $phpBinaryPath, int|null $makeParallelJobs): self
public static function fromPhpBinaryPath(PhpBinaryPath $phpBinaryPath, int|null $makeParallelJobs, bool $dryRun): self
{
$os = $phpBinaryPath->operatingSystem();

Expand Down Expand Up @@ -119,6 +120,7 @@ public static function fromPhpBinaryPath(PhpBinaryPath $phpBinaryPath, int|null
$threadSafety,
$makeParallelJobs,
$windowsCompiler,
$dryRun,
);
}
}
10 changes: 5 additions & 5 deletions test/integration/Building/UnixBuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testUnixBuildCanBuildExtension(): void
$unixBuilder = new UnixBuild();
$builtBinary = $unixBuilder->__invoke(
$downloadedPackage,
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null),
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null, false),
['--enable-pie_test_ext'],
$output,
null,
Expand Down Expand Up @@ -111,7 +111,7 @@ public function testUnixBuildWillThrowExceptionWhenExpectedBinaryNameMismatches(
try {
$unixBuilder->__invoke(
$downloadedPackage,
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null),
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null, false),
['--enable-pie_test_ext'],
$output,
null,
Expand Down Expand Up @@ -149,7 +149,7 @@ public function testUnixBuildCanBuildExtensionWithBuildPath(): void
$unixBuilder = new UnixBuild();
$builtBinary = $unixBuilder->__invoke(
$downloadedPackage,
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null),
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null, false),
['--enable-pie_test_ext'],
$output,
null,
Expand Down Expand Up @@ -206,7 +206,7 @@ public function testCleanupDoesNotCleanWhenConfigureIsMissing(): void
$unixBuilder = new UnixBuild();
$unixBuilder->__invoke(
$downloadedPackage,
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null),
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null, false),
['--enable-pie_test_ext'],
$output,
null,
Expand Down Expand Up @@ -248,7 +248,7 @@ public function testVerboseOutputShowsCleanupMessages(): void
$unixBuilder = new UnixBuild();
$unixBuilder->__invoke(
$downloadedPackage,
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null),
TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null, false),
['--enable-pie_test_ext'],
$output,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testDependenciesAreResolvedToExpectedVersions(
$container = Container::factory();
$resolve = $container->get(DependencyResolver::class);

$targetPlatform = TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null);
$targetPlatform = TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromCurrentProcess(), null, false);
$requestedPackageAndVersion = new RequestedPackageAndVersion(
'asgrim/example-pie-extension',
$requestedVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testDeterminingReleaseAssetUrlForWindows(): void
ThreadSafetyMode::ThreadSafe,
1,
WindowsCompiler::VS16,
false,
);

$package = new Package(
Expand Down
Loading

0 comments on commit 6dce19e

Please sign in to comment.