mirror of
https://github.com/insistence/RuoYi-Vue3-FastAPI.git
synced 2026-09-21 20:55:15 +00:00
feat: 新增cli系统 (#103)
* feat: 新增cli系统 * feat: ruoyi completion支持powershell * perf: 优化tui显示 * fix: 修复前端构建异常
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from .command import app
|
||||
|
||||
__all__ = ['app']
|
||||
@@ -0,0 +1,74 @@
|
||||
from typing import Annotated
|
||||
|
||||
import typer
|
||||
|
||||
from cli.context import EnvOption, OutputOption
|
||||
|
||||
from .controller import DevCommandController
|
||||
|
||||
app = typer.Typer(
|
||||
help='开发相关命令',
|
||||
no_args_is_help=True,
|
||||
context_settings={'help_option_names': ['-h', '--help']},
|
||||
)
|
||||
_DEV_COMMAND_CONTROLLER = DevCommandController()
|
||||
|
||||
|
||||
@app.command('lint', help='执行 Ruff 格式化与静态检查')
|
||||
def lint(
|
||||
targets: Annotated[list[str] | None, typer.Argument(help='待检查路径,可传多个')] = None,
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
check_only: bool = typer.Option(False, '--check-only', help='仅检查,不写回格式化结果'),
|
||||
fix: bool = typer.Option(False, '--fix', help='执行 ruff check --fix'),
|
||||
unsafe_fixes: bool = typer.Option(False, '--unsafe-fixes', help='允许 Ruff 不安全修复'),
|
||||
) -> None:
|
||||
"""
|
||||
执行 Ruff 格式化与静态检查。
|
||||
|
||||
:param targets: 待检查路径列表
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param check_only: 是否仅检查不写回
|
||||
:param fix: 是否自动修复可修复问题
|
||||
:param unsafe_fixes: 是否允许不安全修复
|
||||
:return: None
|
||||
"""
|
||||
_DEV_COMMAND_CONTROLLER.lint(
|
||||
targets,
|
||||
env,
|
||||
output,
|
||||
check_only=check_only,
|
||||
fix=fix,
|
||||
unsafe_fixes=unsafe_fixes,
|
||||
)
|
||||
|
||||
|
||||
@app.command('test', help='执行项目测试')
|
||||
def test(
|
||||
targets: Annotated[list[str] | None, typer.Argument(help='待执行的测试路径,可传多个')] = None,
|
||||
env: EnvOption = 'dev',
|
||||
output: OutputOption = 'text',
|
||||
keyword: str = typer.Option('', '--keyword', '-k', help='pytest -k 过滤表达式'),
|
||||
maxfail: int = typer.Option(0, '--maxfail', min=0, help='最大失败数,0 表示不限制'),
|
||||
quiet: bool = typer.Option(False, '--quiet', '-q', help='启用简洁输出'),
|
||||
) -> None:
|
||||
"""
|
||||
执行项目测试。
|
||||
|
||||
:param targets: 测试目标路径列表
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param keyword: pytest 关键字过滤表达式
|
||||
:param maxfail: 最大失败数
|
||||
:param quiet: 是否启用简洁输出
|
||||
:return: None
|
||||
"""
|
||||
_DEV_COMMAND_CONTROLLER.test(
|
||||
targets,
|
||||
env,
|
||||
output,
|
||||
keyword=keyword,
|
||||
maxfail=maxfail,
|
||||
quiet=quiet,
|
||||
)
|
||||
@@ -0,0 +1,103 @@
|
||||
from cli.core import (
|
||||
DEFAULT_CORE_SERVICES,
|
||||
CliContextFactory,
|
||||
CliExecutionService,
|
||||
)
|
||||
from cli.runtime.dev import DEVELOPMENT_RUNTIME, DevelopmentRuntimeService
|
||||
|
||||
from .presenter import DevCommandPresenter
|
||||
|
||||
|
||||
class DevCommandController:
|
||||
"""
|
||||
开发命令控制器。
|
||||
|
||||
该控制器负责组织 `dev` 命令组的上下文准备、runtime 调用、
|
||||
payload 注入,以及基于输出格式选择 presenter 或直接返回 JSON。
|
||||
|
||||
:param context_factory: CLI 上下文工厂
|
||||
:param execution_service: CLI 执行服务
|
||||
:param presenter: 开发命令文本渲染器
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
context_factory: CliContextFactory | None = None,
|
||||
execution_service: CliExecutionService | None = None,
|
||||
presenter: DevCommandPresenter | None = None,
|
||||
runtime_service: DevelopmentRuntimeService | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
初始化开发命令控制器。
|
||||
|
||||
:param context_factory: CLI 上下文工厂
|
||||
:param execution_service: CLI 执行服务
|
||||
:param presenter: 开发命令文本渲染器
|
||||
:param runtime_service: 开发运行时服务
|
||||
:return: None
|
||||
"""
|
||||
self.context_factory = context_factory or DEFAULT_CORE_SERVICES.context_factory
|
||||
self.execution_service = execution_service or DEFAULT_CORE_SERVICES.execution_service
|
||||
self.presenter = presenter or DevCommandPresenter()
|
||||
self.runtime_service = runtime_service or DEVELOPMENT_RUNTIME
|
||||
|
||||
def lint(
|
||||
self,
|
||||
targets: list[str] | None,
|
||||
env: str,
|
||||
output: str,
|
||||
*,
|
||||
check_only: bool,
|
||||
fix: bool,
|
||||
unsafe_fixes: bool,
|
||||
) -> None:
|
||||
"""
|
||||
执行 Ruff 格式化与静态检查。
|
||||
|
||||
:param targets: 待检查路径列表
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param check_only: 是否仅检查不写回
|
||||
:param fix: 是否自动修复可修复问题
|
||||
:param unsafe_fixes: 是否允许不安全修复
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
payload = self.runtime_service.run_lint(targets, check_only=check_only, fix=fix, unsafe_fixes=unsafe_fixes)
|
||||
payload['env'] = ctx.env
|
||||
self.execution_service.complete_payload_with_text(
|
||||
ctx,
|
||||
payload,
|
||||
text_builder=self.presenter.build_dev_lint_text,
|
||||
)
|
||||
|
||||
def test(
|
||||
self,
|
||||
targets: list[str] | None,
|
||||
env: str,
|
||||
output: str,
|
||||
*,
|
||||
keyword: str,
|
||||
maxfail: int,
|
||||
quiet: bool,
|
||||
) -> None:
|
||||
"""
|
||||
执行项目测试。
|
||||
|
||||
:param targets: 测试目标路径列表
|
||||
:param env: 当前命令运行环境
|
||||
:param output: 输出格式
|
||||
:param keyword: pytest 关键字过滤表达式
|
||||
:param maxfail: 最大失败数
|
||||
:param quiet: 是否启用简洁输出
|
||||
:return: None
|
||||
"""
|
||||
ctx = self.context_factory.build_readonly(env, output)
|
||||
payload = self.runtime_service.run_tests(targets, keyword=keyword, maxfail=maxfail, quiet=quiet)
|
||||
payload['env'] = ctx.env
|
||||
self.execution_service.complete_payload_with_text(
|
||||
ctx,
|
||||
payload,
|
||||
text_builder=self.presenter.build_dev_test_text,
|
||||
)
|
||||
@@ -0,0 +1,96 @@
|
||||
class DevCommandPresenter:
|
||||
"""
|
||||
开发命令文本渲染器。
|
||||
|
||||
该渲染器负责将 `dev` 命令组产生的结构化 payload 转换为稳定的文本摘要,
|
||||
同时保持 JSON 输出仍由控制器直接返回,不在此处做契约变形。
|
||||
"""
|
||||
|
||||
def build_dev_lint_text(self, payload: dict[str, object]) -> str:
|
||||
"""
|
||||
将 lint 执行结果渲染为文本摘要。
|
||||
|
||||
:param payload: lint 执行结果字典
|
||||
:return: 文本摘要
|
||||
"""
|
||||
targets = payload.get('targets')
|
||||
lines = [
|
||||
f'ok: {str(payload.get("ok", False)).lower()}',
|
||||
f'env: {payload.get("env", "")}',
|
||||
f'check_only: {str(payload.get("checkOnly", False)).lower()}',
|
||||
f'fix: {str(payload.get("fix", False)).lower()}',
|
||||
f'unsafe_fixes: {str(payload.get("unsafeFixes", False)).lower()}',
|
||||
]
|
||||
if isinstance(targets, list) and targets:
|
||||
lines.append('targets:')
|
||||
lines.extend(f' - {target}' for target in targets)
|
||||
else:
|
||||
lines.append('targets: none')
|
||||
lines.extend(self._build_command_result_section('format', payload.get('format')))
|
||||
lines.extend(self._build_command_result_section('check', payload.get('check')))
|
||||
return '\n'.join(lines)
|
||||
|
||||
def build_dev_test_text(self, payload: dict[str, object]) -> str:
|
||||
"""
|
||||
将测试执行结果渲染为文本摘要。
|
||||
|
||||
:param payload: 测试执行结果字典
|
||||
:return: 文本摘要
|
||||
"""
|
||||
targets = payload.get('targets')
|
||||
lines = [
|
||||
f'ok: {str(payload.get("ok", False)).lower()}',
|
||||
f'env: {payload.get("env", "")}',
|
||||
f'keyword: {payload.get("keyword", "") or "-"}',
|
||||
f'maxfail: {payload.get("maxfail", 0)}',
|
||||
f'quiet: {str(payload.get("quiet", False)).lower()}',
|
||||
]
|
||||
if isinstance(targets, list) and targets:
|
||||
lines.append('targets:')
|
||||
lines.extend(f' - {target}' for target in targets)
|
||||
else:
|
||||
lines.append('targets: none')
|
||||
lines.extend(self._build_command_result_section('test', payload.get('test')))
|
||||
return '\n'.join(lines)
|
||||
|
||||
@staticmethod
|
||||
def _build_command_result_section(title: str, payload: dict[str, object] | None) -> list[str]:
|
||||
"""
|
||||
构建单个子命令执行结果段落。
|
||||
|
||||
:param title: 段落标题
|
||||
:param payload: 子命令执行结果
|
||||
:return: 文本行列表
|
||||
"""
|
||||
if not isinstance(payload, dict):
|
||||
return [f'{title}: none']
|
||||
|
||||
lines = [
|
||||
f'{title}:',
|
||||
f' ok: {str(payload.get("ok", False)).lower()}',
|
||||
f' return_code: {payload.get("returnCode", "-")}',
|
||||
f' command: {DevCommandPresenter._format_command_text(payload.get("command"))}',
|
||||
]
|
||||
stdout = payload.get('stdout')
|
||||
stderr = payload.get('stderr')
|
||||
if stdout:
|
||||
lines.append(' stdout:')
|
||||
lines.append(' |')
|
||||
lines.extend(f' {line}' for line in str(stdout).splitlines())
|
||||
if stderr:
|
||||
lines.append(' stderr:')
|
||||
lines.append(' |')
|
||||
lines.extend(f' {line}' for line in str(stderr).splitlines())
|
||||
return lines
|
||||
|
||||
@staticmethod
|
||||
def _format_command_text(command: object) -> str:
|
||||
"""
|
||||
将命令参数列表格式化为可读文本。
|
||||
|
||||
:param command: 命令参数列表
|
||||
:return: 格式化后的命令文本
|
||||
"""
|
||||
if not isinstance(command, list):
|
||||
return '-'
|
||||
return ' '.join(str(item) for item in command) or '-'
|
||||
Reference in New Issue
Block a user