Skip to content

Commit 41f49d3

Browse files
committed
Attempt to auto unfold extracted archive paths into DownloadedPackage->extractedSourcePath
1 parent 1d82541 commit 41f49d3

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

src/Downloading/DownloadedPackage.php

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
namespace Php\Pie\Downloading;
66

77
use Php\Pie\DependencyResolver\Package;
8+
use Php\Pie\Platform\PrePackagedSourceAssetName;
89

10+
use function array_map;
11+
use function array_unique;
12+
use function file_exists;
13+
use function is_dir;
914
use function is_string;
15+
use function pathinfo;
1016
use function realpath;
1117
use function str_replace;
1218

1319
use const DIRECTORY_SEPARATOR;
20+
use const PATHINFO_FILENAME;
1421

1522
/**
1623
* @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 @@ private function __construct(
2532
) {
2633
}
2734

35+
private static function unfoldUnarchivedSourcePaths(Package $package, string $extractedSourcePath): string
36+
{
37+
// There is already something buildable here, don't need to unfold
38+
if (
39+
file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . 'config.m4')
40+
|| file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . 'config.w32')
41+
) {
42+
return $extractedSourcePath;
43+
}
44+
45+
$possibleAssetNames = array_unique(array_map(
46+
static fn (string $assetName): string => pathinfo($assetName, PATHINFO_FILENAME),
47+
PrePackagedSourceAssetName::packageNames($package),
48+
));
49+
foreach ($possibleAssetNames as $possibleAssetName) {
50+
if (
51+
! file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName)
52+
|| ! is_dir($extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName)
53+
) {
54+
continue;
55+
}
56+
57+
if (
58+
file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName . DIRECTORY_SEPARATOR . 'config.m4')
59+
|| file_exists($extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName . DIRECTORY_SEPARATOR . 'config.w32')
60+
) {
61+
return $extractedSourcePath . DIRECTORY_SEPARATOR . $possibleAssetName;
62+
}
63+
}
64+
65+
return $extractedSourcePath;
66+
}
67+
68+
private static function overrideSourcePathUsingBuildPath(Package $package, string $extractedSourcePath): string
69+
{
70+
if ($package->buildPath() === null) {
71+
return $extractedSourcePath;
72+
}
73+
74+
$extractedSourcePathWithBuildPath = realpath(
75+
$extractedSourcePath
76+
. DIRECTORY_SEPARATOR
77+
. str_replace('{version}', $package->version(), $package->buildPath()),
78+
);
79+
80+
if (! is_string($extractedSourcePathWithBuildPath)) {
81+
return $extractedSourcePath;
82+
}
83+
84+
return $extractedSourcePathWithBuildPath;
85+
}
86+
2887
public static function fromPackageAndExtractedPath(Package $package, string $extractedSourcePath): self
2988
{
89+
$sourcePath = self::unfoldUnarchivedSourcePaths($package, $extractedSourcePath);
90+
3091
if ($package->buildPath() !== null) {
31-
$extractedSourcePathWithBuildPath = realpath(
32-
$extractedSourcePath
33-
. DIRECTORY_SEPARATOR
34-
. str_replace('{version}', $package->version(), $package->buildPath()),
35-
);
36-
37-
if (is_string($extractedSourcePathWithBuildPath)) {
38-
$extractedSourcePath = $extractedSourcePathWithBuildPath;
39-
}
92+
$sourcePath = self::overrideSourcePathUsingBuildPath($package, $extractedSourcePath);
4093
}
4194

42-
return new self($package, $extractedSourcePath);
95+
return new self($package, $sourcePath);
4396
}
4497
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
not really a config.m4 ;)

test/unit/Downloading/DownloadedPackageTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,21 @@ public function testFromPackageAndExtractedPathWithBuildPathWithVersionTemplate(
7474
self::assertSame($extractedSourcePath . DIRECTORY_SEPARATOR . 'package-1.2.3', $downloadedPackage->extractedSourcePath);
7575
self::assertSame($package, $downloadedPackage->package);
7676
}
77+
78+
public function testBuildPathDetectedFromExtractedPrePackagedSourceAsset(): void
79+
{
80+
$composerPackage = $this->createMock(CompletePackage::class);
81+
$composerPackage->method('getPrettyName')->willReturn('foo/bar');
82+
$composerPackage->method('getPrettyVersion')->willReturn('1.2.3');
83+
$composerPackage->method('getType')->willReturn('php-ext');
84+
85+
$package = Package::fromComposerCompletePackage($composerPackage);
86+
87+
$extractedSourcePath = realpath(__DIR__ . '/../../assets');
88+
89+
$downloadedPackage = DownloadedPackage::fromPackageAndExtractedPath($package, $extractedSourcePath);
90+
91+
self::assertSame($extractedSourcePath . DIRECTORY_SEPARATOR . 'php_bar-1.2.3-src', $downloadedPackage->extractedSourcePath);
92+
self::assertSame($package, $downloadedPackage->package);
93+
}
7794
}

0 commit comments

Comments
 (0)