Automate App Setup with Laravel Initializer

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:

namespace App; use MadWeb\Initializer\Contracts\Runner; class Install { public function production(Runner $run) { return $run ->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader') ->artisan('key:generate') ->artisan('migrate', ['--force' => true]) ->artisan('storage:link') ->external('npm', 'install', '--production') ->external('npm', 'run', 'production') ->artisan('route:cache') ->artisan('config:cache') ->artisan('event:cache'); } public function local(Runner $run) { return $run ->external('composer', 'install') ->artisan('key:generate') ->artisan('migrate') ->artisan('storage:link') ->external('npm', 'install') ->external('npm', 'run', 'development'); } }

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

namespace App; use MadWeb\Initializer\Contracts\Runner; class Update { public function production(Runner $run) { return $run ->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader') ->external('npm', 'install', '--production') ->external('npm', 'run', 'production') ->artisan('route:cache') ->artisan('config:cache') ->artisan('event:cache') ->artisan('migrate', ['--force' => true]) ->artisan('cache:clear') ->artisan('queue:restart'); ->artisan('horizon:terminate'); } public function local(Runner $run) { return $run ->external('composer', 'install') ->external('npm', 'install') ->external('npm', 'run', 'development') ->artisan('migrate') ->artisan('cache:clear'); } }

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:

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

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

* * * * * 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.