forked from laravel/jetstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstallCommand.php
502 lines (413 loc) · 20.2 KB
/
InstallCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?php
namespace Laravel\Jetstream\Console;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;
class InstallCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'jetstream:install {stack : The development stack that should be installed}
{--teams : Indicates if team support should be installed}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install the Jetstream components and resources';
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
// Publish...
$this->callSilent('vendor:publish', ['--tag' => 'jetstream-config', '--force' => true]);
$this->callSilent('vendor:publish', ['--tag' => 'jetstream-migrations', '--force' => true]);
$this->callSilent('vendor:publish', ['--tag' => 'fortify-config', '--force' => true]);
$this->callSilent('vendor:publish', ['--tag' => 'fortify-support', '--force' => true]);
$this->callSilent('vendor:publish', ['--tag' => 'fortify-migrations', '--force' => true]);
// "Home" Route...
$this->replaceInFile('/home', '/dashboard', app_path('Providers/RouteServiceProvider.php'));
// Fortify Provider...
$this->installFortifyServiceProvider();
// Configure Session...
$this->configureSession();
// AuthenticateSession Middleware...
$this->replaceInFile(
'// \Illuminate\Session\Middleware\AuthenticateSession::class',
'\Laravel\Jetstream\Http\Middleware\AuthenticateSession::class',
app_path('Http/Kernel.php')
);
// Install Stack...
if ($this->argument('stack') === 'livewire') {
$this->installLivewireStack();
} elseif ($this->argument('stack') === 'inertia') {
$this->installInertiaStack();
}
}
/**
* Install the Fortify service providers in the application configuration file.
*
* @return void
*/
protected function installFortifyServiceProvider()
{
if (! Str::contains($appConfig = file_get_contents(config_path('app.php')), 'App\\Providers\\FortifyServiceProvider::class')) {
file_put_contents(config_path('app.php'), str_replace(
"App\\Providers\RouteServiceProvider::class,".PHP_EOL,
"App\\Providers\RouteServiceProvider::class,".PHP_EOL." App\Providers\FortifyServiceProvider::class,".PHP_EOL,
$appConfig
));
}
}
/**
* Configure the session driver for Jetstream.
*
* @return void
*/
protected function configureSession()
{
if (! class_exists('CreateSessionsTable')) {
try {
$this->call('session:table');
} catch (Exception $e) {
//
}
}
$this->replaceInFile("'SESSION_DRIVER', 'file'", "'SESSION_DRIVER', 'database'", config_path('session.php'));
$this->replaceInFile('SESSION_DRIVER=file', 'SESSION_DRIVER=database', base_path('.env'));
$this->replaceInFile('SESSION_DRIVER=file', 'SESSION_DRIVER=database', base_path('.env.example'));
foreach ((new Filesystem)->files(database_path('migrations')) as $file) {
if (Str::contains($file->getRealPath(), 'create_sessions_table')) {
$this->replaceInFile("foreignId('user_id')", "uuid('user_id')", $file->getRealPath());
break;
}
}
}
/**
* Install the Livewire stack into the application.
*
* @return void
*/
protected function installLivewireStack()
{
// Install Livewire...
(new Process(['composer', 'require', 'livewire/livewire', 'laravel/sanctum:^2.6'], base_path()))
->run(function ($type, $output) {
$this->output->write($output);
});
// Sanctum...
(new Process(['php', 'artisan', 'vendor:publish', '--provider=Laravel\Sanctum\SanctumServiceProvider', '--force'], base_path()))
->run(function ($type, $output) {
$this->output->write($output);
});
$this->replaceInFile('morphs', 'uuidMorphs', database_path('migrations/2019_12_14_000001_create_personal_access_tokens_table.php'));
// Update Configuration...
$this->replaceInFile('inertia', 'livewire', config_path('jetstream.php'));
$this->replaceInFile("'guard' => 'web'", "'guard' => 'sanctum'", config_path('auth.php'));
// NPM Packages...
$this->updateNodePackages(function ($packages) {
return [
'@tailwindcss/ui' => '^0.5.0',
'postcss-import' => '^12.0.1',
'tailwindcss' => '^1.3.0',
] + $packages;
});
// Tailwind Configuration...
copy(__DIR__.'/../../stubs/livewire/tailwind.config.js', base_path('tailwind.config.js'));
copy(__DIR__.'/../../stubs/webpack.mix.js', base_path('webpack.mix.js'));
// Directories...
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Fortify'));
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Jetstream'));
(new Filesystem)->ensureDirectoryExists(app_path('View/Components'));
(new Filesystem)->ensureDirectoryExists(public_path('css'));
(new Filesystem)->ensureDirectoryExists(resource_path('css'));
(new Filesystem)->ensureDirectoryExists(resource_path('views/api'));
(new Filesystem)->ensureDirectoryExists(resource_path('views/auth'));
(new Filesystem)->ensureDirectoryExists(resource_path('views/layouts'));
(new Filesystem)->ensureDirectoryExists(resource_path('views/profile'));
(new Filesystem)->deleteDirectory(resource_path('sass'));
// Service Providers...
copy(__DIR__.'/../../stubs/app/Providers/JetstreamServiceProvider.php', app_path('Providers/JetstreamServiceProvider.php'));
$this->installJetstreamServiceProvider();
// Models...
copy(__DIR__.'/../../stubs/app/Models/User.php', app_path('Models/User.php'));
// Actions...
copy(__DIR__.'/../../stubs/app/Actions/Fortify/CreateNewUser.php', app_path('Actions/Fortify/CreateNewUser.php'));
copy(__DIR__.'/../../stubs/app/Actions/Fortify/UpdateUserProfileInformation.php', app_path('Actions/Fortify/UpdateUserProfileInformation.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/DeleteUser.php', app_path('Actions/Jetstream/DeleteUser.php'));
// View Components...
copy(__DIR__.'/../../stubs/livewire/app/View/Components/AppLayout.php', app_path('View/Components/AppLayout.php'));
copy(__DIR__.'/../../stubs/app/View/Components/GuestLayout.php', app_path('View/Components/GuestLayout.php'));
// Layouts...
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/layouts', resource_path('views/layouts'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/resources/views/layouts', resource_path('views/layouts'));
// Single Blade Views...
copy(__DIR__.'/../../stubs/livewire/resources/views/dashboard.blade.php', resource_path('views/dashboard.blade.php'));
// Other Views...
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/api', resource_path('views/api'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/profile', resource_path('views/profile'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/resources/views/auth', resource_path('views/auth'));
// Routes...
$this->replaceInFile('auth:api', 'auth:sanctum', base_path('routes/api.php'));
if (! Str::contains(file_get_contents(base_path('routes/web.php')), "'/dashboard'")) {
(new Filesystem)->append(base_path('routes/web.php'), $this->livewireRouteDefinition());
}
// Assets...
copy(__DIR__.'/../../stubs/public/css/app.css', public_path('css/app.css'));
copy(__DIR__.'/../../stubs/resources/css/app.css', resource_path('css/app.css'));
// Teams...
if ($this->option('teams')) {
$this->installLivewireTeamStack();
}
$this->line('');
$this->info('Livewire scaffolding installed successfully.');
}
/**
* Install the Livewire team stack into the application.
*
* @return void
*/
protected function installLivewireTeamStack()
{
// Directories...
(new Filesystem)->ensureDirectoryExists(resource_path('views/teams'));
// Other Views...
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/teams', resource_path('views/teams'));
$this->ensureApplicationIsTeamCompatible();
}
/**
* Get the route definition(s) that should be installed for Livewire.
*
* @return string
*/
protected function livewireRouteDefinition()
{
return <<<'EOF'
Route::middleware(['auth', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
EOF;
}
/**
* Install the Inertia stack into the application.
*
* @return void
*/
protected function installInertiaStack()
{
// Install Inertia...
(new Process(['composer', 'require', 'inertiajs/inertia-laravel', 'laravel/sanctum:^2.6'], base_path()))
->run(function ($type, $output) {
$this->output->write($output);
});
// Install NPM packages...
$this->updateNodePackages(function ($packages) {
return [
'@inertiajs/inertia' => '^0.1.7',
'@inertiajs/inertia-vue' => '^0.1.2',
'@tailwindcss/ui' => '^0.1.3',
'laravel-jetstream' => '^0.0.3',
'portal-vue' => '^2.1.7',
'postcss-import' => '^12.0.1',
'tailwindcss' => '^1.3.0',
'vue' => '^2.5.17',
'vue-template-compiler' => '^2.6.10',
] + $packages;
});
// Sanctum...
(new Process(['php', 'artisan', 'vendor:publish', '--provider=Laravel\Sanctum\SanctumServiceProvider', '--force'], base_path()))
->run(function ($type, $output) {
$this->output->write($output);
});
$this->replaceInFile('morphs', 'uuidMorphs', database_path('migrations/2019_12_14_000001_create_personal_access_tokens_table.php'));
// Update Configuration...
$this->replaceInFile("'guard' => 'web'", "'guard' => 'sanctum'", config_path('auth.php'));
// Tailwind Configuration...
copy(__DIR__.'/../../stubs/inertia/tailwind.config.js', base_path('tailwind.config.js'));
copy(__DIR__.'/../../stubs/webpack.mix.js', base_path('webpack.mix.js'));
// Directories...
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Fortify'));
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Jetstream'));
(new Filesystem)->ensureDirectoryExists(app_path('View/Components'));
(new Filesystem)->ensureDirectoryExists(public_path('css'));
(new Filesystem)->ensureDirectoryExists(resource_path('css'));
(new Filesystem)->ensureDirectoryExists(resource_path('js/Jetstream'));
(new Filesystem)->ensureDirectoryExists(resource_path('js/Layouts'));
(new Filesystem)->ensureDirectoryExists(resource_path('js/Mixins'));
(new Filesystem)->ensureDirectoryExists(resource_path('js/Pages'));
(new Filesystem)->ensureDirectoryExists(resource_path('js/Pages/API'));
(new Filesystem)->ensureDirectoryExists(resource_path('js/Pages/Profile'));
(new Filesystem)->ensureDirectoryExists(resource_path('views/layouts'));
(new Filesystem)->deleteDirectory(resource_path('sass'));
// Service Providers...
copy(__DIR__.'/../../stubs/app/Providers/JetstreamServiceProvider.php', app_path('Providers/JetstreamServiceProvider.php'));
$this->installJetstreamServiceProvider();
// Models...
copy(__DIR__.'/../../stubs/app/Models/User.php', app_path('Models/User.php'));
// Actions...
copy(__DIR__.'/../../stubs/app/Actions/Fortify/CreateNewUser.php', app_path('Actions/Fortify/CreateNewUser.php'));
copy(__DIR__.'/../../stubs/app/Actions/Fortify/UpdateUserProfileInformation.php', app_path('Actions/Fortify/UpdateUserProfileInformation.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/DeleteUser.php', app_path('Actions/Jetstream/DeleteUser.php'));
// View Components...
copy(__DIR__.'/../../stubs/app/View/Components/GuestLayout.php', app_path('View/Components/GuestLayout.php'));
// Blade Views...
copy(__DIR__.'/../../stubs/inertia/resources/views/app.blade.php', resource_path('views/app.blade.php'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/resources/views/auth', resource_path('views/auth'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/resources/views/layouts', resource_path('views/layouts'));
// Inertia Pages...
copy(__DIR__.'/../../stubs/inertia/resources/js/Pages/Dashboard.vue', resource_path('js/Pages/Dashboard.vue'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Jetstream', resource_path('js/Jetstream'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Layouts', resource_path('js/Layouts'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Mixins', resource_path('js/Mixins'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/API', resource_path('js/Pages/API'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/Profile', resource_path('js/Pages/Profile'));
// Routes...
$this->replaceInFile('auth:api', 'auth:sanctum', base_path('routes/api.php'));
if (! Str::contains(file_get_contents(base_path('routes/web.php')), "'/dashboard'")) {
(new Filesystem)->append(base_path('routes/web.php'), $this->inertiaRouteDefinition());
}
// Assets...
copy(__DIR__.'/../../stubs/public/css/app.css', public_path('css/app.css'));
copy(__DIR__.'/../../stubs/resources/css/app.css', resource_path('css/app.css'));
copy(__DIR__.'/../../stubs/inertia/resources/js/app.js', resource_path('js/app.js'));
// Flush node_modules...
// static::flushNodeModules();
// Teams...
if ($this->option('teams')) {
$this->installInertiaTeamStack();
}
$this->line('');
$this->info('Inertia scaffolding installed successfully.');
$this->comment('Please execute the "npm install && npm run dev" command to build your assets.');
}
/**
* Install the Inertia team stack into the application.
*
* @return void
*/
protected function installInertiaTeamStack()
{
// Directories...
(new Filesystem)->ensureDirectoryExists(resource_path('js/Pages/Profile'));
// Pages...
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/Teams', resource_path('js/Pages/Teams'));
$this->ensureApplicationIsTeamCompatible();
}
/**
* Get the route definition(s) that should be installed for Inertia.
*
* @return string
*/
protected function inertiaRouteDefinition()
{
return <<<EOF
Route::middleware(['auth', 'verified'])->get('/dashboard', function () {
return Inertia\Inertia::render('Dashboard');
})->name('dashboard');
EOF;
}
/**
* Ensure the installed user model is ready for team usage.
*
* @return void
*/
protected function ensureApplicationIsTeamCompatible()
{
// Publish Team Migrations...
$this->callSilent('vendor:publish', ['--tag' => 'jetstream-team-migrations', '--force' => true]);
// Configuration...
$this->replaceInFile('// Features::teams()', 'Features::teams()', config_path('jetstream.php'));
// Directories...
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Jetstream'));
(new Filesystem)->ensureDirectoryExists(app_path('Events'));
(new Filesystem)->ensureDirectoryExists(app_path('Policies'));
// Service Providers...
copy(__DIR__.'/../../stubs/app/Providers/AuthServiceProvider.php', app_path('Providers/AuthServiceProvider.php'));
copy(__DIR__.'/../../stubs/app/Providers/JetstreamWithTeamsServiceProvider.php', app_path('Providers/JetstreamServiceProvider.php'));
// Models...
copy(__DIR__.'/../../stubs/app/Models/Membership.php', app_path('Models/Membership.php'));
copy(__DIR__.'/../../stubs/app/Models/Team.php', app_path('Models/Team.php'));
copy(__DIR__.'/../../stubs/app/Models/UserWithTeams.php', app_path('Models/User.php'));
// Actions...
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/AddTeamMember.php', app_path('Actions/Jetstream/AddTeamMember.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/CreateTeam.php', app_path('Actions/Jetstream/CreateTeam.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/DeleteTeam.php', app_path('Actions/Jetstream/DeleteTeam.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/DeleteUserWithTeams.php', app_path('Actions/Jetstream/DeleteUser.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/UpdateTeamName.php', app_path('Actions/Jetstream/UpdateTeamName.php'));
copy(__DIR__.'/../../stubs/app/Actions/Fortify/CreateNewUserWithTeams.php', app_path('Actions/Fortify/CreateNewUser.php'));
// Policies...
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/app/Policies', app_path('Policies'));
}
/**
* Install the Jetstream service providers in the application configuration file.
*
* @return void
*/
protected function installJetstreamServiceProvider()
{
if (! Str::contains($appConfig = file_get_contents(config_path('app.php')), 'App\\Providers\\JetstreamServiceProvider::class')) {
file_put_contents(config_path('app.php'), str_replace(
"App\\Providers\FortifyServiceProvider::class,".PHP_EOL,
"App\\Providers\FortifyServiceProvider::class,".PHP_EOL." App\Providers\JetstreamServiceProvider::class,".PHP_EOL,
$appConfig
));
}
}
/**
* Update the "package.json" file.
*
* @param callable $callback
* @param bool $dev
* @return void
*/
protected static function updateNodePackages(callable $callback, $dev = true)
{
if (! file_exists(base_path('package.json'))) {
return;
}
$configurationKey = $dev ? 'devDependencies' : 'dependencies';
$packages = json_decode(file_get_contents(base_path('package.json')), true);
$packages[$configurationKey] = $callback(
array_key_exists($configurationKey, $packages) ? $packages[$configurationKey] : [],
$configurationKey
);
ksort($packages[$configurationKey]);
file_put_contents(
base_path('package.json'),
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT).PHP_EOL
);
}
/**
* Delete the "node_modules" directory and remove the associated lock files.
*
* @return void
*/
protected static function flushNodeModules()
{
tap(new Filesystem, function ($files) {
$files->deleteDirectory(base_path('node_modules'));
$files->delete(base_path('yarn.lock'));
$files->delete(base_path('package-lock.json'));
});
}
/**
* Replace a given string within a given file.
*
* @param string $search
* @param string $replace
* @param string $path
* @return void
*/
protected function replaceInFile($search, $replace, $path)
{
file_put_contents($path, str_replace($search, $replace, file_get_contents($path)));
}
}