27

Automate App Setup with Laravel Initializer

 4 years ago
source link: https://laravel-news.com/automate-app-setup-with-laravel-initializer
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 .

Filed in: News / packages


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK