Skip to content

Commit 6cdf785

Browse files
simonhampSRWieZ
authored andcommitted
wip
1 parent 8b929a6 commit 6cdf785

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

config/nativephp-internal.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
'zephpyr' => [
3434
'host' => env('ZEPHPYR_HOST', 'zephpyr.com'),
35-
'key' => env('ZEPHPYR_SECRET'),
35+
'token' => env('ZEPHPYR_TOKEN'),
36+
'key' => env('ZEPHPYR_KEY'),
3637
],
3738
];

config/nativephp.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* It is used to determine if the app needs to be updated.
77
* Increment this value every time you release a new version of your app.
88
*/
9-
'version' => env('NATIVEPHP_APP_VERSION', '1.0.0'),
9+
'version' => env('NATIVEPHP_APP_VERSION', 1),
1010

1111
/**
1212
* The ID of your application. This should be a unique identifier
@@ -47,6 +47,7 @@
4747
'AWS_*',
4848
'GITHUB_*',
4949
'DO_SPACES_*',
50+
'ZEPHPYR_*',
5051
'*_SECRET',
5152
'NATIVEPHP_UPDATER_PATH',
5253
'NATIVEPHP_APPLE_ID',

src/Commands/BundleCommand.php

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33
namespace Native\Laravel\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Http;
67
use Native\Electron\Traits\CleansEnvFile;
7-
use Native\Laravel\NativeServiceProvider;
88
use Symfony\Component\Finder\Finder;
99
use ZipArchive;
1010

1111
class BundleCommand extends Command
1212
{
1313
use CleansEnvFile;
1414

15-
protected $name = 'native:bundle';
15+
protected $signature = 'native:bundle {--fetch}';
1616

1717
protected $description = 'Bundle your application for distribution.';
1818

1919
private ?string $key;
20+
2021
private string $zipPath;
22+
2123
private string $zipName;
2224

2325
public function handle()
2426
{
2527
$this->key = config('nativephp-internal.zephpyr.key');
2628

27-
if (!$this->key) {
29+
if (! $this->key) {
2830
$this->line('');
29-
$this->warn('No ZEPHPYR_SECRET found. Cannot bundle!');
31+
$this->warn('No ZEPHPYR_KEY found. Cannot bundle!');
3032
$this->line('');
31-
$this->line('Add this app\'s ZEPHPYR_SECRET to its .env file:');
33+
$this->line('Add this app\'s ZEPHPYR_KEY to its .env file:');
3234
$this->line(base_path('.env'));
3335
$this->line('');
3436
$this->info('Not set up with Zephpyr yet? Secure your NativePHP app builds and more!');
@@ -38,24 +40,43 @@ public function handle()
3840
return static::FAILURE;
3941
}
4042

43+
if ($this->option('fetch')) {
44+
if (! $this->fetchLatestBundle()) {
45+
$this->warn("Latest bundle not yet available. Try again soon.");
46+
return static::FAILURE;
47+
}
48+
49+
$this->info("Latest bundle downloaded.");
50+
return static::SUCCESS;
51+
}
52+
4153
// Package the app up into a zip
4254
if (! $this->zipApplication()) {
4355
$this->error("Failed to create zip archive at {$this->zipPath}.");
56+
4457
return static::FAILURE;
4558
}
4659

4760
// Send the zip file
48-
if (! $this->sendToZephpyr()) {
61+
dd($result = $this->sendToZephpyr());
62+
63+
if ($result->failed()) {
4964
$this->error("Failed to upload zip [{$this->zipPath}] to Zephpyr.");
65+
5066
return static::FAILURE;
5167
}
5268

69+
@unlink($this->zipPath);
70+
71+
$this->info('Successfully uploaded to Zephpyr.');
72+
$this->line('Use native:bundle --fetch to retrieve the latest bundle.');
73+
5374
return static::SUCCESS;
5475
}
5576

5677
private function zipApplication(): bool
5778
{
58-
$this->zipName = 'app_' . str()->random(8) . '.zip';
79+
$this->zipName = 'app_'.str()->random(8).'.zip';
5980
$this->zipPath = storage_path($this->zipName);
6081

6182
$zip = new ZipArchive;
@@ -77,54 +98,68 @@ private function zipApplication(): bool
7798

7899
private function addFilesToZip(ZipArchive $zip): void
79100
{
80-
$app = (new Finder())->files()
101+
// TODO: Check the composer.json to make sure there are no symlinked or private packages as these will be a
102+
// pain later
103+
104+
$app = (new Finder)->files()
81105
->followLinks()
82106
->ignoreVCSIgnored(true)
83107
->in(base_path())
84108
->exclude([
109+
'vendor',
110+
'dist',
111+
'build',
85112
'tests',
86113
...config('nativephp.cleanup_exclude_files', []),
87114
]);
88115

89116
$this->finderToZip($app, $zip);
90117

91-
$vendor = (new Finder())->files()
118+
$vendor = (new Finder)->files()
92119
->exclude([
93-
'vendor/nativephp/php-bin',
120+
'nativephp/php-bin',
121+
'nativephp/electron/resources/js',
122+
'nativephp/*/vendor',
94123
])
95124
->in(base_path('vendor'));
96125

97-
$this->finderToZip($vendor, $zip);
126+
$this->finderToZip($vendor, $zip, 'vendor');
98127

99-
$nodeModules = (new Finder())->files()
128+
$nodeModules = (new Finder)->files()
100129
->in(base_path('node_modules'));
101130

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);
131+
$this->finderToZip($nodeModules, $zip, 'node_modules');
110132
}
111133

112-
private function finderToZip(Finder $finder, ZipArchive $zip): void
134+
private function finderToZip(Finder $finder, ZipArchive $zip, ?string $path = null): void
113135
{
114136
foreach ($finder as $file) {
115-
dump([$file->getRealPath(), $file->getRelativePath()]);
116-
$zip->addFile($file->getRealPath(), $file->getRelativePathname());
137+
if ($file->getRealPath() === false) {
138+
continue;
139+
}
140+
141+
$zip->addFile($file->getRealPath(), str($path)->finish(DIRECTORY_SEPARATOR) . $file->getRelativePathname());
117142
}
118143
}
119144

120-
private function sendToZephpyr(): bool
145+
private function sendToZephpyr()
121146
{
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-
]);
147+
return Http::withToken(config('nativephp-internal.zephpyr.token'))
148+
->attach('archive', fopen($this->zipPath, 'r'), $this->zipName)
149+
->post(str(config('nativephp-internal.zephpyr.host'))->finish('/') . 'api/build/' . $this->key);
150+
}
151+
152+
private function fetchLatestBundle(): bool
153+
{
154+
$response = Http::withToken(config('nativephp-internal.zephpyr.token'))
155+
->get(str(config('nativephp-internal.zephpyr.host'))->finish('/') . 'api/download/' . $this->key);
127156

128-
return $response->successful();
157+
if ($response->failed()) {
158+
return false;
159+
}
160+
161+
file_put_contents(base_path('build/__nativephp_app_bundle'), $response->body());
162+
163+
return true;
129164
}
130165
}

0 commit comments

Comments
 (0)