Skip to content

Commit 4d74594

Browse files
committed
Add support to update packages.txt file
1 parent 7b29454 commit 4d74594

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/Console/Command/WinlibsCommand.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function handle(): int
4747
if ($files) {
4848
if($data['type'] === 'php') {
4949
$this->copyPhpFiles($files, $data['library'], $data['vs_version_targets']);
50-
$this->updateSeriesFiles(
50+
$this->updatePhpSeriesFiles(
5151
$files,
5252
$data['library'],
5353
$data['php_versions'],
@@ -56,6 +56,7 @@ public function handle(): int
5656
);
5757
} else {
5858
$this->copyPeclFiles($files, $data['library']);
59+
$this->updatePackagesFile($files, $data['library']);
5960
}
6061
}
6162

@@ -124,7 +125,7 @@ private function copyPeclFiles(array $files, string $library): void
124125
}
125126
}
126127

127-
private function updateSeriesFiles(
128+
private function updatePhpSeriesFiles(
128129
array $files,
129130
string $library,
130131
string $php_versions,
@@ -170,4 +171,45 @@ private function updateSeriesFiles(
170171
}
171172
}
172173
}
174+
175+
private function updatePackagesFile(array $files, string $library): void
176+
{
177+
$baseDirectory = $this->baseDirectory . "/pecl/deps";
178+
$packagesFile = $baseDirectory . "/packages.txt";
179+
$syncFile = $packagesFile . '.sync';
180+
181+
if (!is_dir($baseDirectory)) {
182+
mkdir($baseDirectory, 0755, true);
183+
}
184+
185+
$file_lines = [];
186+
if (file_exists($packagesFile)) {
187+
$file_lines = file($packagesFile, FILE_IGNORE_NEW_LINES);
188+
}
189+
190+
if(!file_exists($syncFile)) {
191+
$file_lines = array_map(function($file) {
192+
return basename($file);
193+
}, glob($baseDirectory . '/*.zip'));
194+
} else {
195+
foreach ($files as $file) {
196+
$fileName = str_replace($file['artifact_name'], $library, $file['file_name']);
197+
$found = false;
198+
foreach ($file_lines as $no => $line) {
199+
if (str_starts_with($line, $library)) {
200+
$file_lines[$no] = $fileName;
201+
$found = true;
202+
}
203+
}
204+
if (!$found) {
205+
$file_lines[] = $fileName;
206+
}
207+
}
208+
}
209+
sort($file_lines);
210+
file_put_contents($packagesFile, implode("\n", $file_lines));
211+
if(!file_exists($syncFile)) {
212+
touch($syncFile);
213+
}
214+
}
173215
}

tests/Console/Command/WinlibsCommandTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,53 @@ public function testSuccessfulPeclFileOperations(): void
182182
$this->assertFileExists($this->baseDirectory . "/pecl/deps/$library-$ref-$vsVersion-$arch.zip");
183183
}
184184

185+
public function testPackagesFileIsGeneratedWithoutSyncFile(): void
186+
{
187+
$result = $this->runPeclWinlibsCommand('redis', 'phpredis', '5.3.7', 'vs16', 'x64');
188+
189+
$this->assertEquals(0, $result, 'Command should return success.');
190+
191+
$packagesFile = $this->baseDirectory . '/pecl/deps/packages.txt';
192+
$syncFile = $packagesFile . '.sync';
193+
194+
$this->assertFileExists($packagesFile);
195+
$this->assertFileExists($syncFile);
196+
$this->assertSame(
197+
['phpredis-5.3.7-vs16-x64.zip'],
198+
file($packagesFile, FILE_IGNORE_NEW_LINES)
199+
);
200+
}
201+
202+
public function testPackagesFileUpdatesExistingEntriesAndMaintainsSorting(): void
203+
{
204+
$this->runPeclWinlibsCommand('redis', 'phpredis', '5.3.7', 'vs16', 'x64');
205+
206+
$packagesFile = $this->baseDirectory . '/pecl/deps/packages.txt';
207+
$this->assertFileExists($packagesFile);
208+
$this->assertSame(
209+
['phpredis-5.3.7-vs16-x64.zip'],
210+
file($packagesFile, FILE_IGNORE_NEW_LINES)
211+
);
212+
213+
$this->runPeclWinlibsCommand('imagick', 'imagick', '3.7.0', 'vs16', 'x64');
214+
$this->assertSame(
215+
[
216+
'imagick-3.7.0-vs16-x64.zip',
217+
'phpredis-5.3.7-vs16-x64.zip',
218+
],
219+
file($packagesFile, FILE_IGNORE_NEW_LINES)
220+
);
221+
222+
$this->runPeclWinlibsCommand('redis', 'phpredis', '5.3.8', 'vs16', 'x64');
223+
$this->assertSame(
224+
[
225+
'imagick-3.7.0-vs16-x64.zip',
226+
'phpredis-5.3.8-vs16-x64.zip',
227+
],
228+
file($packagesFile, FILE_IGNORE_NEW_LINES)
229+
);
230+
}
231+
185232
public static function versionProvider(): array
186233
{
187234
return [
@@ -278,4 +325,32 @@ public static function fileProvider(): array
278325
]],
279326
];
280327
}
328+
329+
private function runPeclWinlibsCommand(string $buildName, string $library, string $ref, string $vsVersion, string $arch): int
330+
{
331+
$directory = $this->winlibsDirectory . '/' . $buildName;
332+
mkdir($directory, 0755, true);
333+
334+
file_put_contents($directory . '/data.json', json_encode([
335+
'type' => 'pecl',
336+
'library' => $library,
337+
'ref' => $ref,
338+
'vs_version_targets' => $vsVersion,
339+
'php_versions' => '8.2',
340+
'stability' => 'stable'
341+
]));
342+
343+
$zipPath = $directory . "/$buildName-$ref-$vsVersion-$arch.zip";
344+
$zip = new ZipArchive();
345+
if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE) {
346+
$zip->addFromString('dummy_file.txt', 'dummy content');
347+
$zip->close();
348+
}
349+
350+
$command = new WinlibsCommand();
351+
$command->setOption('base-directory', $this->baseDirectory);
352+
$command->setOption('builds-directory', $this->buildsDirectory);
353+
354+
return $command->handle();
355+
}
281356
}

0 commit comments

Comments
 (0)