Skip to content

Commit dcf235d

Browse files
authored
Fix code generation template params and filename (#784)
1 parent 1dfcd7a commit dcf235d

File tree

7 files changed

+30
-28
lines changed

7 files changed

+30
-28
lines changed

backend/plugin/code_generator/plugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[plugin]
22
summary = '代码生成'
3-
version = '0.0.3'
3+
version = '0.0.4'
44
description = '生成通用业务代码'
55
author = 'wu-clan'
66

backend/plugin/code_generator/service/code_service.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,21 @@ async def preview(self, *, pk: int) -> dict[str, bytes]:
125125
tpl_code_map = await self.render_tpl_code(business=business)
126126

127127
codes = {}
128-
for tpl, code in tpl_code_map.items():
129-
if tpl.startswith('python'):
128+
for tpl_path, code in tpl_code_map.items():
129+
if tpl_path.startswith('python'):
130130
rootpath = f'fastapi_best_architecture/backend/app/{business.app_name}'
131-
template_name = tpl.split('/')[-1]
131+
template_name = tpl_path.split('/')[-1]
132132
match template_name:
133133
case 'api.jinja':
134-
filepath = f'{rootpath}/api/{business.api_version}/{business.app_name}.py'
134+
filepath = f'{rootpath}/api/{business.api_version}/{business.filename}.py'
135135
case 'crud.jinja':
136-
filepath = f'{rootpath}/crud/crud_{business.app_name}.py'
136+
filepath = f'{rootpath}/crud/crud_{business.filename}.py'
137137
case 'model.jinja':
138-
filepath = f'{rootpath}/model/{business.app_name}.py'
138+
filepath = f'{rootpath}/model/{business.filename}.py'
139139
case 'schema.jinja':
140-
filepath = f'{rootpath}/schema/{business.app_name}.py'
140+
filepath = f'{rootpath}/schema/{business.filename}.py'
141141
case 'service.jinja':
142-
filepath = f'{rootpath}/service/{business.app_name}_service.py'
142+
filepath = f'{rootpath}/service/{business.filename}_service.py'
143143
codes[filepath] = code.encode('utf-8')
144144

145145
return codes
@@ -157,7 +157,7 @@ async def get_generate_path(*, pk: int) -> list[str]:
157157
if not business:
158158
raise errors.NotFoundError(msg='业务不存在')
159159

160-
gen_path = business.gen_path or 'fba-backend-app-dir'
160+
gen_path = business.gen_path or '.../backend/app/'
161161
target_files = gen_template.get_code_gen_paths(business)
162162

163163
return [os.path.join(gen_path, *target_file.split('/')) for target_file in target_files]

backend/plugin/code_generator/templates/python/crud.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ class CRUD{{ class_name }}(CRUDPlus[{{ schema_name }}]):
6666
return await self.delete_model_by_column(db, allow_multiple=True, id__in=pks)
6767

6868

69-
{{ instance_name }}_dao: CRUD{{ class_name }} = CRUD{{ class_name }}({{ class_name }})
69+
{{ table_name }}_dao: CRUD{{ class_name }} = CRUD{{ class_name }}({{ class_name }})

backend/plugin/code_generator/templates/python/model.jinja

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
{% if default_datetime_column %}
34
from datetime import datetime
5+
6+
{% endif %}
47
from uuid import UUID
58

69
import sqlalchemy as sa
@@ -12,10 +15,10 @@ from sqlalchemy.dialects import postgresql
1215
{% endif -%}
1316
from sqlalchemy.orm import Mapped, mapped_column
1417

15-
from backend.common.model import {% if default_datetime_column %}Base{% else %}MappedBase{% endif %}, id_key
18+
from backend.common.model import {% if default_datetime_column %}Base{% else %}DataClassBase{% endif %}, id_key
1619

1720

18-
class {{ class_name }}({% if default_datetime_column %}Base{% else %}MappedBase{% endif %}):
21+
class {{ class_name }}({% if default_datetime_column %}Base{% else %}DataClassBase{% endif %}):
1922
"""{{ table_comment }}"""
2023

2124
__tablename__ = '{{ table_name }}'

backend/plugin/code_generator/templates/python/schema.jinja

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
{% if default_datetime_column %}
34
from datetime import datetime
45

6+
{% endif %}
57
from pydantic import ConfigDict, Field
68

79
from backend.common.schema import SchemaBase

backend/plugin/code_generator/templates/python/service.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ class {{ class_name }}Service:
7575
return count
7676

7777

78-
{{ instance_name }}_service: {{ class_name }}Service = {{ class_name }}Service()
78+
{{ table_name }}_service: {{ class_name }}Service = {{ class_name }}Service()

backend/plugin/code_generator/utils/code_template.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Sequence
44

55
from jinja2 import Environment, FileSystemLoader, Template, select_autoescape
6-
from pydantic.alias_generators import to_pascal, to_snake
76

87
from backend.core.conf import settings
98
from backend.plugin.code_generator.model import GenBusiness, GenColumn
@@ -39,13 +38,13 @@ def get_template_files() -> list[str]:
3938
4039
:return:
4140
"""
42-
files = []
43-
44-
# python
45-
python_template_path = JINJA2_TEMPLATE_DIR / 'python'
46-
files.extend([f'python/{file.name}' for file in python_template_path.iterdir() if file.is_file()])
47-
48-
return files
41+
return [
42+
'python/api.jinja',
43+
'python/crud.jinja',
44+
'python/model.jinja',
45+
'python/schema.jinja',
46+
'python/service.jinja',
47+
]
4948

5049
@staticmethod
5150
def get_code_gen_paths(business: GenBusiness) -> list[str]:
@@ -73,8 +72,7 @@ def get_code_gen_path(self, tpl_path: str, business: GenBusiness) -> str:
7372
:param business: 代码生成业务对象
7473
:return:
7574
"""
76-
target_files = self.get_code_gen_paths(business)
77-
code_gen_path_mapping = dict(zip(self.get_template_files(), target_files))
75+
code_gen_path_mapping = dict(zip(self.get_template_files(), self.get_code_gen_paths(business)))
7876
return code_gen_path_mapping[tpl_path]
7977

8078
@staticmethod
@@ -88,12 +86,11 @@ def get_vars(business: GenBusiness, models: Sequence[GenColumn]) -> dict[str, st
8886
"""
8987
return {
9088
'app_name': business.app_name,
91-
'table_name': to_snake(business.table_name),
89+
'table_name': business.table_name,
9290
'doc_comment': business.doc_comment,
9391
'table_comment': business.table_comment,
94-
'class_name': to_pascal(business.class_name),
95-
'instance_name': to_snake(business.class_name),
96-
'schema_name': to_pascal(business.schema_name),
92+
'class_name': business.class_name,
93+
'schema_name': business.schema_name,
9794
'default_datetime_column': business.default_datetime_column,
9895
'permission': str(business.table_name.replace('_', ':')),
9996
'database_type': settings.DATABASE_TYPE,

0 commit comments

Comments
 (0)