Skip to content

Commit 380502c

Browse files
authored
Update TailwindBinary::getBinaryName() for 4.0 (#104)
* Update `TailwindBinary::getBinaryName()` for 4.0 * cs fixes * fix windows
1 parent 408c65b commit 380502c

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

src/TailwindBinary.php

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function getBinaryPath(): string
9595
return $this->binaryPath;
9696
}
9797

98-
$this->binaryPath = $this->binaryDownloadDir.'/'.$this->getVersion().'/'.self::getBinaryName();
98+
$this->binaryPath = $this->binaryDownloadDir.'/'.$this->getVersion().'/'.self::getBinaryName($this->getRawVersion());
9999

100100
if (!is_file($this->binaryPath)) {
101101
$this->downloadExecutable();
@@ -106,15 +106,16 @@ private function getBinaryPath(): string
106106

107107
private function downloadExecutable(): void
108108
{
109-
$url = \sprintf('https://github.com/tailwindlabs/tailwindcss/releases/download/%s/%s', $this->getVersion(), self::getBinaryName());
109+
$binaryName = self::getBinaryName($this->getRawVersion());
110+
$url = \sprintf('https://github.com/tailwindlabs/tailwindcss/releases/download/%s/%s', $this->getVersion(), $binaryName);
110111

111112
$this->output?->note(\sprintf('Downloading TailwindCSS binary from %s', $url));
112113

113114
if (!is_dir($this->binaryDownloadDir.'/'.$this->getVersion())) {
114115
mkdir($this->binaryDownloadDir.'/'.$this->getVersion(), 0777, true);
115116
}
116117

117-
$targetPath = $this->binaryDownloadDir.'/'.$this->getVersion().'/'.self::getBinaryName();
118+
$targetPath = $this->binaryDownloadDir.'/'.$this->getVersion().'/'.$binaryName;
118119
$progressBar = null;
119120

120121
$response = $this->httpClient->request('GET', $url, [
@@ -145,47 +146,53 @@ private function downloadExecutable(): void
145146
/**
146147
* @internal
147148
*/
148-
public static function getBinaryName(): string
149+
public static function getBinaryName(string $version): string
149150
{
150151
$os = strtolower(\PHP_OS);
151152
$machine = strtolower(php_uname('m'));
152153

153-
if (str_contains($os, 'darwin')) {
154-
if ('arm64' === $machine) {
155-
return 'tailwindcss-macos-arm64';
154+
$systems = [
155+
'linux' => 'linux',
156+
'darwin' => 'macos',
157+
'win' => 'windows',
158+
];
159+
160+
$architectures = [
161+
'arm64' => 'arm64',
162+
'aarch64' => 'arm64',
163+
'armv7' => 'armv7',
164+
'x86_64' => 'x64',
165+
'amd64' => 'x64',
166+
];
167+
168+
// Detect the current system
169+
$system = null;
170+
foreach ($systems as $key => $name) {
171+
if (str_contains($os, $key)) {
172+
$system = $name;
173+
break;
156174
}
157-
if ('x86_64' === $machine) {
158-
return 'tailwindcss-macos-x64';
159-
}
160-
161-
throw new \Exception(\sprintf('No matching machine found for Darwin platform (Machine: %s).', $machine));
162175
}
163176

164-
if (str_contains($os, 'linux')) {
165-
if ('arm64' === $machine || 'aarch64' === $machine) {
166-
return 'tailwindcss-linux-arm64';
167-
}
168-
if ('armv7' === $machine) {
169-
return 'tailwindcss-linux-armv7';
170-
}
171-
if ('x86_64' === $machine) {
172-
return 'tailwindcss-linux-x64';
173-
}
177+
// Detect the current architecture
178+
$arch = $architectures[$machine] ?? null;
174179

175-
throw new \Exception(\sprintf('No matching machine found for Linux platform (Machine: %s).', $machine));
180+
if (!$system || !$arch) {
181+
throw new \Exception(\sprintf('Unknown platform or architecture (OS: %s, Machine: %s).', $os, $machine));
176182
}
177183

178-
if (str_contains($os, 'win')) {
179-
if ('arm64' === $machine) {
180-
return 'tailwindcss-windows-arm64.exe';
181-
}
182-
if ('x86_64' === $machine || 'amd64' === $machine) {
183-
return 'tailwindcss-windows-x64.exe';
184-
}
184+
// Detect MUSL only when version >= 4.0.0
185+
if ('linux' === $system && version_compare($version, '4.0.0', '>=')) {
186+
$libs = [
187+
'x64' => 'x86_64',
188+
'arm64' => 'aarch64',
189+
];
190+
191+
$isMusl = isset($libs[$arch]) && file_exists("/lib/ld-musl-{$libs[$arch]}.so.1");
185192

186-
throw new \Exception(\sprintf('No matching machine found for Windows platform (Machine: %s).', $machine));
193+
return "tailwindcss-{$system}-{$arch}".($isMusl ? '-musl' : '');
187194
}
188195

189-
throw new \Exception(\sprintf('Unknown platform or architecture (OS: %s, Machine: %s).', $os, $machine));
196+
return "tailwindcss-{$system}-{$arch}".(('windows' === $system) ? '.exe' : '');
190197
}
191198
}

tests/TailwindBinaryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testBinaryIsDownloadedAndProcessCreated()
3333

3434
$binary = new TailwindBinary($binaryDownloadDir, __DIR__, null, 'fake-version', null, $client);
3535
$process = $binary->createProcess(['-i', 'fake.css']);
36-
$binaryFile = $binaryDownloadDir.'/fake-version/'.TailwindBinary::getBinaryName();
36+
$binaryFile = $binaryDownloadDir.'/fake-version/'.TailwindBinary::getBinaryName('4.0.0');
3737
$this->assertFileExists($binaryFile);
3838

3939
$this->assertSame(
@@ -53,7 +53,7 @@ public function testGetVersionFromBinary(string $version)
5353
$fs->remove($binaryDownloadDir);
5454
}
5555
$fs->mkdir($binaryDownloadDir);
56-
$binaryFile = $binaryDownloadDir.'/'.$version.'/'.TailwindBinary::getBinaryName();
56+
$binaryFile = $binaryDownloadDir.'/'.$version.'/'.TailwindBinary::getBinaryName(ltrim($version, 'v'));
5757

5858
$binary1 = new TailwindBinary($binaryDownloadDir, __DIR__, null, $version);
5959

0 commit comments

Comments
 (0)