codedge / laravel-selfupdater

This package provides some basic methods to implement a self updating functionality for your Laravel application. Already bundled are some methods to provide a self-update mechanism via Github or some private repository via http.
MIT License
394 stars 80 forks source link

[Laravel 10.*] Artisan commands not working #470

Open fozooni opened 1 year ago

fozooni commented 1 year ago

Hello dear, I just create new laravel project and install laravel-selfupdater. Everything works perfectly and I thank you for that. But non of artisan commands working in self-updater.php at all, I don't have any idea how can I fix this. Commands are executed directly through the console without any problems.

self-updater.php file:

    'artisan_commands' => [
        'pre_update' => [
//            'tkt:pre-update' => [
//                'class' => \App\Console\Commands\PreUpdateCommand::class,
//                'params' => []
//            ]
        ],
        'post_update' => [
            'tkt:post-update' => [
                'class' => \App\Console\Commands\UpdateCommand::class,
                'params' => []
            ]
        ],
    ],

UpdateCommand.php file:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class UpdateCommand extends Command
{
    protected $name = 'tkt:post-update';
    protected $signature = 'tkt:post-update';
    protected $description = 'Upgrade commands';
    public function handle()
    {
        Artisan::call('migrate');
    }
}

I think this should work correctly but it doesn't! What do you think is the problem?

richard-muvirimi commented 3 months ago

After going through the code, it seems the base source classes do not have the functionality to call the pre_update and post_update commands. Not sure if it's an intended omission or the library is still in development, but there is a way to make this work, just replace from the example

$this->updater->source()

with the following

$sourceRepository = new SourceRepository($this->updater->source(), $this->updateExecutor);

if ($sourceRepository->isNewVersionAvailable()) {

  // Your fetch and update code ...

  $sourceRepository->preUpdateArtisanCommands();

  $updated = $sourceRepository->update($release);

  if ($updated) {
      $sourceRepository->postUpdateArtisanCommands();
  }
}

where $this->updateExecutor can be injected as a dependency as shown below (or using app())

    public function __construct(private readonly UpdaterManager $updater, private readonly UpdateExecutor $updateExecutor)
    {
        parent::__construct();
    }

Don't forget the relevant imports

use Codedge\Updater\Models\UpdateExecutor;
use Codedge\Updater\SourceRepository;
use Codedge\Updater\UpdaterManager;
fozooni commented 2 months ago

Thanks buddy! At that time I was very much waiting for a solution to solve my problem, but unfortunately the development was stopped. Anyway, I will check the solution you provided. I hope we can use it in future projects.