From d276cb73833ee7807a776c8ac540fcb6383b5705 Mon Sep 17 00:00:00 2001 From: Henry Whitaker Date: Fri, 10 Apr 2020 15:55:19 +0100 Subject: [PATCH] Finished update functions Should hopefully work well. Doesn't clean up atm, but all the new files are stored in /tmp/ so not that important --- app/Helpers/UpdateHelper.php | 79 ++++++++++++++++++++++++++++++++++-- config/speedtest.php | 28 +++++++++++++ 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/app/Helpers/UpdateHelper.php b/app/Helpers/UpdateHelper.php index 9f8b08fd..3fe46756 100644 --- a/app/Helpers/UpdateHelper.php +++ b/app/Helpers/UpdateHelper.php @@ -145,17 +145,18 @@ class UpdateHelper { $this->deleteExcluded($dir); $this->backupCurrent(); + $this->moveFiles(); Log::info('Successfully applied update'); } private function deleteExcluded($path) { - Log::info('Deleting excluded items'); + Log::info('Deleting excluded items from update directory'); $exclude_dirs = config('speedtest.exclude_dirs', []); foreach($exclude_dirs as $dir) { $dir = $path . $dir; - Log::info('Deleting excluded directory: ' . $dir); + Log::debug('Deleting excluded directory: ' . $dir); File::deleteDirectory($dir); } @@ -163,11 +164,11 @@ class UpdateHelper { $exclude_files = config('speedtest.exclude_files', []); foreach($exclude_files as $file) { $file = $path . $file; - Log::info('Deleting excluded file: ' . $file); + Log::debug('Deleting excluded file: ' . $file); File::delete($file); } - Log::info('Excluded items deleted'); + Log::info('Excluded items deleted from update directory'); } private function backupCurrent() @@ -209,6 +210,76 @@ class UpdateHelper { private function moveFiles() { + $new = array_filter(glob('/tmp/'.$this->repo.'-update/*'), 'is_dir'); + $new = $new[0].DIRECTORY_SEPARATOR; + + foreach(File::files($new) as $file) { + $filename = explode('/', $file); + $filename = array_slice($filename, -1)[0]; + try { + Log::info('Overwriting ' . $filename); + Log::debug('From: ' . $file . ' to: ' . base_path().DIRECTORY_SEPARATOR.$filename); + File::delete(base_path().DIRECTORY_SEPARATOR.$filename); + File::move($file, base_path().DIRECTORY_SEPARATOR.$filename); + } catch(Exception $e) { + Log::error('Failed to overwrite: ' . $filename); + Log::debug($e); + } + } + + $this->tempStoreExcludedFiles(); + + foreach(File::directories($new) as $dir) { + $dirname = explode('/', $dir); + $dirname = array_slice($dirname, -1)[0]; + Log::info('Overwriting ' . $dir); + File::deleteDirectory(base_path().DIRECTORY_SEPARATOR.$dirname); + File::move($dir, base_path().DIRECTORY_SEPARATOR.$dirname); + } + + $this->restoreExcludedFiles(); } + + private function tempStoreExcludedFiles() + { + Log::info('Temporarily moving exluded files from root directory'); + foreach(config('speedtest.exclude_files', []) as $file) { + try { + Log::info('Moving ' . $file); + File::copy(base_path().DIRECTORY_SEPARATOR.$file, '/tmp/'.$file); + } catch(Exception $e) { + Log::error('Couldn\'t backup '.$file); + } + } + foreach(config('speedtest.exclude_dirs', []) as $dir) { + try { + Log::info('Moving ' . $dir); + File::copyDirectory(base_path().DIRECTORY_SEPARATOR.$dir, '/tmp/'.$dir); + } catch(Exception $e) { + Log::error('Couldn\'t backup '.$dir); + } + } + } + + private function restoreExcludedFiles() + { + Log::info('Restoring exluded files to root directory'); + foreach(config('speedtest.exclude_files', []) as $file) { + try { + Log::info('Moving ' . $file); + File::copy('/tmp/'.$file, base_path().DIRECTORY_SEPARATOR.$file); + } catch(Exception $e) { + Log::error('Couldn\'t restore '.$file); + } + } + foreach(config('speedtest.exclude_dirs', []) as $dir) { + try { + Log::info('Moving ' . $dir); + File::copyDirectory('/tmp/'.$dir, base_path().DIRECTORY_SEPARATOR.$dir); + } catch(Exception $e) { + Log::error('Couldn\' restore ' . $dir); + } + } + } } diff --git a/config/speedtest.php b/config/speedtest.php index a49e37b9..3c87f73f 100644 --- a/config/speedtest.php +++ b/config/speedtest.php @@ -18,4 +18,32 @@ return [ 'user' => 'henrywhitaker3', 'repo' => 'Speedtest-Tracker', 'branch' => 'master', + + /* + |-------------------------------------------------------------------------- + | Excluded Dirs + |-------------------------------------------------------------------------- + | + | Dirs excluded from the self-updating function + */ + + 'exclude_dirs' => [ + 'node_modules/', + 'bootstrap/cache/', + 'bower/', + 'storage/', + 'vendor/', + ], + + /* + |-------------------------------------------------------------------------- + | Excluded Files + |-------------------------------------------------------------------------- + | + | Files excluded from the self-updating function + */ + + 'exclude_files' => [ + 'database/speed.db', + ], ];