Skip to content

Commit 04a4ef8

Browse files
committed
Update musl detection logic in TailwindBinary
1 parent f270e7c commit 04a4ef8

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
abstract_arg('Tailwind binary version'),
2222
abstract_arg('path to Tailwind CSS config file'),
2323
abstract_arg('path to PostCSS config file'),
24+
abstract_arg('Tailwind binary platform'),
2425
])
2526

2627
->set('tailwind.command.build', TailwindBuildCommand::class)

src/DependencyInjection/TailwindExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function load(array $configs, ContainerBuilder $container): void
3939
->replaceArgument(4, $config['binary_version'])
4040
->replaceArgument(5, $config['config_file'])
4141
->replaceArgument(6, $config['postcss_config_file'])
42+
->replaceArgument(7, $config['binary_platform'])
4243
;
4344
}
4445

@@ -57,6 +58,7 @@ public function getConfigTreeBuilder(): TreeBuilder
5758
$treeBuilder = new TreeBuilder('symfonycasts_tailwind');
5859
$rootNode = $treeBuilder->getRootNode();
5960
\assert($rootNode instanceof ArrayNodeDefinition);
61+
$platforms = ['auto', 'linux-arm64', 'linux-arm64-musl', 'linux-x64', 'linux-x64-musl', 'macos-arm64', 'macos-x64', 'windows-x64'];
6062

6163
$rootNode
6264
->children()
@@ -84,6 +86,11 @@ public function getConfigTreeBuilder(): TreeBuilder
8486
})
8587
->end()
8688
->end()
89+
->enumNode('binary_platform')
90+
->values($platforms)
91+
->info('Tailwind CLI platform to download - "auto" will try to detect the platform automatically')
92+
->defaultValue('auto')
93+
->end()
8794
->scalarNode('postcss_config_file')
8895
->info('Path to PostCSS config file which is passed to the Tailwind CLI')
8996
->defaultNull()

src/TailwindBinary.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct(
3232
private ?string $binaryVersion,
3333
private ?SymfonyStyle $output = null,
3434
?HttpClientInterface $httpClient = null,
35+
private string $binaryPlatform = 'auto',
3536
) {
3637
$this->httpClient = $httpClient ?? HttpClient::create();
3738

@@ -95,7 +96,7 @@ private function getBinaryPath(): string
9596
return $this->binaryPath;
9697
}
9798

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

100101
if (!is_file($this->binaryPath)) {
101102
$this->downloadExecutable();
@@ -106,7 +107,7 @@ private function getBinaryPath(): string
106107

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

112113
$this->output?->note(\sprintf('Downloading TailwindCSS binary from %s', $url));
@@ -146,8 +147,19 @@ private function downloadExecutable(): void
146147
/**
147148
* @internal
148149
*/
149-
public static function getBinaryName(string $version): string
150+
public static function getBinaryName(string $version, string $platform = 'auto'): string
150151
{
152+
$system = self::getBinarySystem($version, $platform);
153+
$isWindows = str_contains($system, 'windows');
154+
155+
return "tailwindcss-{$system}".(($isWindows) ? '.exe' : '');
156+
}
157+
158+
private static function getBinarySystem(string $version, string $platform): string
159+
{
160+
if ('auto' !== $platform) {
161+
return $platform;
162+
}
151163
$os = strtolower(\PHP_OS);
152164
$machine = strtolower(php_uname('m'));
153165

@@ -183,11 +195,6 @@ public static function getBinaryName(string $version): string
183195

184196
// Detect MUSL only when version >= 4.0.0
185197
if ('linux' === $system && version_compare($version, '4.0.0', '>=')) {
186-
$libs = [
187-
'x64' => 'x86_64',
188-
'arm64' => 'aarch64',
189-
];
190-
191198
$isMusl = false;
192199
if (is_executable('/usr/bin/ldd') || is_executable('/bin/ldd')) {
193200
$ldd = shell_exec('ldd --version 2>&1');
@@ -196,9 +203,9 @@ public static function getBinaryName(string $version): string
196203
}
197204
}
198205

199-
return "tailwindcss-{$system}-{$arch}".($isMusl ? '-musl' : '');
206+
return "{$system}-{$arch}".($isMusl ? '-musl' : '');
200207
}
201208

202-
return "tailwindcss-{$system}-{$arch}".(('windows' === $system) ? '.exe' : '');
209+
return "{$system}-{$arch}";
203210
}
204211
}

