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 version configuration #38

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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'),
fracsi marked this conversation as resolved.
Show resolved Hide resolved
])

->set('tailwind.command.build', TailwindBuildCommand::class)
Expand Down
12 changes: 12 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,15 @@ 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
fracsi marked this conversation as resolved.
Show resolved Hide resolved
------------------------

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'
5 changes: 5 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,10 @@ 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')
fracsi marked this conversation as resolved.
Show resolved Hide resolved
->defaultNull()
->end()
->end();

return $treeBuilder;
Expand Down
24 changes: 15 additions & 9 deletions src/TailwindBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
*/
class TailwindBinary
{
private const DEFAULT_VERSION = 'v3.3.5';
private HttpClientInterface $httpClient;
private ?string $cachedVersion = null;

public function __construct(
private string $binaryDownloadDir,
private string $cwd,
private ?string $binaryPath,
private ?string $binaryVersion,
private ?SymfonyStyle $output = null,
HttpClientInterface $httpClient = null,
) {
Expand All @@ -37,7 +38,7 @@ public function __construct(
public function createProcess(array $arguments = []): Process
{
if (null === $this->binaryPath) {
$binary = $this->binaryDownloadDir.'/'.self::getBinaryName();
$binary = $this->binaryDownloadDir.'/'.$this->getVersion().'/'.self::getBinaryName();
if (!is_file($binary)) {
$this->downloadExecutable();
}
Expand All @@ -53,15 +54,15 @@ public function createProcess(array $arguments = []): Process

private function downloadExecutable(): void
{
$url = sprintf('https://github.com/tailwindlabs/tailwindcss/releases/download/%s/%s', $this->getLatestVersion(), 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));

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

$targetPath = $this->binaryDownloadDir.'/'.self::getBinaryName();
$targetPath = $this->binaryDownloadDir.'/'.$this->getVersion().'/'.self::getBinaryName();
$progressBar = null;

$response = $this->httpClient->request('GET', $url, [
Expand Down Expand Up @@ -89,14 +90,19 @@ private function downloadExecutable(): void
chmod($targetPath, 0777);
}

private function getVersion(): string
{
return $this->cachedVersion ??= $this->binaryVersion ?? $this->getLatestVersion();
}

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

return $response->toArray()['name'] ?? self::DEFAULT_VERSION;
} catch (\Throwable) {
return self::DEFAULT_VERSION;
return $response->toArray()['name'] ?? throw new \Exception('Cannot get version name from response JSON.');
fracsi marked this conversation as resolved.
Show resolved Hide resolved
} catch (\Throwable $e) {
throw new \Exception('Cannot determine latest Tailwind CLI binary version. Please specify a version in the configuration.', previous: $e);
}
}

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this will introduce a possible BC break, though not sure if we can neglect it and mention in the release, it's convenient to have those 2 args close to each other

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TailwindBinary is basically internal,, so mentioning it in the release is OK i guess.

}
}
9 changes: 4 additions & 5 deletions tests/TailwindBinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ public function testBinaryIsDownloadedAndProcessCreated()
$fs->mkdir($binaryDownloadDir);

$client = new MockHttpClient([
new MockResponse('{}'),
new MockResponse('fake binary contents'),
]);

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

// Windows doesn't wrap arguments in quotes
$expectedTemplate = '\\' === \DIRECTORY_SEPARATOR ? '"%s" -i fake.css' : "'%s' '-i' 'fake.css'";

$this->assertSame(
sprintf($expectedTemplate, $binaryDownloadDir.'/'.TailwindBinary::getBinaryName()),
sprintf($expectedTemplate, $binaryDownloadDir.'/fake-version/'.TailwindBinary::getBinaryName()),
$process->getCommandLine()
);
}
Expand All @@ -48,7 +47,7 @@ public function testCustomBinaryUsed()
{
$client = new MockHttpClient();

$binary = new TailwindBinary('', __DIR__, 'custom-binary', null, $client);
$binary = new TailwindBinary('', __DIR__, 'custom-binary', null, 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
7 changes: 3 additions & 4 deletions tests/TailwindBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ protected function setUp(): void

protected function tearDown(): void
{
$fs = new Filesystem();
$finder = new Finder();
$finder->in(__DIR__.'/fixtures/var/tailwind')->files();
foreach ($finder as $file) {
unlink($file->getRealPath());
}
$finder->in(__DIR__.'/fixtures/var/tailwind');
$fs->remove($finder);
}

public function testIntegrationWithDefaultOptions(): void
Expand Down