From f8e1c771e444926c6ea9bf97a95800cc06554e2e Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 17 Apr 2026 16:47:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(cli):=20=E6=B7=BB=E5=8A=A0=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=98=BE=E7=A4=BA=E5=8A=9F=E8=83=BD=E5=92=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=8F=82=E6=95=B0=E4=BF=A1=E6=81=AF=E5=A4=B4=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在编译器选项配置中添加 version 参数支持 - 定义应用名称和版本常量用于版本显示 - 实现 showVersion 方法用于输出版本信息 - 修改 showUsage 方法使用统一的版本显示逻辑 - 重构 getArgInfoHeaderFile 方法简化路径处理逻辑 - 更新存根文件生成逻辑使用新的头文件生成方法 --- src/Php/Translator.php | 26 +++++++++++++++----------- src/config/compiler_options.php | 6 ++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Php/Translator.php b/src/Php/Translator.php index c40429cf..371702dc 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -28,6 +28,8 @@ use Symfony\Component\Yaml\Yaml; class Translator extends Preprocessor { + public const string VERSION = '0.1.0'; + public const string APP_NAME = 'Swoole-Compiler (AOT)'; protected string $targetName = 'app'; protected array $sourceDirs = []; protected bool $verbose = false; @@ -63,12 +65,16 @@ class Translator extends Preprocessor $this->showUsage(); exit(0); } + if ($this->climate->arguments->defined('version')) { + $this->showVersion(); + exit(0); + } } public function showUsage(): void { $climate = $this->climate; - $climate->bold()->green('PHP AOT Compiler v1.0.0'); + $this->showVersion(); $climate->br(); $climate->bold('USAGE:'); @@ -100,6 +106,11 @@ class Translator extends Preprocessor $climate->br(); } + private function showVersion(): void + { + $this->climate->bold()->out(self::APP_NAME . ' v' . self::VERSION); + } + public function convertFile(string $file): string { $file = realpath($file); @@ -552,15 +563,9 @@ class Translator extends Preprocessor return str_replace('-', '_', $rs); } - public function getArgInfoHeaderFile(string $stubFilenameWithoutExtension, bool $relative = false): string + public function getArgInfoHeaderFile(string $file, bool $relative = false): string { - foreach ($this->sourceDirs as $srcDir) { - if (!is_dir($srcDir) or str_starts_with($stubFilenameWithoutExtension, $srcDir)) { - $filePath = ltrim($this->removeCommonPrefix($srcDir, $stubFilenameWithoutExtension), '/'); - break; - } - } - + $filePath = $this->getRelativePath(str_replace(['.stub.php', '.php'], '', $file)); $filename = self::PREFIX . str_replace('/', '_', $filePath); $filename = $this->escapeFileName($filename); $absPath = $this->getIncludeDir() . "/{$filename}_arginfo.h"; @@ -1000,8 +1005,7 @@ class Translator extends Preprocessor protected function genStubFile(string $file): void { - $stubFilenameWithoutExtension = str_replace(['.stub.php', '.php'], '', $file); - $headerFile = $this->getArgInfoHeaderFile($stubFilenameWithoutExtension, true); + $headerFile = $this->getArgInfoHeaderFile($file, true); $this->climate->info('generate stub file: ' . $this->getRelativePath($file)); generateStubFile($file, $this->getIncludeDir() . '/' . $headerFile, true); diff --git a/src/config/compiler_options.php b/src/config/compiler_options.php index 60183ad5..94f62ea8 100644 --- a/src/config/compiler_options.php +++ b/src/config/compiler_options.php @@ -19,6 +19,12 @@ return [ 'description' => 'Show help', 'noValue' => true, ], + 'version' => [ + 'prefix' => 'v', + 'longPrefix' => 'version', + 'description' => 'Show Version', + 'noValue' => true, + ], 'profile' => [ 'longPrefix' => 'profile', 'description' => 'Enable performance profiling',