200 lines
4.6 KiB
PHP
Raw Normal View History

2019-12-03 15:41:56 +03:00
<?php
namespace App\Console\Commands;
2020-12-24 02:16:00 +03:00
use App\Events\Install\UpdateCopied;
use App\Events\Install\UpdateDownloaded;
2021-03-12 17:24:03 +03:00
use App\Events\Install\UpdateFailed;
2020-12-24 02:16:00 +03:00
use App\Events\Install\UpdateUnzipped;
use App\Jobs\Install\CopyFiles;
use App\Jobs\Install\DownloadFile;
use App\Jobs\Install\FinishUpdate;
use App\Jobs\Install\UnzipFile;
use App\Traits\Jobs;
2019-12-03 15:41:56 +03:00
use App\Utilities\Versions;
use Illuminate\Console\Command;
class Update extends Command
{
2020-12-24 02:16:00 +03:00
use Jobs;
2019-12-03 15:41:56 +03:00
const CMD_SUCCESS = 0;
const CMD_ERROR = 1;
public $alias;
2020-12-25 12:08:15 +03:00
public $company;
2019-12-03 15:41:56 +03:00
public $new;
public $old;
/**
* The name and signature of the console command.
*
* @var string
*/
2020-02-03 18:57:31 +03:00
protected $signature = 'update {alias} {company} {new=latest}';
2019-12-03 15:41:56 +03:00
/**
* The console command description.
*
* @var string
*/
2019-12-03 15:46:51 +03:00
protected $description = 'Allows to update Akaunting and modules directly through CLI';
2020-02-03 18:57:31 +03:00
2019-12-03 15:41:56 +03:00
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
set_time_limit(3600); // 1 hour
2019-12-03 15:41:56 +03:00
$this->alias = $this->argument('alias');
2020-12-25 12:08:15 +03:00
$this->company = $this->argument('company');
2019-12-03 15:41:56 +03:00
2020-02-21 11:45:37 +03:00
if (false === $this->new = $this->getNewVersion()) {
2021-03-12 17:24:03 +03:00
$message = 'Not able to get the latest version of ' . $this->alias . '!';
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Version', $message));
2020-02-21 11:45:37 +03:00
return self::CMD_ERROR;
}
2019-12-03 15:41:56 +03:00
$this->old = $this->getOldVersion();
2023-06-05 17:05:54 +03:00
if (version_compare($this->old, $this->new, '>=')) {
$message = 'The current version for the ' . $this->alias . ' is the latest version!';
$this->info($message);
return self::CMD_SUCCESS;
}
2021-04-16 00:59:43 +03:00
company($this->company)->makeCurrent();
2019-12-03 15:41:56 +03:00
if (!$path = $this->download()) {
return self::CMD_ERROR;
}
if (!$this->unzip($path)) {
return self::CMD_ERROR;
}
if (!$this->copyFiles($path)) {
return self::CMD_ERROR;
}
if (!$this->finish()) {
return self::CMD_ERROR;
}
return self::CMD_SUCCESS;
}
public function getNewVersion()
{
2023-04-25 09:46:22 +03:00
return ($this->argument('new') == 'latest') ? Versions::latest($this->alias)?->latest : $this->argument('new');
2019-12-03 15:41:56 +03:00
}
public function getOldVersion()
{
2020-02-05 17:19:52 +03:00
if ($this->alias == 'core') {
return version('short');
}
if ($module = module($this->alias)) {
return $module->get('version');
}
return '1.0.0';
2019-12-03 15:41:56 +03:00
}
public function download()
{
$this->info('Downloading update...');
try {
2020-12-24 02:16:00 +03:00
$path = $this->dispatch(new DownloadFile($this->alias, $this->new));
event(new UpdateDownloaded($this->alias, $this->new, $this->old));
2019-12-03 15:41:56 +03:00
} catch (\Exception $e) {
2021-03-12 17:24:03 +03:00
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Download', $message));
2019-12-03 15:41:56 +03:00
return false;
}
return $path;
}
public function unzip($path)
{
$this->info('Unzipping update...');
try {
2020-12-24 02:16:00 +03:00
$this->dispatch(new UnzipFile($this->alias, $path));
event(new UpdateUnzipped($this->alias, $this->new, $this->old));
2019-12-03 15:41:56 +03:00
} catch (\Exception $e) {
2021-03-12 17:24:03 +03:00
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Unzip', $message));
2019-12-03 15:41:56 +03:00
return false;
}
return true;
}
public function copyFiles($path)
{
$this->info('Copying update files...');
try {
2020-12-24 02:16:00 +03:00
$this->dispatch(new CopyFiles($this->alias, $path));
event(new UpdateCopied($this->alias, $this->new, $this->old));
2019-12-03 15:41:56 +03:00
} catch (\Exception $e) {
2021-03-12 17:24:03 +03:00
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Copy Files', $message));
2019-12-03 15:41:56 +03:00
return false;
}
return true;
}
public function finish()
{
$this->info('Finishing update...');
try {
2020-12-25 12:08:15 +03:00
$this->dispatch(new FinishUpdate($this->alias, $this->new, $this->old, $this->company));
2019-12-03 15:41:56 +03:00
} catch (\Exception $e) {
2021-03-12 17:24:03 +03:00
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Finish', $message));
2019-12-03 15:41:56 +03:00
return false;
}
return true;
}
}