9
9
import cappa
10
10
import granian
11
11
12
- from cappa import ValueFrom
12
+ from cappa . output import error_format
13
13
from rich .panel import Panel
14
14
from rich .prompt import IntPrompt
15
15
from rich .table import Table
16
16
from rich .text import Text
17
17
from sqlalchemy import text
18
18
from watchfiles import PythonFilter
19
19
20
- from backend import console , get_version
20
+ from backend import __version__
21
21
from backend .common .enums import DataBaseType , PrimaryKeyType
22
22
from backend .common .exception .errors import BaseExceptionMixin
23
23
from backend .core .conf import settings
27
27
from backend .plugin .code_generator .service .code_service import gen_service
28
28
from backend .plugin .tools import get_plugin_sql
29
29
from backend .utils ._await import run_await
30
+ from backend .utils .console import console
30
31
from backend .utils .file_ops import install_git_plugin , install_zip_plugin , parse_sql_script
31
32
33
+ output_help = '\n 更多信息,尝试 "[cyan]--help[/]"'
34
+
32
35
33
36
class CustomReloadFilter (PythonFilter ):
34
37
"""自定义重载过滤器"""
@@ -144,9 +147,13 @@ async def import_table(
144
147
raise cappa .Exit (e .msg if isinstance (e , BaseExceptionMixin ) else str (e ), code = 1 )
145
148
146
149
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
+
149
155
try :
156
+ ids = []
150
157
results = run_await (gen_business_service .get_all )()
151
158
152
159
if not results :
@@ -159,26 +166,18 @@ def get_business() -> int:
159
166
table .add_column ('备注' , style = 'blue' )
160
167
161
168
for result in results :
169
+ ids .append (result .id )
162
170
table .add_row (
163
171
str (result .id ),
164
172
result .app_name ,
165
173
result .gen_path or f'应用 { result .app_name } 根路径' ,
166
174
result .remark or '' ,
167
175
)
168
- ids .append (result .id )
169
176
170
177
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 ])
175
179
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 )
182
181
except Exception as e :
183
182
raise cappa .Exit (e .msg if isinstance (e , BaseExceptionMixin ) else str (e ), code = 1 )
184
183
@@ -308,40 +307,33 @@ async def __call__(self):
308
307
await import_table (self .app , self .table_schema , self .table_name )
309
308
310
309
311
- @cappa .command (name = 'gen ' , help = '代码生成,体验完成功能, 请自行部署 fba vben 前端工程! ' , default_long = True )
310
+ @cappa .command (name = 'codegen ' , help = '代码生成(体验完整功能, 请自行部署 fba vben 前端工程) ' , default_long = True )
312
311
@dataclass
313
312
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 = '执行代码生成 ' ),
317
316
]
318
317
subcmd : cappa .Subcommands [Import | None ] = None
319
318
320
319
def __call__ (self ):
321
- if self .business :
322
- generate (self .business )
320
+ generate (self .gen )
323
321
324
322
325
323
@cappa .command (help = '一个高效的 fba 命令行界面' , default_long = True )
326
324
@dataclass
327
325
class FbaCli :
328
- version : Annotated [
329
- bool ,
330
- cappa .Arg (short = '-V' , default = False , show_default = False , help = '打印当前版本号' ),
331
- ]
332
326
sql : Annotated [
333
327
str ,
334
328
cappa .Arg (value_name = 'PATH' , default = '' , show_default = False , help = '在事务中执行 SQL 脚本' ),
335
329
]
336
330
subcmd : cappa .Subcommands [Run | Celery | Add | CodeGenerate | None ] = None
337
331
338
332
async def __call__ (self ):
339
- if self .version :
340
- get_version ()
341
333
if self .sql :
342
334
await execute_sql_scripts (self .sql )
343
335
344
336
345
337
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