|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Moox\Devlink\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | + |
| 7 | +class DeployPackages extends Command |
| 8 | +{ |
| 9 | + protected $signature = 'devlink:deploy'; |
| 10 | + |
| 11 | + protected $description = 'Symlink Moox packages into the project from multiple base paths and ensure composer.json is updated'; |
| 12 | + |
| 13 | + protected array $basePaths; |
| 14 | + |
| 15 | + protected array $packages; |
| 16 | + |
| 17 | + protected string $composerJsonPath; |
| 18 | + |
| 19 | + protected string $packagesPath; |
| 20 | + |
| 21 | + public function __construct() |
| 22 | + { |
| 23 | + parent::__construct(); |
| 24 | + |
| 25 | + $this->basePaths = config('devlink.base_paths', []); |
| 26 | + $this->packages = config('devlink.packages', []); |
| 27 | + |
| 28 | + if (empty($this->basePaths)) { |
| 29 | + $this->warn('No base paths configured in config/devlink.php'); |
| 30 | + } |
| 31 | + |
| 32 | + if (empty($this->packages)) { |
| 33 | + $this->warn('No packages configured in config/devlink.php'); |
| 34 | + } |
| 35 | + |
| 36 | + $this->composerJsonPath = base_path('composer.json'); |
| 37 | + $this->packagesPath = config('devlink.packages_path', base_path('packages')); |
| 38 | + } |
| 39 | + |
| 40 | + public function handle(): void |
| 41 | + { |
| 42 | + $this->art(); |
| 43 | + $this->hello(); |
| 44 | + $this->checks(); |
| 45 | + $this->removeAllSymlinks(); |
| 46 | + $this->removePackagesDirectoryIfEmpty(); |
| 47 | + $this->restoreComposerJson(); |
| 48 | + $this->runComposerUpdate(); |
| 49 | + $this->optimizeClear(); |
| 50 | + $this->queueRestart(); |
| 51 | + $this->goodbye(); |
| 52 | + } |
| 53 | + |
| 54 | + public function art(): void |
| 55 | + { |
| 56 | + $this->info(' |
| 57 | +
|
| 58 | + ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ |
| 59 | + ▓▓▒░░▒▓▓▒▒░░░░░░▒▒▓▓▓▒░░░░░░░▒▓▓ ▓▓▓▓▒░░░░░░░▒▓▓▓▓ ▓▓▓▓▓▒░░░░░░░▒▒▓▓▓▓▓▒▒▒▒▓▓ ▓▓▓▒▒▒▒▓▓ |
| 60 | + ▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓░░░░░▒▓▓ ▓▓▒░░░░░▓▓ |
| 61 | + ▓▒░░░░░░▒▓▓▓▓▒░░░░░░░▒▓▓▓▓░░░░░▒▓▓▓░░░░░▒▓▓▓▓▒░░░░░░░▓▓▓▓░░░░░░▒▓▓▓▓▓░░░░░░▒▓▓░░░░░▒▓▓▓▓▓░░░░░▒▓▓ |
| 62 | + ▓▒░░░░▓▓▓▓ ▓▓░░░░░▓▓▓ ▓▓▓░░░░▒▓▓░░░░▒▓▓▓ ▓▓▓▓░░░░░▓░░░░░░▓▓▓▓ ▓▓▓▒░░░░▓▓▓▒░░░░░▓▓▓░░░░░▓▓▓ |
| 63 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓▓ ▓▓▓░░▒░░░░░▓▓▓ ▓▓░░░░▒▓▓▓▓░░░░░░░░░░░▓▓ |
| 64 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓ ▓▓▓░░░░░▒▓▓ ▓▓▒░░░░▓ ▓▓▓░░░░░░░░░▓▓ |
| 65 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓▓ ▓▓▒░░░░░▒░░▒▓▓ ▓▓░░░░▒▓▓▓▒░░░░░▒░░░░░▒▓ |
| 66 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓░░░░▒▓▓▓ ▓▓▓▒░░░░░▒▒░░░░░▒▓▓▓ ▓▓▓░░░░░▓▓▓░░░░░▒▓▓▓░░░░░▒▓▓ |
| 67 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓▓░░░░░░▒▒▓▓▒░░░░░░▒▓▓▓▓░░░░░░░▒▒▓▓▒░░░░░░▓▓▓░░░░░▒▓▓▓▓▓▒░░░░░▓▓ |
| 68 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▒░░░░░▓▓▓ ▓▓▒░░░░░▒▓ |
| 69 | + ▓▓░░░▒▓▓ ▓▓▒░░░▒▓▓ ▓▓░░░░▓▓ ▓▓▓▓▒░░░░░░▒▒▓▓▓▓ ▓▓▓▓▓▒▒░░░░░▒▒▓▓▓▓▓░░░░▒▓▓ ▓▓▓░░░░▒▓ |
| 70 | + ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ |
| 71 | +
|
| 72 | + '); |
| 73 | + } |
| 74 | + |
| 75 | + private function hello(): void |
| 76 | + { |
| 77 | + $this->info('Hello, I will prepare your project for deployment.'); |
| 78 | + } |
| 79 | + |
| 80 | + private function checks(): void |
| 81 | + { |
| 82 | + $this->line("\nConfiguration:"); |
| 83 | + $this->line('Base paths:'); |
| 84 | + if (empty($this->basePaths)) { |
| 85 | + $this->warn('- No base paths configured'); |
| 86 | + } else { |
| 87 | + foreach ($this->basePaths as $path) { |
| 88 | + $resolvedPath = $this->resolvePath($path); |
| 89 | + $this->line("- $path"); |
| 90 | + $this->line(" → $resolvedPath".(is_dir($resolvedPath) ? ' (exists)' : ' (not found)')); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + $this->line("\nConfigured packages:"); |
| 95 | + if (empty($this->packages)) { |
| 96 | + $this->warn('No packages configured in config/devlink.php'); |
| 97 | + } else { |
| 98 | + foreach ($this->packages as $package) { |
| 99 | + $this->line("- $package"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $this->line(''); |
| 104 | + } |
| 105 | + |
| 106 | + private function removeAllSymlinks(): void |
| 107 | + { |
| 108 | + if (is_dir($this->packagesPath)) { |
| 109 | + foreach (scandir($this->packagesPath) as $item) { |
| 110 | + if ($item !== '.' && $item !== '..' && is_link("$this->packagesPath/$item")) { |
| 111 | + unlink("$this->packagesPath/$item"); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private function removePackagesDirectoryIfEmpty(): void |
| 118 | + { |
| 119 | + if (is_dir($this->packagesPath) && count(scandir($this->packagesPath)) === 2) { |
| 120 | + rmdir($this->packagesPath); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private function restoreComposerJson(): void |
| 125 | + { |
| 126 | + $deployFile = $this->composerJsonPath.'-deploy'; |
| 127 | + if (! file_exists($deployFile)) { |
| 128 | + $this->error('composer.json-deploy not found!'); |
| 129 | + |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + unlink($this->composerJsonPath); |
| 134 | + rename($deployFile, $this->composerJsonPath); |
| 135 | + $this->info('Restored composer.json from composer.json-deploy'); |
| 136 | + } |
| 137 | + |
| 138 | + private function runComposerUpdate(): void |
| 139 | + { |
| 140 | + if ($this->confirm('Run composer update now?', true)) { |
| 141 | + $output = []; |
| 142 | + $returnVar = 0; |
| 143 | + exec('composer update 2>&1', $output, $returnVar); |
| 144 | + |
| 145 | + if ($returnVar !== 0) { |
| 146 | + $this->error('Composer update failed: '.implode("\n", $output)); |
| 147 | + |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + $this->info('Composer update completed successfully'); |
| 152 | + } else { |
| 153 | + $this->info("Please run 'composer update' manually"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private function optimizeClear(): void |
| 158 | + { |
| 159 | + if ($this->confirm('Run artisan optimize:clear now?', true)) { |
| 160 | + $this->info('Clearing cache...'); |
| 161 | + $this->call('optimize:clear'); |
| 162 | + $this->info('Cache cleared successfully'); |
| 163 | + } else { |
| 164 | + $this->info("Please run 'artisan optimize:clear' manually"); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private function queueRestart(): void |
| 169 | + { |
| 170 | + if ($this->confirm('Run queue:restart now?', false)) { |
| 171 | + $this->info('Restarting queue...'); |
| 172 | + $this->call('queue:restart'); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private function goodbye(): void |
| 177 | + { |
| 178 | + $this->info('Have a nice dev!'); |
| 179 | + } |
| 180 | + |
| 181 | + private function resolvePath(string $path): string |
| 182 | + { |
| 183 | + return str_starts_with($path, '~/') ? str_replace('~', getenv('HOME'), $path) : rtrim(realpath($path) ?: $path, '/'); |
| 184 | + } |
| 185 | +} |
0 commit comments