Skip to content

Commit

Permalink
Add custom code template pathname config (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan authored Jul 28, 2024
1 parent ceb8fb1 commit 863dfed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
3 changes: 3 additions & 0 deletions backend/app/generator/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class GeneratorSettings(BaseSettings):

model_config = SettingsConfigDict(env_file=f'{BasePath}/.env', env_file_encoding='utf-8', extra='ignore')

# 模版目录
TEMPLATE_BACKEND_DIR_NAME: str = 'py'

# 代码下载
ZIP_FILENAME: str = 'fba_generator'

Expand Down
31 changes: 16 additions & 15 deletions backend/utils/gen_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from jinja2 import Environment, FileSystemLoader, Template, select_autoescape
from pydantic.alias_generators import to_pascal, to_snake

from backend.app.generator.conf import generator_settings
from backend.app.generator.model import GenBusiness, GenModel
from backend.core.path_conf import JINJA2_TEMPLATE_DIR

Expand Down Expand Up @@ -37,30 +38,30 @@ def get_template_paths() -> list[str]:
:return:
"""
return [
'py/api.jinja',
'py/crud.jinja',
'py/model.jinja',
'py/schema.jinja',
'py/service.jinja',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/api.jinja',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/crud.jinja',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/model.jinja',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/schema.jinja',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/service.jinja',
]

@staticmethod
def get_code_gen_path(tpl_path: str, business: GenBusiness) -> str:
def get_code_gen_path(self, tpl_path: str, business: GenBusiness) -> str:
"""
获取代码生成路径
:return:
"""
app_name = business.app_name
module_name = business.table_name_en
code_gen_path_mapping = {
'py/api.jinja': f'py/{app_name}/api/{business.api_version}/{module_name}.py',
'py/crud.jinja': f'py/{app_name}/crud/crud_{module_name}.py',
'py/model.jinja': f'py/{app_name}/model/{module_name}.py',
'py/schema.jinja': f'py/{app_name}/schema/{module_name}.py',
'py/service.jinja': f'py/{app_name}/service/{module_name}_service.py',
}
return code_gen_path_mapping.get(tpl_path)
target_files = [
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/{app_name}/api/{business.api_version}/{module_name}.py',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/{app_name}/crud/crud_{module_name}.py',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/{app_name}/model/{module_name}.py',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/{app_name}/schema/{module_name}.py',
f'{generator_settings.TEMPLATE_BACKEND_DIR_NAME}/{app_name}/service/{module_name}_service.py',
]
code_gen_path_mapping = dict(zip(self.get_template_paths(), target_files))
return code_gen_path_mapping[tpl_path]

@staticmethod
def get_vars(business: GenBusiness, models: list[GenModel]) -> dict:
Expand Down

0 comments on commit 863dfed

Please sign in to comment.