From 5621b5d8d25479a263239646250919d4ac7a7edf Mon Sep 17 00:00:00 2001 From: Simone Alers Date: Sat, 9 Dec 2023 20:48:29 +0100 Subject: [PATCH 1/7] Add binary version configuration --- config/services.php | 1 + doc/index.rst | 13 ++++++ .../SymfonycastsSassExtension.php | 7 +++- src/SassBinary.php | 15 +++++-- src/SassBuilder.php | 3 +- tests/SassBuilderTest.php | 1 + tests/VersionTest.php | 40 +++++++++++++++++++ 7 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 tests/VersionTest.php diff --git a/config/services.php b/config/services.php index 3d0e914..fa6b1b0 100644 --- a/config/services.php +++ b/config/services.php @@ -19,6 +19,7 @@ abstract_arg('path to css directory'), param('kernel.project_dir'), abstract_arg('path to binary'), + abstract_arg('path to binary version'), abstract_arg('embed sourcemap'), ]) diff --git a/doc/index.rst b/doc/index.rst index ac9c515..1e13c7e 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -142,6 +142,17 @@ To see the full config from this bundle, run: The main option is ``root_sass`` option, which defaults to ``assets/styles/app.scss``. This represents the source Sass file. +Using a different version +-------------------------- +This bundle installs for you a default version. However, if you want an explicit version of Dart Sass you can instruct the bundle to download that version, set the ``version`` option: + +.. code-block:: yaml + + symfonycasts_sass: + version: 1.69.0 + +When you change this version, it will not be upgraded automatically. Remove the `var/dart-sass` directory first to rebuild with the configured version. + Using a different binary -------------------------- @@ -151,3 +162,5 @@ This bundle already installed for you the right binary. However, if you already symfonycasts_sass: binary: 'node_modules/.bin/sass' + +This configuration overrides any specific version configuration. \ No newline at end of file diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index c07d9a4..ddcfbbb 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -31,7 +31,8 @@ public function load(array $configs, ContainerBuilder $container): void ->replaceArgument(0, $config['root_sass']) ->replaceArgument(1, '%kernel.project_dir%/var/sass') ->replaceArgument(3, $config['binary']) - ->replaceArgument(4, $config['embed_sourcemap']) + ->replaceArgument(4, $config['version']) + ->replaceArgument(5, $config['embed_sourcemap']) ; $container->findDefinition('sass.css_asset_compiler') @@ -81,6 +82,10 @@ public function getConfigTreeBuilder(): TreeBuilder ->info('The Sass binary to use') ->defaultNull() ->end() + ->scalarNode('version') + ->info('The Sass binary version to download') + ->defaultNull() + ->end() ->scalarNode('embed_sourcemap') ->info('Whether to embed the sourcemap in the compiled CSS. By default, enabled only when debug mode is on.') ->defaultValue('%kernel.debug%') diff --git a/src/SassBinary.php b/src/SassBinary.php index 22410cb..6696b32 100644 --- a/src/SassBinary.php +++ b/src/SassBinary.php @@ -16,16 +16,18 @@ class SassBinary { - private const VERSION = '1.63.6'; + private const DEFAULT_VERSION = '1.63.6'; private HttpClientInterface $httpClient; public function __construct( private string $binaryDownloadDir, private ?string $binaryPath = null, + private ?string $binaryVersion = null, private ?SymfonyStyle $output = null, HttpClientInterface $httpClient = null ) { $this->httpClient = $httpClient ?? HttpClient::create(); + $this->binaryVersion ??= self::DEFAULT_VERSION; } /** @@ -49,7 +51,7 @@ public function createProcess(array $args): Process public function downloadExecutable(): void { - $url = sprintf('https://github.com/sass/dart-sass/releases/download/%s/%s', self::VERSION, $this->getBinaryName()); + $url = sprintf('https://github.com/sass/dart-sass/releases/download/%s/%s', $this->binaryVersion, $this->getBinaryName()); $isZip = str_ends_with($url, '.zip'); $this->output?->note('Downloading Sass binary from '.$url); @@ -75,6 +77,13 @@ public function downloadExecutable(): void }, ]); + if (404 === $response->getStatusCode()) { + if (self::DEFAULT_VERSION !== $this->binaryVersion) { + throw new \Exception(sprintf('Cannot download sass binary. Please verify version `%s` exists for your machine.', $this->binaryVersion)); + } + throw new \Exception(sprintf('Cannot download sass binary. Response code: %d', $response->getStatusCode())); + } + $fileHandler = fopen($targetPath, 'w'); foreach ($this->httpClient->stream($response) as $chunk) { fwrite($fileHandler, $chunk->getContent()); @@ -155,7 +164,7 @@ public function getBinaryName(): string private function buildBinaryFileName(string $os, bool $isWindows = false): string { - return 'dart-sass-'.self::VERSION.'-'.$os.($isWindows ? '.zip' : '.tar.gz'); + return 'dart-sass-'.$this->binaryVersion.'-'.$os.($isWindows ? '.zip' : '.tar.gz'); } private function getDefaultBinaryPath(): string diff --git a/src/SassBuilder.php b/src/SassBuilder.php index c200a9c..2afbd2d 100644 --- a/src/SassBuilder.php +++ b/src/SassBuilder.php @@ -25,6 +25,7 @@ public function __construct( private readonly string $cssPath, private readonly string $projectRootDir, private readonly ?string $binaryPath, + private readonly ?string $binaryVersion, private readonly bool $embedSourcemap, ) { } @@ -99,6 +100,6 @@ public static function guessCssNameFromSassFile(string $sassFile, string $output private function createBinary(): SassBinary { - return new SassBinary($this->projectRootDir.'/var', $this->binaryPath, $this->output); + return new SassBinary($this->projectRootDir.'/var', $this->binaryPath, $this->binaryVersion, $this->output); } } diff --git a/tests/SassBuilderTest.php b/tests/SassBuilderTest.php index a651d96..fe29c00 100644 --- a/tests/SassBuilderTest.php +++ b/tests/SassBuilderTest.php @@ -27,6 +27,7 @@ public function testIntegration(): void __DIR__.'/fixtures/assets', __DIR__.'/fixtures', null, + null, false ); diff --git a/tests/VersionTest.php b/tests/VersionTest.php new file mode 100644 index 0000000..5f3d5e8 --- /dev/null +++ b/tests/VersionTest.php @@ -0,0 +1,40 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfonycasts\SassBundle\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Process\Process; +use Symfonycasts\SassBundle\SassBinary; + +class VersionTest extends TestCase +{ + protected function tearDown(): void + { + if (file_exists(__DIR__.'/fixtures/var')) { + $filesystem = new Filesystem(); + $filesystem->remove(__DIR__.'/fixtures/var'); + } + } + + public function testVersionDownloaded(): void + { + $testedVersion = '1.69.5'; // This should differ from the SassBinary::DEFAULT_VERSION constant + $binary = new SassBinary(__DIR__.'/fixtures/var/version', null, $testedVersion); + + $binary->downloadExecutable(); + $sassVersionProcess = new Process([__DIR__.'/fixtures/var/version/dart-sass/sass', '--version']); + $sassVersionProcess->run(); + + $this->assertSame(trim($sassVersionProcess->getOutput(), \PHP_EOL), $testedVersion); + } +} From 771b3e3dd55d440cb6b0b3ee81adf576a7b40ce1 Mon Sep 17 00:00:00 2001 From: Simone Alers Date: Fri, 15 Dec 2023 10:52:56 +0100 Subject: [PATCH 2/7] Improve naming for binary version configuration --- config/services.php | 2 +- doc/index.rst | 6 ++---- src/DependencyInjection/SymfonycastsSassExtension.php | 7 ++++--- src/SassBinary.php | 11 +++++------ src/SassBuilder.php | 2 +- tests/SassBuilderTest.php | 5 +++-- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/config/services.php b/config/services.php index fa6b1b0..662997a 100644 --- a/config/services.php +++ b/config/services.php @@ -19,7 +19,7 @@ abstract_arg('path to css directory'), param('kernel.project_dir'), abstract_arg('path to binary'), - abstract_arg('path to binary version'), + abstract_arg('binary version'), abstract_arg('embed sourcemap'), ]) diff --git a/doc/index.rst b/doc/index.rst index 1e13c7e..6c902a4 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -144,12 +144,12 @@ The main option is ``root_sass`` option, which defaults to ``assets/styles/app.s Using a different version -------------------------- -This bundle installs for you a default version. However, if you want an explicit version of Dart Sass you can instruct the bundle to download that version, set the ``version`` option: +This bundle installs for you a default version. However, if you want an explicit version of Dart Sass you can instruct the bundle to download that version, set the ``binary_version`` option: .. code-block:: yaml symfonycasts_sass: - version: 1.69.0 + binary_version: 1.69.0 When you change this version, it will not be upgraded automatically. Remove the `var/dart-sass` directory first to rebuild with the configured version. @@ -162,5 +162,3 @@ This bundle already installed for you the right binary. However, if you already symfonycasts_sass: binary: 'node_modules/.bin/sass' - -This configuration overrides any specific version configuration. \ No newline at end of file diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index ddcfbbb..cad1ab6 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -16,6 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; +use Symfonycasts\SassBundle\SassBinary; class SymfonycastsSassExtension extends Extension implements ConfigurationInterface { @@ -31,7 +32,7 @@ public function load(array $configs, ContainerBuilder $container): void ->replaceArgument(0, $config['root_sass']) ->replaceArgument(1, '%kernel.project_dir%/var/sass') ->replaceArgument(3, $config['binary']) - ->replaceArgument(4, $config['version']) + ->replaceArgument(4, $config['binary_version']) ->replaceArgument(5, $config['embed_sourcemap']) ; @@ -82,9 +83,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->info('The Sass binary to use') ->defaultNull() ->end() - ->scalarNode('version') + ->scalarNode('binary_version') ->info('The Sass binary version to download') - ->defaultNull() + ->defaultValue(SassBinary::DEFAULT_VERSION) ->end() ->scalarNode('embed_sourcemap') ->info('Whether to embed the sourcemap in the compiled CSS. By default, enabled only when debug mode is on.') diff --git a/src/SassBinary.php b/src/SassBinary.php index 6696b32..be18efc 100644 --- a/src/SassBinary.php +++ b/src/SassBinary.php @@ -16,18 +16,17 @@ class SassBinary { - private const DEFAULT_VERSION = '1.63.6'; + public const DEFAULT_VERSION = '1.63.6'; private HttpClientInterface $httpClient; public function __construct( private string $binaryDownloadDir, private ?string $binaryPath = null, - private ?string $binaryVersion = null, + private ?string $binaryVersion = self::DEFAULT_VERSION, private ?SymfonyStyle $output = null, HttpClientInterface $httpClient = null ) { $this->httpClient = $httpClient ?? HttpClient::create(); - $this->binaryVersion ??= self::DEFAULT_VERSION; } /** @@ -79,9 +78,9 @@ public function downloadExecutable(): void if (404 === $response->getStatusCode()) { if (self::DEFAULT_VERSION !== $this->binaryVersion) { - throw new \Exception(sprintf('Cannot download sass binary. Please verify version `%s` exists for your machine.', $this->binaryVersion)); + throw new \Exception(sprintf('Cannot download Sass binary. Please verify version `%s` exists for your machine.', $this->binaryVersion)); } - throw new \Exception(sprintf('Cannot download sass binary. Response code: %d', $response->getStatusCode())); + throw new \Exception(sprintf('Cannot download Sass binary. Response code: %d', $response->getStatusCode())); } $fileHandler = fopen($targetPath, 'w'); @@ -95,7 +94,7 @@ public function downloadExecutable(): void if ($isZip) { if (!\extension_loaded('zip')) { - throw new \Exception('Cannot unzip the downloaded sass binary. Please install the "zip" PHP extension.'); + throw new \Exception('Cannot unzip the downloaded Sass binary. Please install the "zip" PHP extension.'); } $archive = new \ZipArchive(); $archive->open($targetPath); diff --git a/src/SassBuilder.php b/src/SassBuilder.php index 2afbd2d..0663b54 100644 --- a/src/SassBuilder.php +++ b/src/SassBuilder.php @@ -25,8 +25,8 @@ public function __construct( private readonly string $cssPath, private readonly string $projectRootDir, private readonly ?string $binaryPath, - private readonly ?string $binaryVersion, private readonly bool $embedSourcemap, + private readonly ?string $binaryVersion, ) { } diff --git a/tests/SassBuilderTest.php b/tests/SassBuilderTest.php index fe29c00..68deac2 100644 --- a/tests/SassBuilderTest.php +++ b/tests/SassBuilderTest.php @@ -10,6 +10,7 @@ namespace Symfonycasts\SassBundle\Tests; use PHPUnit\Framework\TestCase; +use Symfonycasts\SassBundle\SassBinary; use Symfonycasts\SassBundle\SassBuilder; class SassBuilderTest extends TestCase @@ -27,8 +28,8 @@ public function testIntegration(): void __DIR__.'/fixtures/assets', __DIR__.'/fixtures', null, - null, - false + false, + SassBinary::DEFAULT_VERSION ); $process = $builder->runBuild(false); From d6a74c5a38986387d737cc0497b4c9dfeca46b1b Mon Sep 17 00:00:00 2001 From: Simone Alers Date: Fri, 15 Dec 2023 10:54:34 +0100 Subject: [PATCH 3/7] Save dart binary in version directory, move version test --- doc/index.rst | 2 -- src/SassBinary.php | 15 +++++++++------ tests/FunctionalTest.php | 19 +++++++++++++++++++ tests/VersionTest.php | 40 ---------------------------------------- 4 files changed, 28 insertions(+), 48 deletions(-) delete mode 100644 tests/VersionTest.php diff --git a/doc/index.rst b/doc/index.rst index 6c902a4..bf5e0b1 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -151,8 +151,6 @@ This bundle installs for you a default version. However, if you want an explicit symfonycasts_sass: binary_version: 1.69.0 -When you change this version, it will not be upgraded automatically. Remove the `var/dart-sass` directory first to rebuild with the configured version. - Using a different binary -------------------------- diff --git a/src/SassBinary.php b/src/SassBinary.php index be18efc..b06dc41 100644 --- a/src/SassBinary.php +++ b/src/SassBinary.php @@ -35,7 +35,7 @@ public function __construct( public function createProcess(array $args): Process { if (null === $this->binaryPath) { - $binary = $this->getDefaultBinaryPath(); + $binary = $this->getDefaultBinaryPath($this->binaryVersion); if (!is_file($binary)) { $this->downloadExecutable(); } @@ -98,7 +98,7 @@ public function downloadExecutable(): void } $archive = new \ZipArchive(); $archive->open($targetPath); - $archive->extractTo($this->binaryDownloadDir); + $archive->extractTo($this->binaryDownloadDir.'/dart-sass'); $archive->close(); unlink($targetPath); @@ -106,7 +106,7 @@ public function downloadExecutable(): void } else { $archive = new \PharData($targetPath); $archive->decompress(); - $archive->extractTo($this->binaryDownloadDir); + $archive->extractTo($this->binaryDownloadDir.'/dart-sass'); // delete the .tar (the .tar.gz is deleted below) unlink(substr($targetPath, 0, -3)); @@ -114,7 +114,10 @@ public function downloadExecutable(): void unlink($targetPath); - $binaryPath = $this->getDefaultBinaryPath(); + // Rename the extracted directory to its version + rename($this->binaryDownloadDir.'/dart-sass/dart-sass', $this->binaryDownloadDir.'/dart-sass/'.$this->binaryVersion); + + $binaryPath = $this->getDefaultBinaryPath($this->binaryVersion); if (!is_file($binaryPath)) { throw new \Exception(sprintf('Could not find downloaded binary in "%s".', $binaryPath)); } @@ -166,8 +169,8 @@ private function buildBinaryFileName(string $os, bool $isWindows = false): strin return 'dart-sass-'.$this->binaryVersion.'-'.$os.($isWindows ? '.zip' : '.tar.gz'); } - private function getDefaultBinaryPath(): string + private function getDefaultBinaryPath(string $version): string { - return $this->binaryDownloadDir.'/dart-sass/sass'; + return $this->binaryDownloadDir.'/dart-sass/'.$version.'/sass'; } } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 8f17645..9216e49 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -13,6 +13,8 @@ use Symfony\Component\AssetMapper\AssetMapperInterface; use Symfony\Component\AssetMapper\MappedAsset; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Process\Process; +use Symfonycasts\SassBundle\SassBinary; class FunctionalTest extends KernelTestCase { @@ -34,6 +36,10 @@ protected function setUp(): void protected function tearDown(): void { unlink(__DIR__.'/fixtures/assets/app.css'); + if (file_exists(__DIR__.'/fixtures/var')) { + $filesystem = new Filesystem(); + $filesystem->remove(__DIR__.'/fixtures/var'); + } } public function testBuildCssIfUsed(): void @@ -53,4 +59,17 @@ public function testBuildCssIfUsed(): void $this->assertStringContainsString('color: red', $asset->content); } } + + public function testVersionDownloaded(): void + { + $testedVersion = '1.69.5'; // This should differ from the SassBinary::DEFAULT_VERSION constant + $binary = new SassBinary(binaryDownloadDir: __DIR__.'/fixtures/var/version', binaryVersion: $testedVersion); + + $binary->downloadExecutable(); + $this->assertDirectoryExists(__DIR__.'/fixtures/var/version/dart-sass/1.69.5'); + + $sassVersionProcess = new Process([__DIR__.'/fixtures/var/version/dart-sass/1.69.5/sass', '--version']); + $sassVersionProcess->run(); + $this->assertSame(trim($sassVersionProcess->getOutput(), \PHP_EOL), $testedVersion); + } } diff --git a/tests/VersionTest.php b/tests/VersionTest.php deleted file mode 100644 index 5f3d5e8..0000000 --- a/tests/VersionTest.php +++ /dev/null @@ -1,40 +0,0 @@ - - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfonycasts\SassBundle\Tests; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Process\Process; -use Symfonycasts\SassBundle\SassBinary; - -class VersionTest extends TestCase -{ - protected function tearDown(): void - { - if (file_exists(__DIR__.'/fixtures/var')) { - $filesystem = new Filesystem(); - $filesystem->remove(__DIR__.'/fixtures/var'); - } - } - - public function testVersionDownloaded(): void - { - $testedVersion = '1.69.5'; // This should differ from the SassBinary::DEFAULT_VERSION constant - $binary = new SassBinary(__DIR__.'/fixtures/var/version', null, $testedVersion); - - $binary->downloadExecutable(); - $sassVersionProcess = new Process([__DIR__.'/fixtures/var/version/dart-sass/sass', '--version']); - $sassVersionProcess->run(); - - $this->assertSame(trim($sassVersionProcess->getOutput(), \PHP_EOL), $testedVersion); - } -} From 84034959ce68df22a1976ee9f2e8827593deb2b5 Mon Sep 17 00:00:00 2001 From: Simone Alers Date: Sat, 6 Jan 2024 14:43:51 +0100 Subject: [PATCH 4/7] Download latest Dart sass binary unless explicitly configured --- .../SymfonycastsSassExtension.php | 9 +++-- src/SassBinary.php | 34 ++++++++++++++----- tests/SassBuilderTest.php | 3 +- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index cad1ab6..7d31eab 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -16,7 +16,6 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; -use Symfonycasts\SassBundle\SassBinary; class SymfonycastsSassExtension extends Extension implements ConfigurationInterface { @@ -32,8 +31,8 @@ public function load(array $configs, ContainerBuilder $container): void ->replaceArgument(0, $config['root_sass']) ->replaceArgument(1, '%kernel.project_dir%/var/sass') ->replaceArgument(3, $config['binary']) - ->replaceArgument(4, $config['binary_version']) - ->replaceArgument(5, $config['embed_sourcemap']) + ->replaceArgument(4, $config['embed_sourcemap']) + ->replaceArgument(5, $config['binary_version']) ; $container->findDefinition('sass.css_asset_compiler') @@ -84,8 +83,8 @@ public function getConfigTreeBuilder(): TreeBuilder ->defaultNull() ->end() ->scalarNode('binary_version') - ->info('The Sass binary version to download') - ->defaultValue(SassBinary::DEFAULT_VERSION) + ->info('The Sass binary version to download - null means the latest version') + ->defaultNull() ->end() ->scalarNode('embed_sourcemap') ->info('Whether to embed the sourcemap in the compiled CSS. By default, enabled only when debug mode is on.') diff --git a/src/SassBinary.php b/src/SassBinary.php index b06dc41..2678e9b 100644 --- a/src/SassBinary.php +++ b/src/SassBinary.php @@ -16,13 +16,13 @@ class SassBinary { - public const DEFAULT_VERSION = '1.63.6'; private HttpClientInterface $httpClient; + private ?string $cachedVersion = null; public function __construct( private string $binaryDownloadDir, private ?string $binaryPath = null, - private ?string $binaryVersion = self::DEFAULT_VERSION, + private ?string $binaryVersion = null, private ?SymfonyStyle $output = null, HttpClientInterface $httpClient = null ) { @@ -35,7 +35,7 @@ public function __construct( public function createProcess(array $args): Process { if (null === $this->binaryPath) { - $binary = $this->getDefaultBinaryPath($this->binaryVersion); + $binary = $this->getDefaultBinaryPath($this->getVersion()); if (!is_file($binary)) { $this->downloadExecutable(); } @@ -50,7 +50,7 @@ public function createProcess(array $args): Process public function downloadExecutable(): void { - $url = sprintf('https://github.com/sass/dart-sass/releases/download/%s/%s', $this->binaryVersion, $this->getBinaryName()); + $url = sprintf('https://github.com/sass/dart-sass/releases/download/%s/%s', $this->getVersion(), $this->getBinaryName()); $isZip = str_ends_with($url, '.zip'); $this->output?->note('Downloading Sass binary from '.$url); @@ -77,8 +77,8 @@ public function downloadExecutable(): void ]); if (404 === $response->getStatusCode()) { - if (self::DEFAULT_VERSION !== $this->binaryVersion) { - throw new \Exception(sprintf('Cannot download Sass binary. Please verify version `%s` exists for your machine.', $this->binaryVersion)); + if ($this->getLatestVersion() !== $this->getVersion()) { + throw new \Exception(sprintf('Cannot download Sass binary. Please verify version `%s` exists for your machine.', $this->getVersion())); } throw new \Exception(sprintf('Cannot download Sass binary. Response code: %d', $response->getStatusCode())); } @@ -115,9 +115,9 @@ public function downloadExecutable(): void unlink($targetPath); // Rename the extracted directory to its version - rename($this->binaryDownloadDir.'/dart-sass/dart-sass', $this->binaryDownloadDir.'/dart-sass/'.$this->binaryVersion); + rename($this->binaryDownloadDir.'/dart-sass/dart-sass', $this->binaryDownloadDir.'/dart-sass/'.$this->getVersion()); - $binaryPath = $this->getDefaultBinaryPath($this->binaryVersion); + $binaryPath = $this->getDefaultBinaryPath($this->getVersion()); if (!is_file($binaryPath)) { throw new \Exception(sprintf('Could not find downloaded binary in "%s".', $binaryPath)); } @@ -166,11 +166,27 @@ public function getBinaryName(): string private function buildBinaryFileName(string $os, bool $isWindows = false): string { - return 'dart-sass-'.$this->binaryVersion.'-'.$os.($isWindows ? '.zip' : '.tar.gz'); + return 'dart-sass-'.$this->getVersion().'-'.$os.($isWindows ? '.zip' : '.tar.gz'); } private function getDefaultBinaryPath(string $version): string { return $this->binaryDownloadDir.'/dart-sass/'.$version.'/sass'; } + + 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/sass/dart-sass/releases/latest'); + + return $response->toArray()['tag_name'] ?? throw new \Exception('Cannot get the latest version name from response JSON.'); + } catch (\Throwable $e) { + throw new \Exception('Cannot determine latest Dart Sass CLI binary version. Please specify a version in the configuration.', previous: $e); + } + } } diff --git a/tests/SassBuilderTest.php b/tests/SassBuilderTest.php index 68deac2..12b6102 100644 --- a/tests/SassBuilderTest.php +++ b/tests/SassBuilderTest.php @@ -10,7 +10,6 @@ namespace Symfonycasts\SassBundle\Tests; use PHPUnit\Framework\TestCase; -use Symfonycasts\SassBundle\SassBinary; use Symfonycasts\SassBundle\SassBuilder; class SassBuilderTest extends TestCase @@ -29,7 +28,7 @@ public function testIntegration(): void __DIR__.'/fixtures', null, false, - SassBinary::DEFAULT_VERSION + null ); $process = $builder->runBuild(false); From cda851062b3131b956f6f42f77ea4d48752d3631 Mon Sep 17 00:00:00 2001 From: Simone Alers Date: Fri, 12 Jan 2024 09:19:51 +0100 Subject: [PATCH 5/7] Update configuration argument order --- src/DependencyInjection/SymfonycastsSassExtension.php | 9 ++++----- tests/FunctionalTest.php | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index 8587f8f..02ac1f2 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -88,6 +88,10 @@ public function getConfigTreeBuilder(): TreeBuilder ->info('The Sass binary to use') ->defaultNull() ->end() + ->scalarNode('binary_version') + ->info('The Sass binary version to download - null means the latest version') + ->defaultNull() + ->end() ->arrayNode('sass_options') ->addDefaultsIfNotSet() ->children() @@ -127,14 +131,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ->end() - ->scalarNode('binary_version') - ->info('The Sass binary version to download - null means the latest version') - ->defaultNull() - ->end() ->booleanNode('embed_sourcemap') ->setDeprecated('symfonycast/sass-bundle', '0.4', 'Option "%node%" at "%path%" is deprecated. Use "sass_options.embed_source_map" instead".') ->defaultNull() - ->end() ->end() ->end() ; diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 9216e49..97f45d5 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -62,7 +62,7 @@ public function testBuildCssIfUsed(): void public function testVersionDownloaded(): void { - $testedVersion = '1.69.5'; // This should differ from the SassBinary::DEFAULT_VERSION constant + $testedVersion = '1.69.5'; // This should differ from the latest version which downloaded by default $binary = new SassBinary(binaryDownloadDir: __DIR__.'/fixtures/var/version', binaryVersion: $testedVersion); $binary->downloadExecutable(); From 5b522c032ff48b88ee2569e87be84c1841eb2420 Mon Sep 17 00:00:00 2001 From: Simone Alers Date: Sat, 13 Jan 2024 10:25:12 +0100 Subject: [PATCH 6/7] Remove unneeded option argument in tests --- tests/SassBuilderTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/SassBuilderTest.php b/tests/SassBuilderTest.php index 643b4d8..c5bb76b 100644 --- a/tests/SassBuilderTest.php +++ b/tests/SassBuilderTest.php @@ -38,8 +38,7 @@ public function testIntegration(): void __DIR__.'/fixtures/assets', __DIR__.'/fixtures', null, - null, - false, + null ); $process = $builder->runBuild(false); @@ -57,8 +56,7 @@ public function testSassDefaultOptions(): void __DIR__.'/fixtures/assets', __DIR__.'/fixtures', null, - null, - false, + null ); $process = $builder->runBuild(false); From f9f8acca3804e42dff0c519115e729ae29511266 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Tue, 20 Feb 2024 12:06:33 +0100 Subject: [PATCH 7/7] Fix bad merging - remove duplicated method --- src/SassBuilder.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/SassBuilder.php b/src/SassBuilder.php index b3b8547..ad2c7ef 100644 --- a/src/SassBuilder.php +++ b/src/SassBuilder.php @@ -106,11 +106,6 @@ public function runBuild(bool $watch): Process return $process; } - private function createBinary(): SassBinary - { - return new SassBinary($this->projectRootDir.'/var', $this->binaryPath, $this->binaryVersion, $this->output); - } - /** * @return array */ @@ -180,7 +175,7 @@ public function setOutput(SymfonyStyle $output): void private function createBinary(): SassBinary { - return new SassBinary($this->projectRootDir.'/var', $this->binaryPath, $this->output); + return new SassBinary($this->projectRootDir.'/var', $this->binaryPath, $this->binaryVersion, $this->output); } /**