21
21
from backend .common .exception import errors
22
22
from backend .core .path_conf import BASE_PATH
23
23
from backend .database .db import async_db_session
24
- from backend .utils .gen_template import gen_template
25
- from backend .utils .type_conversion import sql_type_to_pydantic
24
+ from backend .utils .generator . gen_template import gen_template
25
+ from backend .utils .generator . type_conversion import sql_type_to_pydantic
26
26
27
27
28
28
class GenService :
@@ -99,7 +99,7 @@ async def render_tpl_code(*, business: GenBusiness) -> dict[str, str]:
99
99
gen_vars = gen_template .get_vars (business , gen_models )
100
100
return {
101
101
tpl_path : await gen_template .get_template (tpl_path ).render_async (** gen_vars )
102
- for tpl_path in gen_template .get_template_paths ()
102
+ for tpl_path in gen_template .get_template_files ()
103
103
}
104
104
105
105
async def preview (self , * , pk : int ) -> dict [str , bytes ]:
@@ -115,10 +115,13 @@ async def preview(self, *, pk: int) -> dict[str, bytes]:
115
115
raise errors .NotFoundError (msg = '业务不存在' )
116
116
117
117
tpl_code_map = await self .render_tpl_code (business = business )
118
- return {
119
- tpl .replace ('.jinja' , '.py' ) if tpl .startswith ('py' ) else ...: code .encode ('utf-8' )
120
- for tpl , code in tpl_code_map .items ()
121
- }
118
+
119
+ codes = {}
120
+ for tpl , code in tpl_code_map .items ():
121
+ if tpl .startswith ('python' ):
122
+ codes [tpl .replace ('.jinja' , '.py' ).split ('/' )[- 1 ]] = code .encode ('utf-8' )
123
+
124
+ return codes
122
125
123
126
@staticmethod
124
127
async def get_generate_path (* , pk : int ) -> list [str ]:
@@ -133,9 +136,10 @@ async def get_generate_path(*, pk: int) -> list[str]:
133
136
if not business :
134
137
raise errors .NotFoundError (msg = '业务不存在' )
135
138
136
- gen_path = business .gen_path or 'fba-backend-app-path '
139
+ gen_path = business .gen_path or 'fba-backend-app-dir '
137
140
target_files = gen_template .get_code_gen_paths (business )
138
- return [os .path .join (gen_path , * target_file .split ('/' )[1 :]) for target_file in target_files ]
141
+
142
+ return [os .path .join (gen_path , * target_file .split ('/' )) for target_file in target_files ]
139
143
140
144
async def generate (self , * , pk : int ) -> None :
141
145
"""
@@ -155,32 +159,29 @@ async def generate(self, *, pk: int) -> None:
155
159
for tpl_path , code in tpl_code_map .items ():
156
160
code_filepath = os .path .join (
157
161
gen_path ,
158
- * gen_template .get_code_gen_path (tpl_path , business ).split ('/' )[ 1 :] ,
162
+ * gen_template .get_code_gen_path (tpl_path , business ).split ('/' ),
159
163
)
160
- code_folder = Path (str (code_filepath )).parent
161
- code_folder .mkdir (parents = True , exist_ok = True )
162
164
163
165
# 写入 init 文件
166
+ str_code_filepath = str (code_filepath )
167
+ code_folder = Path (str_code_filepath ).parent
168
+ code_folder .mkdir (parents = True , exist_ok = True )
169
+
164
170
init_filepath = code_folder .joinpath ('__init__.py' )
165
- if not init_filepath .exists ():
166
- async with aiofiles .open (init_filepath , 'w' , encoding = 'utf-8' ) as f :
167
- await f .write (gen_template .init_content )
171
+ async with aiofiles .open (init_filepath , 'w' , encoding = 'utf-8' ) as f :
172
+ await f .write (gen_template .init_content )
168
173
169
- if ' api' in str ( code_folder ):
170
- # api __init__.py
174
+ # api __init__.py
175
+ if ' api' in str_code_filepath :
171
176
api_init_filepath = code_folder .parent .joinpath ('__init__.py' )
172
- if not api_init_filepath .exists ():
173
- async with aiofiles .open (api_init_filepath , 'w' , encoding = 'utf-8' ) as f :
174
- await f .write (gen_template .init_content )
175
- # app __init__.py
176
- app_init_filepath = api_init_filepath .parent .joinpath ('__init__.py' )
177
- if not app_init_filepath .exists ():
178
- async with aiofiles .open (app_init_filepath , 'w' , encoding = 'utf-8' ) as f :
179
- await f .write (gen_template .init_content )
177
+ async with aiofiles .open (api_init_filepath , 'w' , encoding = 'utf-8' ) as f :
178
+ await f .write (gen_template .init_content )
180
179
181
- # 写入代码文件
182
- async with aiofiles .open (code_filepath , 'w' , encoding = 'utf-8' ) as f :
183
- await f .write (code )
180
+ # app __init__.py
181
+ if 'service' in str_code_filepath :
182
+ app_init_filepath = code_folder .parent .joinpath ('__init__.py' )
183
+ async with aiofiles .open (app_init_filepath , 'w' , encoding = 'utf-8' ) as f :
184
+ await f .write (gen_template .init_content )
184
185
185
186
# model init 文件补充
186
187
if code_folder .name == 'model' :
@@ -190,6 +191,10 @@ async def generate(self, *, pk: int) -> None:
190
191
f'import { to_pascal (business .table_name_en )} \n ' ,
191
192
)
192
193
194
+ # 写入代码文件
195
+ async with aiofiles .open (code_filepath , 'w' , encoding = 'utf-8' ) as f :
196
+ await f .write (code )
197
+
193
198
async def download (self , * , pk : int ) -> io .BytesIO :
194
199
"""
195
200
下载生成的代码
@@ -206,13 +211,12 @@ async def download(self, *, pk: int) -> io.BytesIO:
206
211
with zipfile .ZipFile (bio , 'w' ) as zf :
207
212
tpl_code_map = await self .render_tpl_code (business = business )
208
213
for tpl_path , code in tpl_code_map .items ():
209
- # 写入代码文件
210
- new_code_path = gen_template .get_code_gen_path (tpl_path , business )
211
- zf .writestr (new_code_path , code )
214
+ code_filepath = gen_template .get_code_gen_path (tpl_path , business )
212
215
213
216
# 写入 init 文件
214
- init_filepath = os .path .join (* new_code_path .split ('/' )[:- 1 ], '__init__.py' )
215
- if 'model' not in new_code_path .split ('/' ):
217
+ code_dir = os .path .dirname (code_filepath )
218
+ init_filepath = os .path .join (code_dir , '__init__.py' )
219
+ if 'model' not in code_filepath .split ('/' ):
216
220
zf .writestr (init_filepath , gen_template .init_content )
217
221
else :
218
222
zf .writestr (
@@ -222,11 +226,19 @@ async def download(self, *, pk: int) -> io.BytesIO:
222
226
f'import { to_pascal (business .table_name_en )} \n ' ,
223
227
)
224
228
225
- if ' api' in new_code_path :
226
- # api __init__.py
227
- api_init_filepath = os .path .join (* new_code_path . split ( '/' )[: - 2 ] , '__init__.py' )
229
+ # api __init__.py
230
+ if ' api' in code_dir :
231
+ api_init_filepath = os .path .join (os . path . dirname ( code_dir ) , '__init__.py' )
228
232
zf .writestr (api_init_filepath , gen_template .init_content )
229
233
234
+ # app __init__.py
235
+ if 'service' in code_dir :
236
+ app_init_filepath = os .path .join (os .path .dirname (code_dir ), '__init__.py' )
237
+ zf .writestr (app_init_filepath , gen_template .init_content )
238
+
239
+ # 写入代码文件
240
+ zf .writestr (code_filepath , code )
241
+
230
242
bio .seek (0 )
231
243
return bio
232
244
0 commit comments