From 28217df65451d59706be870196232e5f9c5cca19 Mon Sep 17 00:00:00 2001 From: Mohit Panjwani Date: Thu, 3 Dec 2020 12:49:37 +0530 Subject: [PATCH] add ability to delete files on update --- app/Console/Commands/UpdateCommand.php | 59 ++++++++++++++++++-------- app/Space/Updater.php | 11 +++++ 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/app/Console/Commands/UpdateCommand.php b/app/Console/Commands/UpdateCommand.php index c341ed60..f734f513 100644 --- a/app/Console/Commands/UpdateCommand.php +++ b/app/Console/Commands/UpdateCommand.php @@ -13,6 +13,8 @@ class UpdateCommand extends Command public $version; + public $response; + /** * The name and signature of the console command. * @@ -45,9 +47,10 @@ class UpdateCommand extends Command set_time_limit(3600); // 1 hour $this->installed = $this->getInstalledVersion(); - $this->version = $this->getLatestVersion(); + $this->response = $this->getLatestVersionResponse(); + $this->version = ($this->response) ? $this->response->version : false; - if ($this->version == 'extension_required') { + if ($this->response == 'extension_required') { $this->info('Sorry! Your system does not meet the minimum requirements for this update.'); $this->info('Please retry after installing the required version/extensions.'); @@ -75,6 +78,12 @@ class UpdateCommand extends Command return; } + if(isset($this->response->deleted_files) && !empty($this->response->deleted_files)) { + if (!$this->deleteFiles($this->response->deleted_files)) { + return; + } + } + if (!$this->migrateUpdate()) { return; } @@ -91,7 +100,7 @@ class UpdateCommand extends Command return Setting::getSetting('version'); } - public function getLatestVersion() + public function getLatestVersionResponse() { $this->info('Your currently installed version is ' . $this->installed); $this->line(''); @@ -100,26 +109,27 @@ class UpdateCommand extends Command try { $response = Updater::checkForUpdate($this->installed); - $extensions = $response->version->extensions; + if ($response->success) { - $is_required = false; + $extensions = $response->version->extensions; - foreach ($extensions as $key => $extension) { + $is_required = false; - if(!$extension) { - $is_required = true; - $this->info('❌ '.$key); + foreach ($extensions as $key => $extension) { + + if(!$extension) { + $is_required = true; + $this->info('❌ '.$key); + } + + $this->info('✅ '.$key); } - $this->info('✅ '.$key); - } + if($is_required) { + return 'extension_required'; + } - if($is_required) { - return 'extension_required'; - } - - if ($response->success) { - return $response->version->version; + return $response->version; } return false; @@ -183,6 +193,21 @@ class UpdateCommand extends Command return true; } + public function deleteFiles($files) + { + $this->info('Deleting unused old files...'); + + try { + Updater::deleteFiles($files); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return false; + } + + return true; + } + public function migrateUpdate() { $this->info('Running Migrations...'); diff --git a/app/Space/Updater.php b/app/Space/Updater.php index 560397d7..3c9d41a8 100644 --- a/app/Space/Updater.php +++ b/app/Space/Updater.php @@ -128,6 +128,17 @@ class Updater return true; } + public static function deleteFiles($json) + { + $files = json_decode($json); + + foreach($files as $file) { + \File::delete(base_path($file)); + } + + return true; + } + public static function migrateUpdate() { Artisan::call('migrate --force');