14
14
use Symfony \Component \Console \Input \InputInterface ;
15
15
use Symfony \Component \Console \Output \OutputInterface ;
16
16
use Symfony \Component \Console \Style \SymfonyStyle ;
17
+ use Symfony \Component \Yaml \Yaml ;
17
18
use Symfonycasts \TailwindBundle \TailwindBuilder ;
19
+ use Symfonycasts \TailwindBundle \TailwindVersionFinder ;
18
20
19
21
#[AsCommand(
20
22
name: 'tailwind:init ' ,
23
25
class TailwindInitCommand extends Command
24
26
{
25
27
public function __construct (
26
- private TailwindBuilder $ tailwindBuilder ,
28
+ private TailwindVersionFinder $ versionFinder ,
29
+ private array $ inputCss ,
30
+ private string $ rootDir ,
27
31
) {
28
32
parent ::__construct ();
29
33
}
30
34
31
- protected function configure (): void
32
- {
33
- }
34
-
35
35
protected function execute (InputInterface $ input , OutputInterface $ output ): int
36
36
{
37
37
$ io = new SymfonyStyle ($ input , $ output );
38
- if (!$ this ->createTailwindConfig ($ io )) {
38
+
39
+ if (!$ input ->isInteractive ()) {
40
+ throw new \RuntimeException ('tailwind:init command must be run interactively. ' );
41
+ }
42
+
43
+ $ bundleConfig = $ this ->bundleConfig ();
44
+
45
+ if ($ io ->confirm ('Are you managing your own Taildind CSS binary? ' , false )) {
46
+ $ binaryPath = $ io ->ask ('Enter the path to your Tailwind CSS binary: ' , 'node_modules/.bin/tailwindcss ' );
47
+ $ bundleConfig ['symfonycasts_tailwind ' ]['binary ' ] = $ binaryPath ;
48
+ } else {
49
+ $ majorVersion = $ io ->ask ('Which major version do you wish to use? ' , '4 ' );
50
+ $ latestVersion = $ this ->versionFinder ->latestVersionFor ($ majorVersion );
51
+ $ bundleConfig ['symfonycasts_tailwind ' ]['binary_version ' ] = $ latestVersion ;
52
+ }
53
+
54
+ file_put_contents ($ this ->bundleConfigFile (), Yaml::dump ($ bundleConfig ));
55
+
56
+ $ builder = new TailwindBuilder (
57
+ $ this ->rootDir ,
58
+ $ this ->inputCss ,
59
+ $ this ->rootDir .'/var/tailwind ' ,
60
+ binaryPath: $ bundleConfig ['symfonycasts_tailwind ' ]['binary ' ] ?? null ,
61
+ binaryVersion: $ bundleConfig ['symfonycasts_tailwind ' ]['binary_version ' ] ?? null ,
62
+ );
63
+
64
+ if (!$ this ->createTailwindConfig ($ io , $ builder )) {
39
65
return self ::FAILURE ;
40
66
}
41
67
42
- $ this ->addTailwindDirectives ($ io );
68
+ $ this ->addTailwindDirectives ($ io, $ builder );
43
69
44
70
$ io ->success ('Tailwind CSS is ready to use! ' );
45
71
46
72
return self ::SUCCESS ;
47
73
}
48
74
49
- private function createTailwindConfig (SymfonyStyle $ io ): bool
75
+ private function createTailwindConfig (SymfonyStyle $ io, TailwindBuilder $ builder ): bool
50
76
{
51
- if ($ this -> tailwindBuilder ->createBinary ()->isV4 ()) {
77
+ if ($ builder ->createBinary ()->isV4 ()) {
52
78
$ io ->note ('Tailwind v4 detected: skipping config file creation. ' );
53
79
54
80
return true ;
55
81
}
56
82
57
- $ configFile = $ this ->tailwindBuilder ->getConfigFilePath ();
83
+ $ configFile = $ builder ->getConfigFilePath ();
84
+
58
85
if (file_exists ($ configFile )) {
59
86
$ io ->note (\sprintf ('Tailwind config file already exists in "%s" ' , $ configFile ));
60
87
61
88
return true ;
62
89
}
63
90
64
- $ this -> tailwindBuilder ->setOutput ($ io );
91
+ $ builder ->setOutput ($ io );
65
92
66
- $ process = $ this -> tailwindBuilder ->runInit ();
93
+ $ process = $ builder ->runInit ();
67
94
$ process ->wait (function ($ type , $ buffer ) use ($ io ) {
68
95
$ io ->write ($ buffer );
69
96
});
@@ -96,9 +123,9 @@ private function createTailwindConfig(SymfonyStyle $io): bool
96
123
return true ;
97
124
}
98
125
99
- private function addTailwindDirectives (SymfonyStyle $ io ): void
126
+ private function addTailwindDirectives (SymfonyStyle $ io, TailwindBuilder $ builder ): void
100
127
{
101
- $ inputFile = $ this -> tailwindBuilder ->getInputCssPaths ()[0 ];
128
+ $ inputFile = $ builder ->getInputCssPaths ()[0 ];
102
129
$ contents = is_file ($ inputFile ) ? file_get_contents ($ inputFile ) : '' ;
103
130
if (str_contains ($ contents , '@tailwind base ' ) || str_contains ($ contents , '@import "tailwindcss" ' )) {
104
131
$ io ->note (\sprintf ('Tailwind directives already exist in "%s" ' , $ inputFile ));
@@ -113,12 +140,30 @@ private function addTailwindDirectives(SymfonyStyle $io): void
113
140
@tailwind utilities;
114
141
EOF ;
115
142
116
- if ($ this -> tailwindBuilder ->createBinary ()->isV4 ()) {
143
+ if ($ builder ->createBinary ()->isV4 ()) {
117
144
$ tailwindDirectives = <<<EOF
118
145
@import "tailwindcss";
119
146
EOF ;
120
147
}
121
148
122
149
file_put_contents ($ inputFile , $ tailwindDirectives ."\n\n" .$ contents );
123
150
}
151
+
152
+ private function bundleConfigFile (): string
153
+ {
154
+ return $ this ->rootDir .'/config/packages/symfonycasts_tailwind.yaml ' ;
155
+ }
156
+
157
+ private function bundleConfig (): array
158
+ {
159
+ if (!class_exists (Yaml::class)) {
160
+ throw new \RuntimeException ('You are using a non-standard Symfony setup. You will need to initialize this bundle manually. ' );
161
+ }
162
+
163
+ if (!file_exists ($ this ->bundleConfigFile ())) {
164
+ return [];
165
+ }
166
+
167
+ return Yaml::parseFile ($ this ->bundleConfigFile ());
168
+ }
124
169
}
0 commit comments