feat(cli): 添加版本显示功能和优化参数信息头文件生成

- 在编译器选项配置中添加 version 参数支持
- 定义应用名称和版本常量用于版本显示
- 实现 showVersion 方法用于输出版本信息
- 修改 showUsage 方法使用统一的版本显示逻辑
- 重构 getArgInfoHeaderFile 方法简化路径处理逻辑
- 更新存根文件生成逻辑使用新的头文件生成方法
pull/1/head
韩天峰 4 months ago
parent 78b93ed32c
commit f8e1c771e4
  1. 26
      src/Php/Translator.php
  2. 6
      src/config/compiler_options.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);

@ -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',

Loading…
Cancel
Save