Connect with us

How to Automate App Setup with Laravel Initializer

kizinho

Published

on

Follow
How to Automate App Setup with Laravel Initializer
Photo: Naijacrawl

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.



kizinho

I am a software developer, like meeting people and love blogging, that's why I developed naijacrawl because that's what I love doing.

Continue Reading
Click To Comment

BE First To Comment


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Laravel 5.8.8 Released (bugs fixed and new features )

    kizinho

    Published

    on

    Laravel 5.8.8 Released (bugs fixed and new features )
    Photo: Naijacrawl

    The Laravel team released Laravel 5.8.8 yesterday with a new query builder method along with the latest fixes and changes in the 5.8 branch.

     

    First, a new Builder::forPageBeforeId() method was added to compliment an existing forPageAfterId().

     

    These methods are useful to query for records before or after a given record. In the pull request author Tim MacDonald illustrates the following example to get the records around a “focused” post:

    $posts = new Collection;
    
    $posts = $posts->concat(ChatPost::forPageBeforeId(15, $focused->id)->get()->reverse());
    
    $posts = $posts->push($focused);
    
    $posts = $posts->concat(ChatPost::forPageAfterId(15, $focused->id)->get());

     

     

    You can see the full list of fixes below, and the whole difference  between 5.8.7 and 5.8.8 on GitHub.

     

    The full release notes for Laravel 5.8 are available in the GitHub 5.8 changelog:

     

    v5.8.8

    Added Added Illuminate\Database\Query\Builder::forPageBeforeId() method (#28011)


    Fixed

    BelongsToMany::detach() with custom pivot class (#27997)

    Fixed incorrect event namespace in generated listener by event:generate command (#28007)

     

    Fixed unique validation without ignored column (#27987)


    Changed

     

    Added parameters argument to resolve helper (#28020) Don’t add the path only if path is empty in compiled view (#27976)

     

    Refactoring

    Refactoring of env() helper (#27965)

    Continue Reading

    Latest



    Face of Naijacrawl


    ChukwubuikemSunday ChukwubuikemSunday

    Sunday Chukwubuikem


    Higher - 09061773292

    Trending