Skip to content

Commit

Permalink
Merge pull request #13 from salahhusa9/output
Browse files Browse the repository at this point in the history
Output in run
  • Loading branch information
salahhusa9 committed Oct 28, 2023
2 parents a79d6db + 998a4d7 commit ad19c30
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Salahhusa9\Updater\Events\NewVersionAvailable;
use Salahhusa9\Updater\Facades\Updater;

class UpdaterCommand extends Command
class CheckCommand extends Command
{
public $signature = 'updater:check';

Expand All @@ -21,9 +21,9 @@ public function handle(): int
return self::SUCCESS;
}

$this->info('New version available: '.$newVersionAvailable['new_version']);
$this->info('New version available: '.$newVersionAvailable['latest_version']);

event(new NewVersionAvailable($newVersionAvailable['current_version'], $newVersionAvailable['new_version']));
event(new NewVersionAvailable($newVersionAvailable['current_version'], $newVersionAvailable['latest_version']));

return self::SUCCESS;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/UpdaterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public function handle(): int
return self::FAILURE;
}

$this->comment('Updating to version '.$newVersionAvailable['new_version']);
$this->comment('Updating to version '.$newVersionAvailable['latest_version']);

Updater::update();
Updater::update(output: function ($message) {
$this->comment($message);
});

$this->info('Application updated! You are now on version '.Updater::getCurrentVersion().'!');

