33namespace Native \Laravel \Commands ;
44
55use Illuminate \Console \Command ;
6+ use Illuminate \Support \Facades \Http ;
67use Native \Electron \Traits \CleansEnvFile ;
7- use Native \Laravel \NativeServiceProvider ;
88use Symfony \Component \Finder \Finder ;
99use ZipArchive ;
1010
1111class 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