Blog Detail

30

May
Write Shell Scripts like Blade Components in Laravel cover image

arrow_back Write Shell Scripts like Blade Components in Laravel

Laravel Task Runner is a package to write Shell scripts like Blade Components and run them locally or on a remote server. Support for running tasks in the background and test assertions. Built upon the Process feature in Laravel 10.

Installation

This package requires Laravel 10 and PHP 8.1 or higher. You can install the package via Composer:

composer require protonemedia/laravel-task-runner

Optionally, you can publish the config file with:

php artisan vendor:publish --provider="ProtoneMedia\LaravelTaskRunner\ServiceProvider"

Basic usage

You may use the Artisan make:task command to create a Task class:

php artisan make:task ComposerGlobalUpdate

This will generate two files: app/Tasks/ComposerGlobalUpdate.php and resources/views/tasks/composer-global-update.blade.php.

Once you’ve added your script to the Blade template, you may run it on your local machine by calling the dispatch() method:

ComposerGlobalUpdate::dispatch();

Alternatively, if you don’t want a separate Blade template, you may use the –class option (or -c):

php artisan make:task ComposerGlobalUpdate -c

This allows you to specify the script inline:

class ComposerGlobalUpdate extends Task
{
    public function render(): string
    {
        return 'composer global update';
    }
}

Task output

The dispatch() method returns an instance of ProcessOutput, which can return the output and exit code:

$output = ComposerGlobalUpdate::dispatch();

$output->getBuffer();
$output->getExitCode();

$output->getLines();    // returns the buffer as an array
$output->isSuccessful();    // returns true when the exit code is 0
$output->isTimeout();    // returns true on a timeout

To interact with the underlying ProcessResult, you may call the getIlluminateResult() method:

$output->getIlluminateResult();

Script variables

Just like Blade Components, the public properties and methods of the Task class are available in the template:

class GetFile extends Task
{
    public function __construct(public string $path)
    {
    }

    public function options()
    {
        return '-n';
    }
}

Blade template:

cat {{ $options() }} {{ $path }}

You can create a new instance of the Task using the static make() method:

GetFile::make('/etc/hosts')->dispatch();

Task options

You may specify a timeout. By default, the timeout is based on the task-runner.default_timeout config value.

class ComposerGlobalUpdate extends Task
{
    protected int $timeout = 60;
}

Note

If you are a business and want to leverage Laravel for your next PHP-based web application, CRM Software, E-commerce application, SAAS application, or any custom web application development for your custom requirements, you must discover an amazing team with experience in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. Please feel free to send us an email at rizwan@codebrisk.com or get in touch with us, our business person will get back to you.

Published at : 30-05-2023

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project