3
3
namespace Native \Laravel \Commands ;
4
4
5
5
use Illuminate \Console \Command ;
6
+ use Illuminate \Support \Facades \Http ;
6
7
use Native \Electron \Traits \CleansEnvFile ;
7
8
use Symfony \Component \Finder \Finder ;
8
9
use ZipArchive ;
@@ -11,7 +12,7 @@ class BundleCommand extends Command
11
12
{
12
13
use CleansEnvFile;
13
14
14
- protected $ name = 'native:bundle ' ;
15
+ protected $ signature = 'native:bundle {--fetch} ' ;
15
16
16
17
protected $ description = 'Bundle your application for distribution. ' ;
17
18
@@ -27,9 +28,9 @@ public function handle()
27
28
28
29
if (! $ this ->key ) {
29
30
$ this ->line ('' );
30
- $ this ->warn ('No ZEPHPYR_SECRET found. Cannot bundle! ' );
31
+ $ this ->warn ('No ZEPHPYR_KEY found. Cannot bundle! ' );
31
32
$ 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: ' );
33
34
$ this ->line (base_path ('.env ' ));
34
35
$ this ->line ('' );
35
36
$ this ->info ('Not set up with Zephpyr yet? Secure your NativePHP app builds and more! ' );
@@ -39,6 +40,16 @@ public function handle()
39
40
return static ::FAILURE ;
40
41
}
41
42
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
+
42
53
// Package the app up into a zip
43
54
if (! $ this ->zipApplication ()) {
44
55
$ this ->error ("Failed to create zip archive at {$ this ->zipPath }. " );
@@ -47,12 +58,19 @@ public function handle()
47
58
}
48
59
49
60
// Send the zip file
50
- if (! $ this ->sendToZephpyr ()) {
61
+ dd ($ result = $ this ->sendToZephpyr ());
62
+
63
+ if ($ result ->failed ()) {
51
64
$ this ->error ("Failed to upload zip [ {$ this ->zipPath }] to Zephpyr. " );
52
65
53
66
return static ::FAILURE ;
54
67
}
55
68
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
+
56
74
return static ::SUCCESS ;
57
75
}
58
76
@@ -80,11 +98,17 @@ private function zipApplication(): bool
80
98
81
99
private function addFilesToZip (ZipArchive $ zip ): void
82
100
{
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
+
83
104
$ app = (new Finder )->files ()
84
105
->followLinks ()
85
106
->ignoreVCSIgnored (true )
86
107
->in (base_path ())
87
108
->exclude ([
109
+ 'vendor ' ,
110
+ 'dist ' ,
111
+ 'build ' ,
88
112
'tests ' ,
89
113
...config ('nativephp.cleanup_exclude_files ' , []),
90
114
]);
@@ -93,41 +117,49 @@ private function addFilesToZip(ZipArchive $zip): void
93
117
94
118
$ vendor = (new Finder )->files ()
95
119
->exclude ([
96
- 'vendor/nativephp/php-bin ' ,
120
+ 'nativephp/php-bin ' ,
121
+ 'nativephp/electron/resources/js ' ,
122
+ 'nativephp/*/vendor ' ,
97
123
])
98
124
->in (base_path ('vendor ' ));
99
125
100
- $ this ->finderToZip ($ vendor , $ zip );
126
+ $ this ->finderToZip ($ vendor , $ zip, ' vendor ' );
101
127
102
128
$ nodeModules = (new Finder )->files ()
103
129
->in (base_path ('node_modules ' ));
104
130
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 ' );
113
132
}
114
133
115
- private function finderToZip (Finder $ finder , ZipArchive $ zip ): void
134
+ private function finderToZip (Finder $ finder , ZipArchive $ zip, ? string $ path = null ): void
116
135
{
117
136
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 ());
120
142
}
121
143
}
122
144
123
- private function sendToZephpyr (): bool
145
+ private function sendToZephpyr ()
124
146
{
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
+ }
130
151
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 ;
132
164
}
133
165
}
0 commit comments