You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.0 KiB
118 lines
3.0 KiB
import os
|
|
import logging
|
|
from jinja2 import Environment, FileSystemLoader
|
|
from db import get_table, get_columns, get_enum_field_names
|
|
from utils import *
|
|
import yaml
|
|
|
|
env = Environment(loader=FileSystemLoader("templates/vue"))
|
|
def render(template, out, ctx, overwrite=False):
|
|
|
|
# 文件存在且不允许覆盖 → 跳过
|
|
if os.path.exists(out) and not overwrite:
|
|
logging.info("Skip exists file: %s", out)
|
|
return
|
|
|
|
tpl = env.get_template(template)
|
|
content = tpl.render(**ctx)
|
|
|
|
os.makedirs(os.path.dirname(out), exist_ok=True)
|
|
|
|
with open(out, "w", encoding="utf-8") as f:
|
|
f.write(content)
|
|
|
|
|
|
def build_fields(table, enum_field_names=None):
|
|
|
|
if enum_field_names is None:
|
|
enum_field_names = set()
|
|
|
|
cols = get_columns(table)
|
|
|
|
fields = []
|
|
for c in cols:
|
|
field = {}
|
|
field["name"] = to_camel(c["column_name"])
|
|
field["comment"] = tab_chane_comment(c["column_comment"])
|
|
field["type"] = c["data_type"]
|
|
# 标记是否是枚举字段(基于 enums 表的 enums_name)
|
|
field["is_enum"] = c["column_name"] in enum_field_names
|
|
# ⭐ 在这里调用组件解析
|
|
field["component"] = parse_component(c)
|
|
fields.append(field)
|
|
|
|
return fields
|
|
|
|
def generate_vue(table, cfg , overwrite=False):
|
|
|
|
API_DIR = cfg["frontend"]["root"]+"/"+cfg["frontend"]["api"]
|
|
VIEW_DIR = cfg["frontend"]["root"]+"/"+cfg["frontend"]["views"]
|
|
ROUTER_DIR = cfg["frontend"]["root"]+"/"+cfg["frontend"]["router"]
|
|
MOCK_DIR = cfg["frontend"]["root"]+"/"+cfg["frontend"]["mock"]
|
|
EDIT_FIELDS = cfg["frontend"]["editFields"]
|
|
|
|
table_info = get_table(table)
|
|
entity = table
|
|
|
|
# 从 enums 表查询所有枚举字段名 (enums_name),自动判断哪些列是枚举
|
|
enum_field_names = set(get_enum_field_names())
|
|
fields = build_fields(table, enum_field_names)
|
|
|
|
# 自动构建枚举字段配置:只保留表里实际存在且在 enums 表中的字段
|
|
enum_fields_list = [
|
|
{"name": f["name"], "label": f["comment"]}
|
|
for f in fields
|
|
if f.get("is_enum")
|
|
]
|
|
|
|
ctx = {
|
|
"table":table,
|
|
"table_comment":table_info["table_comment"],
|
|
"old_table": to_kebab(table),
|
|
"entity":entity,
|
|
"editFields": to_class_join(EDIT_FIELDS),
|
|
"enumFieldsList": enum_fields_list,
|
|
"fields":fields
|
|
}
|
|
|
|
render(
|
|
"api.ts.j2",
|
|
f"{API_DIR}/{entity}.ts",
|
|
ctx,
|
|
overwrite
|
|
)
|
|
|
|
render(
|
|
"index.vue.j2",
|
|
f"{VIEW_DIR}/{entity}/index.vue",
|
|
ctx,
|
|
overwrite
|
|
)
|
|
|
|
render(
|
|
"data.ts.j2",
|
|
f"{VIEW_DIR}/{entity}/data.ts",
|
|
ctx,
|
|
overwrite
|
|
)
|
|
|
|
render(
|
|
"form.ts.j2",
|
|
f"{VIEW_DIR}/{entity}/form.ts",
|
|
ctx,
|
|
overwrite
|
|
)
|
|
|
|
render(
|
|
"router.ts.j2",
|
|
f"{ROUTER_DIR}/{entity}.ts",
|
|
ctx,
|
|
overwrite
|
|
)
|
|
|
|
render(
|
|
"mock.ts.j2",
|
|
f"{MOCK_DIR}/{entity}.ts",
|
|
ctx,
|
|
overwrite
|
|
)
|