17
17
use Symfony \Bundle \MakerBundle \FileManager ;
18
18
use Symfony \Bundle \MakerBundle \Generator ;
19
19
use Symfony \Bundle \MakerBundle \InputConfiguration ;
20
+ use Symfony \Bundle \MakerBundle \JsPackageManager ;
20
21
use Symfony \Component \Console \Command \Command ;
21
22
use Symfony \Component \Console \Input \InputArgument ;
22
23
use Symfony \Component \Console \Input \InputInterface ;
23
24
use Symfony \Component \Filesystem \Filesystem ;
24
25
use Symfony \Component \Finder \Finder ;
25
26
use Symfony \Component \HttpKernel \Kernel ;
27
+ use Symfony \Component \Process \ExecutableFinder ;
26
28
use Symfony \Component \Process \Process ;
27
29
28
30
/**
31
33
final class MakeScaffold extends AbstractMaker
32
34
{
33
35
private $ files ;
36
+ private $ jsPackageManager ;
34
37
private $ availableScaffolds ;
38
+ private $ composerBin ;
35
39
private $ installedScaffolds = [];
36
40
private $ installedPackages = [];
41
+ private $ installedJsPackages = [];
37
42
38
43
public function __construct (FileManager $ files )
39
44
{
40
45
$ this ->files = $ files ;
46
+ $ this ->jsPackageManager = new JsPackageManager ($ files );
41
47
}
42
48
43
49
public static function getCommandName (): string
@@ -75,6 +81,18 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
75
81
foreach ($ names as $ name ) {
76
82
$ this ->generateScaffold ($ name , $ io );
77
83
}
84
+
85
+ if ($ this ->installedJsPackages ) {
86
+ if ($ this ->jsPackageManager ->isAvailable ()) {
87
+ $ io ->comment ('Installing JS packages... ' );
88
+ $ this ->jsPackageManager ->install ();
89
+
90
+ $ io ->comment ('Running Webpack Encore... ' );
91
+ $ this ->jsPackageManager ->run ('dev ' );
92
+ } else {
93
+ $ io ->warning ('Unable to detect JS package manager, you need to run "yarn/npm install". ' );
94
+ }
95
+ }
78
96
}
79
97
80
98
public function interact (InputInterface $ input , ConsoleStyle $ io , Command $ command ): void
@@ -108,15 +126,14 @@ private function generateScaffold(string $name, ConsoleStyle $io): void
108
126
$ this ->generateScaffold ($ dependent , $ io );
109
127
}
110
128
111
- $ io ->text ("Generating <info> {$ name }</info> Scaffold... " );
129
+ $ io ->text ("Installing <info> {$ name }</info> Scaffold... " );
112
130
113
131
// install required packages
114
132
foreach ($ scaffold ['packages ' ] ?? [] as $ package => $ env ) {
115
133
if (!$ this ->isPackageInstalled ($ package )) {
116
- $ io ->text ("Installing <comment> {$ package }</comment>... " );
134
+ $ io ->text ("Installing Composer package: <comment> {$ package }</comment>... " );
117
135
118
- // todo composer bin detection
119
- $ command = ['composer ' , 'require ' , '--no-scripts ' , 'dev ' === $ env ? '--dev ' : null , $ package ];
136
+ $ command = [$ this ->composerBin (), 'require ' , '--no-scripts ' , 'dev ' === $ env ? '--dev ' : null , $ package ];
120
137
$ process = new Process (array_filter ($ command ), $ this ->files ->getRootDirectory ());
121
138
122
139
$ process ->run ();
@@ -129,6 +146,16 @@ private function generateScaffold(string $name, ConsoleStyle $io): void
129
146
}
130
147
}
131
148
149
+ // install required js packages
150
+ foreach ($ scaffold ['js_packages ' ] ?? [] as $ package => $ version ) {
151
+ if (!\in_array ($ package , $ this ->installedJsPackages , true )) {
152
+ $ io ->text ("Installing JS package: <comment> {$ package }@ {$ version }</comment>... " );
153
+
154
+ $ this ->jsPackageManager ->add ($ package , $ version );
155
+ $ this ->installedJsPackages [] = $ package ;
156
+ }
157
+ }
158
+
132
159
if (is_dir ($ scaffold ['dir ' ])) {
133
160
$ io ->text ('Copying scaffold files... ' );
134
161
@@ -190,4 +217,17 @@ private function isScaffoldInstalled(string $name): bool
190
217
{
191
218
return \in_array ($ name , $ this ->installedScaffolds , true );
192
219
}
220
+
221
+ private function composerBin (): string
222
+ {
223
+ if ($ this ->composerBin ) {
224
+ return $ this ->composerBin ;
225
+ }
226
+
227
+ if (!$ this ->composerBin = (new ExecutableFinder ())->find ('composer ' )) {
228
+ throw new \RuntimeException ('Unable to detect composer binary. ' );
229
+ }
230
+
231
+ return $ this ->composerBin ;
232
+ }
193
233
}
0 commit comments