99import cappa
1010import granian
1111
12- from cappa import ValueFrom
12+ from cappa . output import error_format
1313from rich .panel import Panel
1414from rich .prompt import IntPrompt
1515from rich .table import Table
1616from rich .text import Text
1717from sqlalchemy import text
1818from watchfiles import PythonFilter
1919
20- from backend import console , get_version
20+ from backend import __version__
2121from backend .common .enums import DataBaseType , PrimaryKeyType
2222from backend .common .exception .errors import BaseExceptionMixin
2323from backend .core .conf import settings
2727from backend .plugin .code_generator .service .code_service import gen_service
2828from backend .plugin .tools import get_plugin_sql
2929from backend .utils ._await import run_await
30+ from backend .utils .console import console
3031from backend .utils .file_ops import install_git_plugin , install_zip_plugin , parse_sql_script
3132
33+ output_help = '\n 更多信息,尝试 "[cyan]--help[/]"'
34+
3235
3336class 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
313312class 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
327325class 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
345337def 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