Automate App Setup with Laravel Initializer

Packages

November 21st, 2019

laravel-initalizer.png

Have you ever found yourself writing multiple manual steps to set up a Laravel application in a new environment? Laravel Initializer is a convenient way to automate installing and updating a Laravel application:

Laravel Initializer gives you the ability to declare multiple processes and run them with app:install and app:update artisan commands, which run predefined actions chain depending on the current environment.

The app:install and app:update commands use two distinct classes that run commands based on a given environment. First, the install command uses the App\Install class:

1namespace App;
2 
3use MadWeb\Initializer\Contracts\Runner;
4 
5class Install
6{
7 public function production(Runner $run)
8 {
9 return $run
10 ->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader')
11 ->artisan('key:generate')
12 ->artisan('migrate', ['--force' => true])
13 ->artisan('storage:link')
14 ->external('npm', 'install', '--production')
15 ->external('npm', 'run', 'production')
16 ->artisan('route:cache')
17 ->artisan('config:cache')
18 ->artisan('event:cache');
19 }
20 
21 public function local(Runner $run)
22 {
23 return $run
24 ->external('composer', 'install')
25 ->artisan('key:generate')
26 ->artisan('migrate')
27 ->artisan('storage:link')
28 ->external('npm', 'install')
29 ->external('npm', 'run', 'development');
30 }
31}

The app:update command looks similar, using an App\Update class:

1namespace App;
2
3use MadWeb\Initializer\Contracts\Runner;
4
5class Update
6{
7 public function production(Runner $run)
8 {
9 return $run
10 ->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader')
11 ->external('npm', 'install', '--production')
12 ->external('npm', 'run', 'production')
13 ->artisan('route:cache')
14 ->artisan('config:cache')
15 ->artisan('event:cache')
16 ->artisan('migrate', ['--force' => true])
17 ->artisan('cache:clear')
18 ->artisan('queue:restart'); ->artisan('horizon:terminate');
19 }
20
21 public function local(Runner $run)
22 {
23 return $run
24 ->external('composer', 'install')
25 ->external('npm', 'install')
26 ->external('npm', 'run', 'development')
27 ->artisan('migrate')
28 ->artisan('cache:clear');
29 }
30}

You can also inject dependencies from the service container if you need to access services while running commands.

This package contains a variety of runner actions you should check out in the readme. I found the MakeCronTask dispatch interesting:

1$run->dispatch(new \MadWeb\Initializer\Jobs\MakeCronTask)

MakeCronTask adds the following to the server’s crontab list:

1* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

You can do other things like creating a supervisord config for a typical queue worker or horizon.

You can learn more about this package, get full installation instructions, and view the source code on GitHub at mad-web/laravel-initializer.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.