-
Notifications
You must be signed in to change notification settings - Fork 22
Add binary version configuration #42
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
base: main
Are you sure you want to change the base?
Changes from all commits
5621b5d
771b3e3
d6a74c5
8403495
0e958fc
cda8510
5b522c0
f92e9d8
f9f8acc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,13 @@ | |
|
||
class SassBinary | ||
{ | ||
private const VERSION = '1.69.7'; | ||
private HttpClientInterface $httpClient; | ||
private ?string $cachedVersion = null; | ||
|
||
public function __construct( | ||
private string $binaryDownloadDir, | ||
private ?string $binaryPath = null, | ||
private ?string $binaryVersion = null, | ||
bocharsky-bw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private ?SymfonyStyle $output = null, | ||
HttpClientInterface $httpClient = null | ||
) { | ||
|
@@ -34,7 +35,7 @@ public function __construct( | |
public function createProcess(array $args): Process | ||
{ | ||
if (null === $this->binaryPath) { | ||
$binary = $this->getDefaultBinaryPath(); | ||
$binary = $this->getDefaultBinaryPath($this->getVersion()); | ||
if (!is_file($binary)) { | ||
$this->downloadExecutable(); | ||
} | ||
|
@@ -49,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', self::VERSION, $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); | ||
|
@@ -75,6 +76,13 @@ public function downloadExecutable(): void | |
}, | ||
]); | ||
|
||
if (404 === $response->getStatusCode()) { | ||
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())); | ||
} | ||
|
||
$fileHandler = fopen($targetPath, 'w'); | ||
foreach ($this->httpClient->stream($response) as $chunk) { | ||
fwrite($fileHandler, $chunk->getContent()); | ||
|
@@ -86,27 +94,30 @@ public function downloadExecutable(): void | |
|
||
if ($isZip) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
the main problem of the failing test also the zip file has a subdirectory called i also tried to run the tests on my alpine system, it is quite strange as they also fail. on linux my not sure if the code is not that stable or my linux bugging. i am also confused why this wasn't a problem before, |
||
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); | ||
$archive->extractTo($this->binaryDownloadDir); | ||
$archive->extractTo($this->binaryDownloadDir.'/dart-sass'); | ||
$archive->close(); | ||
unlink($targetPath); | ||
|
||
return; | ||
} 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)); | ||
} | ||
|
||
unlink($targetPath); | ||
|
||
$binaryPath = $this->getDefaultBinaryPath(); | ||
// Rename the extracted directory to its version | ||
rename($this->binaryDownloadDir.'/dart-sass/dart-sass', $this->binaryDownloadDir.'/dart-sass/'.$this->getVersion()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is never reached for windows (zip download) |
||
|
||
$binaryPath = $this->getDefaultBinaryPath($this->getVersion()); | ||
if (!is_file($binaryPath)) { | ||
throw new \Exception(sprintf('Could not find downloaded binary in "%s".', $binaryPath)); | ||
} | ||
|
@@ -156,11 +167,27 @@ 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->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 getDefaultBinaryPath(): string | ||
private function getLatestVersion(): string | ||
{ | ||
return $this->binaryDownloadDir.'/dart-sass/sass'; | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have no error handling for rate limits in here, in my tests i get a
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ public function __construct( | |
private readonly string $cssPath, | ||
private readonly string $projectRootDir, | ||
private readonly ?string $binaryPath, | ||
private readonly ?string $binaryVersion, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm, this will lead to BC breaks. What about to move this to the end instead? |
||
bool|array $sassOptions = [], | ||
) { | ||
if (\is_bool($sassOptions)) { | ||
|
@@ -174,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); | ||
} | ||
|
||
/** | ||
|
Uh oh!
There was an error while loading. Please reload this page.