Skip to content

Commit 629cf7c

Browse files
authored
Optimize the code generation interaction logic (#799)
1 parent fad6c05 commit 629cf7c

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

backend/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
from backend.common.i18n import i18n
4-
from backend.utils.console import console
54

65
__version__ = '1.8.0'
76

87

9-
def get_version() -> str | None:
10-
console.print(f'[cyan]{__version__}[/]')
11-
12-
138
# 初始化 i18n
149
i18n.load_locales()

backend/cli.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import cappa
1010
import granian
1111

12-
from cappa import ValueFrom
12+
from cappa.output import error_format
1313
from rich.panel import Panel
1414
from rich.prompt import IntPrompt
1515
from rich.table import Table
1616
from rich.text import Text
1717
from sqlalchemy import text
1818
from watchfiles import PythonFilter
1919

20-
from backend import console, get_version
20+
from backend import __version__
2121
from backend.common.enums import DataBaseType, PrimaryKeyType
2222
from backend.common.exception.errors import BaseExceptionMixin
2323
from backend.core.conf import settings
@@ -27,8 +27,11 @@
2727
from backend.plugin.code_generator.service.code_service import gen_service
2828
from backend.plugin.tools import get_plugin_sql
2929
from backend.utils._await import run_await
30+
from backend.utils.console import console
3031
from backend.utils.file_ops import install_git_plugin, install_zip_plugin, parse_sql_script
3132

33+
output_help = '\n更多信息,尝试 "[cyan]--help[/]"'
34+
3235

3336
class CustomReloadFilter(PythonFilter):
3437
"""自定义重载过滤器"""
@@ -144,9 +147,13 @@ async def import_table(
144147
raise cappa.Exit(e.msg if isinstance(e, BaseExceptionMixin) else str(e), code=1)
145148

146149

147-
def get_business() -> int:
148-
ids = []
150+
def generate(gen: bool) -> None:
151+
if not gen:
152+
console.print(output_help)
153+
return
154+
149155
try:
156+
ids = []
150157
results = run_await(gen_business_service.get_all)()
151158

152159
if not results:
@@ -159,26 +166,18 @@ def get_business() -> int:
159166
table.add_column('备注', style='blue')
160167

161168
for result in results:
169+
ids.append(result.id)
162170
table.add_row(
163171
str(result.id),
164172
result.app_name,
165173
result.gen_path or f'应用 {result.app_name} 根路径',
166174
result.remark or '',
167175
)
168-
ids.append(result.id)
169176

170177
console.print(table)
171-
except Exception as e:
172-
raise cappa.Exit(e.msg if isinstance(e, BaseExceptionMixin) else str(e), code=1)
173-
174-
business = IntPrompt.ask('请从中选择一个业务编号', choices=[str(_id) for _id in ids])
178+
business = IntPrompt.ask('请从中选择一个业务编号', choices=[str(_id) for _id in ids])
175179

176-
return business
177-
178-
179-
def generate(pk: int) -> None:
180-
try:
181-
gen_path = run_await(gen_service.generate)(pk=pk)
180+
gen_path = run_await(gen_service.generate)(pk=business)
182181
except Exception as e:
183182
raise cappa.Exit(e.msg if isinstance(e, BaseExceptionMixin) else str(e), code=1)
184183

@@ -308,40 +307,33 @@ async def __call__(self):
308307
await import_table(self.app, self.table_schema, self.table_name)
309308

310309

311-
@cappa.command(name='gen', help='代码生成,体验完成功能,请自行部署 fba vben 前端工程', default_long=True)
310+
@cappa.command(name='codegen', help='代码生成(体验完整功能,请自行部署 fba vben 前端工程', default_long=True)
312311
@dataclass
313312
class CodeGenerate:
314-
business: Annotated[
315-
int,
316-
cappa.Arg(default=ValueFrom(get_business), help='业务编号'),
313+
gen: Annotated[
314+
bool,
315+
cappa.Arg(default=False, show_default=False, help='执行代码生成'),
317316
]
318317
subcmd: cappa.Subcommands[Import | None] = None
319318

320319
def __call__(self):
321-
if self.business:
322-
generate(self.business)
320+
generate(self.gen)
323321

324322

325323
@cappa.command(help='一个高效的 fba 命令行界面', default_long=True)
326324
@dataclass
327325
class FbaCli:
328-
version: Annotated[
329-
bool,
330-
cappa.Arg(short='-V', default=False, show_default=False, help='打印当前版本号'),
331-
]
332326
sql: Annotated[
333327
str,
334328
cappa.Arg(value_name='PATH', default='', show_default=False, help='在事务中执行 SQL 脚本'),
335329
]
336330
subcmd: cappa.Subcommands[Run | Celery | Add | CodeGenerate | None] = None
337331

338332
async def __call__(self):
339-
if self.version:
340-
get_version()
341333
if self.sql:
342334
await execute_sql_scripts(self.sql)
343335

344336

345337
def main() -> None:
346-
output = cappa.Output(error_format='[red]Error[/]: {message}\n\n更多信息,尝试 "[cyan]--help[/]"')
347-
asyncio.run(cappa.invoke_async(FbaCli, output=output))
338+
output = cappa.Output(error_format=f'{error_format}\n{output_help}')
339+
asyncio.run(cappa.invoke_async(FbaCli, version=__version__, output=output))

0 commit comments

Comments
 (0)