src/TailwindBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(
3434
private readonly ?string $binaryVersion = null,
3535
private readonly string $configPath = 'tailwind.config.js',
3636
private readonly ?string $postCssConfigPath = null,
37+
private readonly string $binaryPlatform = 'auto',
3738
) {
3839
$paths = [];
3940
foreach ($inputPaths as $inputPath) {
@@ -155,7 +156,7 @@ public function getOutputCssContent(string $inputFile): string
155156

156157
public function createBinary(): TailwindBinary
157158
{
158-
return $this->binary ??= new TailwindBinary($this->tailwindVarDir, $this->projectRootDir, $this->binaryPath, $this->binaryVersion, $this->output);
159+
return $this->binary ??= new TailwindBinary($this->tailwindVarDir, $this->projectRootDir, $this->binaryPath, $this->binaryVersion, $this->output, null, $this->binaryPlatform);
159160
}
160161

161162
private function validateInputFile(string $inputPath): string

tests/TailwindBinaryTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
class TailwindBinaryTest extends TestCase
2020
{
21-
public function testBinaryIsDownloadedAndProcessCreated()
21+
/**
22+
* @dataProvider platformAndVersionProvider
23+
*/
24+
public function testBinaryIsDownloadedAndProcessCreated(string $version, string $platform, string $expectedBinaryName)
2225
{
2326
$binaryDownloadDir = __DIR__.'/fixtures/download';
2427
$fs = new Filesystem();
@@ -31,9 +34,9 @@ public function testBinaryIsDownloadedAndProcessCreated()
3134
new MockResponse('fake binary contents'),
3235
]);
3336

34-
$binary = new TailwindBinary($binaryDownloadDir, __DIR__, null, 'fake-version', null, $client);
37+
$binary = new TailwindBinary($binaryDownloadDir, __DIR__, null, 'fake-version', null, $client, $platform);
3538
$process = $binary->createProcess(['-i', 'fake.css']);
36-
$binaryFile = $binaryDownloadDir.'/fake-version/'.TailwindBinary::getBinaryName('4.0.0');
39+
$binaryFile = $binaryDownloadDir.'/fake-version/'.$expectedBinaryName;
3740
$this->assertFileExists($binaryFile);
3841

3942
$this->assertSame(
@@ -86,4 +89,21 @@ public function testCustomBinaryUsed()
8689
$process->getCommandLine()
8790
);
8891
}
92+
93+
public function platformAndVersionProvider(): iterable
94+
{
95+
yield ['3.4.17', 'linux-arm64', 'tailwindcss-linux-arm64'];
96+
yield ['3.4.17', 'linux-armv7', 'tailwindcss-linux-armv7'];
97+
yield ['3.4.17', 'linux-x64', 'tailwindcss-linux-x64'];
98+
yield ['3.4.17', 'macos-arm64', 'tailwindcss-macos-arm64'];
99+
yield ['3.4.17', 'macos-x64', 'tailwindcss-macos-x64'];
100+
yield ['3.4.17', 'windows-x64', 'tailwindcss-windows-x64.exe'];
101+
yield ['4.0.0', 'linux-arm64', 'tailwindcss-linux-arm64'];
102+
yield ['4.0.0', 'linux-arm64-musl', 'tailwindcss-linux-arm64-musl'];
103+
yield ['4.0.0', 'linux-x64', 'tailwindcss-linux-x64'];
104+
yield ['4.0.0', 'linux-x64-musl', 'tailwindcss-linux-x64-musl'];
105+
yield ['4.0.0', 'macos-arm64', 'tailwindcss-macos-arm64'];
106+
yield ['4.0.0', 'macos-x64', 'tailwindcss-macos-x64'];
107+
yield ['4.0.0', 'windows-x64', 'tailwindcss-windows-x64.exe'];
108+
}
89109
}

0 commit comments

Comments
 (0)