From 047235b84d15a26bebaad50f6751b61e63fde3f8 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Thu, 17 Apr 2025 20:53:18 +0200 Subject: [PATCH] [TASK] Reorganize and cleanup --- Classes/Config/Deployment.php | 1 + Classes/Tasks/AbstractTasks.php | 18 ++++----- Classes/Tasks/Deploy.php | 58 ++++++++++++++--------------- Classes/Tasks/Local.php | 66 ++++++++++++++++----------------- Classes/Tasks/Remote.php | 28 +++++++------- Classes/Tasks/Rsync.php | 6 +-- 6 files changed, 87 insertions(+), 90 deletions(-) diff --git a/Classes/Config/Deployment.php b/Classes/Config/Deployment.php index 41e361c..baab032 100644 --- a/Classes/Config/Deployment.php +++ b/Classes/Config/Deployment.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Evoweb\DeployerConfig\Config; +use Deployer\Deployer; use Deployer\Exception\Exception; use Evoweb\DeployerConfig\Tasks\Deploy; use Evoweb\DeployerConfig\Tasks\Local; diff --git a/Classes/Tasks/AbstractTasks.php b/Classes/Tasks/AbstractTasks.php index cfe5a48..16935a9 100644 --- a/Classes/Tasks/AbstractTasks.php +++ b/Classes/Tasks/AbstractTasks.php @@ -8,6 +8,7 @@ use Deployer\Deployer; use Deployer\Task\GroupTask; use Deployer\Task\Task; use Evoweb\DeployerConfig\Config\Deployment; +use InvalidArgumentException; use phpDocumentor\Reflection\DocBlockFactory; use ReflectionMethod; @@ -36,11 +37,14 @@ abstract class AbstractTasks array_walk($this->tasks, [$this, 'registerTask']); } - protected function registerTask(array $config, string $name): void + protected function registerTask(array|string $body, string $name): void { - $body = $config['body'] ?? ''; - if (is_string($body) && !method_exists($this, $body)) { - return; + if (!is_array($body)) { + $name = $body; + $body = lcfirst(str_replace('_', '', ucwords(strtolower(preg_replace('/[^:]+:/', '', $body)), '_'))); + if (!method_exists($this, $body)) { + print_r('not found ' . $body . "\n"); + } } if (is_callable([$this, $body])) { @@ -53,13 +57,9 @@ abstract class AbstractTasks } elseif (is_array($body)) { $task = new GroupTask($name, $body); } else { - throw new \InvalidArgumentException('Task body should be a function or an array.'); + throw new InvalidArgumentException('Task body should be a function or an array.'); } - array_map(function ($selector) use ($task) { - $task->addSelector($selector); - }, $config['stages'] ?? []); - $deployer = Deployer::get(); $deployer->tasks->set($name, $task); } diff --git a/Classes/Tasks/Deploy.php b/Classes/Tasks/Deploy.php index 82df28e..681fd2a 100644 --- a/Classes/Tasks/Deploy.php +++ b/Classes/Tasks/Deploy.php @@ -10,40 +10,36 @@ class Deploy extends AbstractTasks { protected array $tasks = [ 'deploy:prepare' => [ - 'body' => [ - 'local:info', - 'local:setup', - 'remote:prepare', - 'rsync:warmup', - 'local:lock', - 'local:release', - 'local:update_code', - 'local:env', - 'local:shared', - 'local:writable', - 'local:write_release', - 'local:create_folder', - 'local:vendors', - ] + 'local:info', + 'local:setup', + 'remote:prepare', + 'rsync:warmup', + 'local:lock', + 'local:release', + 'local:update_code', + 'local:env', + 'local:shared', + 'local:writable', + 'local:write_release', + 'local:create_folder', + 'local:vendors', ], 'deploy:publish' => [ - 'body' => [ - 'local:clear_paths', - 'local:symlink', - 'local:cleanup', - 'rsync:remote', - 'remote:change_group', - 'remote:writable', - 'remote:db_compare', - 'remote:fix_folder_structure', - 'rsync:switch_current', - 'remote:clear_cache', - 'remote:clear_opcache', - 'local:echo_release_number', - 'local:unlock', - 'deploy:success' - ] + 'local:clear_paths', + 'local:symlink', + 'local:cleanup', + 'rsync:remote', + 'remote:change_group', + 'remote:writable', + 'remote:db_compare', + 'remote:fix_folder_structure', + 'rsync:switch_current', + 'remote:clear_cache', + 'remote:clear_opcache', + 'local:echo_release_number', + 'local:unlock', + 'deploy:success', ], ]; diff --git a/Classes/Tasks/Local.php b/Classes/Tasks/Local.php index f31b5f8..b01e7f4 100644 --- a/Classes/Tasks/Local.php +++ b/Classes/Tasks/Local.php @@ -24,28 +24,28 @@ use function Deployer\timestamp; class Local extends AbstractTasks { protected array $tasks = [ - 'local:info' => ['body' => 'localInfo'], - 'local:setup' => ['body' => 'localSetup'], - 'local:lock' => ['body' => 'localLock'], - 'local:unlock' => ['body' => 'localUnlock'], - 'local:release' => ['body' => 'localRelease'], - 'local:update_code' => ['body' => 'localUpdateCode'], - 'local:env' => ['body' => 'localEnv'], - 'local:shared' => ['body' => 'localShared'], - 'local:writable' => ['body' => 'localWritable'], - 'local:write_release' => ['body' => 'localWriteRelease'], - 'local:create_folder' => ['body' => 'localCreateFolder'], - 'local:vendors' => ['body' => 'localVendors'], - 'local:clear_paths' => ['body' => 'localClearPaths'], - 'local:symlink' => ['body' => 'localSymlink'], - 'local:cleanup' => ['body' => 'localCleanup'], - 'local:echo_release_number' => ['body' => 'localEchoReleaseNumber'], + 'local:info', + 'local:setup', + 'local:lock', + 'local:unlock', + 'local:release', + 'local:update_code', + 'local:env', + 'local:shared', + 'local:writable', + 'local:write_release', + 'local:create_folder', + 'local:vendors', + 'local:clear_paths', + 'local:symlink', + 'local:cleanup', + 'local:echo_release_number', ]; /** * Displays info about deployment */ - public function localInfo(): void + public function info(): void { info("deploying {{what}} to {{where}}"); } @@ -53,7 +53,7 @@ class Local extends AbstractTasks /** * Prepares host for deploy */ - public function localSetup(): void + public function setup(): void { runLocally( << {{deploy_path}}/.dep/deploy.lock"); @@ -91,7 +91,7 @@ class Local extends AbstractTasks /** * Unlocks deploy */ - public function localUnlock(): void + public function unlock(): void { // always success runLocally("rm -f {{deploy_path}}/.dep/deploy.lock"); @@ -100,7 +100,7 @@ class Local extends AbstractTasks /** * Prepares release */ - public function localRelease(): void + public function release(): void { // Clean up if there is unfinished release. if (testLocally('[ -h {{deploy_path}}/release ]')) { @@ -163,7 +163,7 @@ class Local extends AbstractTasks /** * Updates code */ - public function localUpdateCode(): void + public function updateCode(): void { $git = get('bin/git'); $repository = get('repository'); @@ -216,7 +216,7 @@ class Local extends AbstractTasks /** * Configure .env file */ - public function localEnv(): void + public function env(): void { if (testLocally('[ ! -e {{release_or_current_path}}/.env ] && [ -f {{release_or_current_path}}/{{dotenv_example}} ]')) { runLocally('cp {{release_or_current_path}}/{{dotenv_example}} {{release_or_current_path}}/.env'); @@ -226,7 +226,7 @@ class Local extends AbstractTasks /** * Creates symlinks for shared files and dirs */ - public function localShared(): void + public function shared(): void { $sharedPath = "{{deploy_path}}/shared"; @@ -298,7 +298,7 @@ class Local extends AbstractTasks /** * Makes writable dirs */ - public function localWritable(): void + public function writable(): void { $dirs = join(' ', get('writable_dirs')); $mode = get('writable_mode'); @@ -385,15 +385,15 @@ class Local extends AbstractTasks } } - public function localWriteRelease(): void + public function writeRelease(): void { - runLocally('echo ' . getEnv('CI_COMMIT_REF_NAME') . ' > {{release_path}}/release'); + runLocally('echo {{target}} > {{release_path}}/release'); } /** * Creating necessary folders */ - public function localCreateFolder(): void + public function createFolder(): void { $sudo = $this->getSudo(); @@ -411,7 +411,7 @@ class Local extends AbstractTasks /** * Installing vendors */ - public function localVendors(): void + public function vendors(): void { if (!$this->commandExist('unzip')) { info( @@ -429,7 +429,7 @@ class Local extends AbstractTasks /** * Cleanup files and/or directories */ - public function localClearPaths(): void + public function clearPaths(): void { $paths = get('clear_paths'); $sudo = get('clear_use_sudo') ? 'sudo' : ''; @@ -448,7 +448,7 @@ class Local extends AbstractTasks /** * Creating symlink to release */ - public function localSymlink(): void + public function symlink(): void { if (get('use_atomic_symlink')) { runLocally("mv -T {{deploy_path}}/release {{current_path}}"); @@ -464,7 +464,7 @@ class Local extends AbstractTasks /** * Cleaning up old releases */ - public function localCleanup(): void + public function cleanup(): void { $releases = get('releases_list'); $keep = get('keep_releases'); @@ -479,7 +479,7 @@ class Local extends AbstractTasks } } - public function localEchoReleaseNumber(): void + public function echoReleaseNumber(): void { info('Folder of this release is: {{release_path}}'); } diff --git a/Classes/Tasks/Remote.php b/Classes/Tasks/Remote.php index 3304462..e8c0a3d 100644 --- a/Classes/Tasks/Remote.php +++ b/Classes/Tasks/Remote.php @@ -14,19 +14,19 @@ use function Deployer\writeln; class Remote extends AbstractTasks { protected array $tasks = [ - 'remote:prepare' => ['body' => 'remotePrepare'], - 'remote:change_group' => ['body' => 'remoteChangeGroup'], - 'remote:writable' => ['body' => 'remoteWritable'], - 'remote:db_compare' => ['body' => 'remoteDbCompare'], - 'remote:fix_folder_structure' => ['body' => 'remoteFixFolderStructure'], - 'remote:clear_cache' => ['body' => 'remoteClearCache'], - 'remote:clear_opcache' => ['body' => 'remoteClearOpcache'], + 'remote:prepare', + 'remote:change_group', + 'remote:writable', + 'remote:db_compare', + 'remote:fix_folder_structure', + 'remote:clear_cache', + 'remote:clear_opcache', ]; /** * Prepare remote path exists */ - public function remotePrepare(): void + public function prepare(): void { if (!$this->hasArray('prepare_dirs')) { return; @@ -43,7 +43,7 @@ class Remote extends AbstractTasks /** * Change usergroup for the hole project */ - public function remoteChangeGroup(): void + public function changeGroup(): void { if ($this->get('http_group')) { $sudo = get('writable_use_sudo') ? 'sudo' : ''; @@ -61,7 +61,7 @@ class Remote extends AbstractTasks /** * Set files and/or folders writable */ - public function remoteWritable(): void + public function writable(): void { if ($this->hasArray('writable_dirs') || $this->hasArray('writable_files')) { $sudo = get('writable_use_sudo') ? 'sudo' : ''; @@ -104,7 +104,7 @@ class Remote extends AbstractTasks /** * Database migration */ - public function remoteDbCompare(): void + public function dbCompare(): void { $path = parse('{{app_container_path}}') ?: parse('{{remote_path}}'); @@ -122,7 +122,7 @@ class Remote extends AbstractTasks /** * Fix folder structure */ - public function remoteFixFolderStructure(): void + public function fixFolderStructure(): void { $path = parse('{{app_container_path}}') ?: parse('{{remote_path}}'); @@ -140,7 +140,7 @@ class Remote extends AbstractTasks /** * Clear cache via typo3 cache:flush */ - public function remoteClearCache(): void + public function clearCache(): void { $path = parse('{{app_container_path}}') ?: parse('{{remote_path}}'); @@ -156,7 +156,7 @@ class Remote extends AbstractTasks /** * Clear opcache via curl on {CI_HOST}/cache.php */ - public function remoteClearOpcache(): void + public function clearOpcache(): void { $webDomain = rtrim($this->get('web_domain'), '/'); $htaccess = $this->has('htaccess') ? '--user ' . $this->get('htaccess') : ''; diff --git a/Classes/Tasks/Rsync.php b/Classes/Tasks/Rsync.php index 7b9c74d..8e2c5ac 100644 --- a/Classes/Tasks/Rsync.php +++ b/Classes/Tasks/Rsync.php @@ -42,9 +42,9 @@ class Rsync extends AbstractTasks ]; protected array $tasks = [ - 'rsync:warmup' => ['body' => 'warmup'], - 'rsync:remote' => ['body' => 'remote'], - 'rsync:switch_current' => ['body' => 'switchCurrent'], + 'rsync:warmup', + 'rsync:remote', + 'rsync:switch_current', ]; protected function setDefaultConfiguration(): void