-
-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathCraftCommand.php
More file actions
178 lines (157 loc) · 6.15 KB
/
CraftCommand.php
File metadata and controls
178 lines (157 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
namespace SPC\command;
use SPC\exception\ValidationException;
use SPC\util\ConfigValidator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\Process;
#[AsCommand('craft', 'Build static-php from craft.yml')]
class CraftCommand extends BaseCommand
{
public function configure(): void
{
$this->addArgument('craft', null, 'Path to craft.yml file', WORKING_DIR . '/craft.yml');
}
public function handle(): int
{
$craft_file = $this->getArgument('craft');
// Check if the craft.yml file exists
if (!file_exists($craft_file)) {
$this->output->writeln('<error>craft.yml not found, please create one!</error>');
return static::FAILURE;
}
// Check if the craft.yml file is valid
try {
$craft = ConfigValidator::validateAndParseCraftFile($craft_file, $this);
if ($craft['debug']) {
$this->input->setOption('debug', true);
}
} catch (ValidationException $e) {
$this->output->writeln('<error>craft.yml parse error: ' . $e->getMessage() . '</error>');
return static::FAILURE;
}
// Craft!!!
$this->output->writeln('<info>Crafting...</info>');
// apply env
if (isset($craft['extra-env'])) {
$env = $craft['extra-env'];
foreach ($env as $key => $val) {
f_putenv("{$key}={$val}");
}
}
$static_extensions = implode(',', $craft['extensions']);
$shared_extensions = implode(',', $craft['shared-extensions'] ?? []);
$libs = implode(',', $craft['libs']);
// init log
if (file_exists(WORKING_DIR . '/craft.log')) {
unlink(WORKING_DIR . '/craft.log');
}
// craft doctor
if ($craft['craft-options']['doctor']) {
$retcode = $this->runCommand('doctor', '--auto-fix');
if ($retcode !== 0) {
$this->output->writeln('<error>craft doctor failed</error>');
$this->log("craft doctor failed with code: {$retcode}", true);
return static::FAILURE;
}
}
// craft download
if ($craft['craft-options']['download']) {
$sharedAppend = $shared_extensions ? ',' . $shared_extensions : '';
$args = ["--for-extensions={$static_extensions}{$sharedAppend}"];
if ($craft['libs'] !== []) {
$args[] = "--for-libs={$libs}";
}
if (isset($craft['php-version'])) {
$args[] = '--with-php=' . $craft['php-version'];
if (!array_key_exists('ignore-cache-sources', $craft['download-options'])) {
$craft['download-options']['ignore-cache-sources'] = 'php-src';
}
}
$this->optionsToArguments($craft['download-options'], $args);
$retcode = $this->runCommand('download', ...$args);
if ($retcode !== 0) {
$this->output->writeln('<error>craft download failed</error>');
$this->log('craft download failed with code: ' . $retcode, true);
return static::FAILURE;
}
}
// craft build
if ($craft['craft-options']['build']) {
$args = [$static_extensions, "--with-libs={$libs}", "--build-shared={$shared_extensions}", ...array_map(fn ($x) => "--build-{$x}", $craft['sapi'])];
$this->optionsToArguments($craft['build-options'], $args);
$retcode = $this->runCommand('build', ...$args);
if ($retcode !== 0) {
$this->output->writeln('<error>craft build failed</error>');
$this->log('craft build failed with code: ' . $retcode, true);
return static::FAILURE;
}
}
return 0;
}
public function processLogCallback($type, $buffer): void
{
if ($type === Process::ERR) {
fwrite(STDERR, $buffer);
} else {
fwrite(STDOUT, $buffer);
$this->log($buffer);
}
}
private function log(string $log, bool $master_log = false): void
{
if ($master_log) {
$log = "\n[static-php-cli]> " . $log . "\n\n";
} else {
$log = preg_replace('/\x1b\[[0-9;]*m/', '', $log);
}
file_put_contents('craft.log', $log, FILE_APPEND);
}
private function runCommand(string $cmd, ...$args): int
{
global $argv;
if ($this->getOption('debug')) {
array_unshift($args, '--debug');
}
$prefix = PHP_SAPI === 'cli' ? [PHP_BINARY, $argv[0]] : [$argv[0]];
$env = getenv();
$process = new Process([...$prefix, $cmd, '--no-motd', ...$args], env: $env, timeout: null);
$this->log("Running: {$process->getCommandLine()}", true);
if (PHP_OS_FAMILY === 'Windows') {
sapi_windows_set_ctrl_handler(function () use ($process) {
if ($process->isRunning()) {
$process->signal(-1073741510);
}
});
} elseif (extension_loaded('pcntl')) {
pcntl_signal(SIGINT, function () use ($process) {
$process->signal(SIGINT);
});
} else {
logger()->debug('You have not enabled `pcntl` extension, cannot prevent download file corruption when Ctrl+C');
}
// $process->setTty(true);
$process->run([$this, 'processLogCallback']);
return $process->getExitCode();
}
private function optionsToArguments(array $options, array &$args): void
{
foreach ($options as $option => $val) {
if ((is_bool($val) && $val) || $val === null) {
$args[] = "--{$option}";
continue;
}
if (is_string($val)) {
$args[] = "--{$option}={$val}";
continue;
}
if (is_array($val)) {
foreach ($val as $v) {
if (is_string($v)) {
$args[] = "--{$option}={$v}";
}
}
}
}
}
}