From 864ada8f36c7f81a642be08e72045bc7a1030e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Thu, 14 Nov 2024 19:34:57 +0100 Subject: [PATCH] Support download minify on Windows (#20) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to @rglozman 's help ! --------- Signed-off-by: Simon André Co-authored-by: rglozman --- CHANGELOG.md | 4 ++++ src/Minifier/SystemUtils.php | 2 +- src/MinifyInstaller.php | 28 +++++++++++++++++++----- tests/Command/MinifyAssetCommandTest.php | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25498b9..5f457a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 0.9.3 + +- Support for Windows + ## 0.9.0 - First version of the bundle diff --git a/src/Minifier/SystemUtils.php b/src/Minifier/SystemUtils.php index 6d3b82d..9423981 100644 --- a/src/Minifier/SystemUtils.php +++ b/src/Minifier/SystemUtils.php @@ -62,7 +62,7 @@ private function getPlatform(string $platform): ?string private function getArchitecture(string $architecture): ?string { - return match ($architecture) { + return match (\strtolower($architecture)) { 'amd64', 'x86_64' => self::ARCH_AMD64, 'arm64' => self::ARCH_ARM64, 'i386', 'i686' => self::ARCH_X86, diff --git a/src/MinifyInstaller.php b/src/MinifyInstaller.php index b5bafef..74238e3 100644 --- a/src/MinifyInstaller.php +++ b/src/MinifyInstaller.php @@ -82,16 +82,32 @@ public function download(string $version): void $this->filesystem->appendToFile($downloadFilename, $chunk->getContent(), true); } - // TODO windows + if (str_ends_with($downloadFilename, '.zip')) { + $download = function () use ($downloadFilename, $tempDir) { + $archive = new \ZipArchive(); + $archive->open($downloadFilename); + $archive->extractTo($tempDir, 'minify'); + $archive->close(); + }; + } else { + $download = function () use ($downloadFilename, $tempDir) { + $archive = new \PharData($downloadFilename); + $archive->extractTo($tempDir, ['minify'], true); + }; + } - $archive = new \PharData($downloadFilename); - if (!isset($archive['minify'])) { - throw new LogicException('The minify binary is missing from the archive.'); + try { + $download(); + } catch (\Throwable $e) { + throw new InstallException(sprintf('Error extracting the binary from archive "%s".', $downloadFilename), 0, $e->getPrevious()); } - $archive->extractTo($tempDir, ['minify'], true); $this->filesystem->mkdir(dirname($this->getInstallBinaryPath())); - $this->filesystem->copy(Path::join($tempDir, 'minify'), $this->getInstallBinaryPath()); + if (str_ends_with($downloadFilename, '.zip')) { + $this->filesystem->copy(Path::join($tempDir, 'minify.exe'), $this->getInstallBinaryPath()); + } else { + $this->filesystem->copy(Path::join($tempDir, 'minify'), $this->getInstallBinaryPath()); + } $this->filesystem->remove($tempDir); } diff --git a/tests/Command/MinifyAssetCommandTest.php b/tests/Command/MinifyAssetCommandTest.php index 0d54f1b..89d5ab4 100644 --- a/tests/Command/MinifyAssetCommandTest.php +++ b/tests/Command/MinifyAssetCommandTest.php @@ -76,7 +76,7 @@ public function testMinifyAssetCommandFailsWhenInputFileIsNotCssOrJS(): void $this->assertSame(Command::FAILURE, $tester->getStatusCode()); $display = $tester->getDisplay(); $this->assertStringContainsString('The type of', $display); - $this->assertStringContainsString('TestKernel.php" is "php", it must be "css" or "js".', $display); + $this->assertStringContainsString('it must be "css" or "js".', $display); } public function testMinifyAssetCommandFailsWhenOutputFileIsNotWritable(): void