Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to configure binary version and default to latest version #37

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
abstract_arg('path to source Tailwind CSS file'),
param('kernel.project_dir').'/var/tailwind',
abstract_arg('path to tailwind binary'),
abstract_arg('tailwind binary version')
])

->set('tailwind.command.build', TailwindBuildCommand::class)
Expand Down
13 changes: 13 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,16 @@ To instruct the bundle to use that binary instead, set the ``binary`` option:
# config/packages/symfonycasts_tailwind.yaml
symfonycasts_tailwind:
binary: 'node_modules/.bin/tailwindcss'

Using a Different Version
------------------------

By default the latest standalone Tailwind binary gets downloaded. However,
if you want to use a different version, you can specify the version to use,
set ``binary_version`` option:

.. code-block:: yaml

# config/packages/symfonycasts_tailwind.yaml
symfonycasts_tailwind:
binary_version: 'v3.3.0'
4 changes: 4 additions & 0 deletions src/DependencyInjection/TailwindExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container->findDefinition('tailwind.builder')
->replaceArgument(1, $config['input_css'])
->replaceArgument(3, $config['binary'])
->replaceArgument(4, $config['binary_version'])
;
}

Expand Down Expand Up @@ -59,6 +60,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('The tailwind binary to use instead of downloading a new one')
->defaultNull()
->end()
->scalarNode('binary_version')
->info('Tailwind CLI version to download - null means latest version')
->defaultNull()
->end();

return $treeBuilder;
Expand Down
27 changes: 25 additions & 2 deletions src/TailwindBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
*/
class TailwindBinary
{
private const VERSION = 'v3.3.5';
private HttpClientInterface $httpClient;

public function __construct(
private string $binaryDownloadDir,
private string $cwd,
private ?string $binaryPath,
private ?string $binaryVersion,
private ?SymfonyStyle $output = null,
HttpClientInterface $httpClient = null,
) {
Expand All @@ -53,7 +53,7 @@ public function createProcess(array $arguments = []): Process

private function downloadExecutable(): void
{
$url = sprintf('https://github.com/tailwindlabs/tailwindcss/releases/download/%s/%s', self::VERSION, self::getBinaryName());
$url = sprintf('https://github.com/tailwindlabs/tailwindcss/releases/download/%s/%s', $this->getVersion(), self::getBinaryName());

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

Expand All @@ -78,6 +78,14 @@ private function downloadExecutable(): void
$progressBar?->setProgress($dlNow);
},
]);

if (404 === $response->getStatusCode()) {
if (null !== $this->binaryVersion) {
throw new \Exception(sprintf('Cannot download Tailwind CLI binary. Please verify configured version `%s` exists for your machine.', $this->binaryVersion));
}
throw new \Exception(sprintf('Cannot download latest Tailwind CLI binary. Response code: %d', $response->getStatusCode()));
}

$fileHandler = fopen($targetPath, 'w');
foreach ($this->httpClient->stream($response) as $chunk) {
fwrite($fileHandler, $chunk->getContent());
Expand All @@ -89,6 +97,21 @@ private function downloadExecutable(): void
chmod($targetPath, 0777);
}

private function getVersion(): string
{
if ($this->binaryVersion) {
return $this->binaryVersion;
}

try {
$response = $this->httpClient->request('GET', 'https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest');

return $response->toArray()['name'] ?? throw new \Exception('Cannot get vesion from response JSON.');
} catch (\Throwable $e) {
throw new \Exception('Cannot determine latest Tailwind CLI binary version. Please specify a version in the configuration.', previous: $e);
}
}

/**
* @internal
*/
Expand Down
3 changes: 2 additions & 1 deletion src/TailwindBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(
string $inputPath,
private readonly string $tailwindVarDir,
private readonly ?string $binaryPath = null,
private readonly ?string $binaryVersion = null,
) {
if (is_file($inputPath)) {
$this->inputPath = $inputPath;
Expand Down Expand Up @@ -115,6 +116,6 @@ public function getOutputCssContent(): string

private function createBinary(): TailwindBinary
{
return new TailwindBinary($this->tailwindVarDir, $this->projectRootDir, $this->binaryPath, $this->output);
return new TailwindBinary($this->tailwindVarDir, $this->projectRootDir, $this->binaryPath, $this->binaryVersion, $this->output);
}
}
5 changes: 3 additions & 2 deletions tests/TailwindBinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public function testBinaryIsDownloadedAndProcessCreated()
$fs->mkdir($binaryDownloadDir);

$client = new MockHttpClient([
new MockResponse('{"name":"v3.3.6"}'),
new MockResponse('fake binary contents'),
]);

$binary = new TailwindBinary($binaryDownloadDir, __DIR__, null, null, $client);
$binary = new TailwindBinary($binaryDownloadDir, __DIR__, null, null, null, $client);
$process = $binary->createProcess(['-i', 'fake.css']);
$this->assertFileExists($binaryDownloadDir.'/'.TailwindBinary::getBinaryName());

Expand All @@ -47,7 +48,7 @@ public function testCustomBinaryUsed()
{
$client = new MockHttpClient();

$binary = new TailwindBinary('', __DIR__, 'custom-binary', null, $client);
$binary = new TailwindBinary('', __DIR__, 'custom-binary', null, null, $client);
$process = $binary->createProcess(['-i', 'fake.css']);
// on windows, arguments are not wrapped in quotes
$expected = '\\' === \DIRECTORY_SEPARATOR ? 'custom-binary -i fake.css' : "'custom-binary' '-i' 'fake.css'";
Expand Down