33namespace Native \Laravel \Commands ;
44
55use Illuminate \Console \Command ;
6+ use Illuminate \Support \Facades \Http ;
67use Native \Electron \Traits \CleansEnvFile ;
78use Symfony \Component \Finder \Finder ;
89use ZipArchive ;
@@ -11,7 +12,7 @@ class BundleCommand extends Command
1112{
1213 use CleansEnvFile;
1314
14- protected $ name = 'native:bundle ' ;
15+ protected $ signature = 'native:bundle {--fetch} ' ;
1516
1617 protected $ description = 'Bundle your application for distribution. ' ;
1718
@@ -27,9 +28,9 @@ public function handle()
2728
2829 if (! $ this ->key ) {
2930 $ this ->line ('' );
30- $ this ->warn ('No ZEPHPYR_SECRET found. Cannot bundle! ' );
31+ $ this ->warn ('No ZEPHPYR_KEY found. Cannot bundle! ' );
3132 $ this ->line ('' );
32- $ 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: ' );
3334 $ this ->line (base_path ('.env ' ));
3435 $ this ->line ('' );
3536 $ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
@@ -39,6 +40,16 @@ public function handle()
3940 return static ::FAILURE ;
4041 }
4142
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+
4253 // Package the app up into a zip
4354 if (! $ this ->zipApplication ()) {
4455 $ this ->error ("Failed to create zip archive at {$ this ->zipPath }. " );
@@ -47,12 +58,19 @@ public function handle()
4758 }
4859
4960 // Send the zip file
50- if (! $ this ->sendToZephpyr ()) {
61+ dd ($ result = $ this ->sendToZephpyr ());
62+
63+ if ($ result ->failed ()) {
5164 $ this ->error ("Failed to upload zip [ {$ this ->zipPath }] to Zephpyr. " );
5265
5366 return static ::FAILURE ;
5467 }
5568
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+
5674 return static ::SUCCESS ;
5775 }
5876
@@ -80,11 +98,17 @@ private function zipApplication(): bool
8098
8199 private function addFilesToZip (ZipArchive $ zip ): void
82100 {
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+
83104 $ app = (new Finder )->files ()
84105 ->followLinks ()
85106 ->ignoreVCSIgnored (true )
86107 ->in (base_path ())
87108 ->exclude ([
109+ 'vendor ' ,
110+ 'dist ' ,
111+ 'build ' ,
88112 'tests ' ,
89113 ...config ('nativephp.cleanup_exclude_files ' , []),
90114 ]);
@@ -93,41 +117,49 @@ private function addFilesToZip(ZipArchive $zip): void
93117
94118 $ vendor = (new Finder )->files ()
95119 ->exclude ([
96- 'vendor/nativephp/php-bin ' ,
120+ 'nativephp/php-bin ' ,
121+ 'nativephp/electron/resources/js ' ,
122+ 'nativephp/*/vendor ' ,
97123 ])
98124 ->in (base_path ('vendor ' ));
99125
100- $ this ->finderToZip ($ vendor , $ zip );
126+ $ this ->finderToZip ($ vendor , $ zip, ' vendor ' );
101127
102128 $ nodeModules = (new Finder )->files ()
103129 ->in (base_path ('node_modules ' ));
104130
105- $ this ->finderToZip ($ nodeModules , $ zip );
106-
107- $ env = (new Finder )->files ()
108- ->ignoreDotFiles (false )
109- ->name ('.env ' )
110- ->in (base_path ());
111-
112- $ this ->finderToZip ($ env , $ zip );
131+ $ this ->finderToZip ($ nodeModules , $ zip , 'node_modules ' );
113132 }
114133
115- private function finderToZip (Finder $ finder , ZipArchive $ zip ): void
134+ private function finderToZip (Finder $ finder , ZipArchive $ zip, ? string $ path = null ): void
116135 {
117136 foreach ($ finder as $ file ) {
118- dump ([$ file ->getRealPath (), $ file ->getRelativePath ()]);
119- $ 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 ());
120142 }
121143 }
122144
123- private function sendToZephpyr (): bool
145+ private function sendToZephpyr ()
124146 {
125- return false ;
126- $ response = Http::attach ('archive ' , fopen ($ this ->zipPath , 'r ' ), $ this ->zipName )
127- ->post (config ('nativephp-internal.zephpyr.host ' ), [
128- 'key ' => $ this ->key ,
129- ]);
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+ }
130151
131- return $ response ->successful ();
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 );
156+
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 ;
132164 }
133165}
0 commit comments