1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Laravel Zero. |
7
|
|
|
* |
8
|
|
|
* (c) Nuno Maduro <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace LaravelZero\Framework\Commands; |
15
|
|
|
|
16
|
|
|
use Illuminate\Console\Application as Artisan; |
17
|
|
|
use Illuminate\Support\Facades\File; |
18
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\Process\Process; |
23
|
|
|
|
24
|
|
|
final class BuildCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
protected $signature = 'app:build |
30
|
|
|
{name? : The build name} |
31
|
|
|
{--build-version= : The build version, if not provided it will be asked} |
32
|
|
|
{--timeout=300 : The timeout in seconds or 0 to disable}'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected $description = 'Build a single file executable'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Holds the configuration on is original state. |
41
|
|
|
* |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
private static $config; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Holds the box.json on is original state. |
48
|
|
|
* |
49
|
|
|
* @var string|null |
50
|
|
|
*/ |
51
|
|
|
private static $box; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Holds the command original output. |
55
|
|
|
* |
56
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
57
|
|
|
*/ |
58
|
|
|
private $originalOutput; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
2 |
|
public function handle() |
64
|
|
|
{ |
65
|
2 |
|
$this->title('Building process'); |
66
|
|
|
|
67
|
2 |
|
$this->build($this->input->getArgument('name') ?? $this->getBinary()); |
|
|
|
|
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
2 |
|
public function run(InputInterface $input, OutputInterface $output) |
74
|
|
|
{ |
75
|
2 |
|
parent::run($input, $this->originalOutput = $output); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Builds the application into a single file. |
80
|
|
|
*/ |
81
|
2 |
|
private function build(string $name): BuildCommand |
82
|
|
|
{ |
83
|
|
|
/* |
84
|
|
|
* We prepare the application for a build, moving it to production. Then, |
85
|
|
|
* after compile all the code to a single file, we move the built file |
86
|
|
|
* to the builds folder with the correct permissions. |
87
|
|
|
*/ |
88
|
2 |
|
$this->prepare() |
89
|
2 |
|
->compile($name) |
90
|
1 |
|
->clear(); |
91
|
|
|
|
92
|
1 |
|
$this->output->writeln( |
93
|
1 |
|
sprintf(' Compiled successfully: <fg=green>%s</>', $this->app->buildsPath($name)) |
94
|
|
|
); |
95
|
|
|
|
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
private function compile(string $name): BuildCommand |
100
|
|
|
{ |
101
|
2 |
|
if (! File::exists($this->app->buildsPath())) { |
102
|
2 |
|
File::makeDirectory($this->app->buildsPath()); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
$process = new Process( |
106
|
2 |
|
'./box compile --working-dir="'.base_path().'" --config="'.base_path('box.json').'"', |
|
|
|
|
107
|
2 |
|
dirname(dirname(__DIR__)).'/bin', |
108
|
2 |
|
null, |
109
|
2 |
|
null, |
110
|
2 |
|
$this->getTimeout() |
111
|
|
|
); |
112
|
|
|
|
113
|
2 |
|
$section = tap($this->originalOutput->section())->write(''); |
|
|
|
|
114
|
|
|
|
115
|
2 |
|
$progressBar = tap( |
116
|
2 |
|
new ProgressBar( |
117
|
2 |
|
$this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL ? new NullOutput() : $section, 25 |
118
|
|
|
) |
119
|
2 |
|
)->setProgressCharacter("\xF0\x9F\x8D\xBA"); |
120
|
|
|
|
121
|
2 |
|
foreach (tap($process)->start() as $type => $data) { |
122
|
2 |
|
$progressBar->advance(); |
123
|
|
|
|
124
|
2 |
|
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) { |
125
|
2 |
|
$process::OUT === $type ? $this->info("$data") : $this->error("$data"); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
$progressBar->finish(); |
130
|
|
|
|
131
|
2 |
|
$section->clear(); |
132
|
|
|
|
133
|
1 |
|
$this->task(' 2. <fg=yellow>Compile</> into a single file'); |
134
|
|
|
|
135
|
1 |
|
$this->output->newLine(); |
136
|
|
|
|
137
|
1 |
|
File::move($this->app->basePath($this->getBinary()).'.phar', $this->app->buildsPath($name)); |
138
|
|
|
|
139
|
1 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
private function prepare(): BuildCommand |
143
|
|
|
{ |
144
|
2 |
|
$configFile = $this->app->configPath('app.php'); |
145
|
2 |
|
static::$config = File::get($configFile); |
|
|
|
|
146
|
|
|
|
147
|
2 |
|
$config = include $configFile; |
148
|
|
|
|
149
|
2 |
|
$config['production'] = true; |
150
|
2 |
|
$version = $this->option('build-version') ?: $this->ask('Build version?', $config['version']); |
151
|
2 |
|
$config['version'] = $version; |
152
|
|
|
|
153
|
2 |
|
$boxFile = $this->app->basePath('box.json'); |
154
|
2 |
|
static::$box = File::get($boxFile); |
|
|
|
|
155
|
|
|
|
156
|
2 |
|
$this->task( |
157
|
2 |
|
' 1. Moving application to <fg=yellow>production mode</>', |
158
|
|
|
function () use ($configFile, $config) { |
159
|
2 |
|
File::put($configFile, '<?php return '.var_export($config, true).';'.PHP_EOL); |
160
|
2 |
|
} |
161
|
|
|
); |
162
|
|
|
|
163
|
2 |
|
$boxContents = json_decode(static::$box, true); |
164
|
2 |
|
$boxContents['main'] = $this->getBinary(); |
165
|
2 |
|
File::put($boxFile, json_encode($boxContents)); |
166
|
|
|
|
167
|
2 |
|
File::put($configFile, '<?php return '.var_export($config, true).';'.PHP_EOL); |
168
|
|
|
|
169
|
2 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
private function clear(): BuildCommand |
173
|
|
|
{ |
174
|
2 |
|
File::put($this->app->configPath('app.php'), static::$config); |
|
|
|
|
175
|
|
|
|
176
|
2 |
|
File::put($this->app->basePath('box.json'), static::$box); |
|
|
|
|
177
|
|
|
|
178
|
2 |
|
static::$config = null; |
179
|
|
|
|
180
|
2 |
|
static::$box = null; |
181
|
|
|
|
182
|
2 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns the artisan binary. |
187
|
|
|
*/ |
188
|
2 |
|
private function getBinary(): string |
189
|
|
|
{ |
190
|
2 |
|
return str_replace(["'", '"'], '', Artisan::artisanBinary()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns a valid timeout value. Non positive values are converted to null, |
195
|
|
|
* meaning no timeout. |
196
|
|
|
* |
197
|
|
|
* @return float|null |
198
|
|
|
* @throws \InvalidArgumentException |
199
|
|
|
*/ |
200
|
2 |
|
private function getTimeout(): ?float |
201
|
|
|
{ |
202
|
2 |
|
if (! is_numeric($this->option('timeout'))) { |
|
|
|
|
203
|
|
|
throw new \InvalidArgumentException('The timeout value must be a number.'); |
204
|
|
|
} |
205
|
|
|
|
206
|
2 |
|
$timeout = (float) $this->option('timeout'); |
207
|
|
|
|
208
|
2 |
|
return $timeout > 0 ? $timeout : null; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Makes sure that the `clear` is performed even |
213
|
|
|
* if the command fails. |
214
|
|
|
* |
215
|
|
|
* @return void |
216
|
|
|
*/ |
217
|
36 |
|
public function __destruct() |
218
|
|
|
{ |
219
|
36 |
|
if (static::$config !== null) { |
|
|
|
|
220
|
1 |
|
$this->clear(); |
221
|
|
|
} |
222
|
36 |
|
} |
223
|
|
|
} |
224
|
|
|
|