From e694e90c16bba82af3e516a457663c0f81e41c2a Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 27 Jun 2026 08:56:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(translator):=20=E6=B7=BB=E5=8A=A0=E6=97=A0?= =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E6=9D=A1=E7=BC=96=E8=AF=91=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 --no-progress 参数用于禁用进度条显示 - 实现按文件逐行输出编译进度功能 - 在串行编译模式下支持进度条禁用选项 - 在并行编译模式下支持进度条禁用选项 - 添加编译失败和异常情况下的索引计数 - 实现百分比和文件名的简短路径显示 --- src/Php/Constants.php | 6 ++++++ src/Php/Translator.php | 44 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Php/Constants.php b/src/Php/Constants.php index c4601188..5609f319 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -207,6 +207,12 @@ class Constants 'required' => false, 'multiple' => true, ], + 'no-progress' => [ + 'longPrefix' => 'no-progress', + 'description' => 'Disable progress bar, output per-file compilation progress line by line', + 'required' => false, + 'noValue' => true, + ], 'lto' => [ 'longPrefix' => 'lto', 'description' => 'Enable Link Time Optimization (-flto)', diff --git a/src/Php/Translator.php b/src/Php/Translator.php index ec664231..1c577659 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -237,6 +237,7 @@ class Translator extends Preprocessor $climate->tab()->out('--target-platform Cross-compilation target triple (e.g. aarch64-linux-gnu)'); $climate->tab()->out('--lto Enable Link Time Optimization (-flto)'); $climate->tab()->out('--no-literal-strings Disable literal strings optimization'); + $climate->tab()->out('--no-progress Disable progress bar, output per-file compilation progress line by line'); $climate->tab()->out('--no-console Hide console window (Windows only, GUI application)'); $climate->tab()->out('--no-color Disable ANSI color output'); $climate->tab()->out('--sanitize Enable sanitizers (address, undefined, etc.)'); @@ -294,6 +295,11 @@ class Translator extends Preprocessor $this->enableProfiler = true; } + // 禁用进度条 + if ($this->climate->arguments->defined('no-progress')) { + $this->noProgress = true; + } + // 隐藏控制台窗口 if ($this->climate->arguments->defined('no-console')) { $this->noConsole = true; @@ -1183,6 +1189,7 @@ CODE; $this->climate->lightBlue("Starting compilation for {$totalFiles} files"); + $index = 0; foreach ($sourceFiles as $cppFile) { $objectFile = $this->getObjectFile($cppFile); @@ -1193,10 +1200,21 @@ CODE; } else { $failedFiles[] = $cppFile; $this->climate->red("Compilation failed: {$cppFile}"); + $index++; + continue; } } catch (\Throwable $e) { $failedFiles[] = $cppFile; $this->climate->red("Compilation error: {$cppFile} - " . $e->getMessage()); + $index++; + continue; + } + + $index++; + if ($this->noProgress) { + $percent = intval($index / $totalFiles * 100); + $cppFileShorted = $this->removeCommonPrefix($this->buildDir, $cppFile); + $this->climate->white("[{$index}/{$totalFiles}] {$percent}% {$cppFileShorted}"); } } @@ -1229,11 +1247,13 @@ CODE; $this->climate->lightBlue("Starting parallel compilation with {$job} jobs for {$totalFiles} files"); - $progress = new Progressbar(); - $progress->barStyle([AnsiTerminal::FG_GREEN]) - ->percentageStyle([AnsiTerminal::TEXT_BOLD]) - ->labelStyle([AnsiTerminal::FG_CYAN]); - $progress->renderInPlace(0, $totalFiles, 'Compiling'); + if (!$this->noProgress) { + $progress = new Progressbar(); + $progress->barStyle([AnsiTerminal::FG_GREEN]) + ->percentageStyle([AnsiTerminal::TEXT_BOLD]) + ->labelStyle([AnsiTerminal::FG_CYAN]); + $progress->renderInPlace(0, $totalFiles, 'Compiling'); + } while ($compiledCount < $totalFiles) { // 启动新进程,直到达到最大并发数 @@ -1286,7 +1306,14 @@ CODE; } } $compiledCount++; - $progress->renderInPlace($compiledCount, $totalFiles, 'Compiling'); + if ($this->noProgress) { + $percent = intval($compiledCount / $totalFiles * 100); + $file = $processInfo['file'] ?? 'unknown'; + $fileShorted = $this->removeCommonPrefix($this->buildDir, $file); + $this->climate->white("[{$compiledCount}/{$totalFiles}] {$percent}% {$fileShorted}"); + } else { + $progress->renderInPlace($compiledCount, $totalFiles, 'Compiling'); + } } } } @@ -1296,9 +1323,14 @@ CODE; $status = null; $pid = pcntl_wait($status); if ($pid > 0) { + $processInfo = $processPipes[$pid] ?? null; unset($processPipes[$pid]); $runningProcesses--; $compiledCount++; + if ($this->noProgress && $processInfo) { + $percent = intval($compiledCount / $totalFiles * 100); + $this->climate->darkGray("[{$compiledCount}/{$totalFiles}] {$percent}% {$processInfo['file']}"); + } } }