|
9 | 9 |
|
10 | 10 | namespace Symfonycasts\TailwindBundle; |
11 | 11 |
|
12 | | -use Psr\Cache\CacheItemInterface; |
13 | 12 | use Symfony\Component\Console\Style\SymfonyStyle; |
14 | 13 | use Symfony\Component\HttpClient\HttpClient; |
15 | 14 | use Symfony\Component\Process\Process; |
16 | | -use Symfony\Contracts\Cache\CacheInterface; |
17 | 15 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
18 | 16 |
|
19 | 17 | /** |
|
23 | 21 | */ |
24 | 22 | class TailwindBinary |
25 | 23 | { |
| 24 | + private const DEFAULT_VERSION = 'v3.4.17'; |
| 25 | + |
26 | 26 | private HttpClientInterface $httpClient; |
27 | | - private ?string $cachedVersion = null; |
28 | 27 |
|
29 | 28 | public function __construct( |
30 | 29 | private string $binaryDownloadDir, |
31 | 30 | private string $cwd, |
32 | 31 | private ?string $binaryPath, |
33 | 32 | private ?string $binaryVersion, |
34 | | - private CacheInterface $cache, |
35 | 33 | private ?SymfonyStyle $output = null, |
36 | 34 | ?HttpClientInterface $httpClient = null, |
37 | 35 | ) { |
38 | 36 | $this->httpClient = $httpClient ?? HttpClient::create(); |
| 37 | + |
| 38 | + if (!$this->binaryVersion && !$this->binaryPath) { |
| 39 | + trigger_deprecation( |
| 40 | + 'symfonycasts/tailwind-bundle', |
| 41 | + '0.8', |
| 42 | + 'Not specifying a "binary" or "binary_version" is deprecated. %s is being used.', |
| 43 | + self::DEFAULT_VERSION, |
| 44 | + ); |
| 45 | + |
| 46 | + $this->binaryVersion = self::DEFAULT_VERSION; |
| 47 | + } |
| 48 | + |
| 49 | + if ($this->binaryVersion && $this->binaryPath) { |
| 50 | + $this->binaryVersion = null; |
| 51 | + } |
39 | 52 | } |
40 | 53 |
|
41 | 54 | public function createProcess(array $arguments = []): Process |
42 | 55 | { |
43 | | - if (null === $this->binaryPath) { |
44 | | - $binary = $this->binaryDownloadDir.'/'.$this->getVersion().'/'.self::getBinaryName(); |
45 | | - if (!is_file($binary)) { |
46 | | - $this->downloadExecutable(); |
47 | | - } |
48 | | - } else { |
49 | | - $binary = $this->binaryPath; |
| 56 | + // add binary to the front of the $arguments array |
| 57 | + array_unshift($arguments, $this->getBinaryPath()); |
| 58 | + |
| 59 | + return new Process($arguments, $this->cwd); |
| 60 | + } |
| 61 | + |
| 62 | + public function getVersion(): string |
| 63 | + { |
| 64 | + if ($this->binaryVersion) { |
| 65 | + return $this->binaryVersion; |
50 | 66 | } |
51 | 67 |
|
52 | | - // add $binary to the front of the $arguments array |
53 | | - array_unshift($arguments, $binary); |
| 68 | + $process = $this->createProcess(['--help']); |
| 69 | + $process->run(); |
54 | 70 |
|
55 | | - return new Process($arguments, $this->cwd); |
| 71 | + if (!$process->isSuccessful()) { |
| 72 | + throw new \RuntimeException('Could not determine the tailwindcss version.'); |
| 73 | + } |
| 74 | + |
| 75 | + if (!preg_match('#(v\d+\.\d+\.\d+)#', $process->getOutput(), $matches)) { |
| 76 | + throw new \RuntimeException('Could not determine the tailwindcss version.'); |
| 77 | + } |
| 78 | + |
| 79 | + return $this->binaryVersion = $matches[1]; |
| 80 | + } |
| 81 | + |
| 82 | + public function isV4(): bool |
| 83 | + { |
| 84 | + return version_compare($this->getRawVersion(), '4.0.0', '>='); |
| 85 | + } |
| 86 | + |
| 87 | + public function getRawVersion(): string |
| 88 | + { |
| 89 | + return ltrim($this->getVersion(), 'v'); |
| 90 | + } |
| 91 | + |
| 92 | + private function getBinaryPath(): string |
| 93 | + { |
| 94 | + if ($this->binaryPath) { |
| 95 | + return $this->binaryPath; |
| 96 | + } |
| 97 | + |
| 98 | + $this->binaryPath = $this->binaryDownloadDir.'/'.$this->getVersion().'/'.self::getBinaryName(); |
| 99 | + |
| 100 | + if (!is_file($this->binaryPath)) { |
| 101 | + $this->downloadExecutable(); |
| 102 | + } |
| 103 | + |
| 104 | + return $this->binaryPath; |
56 | 105 | } |
57 | 106 |
|
58 | 107 | private function downloadExecutable(): void |
@@ -93,25 +142,6 @@ private function downloadExecutable(): void |
93 | 142 | chmod($targetPath, 0777); |
94 | 143 | } |
95 | 144 |
|
96 | | - private function getVersion(): string |
97 | | - { |
98 | | - return $this->cachedVersion ??= $this->binaryVersion ?? $this->getLatestVersion(); |
99 | | - } |
100 | | - |
101 | | - private function getLatestVersion(): string |
102 | | - { |
103 | | - return $this->cache->get('latestVersion', function (CacheItemInterface $item) { |
104 | | - $item->expiresAfter(3600); |
105 | | - try { |
106 | | - $response = $this->httpClient->request('GET', 'https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest'); |
107 | | - |
108 | | - return $response->toArray()['name'] ?? throw new \Exception('Cannot get the latest version name from response JSON.'); |
109 | | - } catch (\Throwable $e) { |
110 | | - throw new \Exception('Cannot determine latest Tailwind CLI binary version. Please specify a version in the configuration.', previous: $e); |
111 | | - } |
112 | | - }); |
113 | | - } |
114 | | - |
115 | 145 | /** |
116 | 146 | * @internal |
117 | 147 | */ |
|
0 commit comments