1
0
mirror of https://github.com/php/pie.git synced 2026-03-23 23:12:17 +01:00

Attempt to auto unfold extracted archive paths into DownloadedPackage->extractedSourcePath

This commit is contained in:
James Titcumb
2025-02-27 11:20:57 +00:00
parent 1d825419ae
commit 41f49d3307
3 changed files with 81 additions and 10 deletions

View File

@@ -5,12 +5,19 @@ declare(strict_types=1);
namespace Php\Pie\Downloading;
use Php\Pie\DependencyResolver\Package;
use Php\Pie\Platform\PrePackagedSourceAssetName;
use function array_map;
use function array_unique;
use function file_exists;
use function is_dir;
use function is_string;
use function pathinfo;
use function realpath;
use function str_replace;
use const DIRECTORY_SEPARATOR;
use const PATHINFO_FILENAME;
/**
* @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks
@@ -25,20 +32,66 @@ final class DownloadedPackage
) {
}
public static function fromPackageAndExtractedPath(Package $package, string $extractedSourcePath): self
private static function unfoldUnarchivedSourcePaths(Package $package, string $extractedSourcePath): string
{
if ($package->buildPath() !== null) {
$extractedSourcePathWithBuildPath = realpath(
$extractedSourcePath
. DIRECTORY_SEPARATOR
. str_replace('{version}', $package->version(), $package->buildPath()),
);
// There is already something buildable here, don't need to unfold
if (
file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . 'config.m4')
|| file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . 'config.w32')
) {
return $extractedSourcePath;
}
if (is_string($extractedSourcePathWithBuildPath)) {
$extractedSourcePath = $extractedSourcePathWithBuildPath;
$possibleAssetNames = array_unique(array_map(
static fn (string $assetName): string => pathinfo($assetName, PATHINFO_FILENAME),
PrePackagedSourceAssetName::packageNames($package),
));
foreach ($possibleAssetNames as $possibleAssetName) {
if (
! file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName)
|| ! is_dir($extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName)
) {
continue;
}
if (
file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName . DIRECTORY_SEPARATOR . 'config.m4')
|| file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName . DIRECTORY_SEPARATOR . 'config.w32')
) {
return $extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName;
}
}
return new self($package, $extractedSourcePath);
return $extractedSourcePath;
}
private static function overrideSourcePathUsingBuildPath(Package $package, string $extractedSourcePath): string
{
if ($package->buildPath() === null) {
return $extractedSourcePath;
}
$extractedSourcePathWithBuildPath = realpath(
$extractedSourcePath
. DIRECTORY_SEPARATOR
. str_replace('{version}', $package->version(), $package->buildPath()),
);
if (! is_string($extractedSourcePathWithBuildPath)) {
return $extractedSourcePath;
}
return $extractedSourcePathWithBuildPath;
}
public static function fromPackageAndExtractedPath(Package $package, string $extractedSourcePath): self
{
$sourcePath = self::unfoldUnarchivedSourcePaths($package, $extractedSourcePath);
if ($package->buildPath() !== null) {
$sourcePath = self::overrideSourcePathUsingBuildPath($package, $extractedSourcePath);
}
return new self($package, $sourcePath);
}
}

View File

@@ -0,0 +1 @@
not really a config.m4 ;)

View File

@@ -74,4 +74,21 @@ final class DownloadedPackageTest extends TestCase
self::assertSame($extractedSourcePath . DIRECTORY_SEPARATOR . 'package-1.2.3', $downloadedPackage->extractedSourcePath);
self::assertSame($package, $downloadedPackage->package);
}
public function testBuildPathDetectedFromExtractedPrePackagedSourceAsset(): void
{
$composerPackage = $this->createMock(CompletePackage::class);
$composerPackage->method('getPrettyName')->willReturn('foo/bar');
$composerPackage->method('getPrettyVersion')->willReturn('1.2.3');
$composerPackage->method('getType')->willReturn('php-ext');
$package = Package::fromComposerCompletePackage($composerPackage);
$extractedSourcePath = realpath(__DIR__ . '/../../assets');
$downloadedPackage = DownloadedPackage::fromPackageAndExtractedPath($package, $extractedSourcePath);
self::assertSame($extractedSourcePath . DIRECTORY_SEPARATOR . 'php_bar-1.2.3-src', $downloadedPackage->extractedSourcePath);
self::assertSame($package, $downloadedPackage->package);
}
}