4
4
5
5
use Illuminate \Console \Command ;
6
6
use Illuminate \Support \Facades \Http ;
7
+ use Illuminate \Support \Number ;
7
8
use Native \Laravel \Commands \Traits \CleansEnvFile ;
8
9
use Symfony \Component \Finder \Finder ;
9
10
use ZipArchive ;
@@ -12,7 +13,7 @@ class BundleCommand extends Command
12
13
{
13
14
use CleansEnvFile;
14
15
15
- protected $ signature = 'native:bundle {--fetch} ' ;
16
+ protected $ signature = 'native:bundle {--fetch} {--without-cleanup} ' ;
16
17
17
18
protected $ description = 'Bundle your application for distribution. ' ;
18
19
@@ -24,42 +25,18 @@ class BundleCommand extends Command
24
25
25
26
public function handle ()
26
27
{
27
- $ this ->key = config ('nativephp-internal.zephpyr.key ' );
28
-
29
- if (! $ this ->key ) {
30
- $ this ->line ('' );
31
- $ this ->warn ('No ZEPHPYR_KEY found. Cannot bundle! ' );
32
- $ this ->line ('' );
33
- $ this ->line ('Add this app \'s ZEPHPYR_KEY to its .env file: ' );
34
- $ this ->line (base_path ('.env ' ));
35
- $ this ->line ('' );
36
- $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
37
- $ this ->info ('Check out https://zephpyr.com ' );
38
- $ this ->line ('' );
39
-
28
+ if (! $ this ->checkForZephpyrKey ()) {
40
29
return static ::FAILURE ;
41
30
}
42
31
43
- if (! config ('nativephp-internal.zephpyr.token ' )) {
44
- $ this ->line ('' );
45
- $ this ->warn ('No ZEPHPYR_TOKEN found. Cannot bundle! ' );
46
- $ this ->line ('' );
47
- $ this ->line ('Add your api ZEPHPYR_TOKEN to its .env file: ' );
48
- $ this ->line (base_path ('.env ' ));
49
- $ this ->line ('' );
50
- $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
51
- $ this ->info ('Check out https://zephpyr.com ' );
52
- $ this ->line ('' );
53
-
32
+ if (! $ this ->checkForZephpyrToken ()) {
54
33
return static ::FAILURE ;
55
34
}
56
35
57
- $ result = $ this ->checkAuthenticated ();
58
-
59
- if ($ result ->failed ()) {
36
+ if (! $ this ->checkAuthenticated ()) {
60
37
$ this ->error ('Invalid API token: check your ZEPHPYR_TOKEN on https://zephpyr.com/user/api-tokens ' );
61
38
62
- return ;
39
+ return static :: FAILURE ;
63
40
}
64
41
65
42
if ($ this ->option ('fetch ' )) {
@@ -80,32 +57,47 @@ public function handle()
80
57
81
58
return static ::FAILURE ;
82
59
}
83
- // $this->zipName = 'app_CcINfsoQ.zip';
84
- // $this->zipPath = base_path('temp/'.$this->zipName);
85
60
86
61
// Send the zip file
87
62
$ result = $ this ->sendToZephpyr ();
88
63
89
- // dd($result->status(), json_decode($result->body()));
64
+ if ($ result ->status () === 413 ) {
65
+ $ fileSize = Number::fileSize (filesize ($ this ->zipPath ));
66
+ $ this ->error ('The zip file is too large to upload to Zephpyr ( ' .$ fileSize .'). Please contact support. ' );
90
67
91
- if ($ result ->code () === 413 ) {
92
- $ this ->error ('The zip file is too large to upload to Zephpyr. Please contact support. ' );
68
+ $ this ->cleanUp ();
93
69
94
70
return static ::FAILURE ;
95
71
} elseif ($ result ->failed ()) {
96
- $ this ->error ("Failed to upload zip [ {$ this ->zipPath }] to Zephpyr. " );
72
+ $ this ->error ("Failed to upload zip to Zephpyr. Error: {$ result ->status ()}" );
73
+ $ this ->cleanUp ();
97
74
98
75
return static ::FAILURE ;
99
76
}
100
77
101
- @unlink ($ this ->zipPath );
102
-
103
78
$ this ->info ('Successfully uploaded to Zephpyr. ' );
104
79
$ this ->line ('Use native:bundle --fetch to retrieve the latest bundle. ' );
105
80
81
+ $ this ->cleanUp ();
82
+
106
83
return static ::SUCCESS ;
107
84
}
108
85
86
+ protected function cleanUp (): void
87
+ {
88
+ if ($ this ->option ('without-cleanup ' )) {
89
+ return ;
90
+ }
91
+
92
+ $ this ->line ('Cleaning up… ' );
93
+
94
+ $ previousBuilds = glob (base_path ('temp/app_*.zip ' ));
95
+
96
+ foreach ($ previousBuilds as $ previousBuild ) {
97
+ @unlink ($ previousBuild );
98
+ }
99
+ }
100
+
109
101
private function zipApplication (): bool
110
102
{
111
103
$ this ->zipName = 'app_ ' .str ()->random (8 ).'.zip ' ;
@@ -138,7 +130,7 @@ private function addFilesToZip(ZipArchive $zip): void
138
130
// TODO: Check the composer.json to make sure there are no symlinked
139
131
// or private packages as these will be a pain later
140
132
141
- $ this ->line ('Adding files to zip… ' );
133
+ $ this ->line ('Creating zip archive … ' );
142
134
143
135
$ app = (new Finder )->files ()
144
136
->followLinks ()
@@ -188,7 +180,7 @@ private function baseUrl(): string
188
180
189
181
private function sendToZephpyr ()
190
182
{
191
- $ this ->line ('Uploading to Zephpyr… ' );
183
+ $ this ->line ('Uploading zip to Zephpyr… ' );
192
184
193
185
return Http::acceptJson ()
194
186
->withoutRedirecting () // Upload won't work if we follow the redirect
@@ -203,7 +195,7 @@ private function checkAuthenticated()
203
195
204
196
return Http::acceptJson ()
205
197
->withToken (config ('nativephp-internal.zephpyr.token ' ))
206
- ->get ($ this ->baseUrl ().'api/user ' );
198
+ ->get ($ this ->baseUrl ().'api/user ' )-> successful () ;
207
199
}
208
200
209
201
private function fetchLatestBundle (): bool
@@ -220,4 +212,44 @@ private function fetchLatestBundle(): bool
220
212
221
213
return true ;
222
214
}
215
+
216
+ private function checkForZephpyrKey ()
217
+ {
218
+ $ this ->key = config ('nativephp-internal.zephpyr.key ' );
219
+
220
+ if (! $ this ->key ) {
221
+ $ this ->line ('' );
222
+ $ this ->warn ('No ZEPHPYR_KEY found. Cannot bundle! ' );
223
+ $ this ->line ('' );
224
+ $ this ->line ('Add this app \'s ZEPHPYR_KEY to its .env file: ' );
225
+ $ this ->line (base_path ('.env ' ));
226
+ $ this ->line ('' );
227
+ $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
228
+ $ this ->info ('Check out https://zephpyr.com ' );
229
+ $ this ->line ('' );
230
+
231
+ return false ;
232
+ }
233
+
234
+ return true ;
235
+ }
236
+
237
+ private function checkForZephpyrToken ()
238
+ {
239
+ if (! config ('nativephp-internal.zephpyr.token ' )) {
240
+ $ this ->line ('' );
241
+ $ this ->warn ('No ZEPHPYR_TOKEN found. Cannot bundle! ' );
242
+ $ this ->line ('' );
243
+ $ this ->line ('Add your api ZEPHPYR_TOKEN to its .env file: ' );
244
+ $ this ->line (base_path ('.env ' ));
245
+ $ this ->line ('' );
246
+ $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
247
+ $ this ->info ('Check out https://zephpyr.com ' );
248
+ $ this ->line ('' );
249
+
250
+ return false ;
251
+ }
252
+
253
+ return true ;
254
+ }
223
255
}
0 commit comments