Remove craft.log

This commit is contained in:
crazywhalecc 2025-08-06 20:46:02 +08:00 committed by Jerry Ma
parent 333b776e77
commit 08fa49b791

View File

@ -54,17 +54,11 @@ class CraftCommand extends BuildCommand
$shared_extensions = implode(',', $craft['shared-extensions'] ?? []); $shared_extensions = implode(',', $craft['shared-extensions'] ?? []);
$libs = implode(',', $craft['libs']); $libs = implode(',', $craft['libs']);
// init log
if (file_exists(WORKING_DIR . '/craft.log')) {
unlink(WORKING_DIR . '/craft.log');
}
// craft doctor // craft doctor
if ($craft['craft-options']['doctor']) { if ($craft['craft-options']['doctor']) {
$retcode = $this->runCommand('doctor', '--auto-fix'); $retcode = $this->runCommand('doctor', '--auto-fix');
if ($retcode !== 0) { if ($retcode !== 0) {
$this->output->writeln('<error>craft doctor failed</error>'); $this->output->writeln('<error>craft doctor failed</error>');
$this->log("craft doctor failed with code: {$retcode}", true);
return static::FAILURE; return static::FAILURE;
} }
} }
@ -73,7 +67,6 @@ class CraftCommand extends BuildCommand
$retcode = $this->runCommand('install-pkg', 'go-xcaddy'); $retcode = $this->runCommand('install-pkg', 'go-xcaddy');
if ($retcode !== 0) { if ($retcode !== 0) {
$this->output->writeln('<error>craft go-xcaddy failed</error>'); $this->output->writeln('<error>craft go-xcaddy failed</error>');
$this->log("craft: spc install-pkg go-xcaddy failed with code: {$retcode}", true);
return static::FAILURE; return static::FAILURE;
} }
} }
@ -82,7 +75,6 @@ class CraftCommand extends BuildCommand
$retcode = $this->runCommand('install-pkg', 'zig'); $retcode = $this->runCommand('install-pkg', 'zig');
if ($retcode !== 0) { if ($retcode !== 0) {
$this->output->writeln('<error>craft zig failed</error>'); $this->output->writeln('<error>craft zig failed</error>');
$this->log("craft: spc install-pkg zig failed with code: {$retcode}", true);
return static::FAILURE; return static::FAILURE;
} }
} }
@ -103,7 +95,6 @@ class CraftCommand extends BuildCommand
$retcode = $this->runCommand('download', ...$args); $retcode = $this->runCommand('download', ...$args);
if ($retcode !== 0) { if ($retcode !== 0) {
$this->output->writeln('<error>craft download failed</error>'); $this->output->writeln('<error>craft download failed</error>');
$this->log('craft download failed with code: ' . $retcode, true);
return static::FAILURE; return static::FAILURE;
} }
} }
@ -115,7 +106,6 @@ class CraftCommand extends BuildCommand
$retcode = $this->runCommand('build', ...$args); $retcode = $this->runCommand('build', ...$args);
if ($retcode !== 0) { if ($retcode !== 0) {
$this->output->writeln('<error>craft build failed</error>'); $this->output->writeln('<error>craft build failed</error>');
$this->log('craft build failed with code: ' . $retcode, true);
return static::FAILURE; return static::FAILURE;
} }
} }
@ -129,31 +119,20 @@ class CraftCommand extends BuildCommand
fwrite(STDERR, $buffer); fwrite(STDERR, $buffer);
} else { } else {
fwrite(STDOUT, $buffer); 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 private function runCommand(string $cmd, ...$args): int
{ {
global $argv; global $argv;
if ($this->getOption('debug')) { if ($this->getOption('debug')) {
array_unshift($args, '--debug'); array_unshift($args, '--debug');
} }
array_unshift($args, '--preserve-log');
$prefix = PHP_SAPI === 'cli' ? [PHP_BINARY, $argv[0]] : [$argv[0]]; $prefix = PHP_SAPI === 'cli' ? [PHP_BINARY, $argv[0]] : [$argv[0]];
$env = getenv(); $env = getenv();
$process = new Process([...$prefix, $cmd, '--no-motd', ...$args], env: $env, timeout: null); $process = new Process([...$prefix, $cmd, '--no-motd', ...$args], env: $env, timeout: null);
$this->log("Running: {$process->getCommandLine()}", true);
if (PHP_OS_FAMILY === 'Windows') { if (PHP_OS_FAMILY === 'Windows') {
sapi_windows_set_ctrl_handler(function () use ($process) { sapi_windows_set_ctrl_handler(function () use ($process) {
@ -163,6 +142,7 @@ class CraftCommand extends BuildCommand
}); });
} elseif (extension_loaded('pcntl')) { } elseif (extension_loaded('pcntl')) {
pcntl_signal(SIGINT, function () use ($process) { pcntl_signal(SIGINT, function () use ($process) {
/* @noinspection PhpComposerExtensionStubsInspection */
$process->signal(SIGINT); $process->signal(SIGINT);
}); });
} else { } else {