Build hot reload artifacts before worker restart
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Server;
|
||||
|
||||
use Kiri\Di\HotReloadState;
|
||||
use Kiri\Router\RouteArtifactState;
|
||||
use Kiri\Router\Router;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Throwable;
|
||||
|
||||
class FileBuildCommand extends Command
|
||||
{
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setName('sw:file-build')
|
||||
->setDescription('build hot reload artifacts for changed files');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
defined('ROUTER_TYPE_HTTP') or define('ROUTER_TYPE_HTTP', 'http');
|
||||
putenv('KIRI_FILE_BUILD=1');
|
||||
|
||||
$changedFiles = di(HotReloadState::class)->peek();
|
||||
$startedAt = microtime(true);
|
||||
|
||||
try {
|
||||
$syntaxError = $this->lintChangedFiles($changedFiles);
|
||||
if ($syntaxError !== null) {
|
||||
$output->writeln('<error>file build failed: ' . $syntaxError . '</error>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
di(Router::class)->scan_build_route();
|
||||
di(HotReloadState::class)->store($changedFiles);
|
||||
di(RouteArtifactState::class)->markBuild(ROUTER_TYPE_HTTP, $changedFiles);
|
||||
|
||||
$elapsed = round((microtime(true) - $startedAt) * 1000, 2);
|
||||
$output->writeln(sprintf('file build complete: %d changed files, %sms', count($changedFiles), $elapsed));
|
||||
return Command::SUCCESS;
|
||||
} catch (Throwable $throwable) {
|
||||
if (class_exists('Kiri')) {
|
||||
\Kiri::getLogger()->json_log($throwable);
|
||||
}
|
||||
$output->writeln('<error>file build failed: ' . $throwable->getMessage() . '</error>');
|
||||
return Command::FAILURE;
|
||||
} finally {
|
||||
putenv('KIRI_FILE_BUILD');
|
||||
}
|
||||
}
|
||||
|
||||
private function lintChangedFiles(array $changedFiles): ?string
|
||||
{
|
||||
foreach ($changedFiles as $file) {
|
||||
if (!is_string($file) || !str_ends_with($file, '.php') || !is_file($file)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = [];
|
||||
$returnCode = 0;
|
||||
exec(escapeshellarg(PHP_BINARY ?: 'php') . ' -l ' . escapeshellarg($file) . ' 2>&1', $output, $returnCode);
|
||||
if ($returnCode !== 0) {
|
||||
return implode("\n", $output);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user