|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Native\Laravel\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Native\Electron\Traits\CleansEnvFile; |
| 7 | +use Native\Laravel\NativeServiceProvider; |
| 8 | +use Symfony\Component\Finder\Finder; |
| 9 | +use ZipArchive; |
| 10 | + |
| 11 | +class BundleCommand extends Command |
| 12 | +{ |
| 13 | + use CleansEnvFile; |
| 14 | + |
| 15 | + protected $name = 'native:bundle'; |
| 16 | + |
| 17 | + protected $description = 'Bundle your application for distribution.'; |
| 18 | + |
| 19 | + private ?string $key; |
| 20 | + private string $zipPath; |
| 21 | + private string $zipName; |
| 22 | + |
| 23 | + public function handle() |
| 24 | + { |
| 25 | + $this->key = config('nativephp-internal.zephpyr.key'); |
| 26 | + |
| 27 | + if (!$this->key) { |
| 28 | + $this->line(''); |
| 29 | + $this->warn('No ZEPHPYR_SECRET found. Cannot bundle!'); |
| 30 | + $this->line(''); |
| 31 | + $this->line('Add this app\'s ZEPHPYR_SECRET to its .env file:'); |
| 32 | + $this->line(base_path('.env')); |
| 33 | + $this->line(''); |
| 34 | + $this->info('Not set up with Zephpyr yet? Secure your NativePHP app builds and more!'); |
| 35 | + $this->info('Check out https://zephpyr.com'); |
| 36 | + $this->line(''); |
| 37 | + |
| 38 | + return static::FAILURE; |
| 39 | + } |
| 40 | + |
| 41 | + // Package the app up into a zip |
| 42 | + if (! $this->zipApplication()) { |
| 43 | + $this->error("Failed to create zip archive at {$this->zipPath}."); |
| 44 | + return static::FAILURE; |
| 45 | + } |
| 46 | + |
| 47 | + // Send the zip file |
| 48 | + if (! $this->sendToZephpyr()) { |
| 49 | + $this->error("Failed to upload zip [{$this->zipPath}] to Zephpyr."); |
| 50 | + return static::FAILURE; |
| 51 | + } |
| 52 | + |
| 53 | + return static::SUCCESS; |
| 54 | + } |
| 55 | + |
| 56 | + private function zipApplication(): bool |
| 57 | + { |
| 58 | + $this->zipName = 'app_' . str()->random(8) . '.zip'; |
| 59 | + $this->zipPath = storage_path($this->zipName); |
| 60 | + |
| 61 | + $zip = new ZipArchive; |
| 62 | + |
| 63 | + if ($zip->open($this->zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + $this->prepareNativeEnv(); |
| 68 | + |
| 69 | + $this->addFilesToZip($zip); |
| 70 | + |
| 71 | + $zip->close(); |
| 72 | + |
| 73 | + $this->restoreWebEnv(); |
| 74 | + |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + private function addFilesToZip(ZipArchive $zip): void |
| 79 | + { |
| 80 | + $app = (new Finder())->files() |
| 81 | + ->followLinks() |
| 82 | + ->ignoreVCSIgnored(true) |
| 83 | + ->in(base_path()) |
| 84 | + ->exclude([ |
| 85 | + 'tests', |
| 86 | + ...config('nativephp.cleanup_exclude_files', []), |
| 87 | + ]); |
| 88 | + |
| 89 | + $this->finderToZip($app, $zip); |
| 90 | + |
| 91 | + $vendor = (new Finder())->files() |
| 92 | + ->exclude([ |
| 93 | + 'vendor/nativephp/php-bin', |
| 94 | + ]) |
| 95 | + ->in(base_path('vendor')); |
| 96 | + |
| 97 | + $this->finderToZip($vendor, $zip); |
| 98 | + |
| 99 | + $nodeModules = (new Finder())->files() |
| 100 | + ->in(base_path('node_modules')); |
| 101 | + |
| 102 | + $this->finderToZip($nodeModules, $zip); |
| 103 | + |
| 104 | + $env = (new Finder())->files() |
| 105 | + ->ignoreDotFiles(false) |
| 106 | + ->name('.env') |
| 107 | + ->in(base_path()); |
| 108 | + |
| 109 | + $this->finderToZip($env, $zip); |
| 110 | + } |
| 111 | + |
| 112 | + private function finderToZip(Finder $finder, ZipArchive $zip): void |
| 113 | + { |
| 114 | + foreach ($finder as $file) { |
| 115 | + dump([$file->getRealPath(), $file->getRelativePath()]); |
| 116 | + $zip->addFile($file->getRealPath(), $file->getRelativePathname()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private function sendToZephpyr(): bool |
| 121 | + { |
| 122 | + return false; |
| 123 | + $response = Http::attach('archive', fopen($this->zipPath, 'r'), $this->zipName) |
| 124 | + ->post(config('nativephp-internal.zephpyr.host'), [ |
| 125 | + 'key' => $this->key, |
| 126 | + ]); |
| 127 | + |
| 128 | + return $response->successful(); |
| 129 | + } |
| 130 | +} |
0 commit comments