Skip to content

Commit 028aec1

Browse files
committed
wip
1 parent 8bbef24 commit 028aec1

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

config/nativephp-internal.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@
2929
* The URL to the NativePHP API.
3030
*/
3131
'api_url' => env('NATIVEPHP_API_URL', 'http://localhost:4000/api/'),
32+
33+
'zephpyr' => [
34+
'host' => env('ZEPHPYR_HOST', 'zephpyr.com'),
35+
'key' => env('ZEPHPYR_SECRET'),
36+
],
3237
];

src/Commands/BundleCommand.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

src/NativeServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\Artisan;
99
use Illuminate\Support\Facades\DB;
1010
use Native\Laravel\ChildProcess as ChildProcessImplementation;
11+
use Native\Laravel\Commands\BundleCommand;
1112
use Native\Laravel\Commands\FreshCommand;
1213
use Native\Laravel\Commands\LoadPHPConfigurationCommand;
1314
use Native\Laravel\Commands\LoadStartupConfigurationCommand;
@@ -36,6 +37,7 @@ public function configurePackage(Package $package): void
3637
FreshCommand::class,
3738
SeedDatabaseCommand::class,
3839
MinifyApplicationCommand::class,
40+
BundleCommand::class,
3941
])
4042
->hasConfigFile()
4143
->hasRoute('api')

0 commit comments

Comments
 (0)