Expand Down
8 changes: 8 additions & 0 deletions src/Pipelines/ArtisanCallCacheClearPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ class ArtisanCallCacheClearPipe implements Pipeline
*/
public function handle($content, Closure $next)
{
if (is_callable($content['output'])) {
call_user_func($content['output'], 'Clearing cache...');
}

Artisan::call('cache:clear');

if (is_callable($content['output'])) {
call_user_func($content['output'], 'Cache cleared!');
}

return $next($content);
}
}
8 changes: 8 additions & 0 deletions src/Pipelines/ArtisanCallConfigClearPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ class ArtisanCallConfigClearPipe implements Pipeline
*/
public function handle($content, Closure $next)
{
if (is_callable($content['output'])) {
call_user_func($content['output'], 'Clearing config cache...');
}

Artisan::call('config:clear');

if (is_callable($content['output'])) {
call_user_func($content['output'], 'Config cache cleared!');
}

return $next($content);
}
}
8 changes: 8 additions & 0 deletions src/Pipelines/ArtisanCallMigratePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ class ArtisanCallMigratePipe implements Pipeline
*/
public function handle($content, Closure $next)
{
if (is_callable($content['output'])) {
call_user_func($content['output'], 'Migrating...');
}

Artisan::call('migrate', [
'--force' => true,
]);

if (is_callable($content['output'])) {
call_user_func($content['output'], 'Migrated!');
}

return $next($content);
}
}
8 changes: 8 additions & 0 deletions src/Pipelines/ArtisanCallOptimizePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ class ArtisanCallOptimizePipe implements Pipeline
*/
public function handle($content, Closure $next)
{
if (is_callable($content['output'])) {
call_user_func($content['output'], 'Optimizing...');
}

Artisan::call('optimize');

if (is_callable($content['output'])) {
call_user_func($content['output'], 'Optimized!');
}

return $next($content);
}
}
8 changes: 8 additions & 0 deletions src/Pipelines/ArtisanCallRouteClearPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ class ArtisanCallRouteClearPipe implements Pipeline
*/
public function handle($content, Closure $next)
{
if (is_callable($content['output'])) {
call_user_func($content['output'], 'Clearing route cache...');
}

Artisan::call('route:clear');

if (is_callable($content['output'])) {
call_user_func($content['output'], 'Route cache cleared!');
}

return $next($content);
}
}
8 changes: 8 additions & 0 deletions src/Pipelines/ArtisanCallViewClearPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ class ArtisanCallViewClearPipe implements Pipeline
*/
public function handle($content, Closure $next)
{
if (is_callable($content['output'])) {
call_user_func($content['output'], 'Clearing view cache...');
}

Artisan::call('view:clear');

if (is_callable($content['output'])) {
call_user_func($content['output'], 'View cache cleared!');
}

return $next($content);
}
}
8 changes: 8 additions & 0 deletions src/Pipelines/GitPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ public function handle($content, Closure $next)
{
$version = $content['new_version'];

if (is_callable($content['output'])) {
call_user_func($content['output'], 'Downloading version '.$version.' ...');
}

Git::auth();
Git::fetch();
Git::pull();
$checkout = Git::checkout($version);

if (Updater::getCurrentVersion() != $version) {
if (is_callable($content['output'])) {
call_user_func($content['output'], 'git checkout failed: '.$checkout);
}

return throw new \Exception('git checkout failed: '.$checkout);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Pipelines/SeedersPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ class SeedersPipe implements Pipeline
*/
public function handle($content, Closure $next)
{
if (is_callable($content['output'])) {
call_user_func($content['output'], 'Seeding...');
}

Artisan::call('db:seed', [
'--class' => implode(' --class=', config('updater.seeders')),
'--force' => true,
]);

if (is_callable($content['output'])) {
call_user_func($content['output'], 'Seeded!');
}

return $next($content);
}
}
31 changes: 30 additions & 1 deletion src/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@

class Updater
{
private $output;

/**
* update
*/
public function update(): string
public function update(callable $output = null): string
{
if (! is_callable($output)) {
throw new \Exception('Output must be callable');
}

$this->output = $output;

if (is_array($this->newVersionAvailable())) {
return $this->updateTo($this->getLatestVersion());
} else {
Expand All @@ -35,6 +43,8 @@ private function updateTo($version): string

try {
if (config('updater.maintenance_mode', false)) {
$this->output('Maintenance mode is on, turning it on...');

Artisan::call(
'down',
config('updater.maintenance_mode_secret', false) ? [
Expand Down Expand Up @@ -94,9 +104,12 @@ private function updateTo($version): string
throw new \Exception('Pipelines is not array or empty');
}

$this->output('Start Updating to version '.$version);

Pipeline::send([
'current_version' => $this->getCurrentVersion(),
'new_version' => $version,
'output' => $this->output,
])
->through($pipelines)
->then(
Expand All @@ -106,6 +119,7 @@ function ($content) {
);

if (config('updater.maintenance_mode', false)) {
$this->output('Maintenance mode is on, turning it off...');
Artisan::call('up');
}

Expand All @@ -114,6 +128,7 @@ function ($content) {
return 'Updated to version '.$version;
} catch (\Throwable $th) {
if (config('updater.maintenance_mode', false)) {
$this->output('Maintenance mode is on, turning it off...');
Artisan::call('up');
}

Expand All @@ -122,10 +137,24 @@ function ($content) {
return throw $th;
}
} else {
$this->output('No new version available');

return 'No new version available';
}
}

/**
* output
*
* @param mixed $message
*/
public function output($message): void
{
if (is_callable($this->output)) {
call_user_func($this->output, $message);
}
}

/**
* newVersionAvailable
*
Expand Down
3 changes: 2 additions & 1 deletion src/UpdaterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Salahhusa9\Updater;

use Salahhusa9\Updater\Commands\CheckCommand;
use Salahhusa9\Updater\Commands\UpdaterCommand;
use Salahhusa9\Updater\Contracts\Repository;
use Salahhusa9\Updater\RepositorySource\GithubRepository;
Expand All @@ -22,7 +23,7 @@ public function configurePackage(Package $package): void
->hasConfigFile()
->hasViews()
->hasMigration('create_laravel-updater_table')
->hasCommand(UpdaterCommand::class);
->hasCommands(UpdaterCommand::class, CheckCommand::class);

$this->app->singleton(Repository::class, function () {
return new (config('updater.repository_source', GithubRepository::class))();
Expand Down

0 comments on commit ad19c30

Please sign in to comment.