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.
 
 
 
 
 
 

101 lines
2.0 KiB

import os
from jinja2 import Environment, FileSystemLoader
from db import get_columns
from utils import *
import yaml
env = Environment(loader=FileSystemLoader("templates/vue"))
def render(template, out, ctx):
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):
cols = get_columns(table)
fields = []
for c in cols:
field = {}
field["name"] = to_camel(c["column_name"])
field["comment"] = c["column_comment"]
field["type"] = c["data_type"]
# ⭐ 在这里调用组件解析
field["component"] = parse_component(c)
fields.append(field)
return fields
def generate(table):
with open("./config.yml", "r", encoding="utf-8") as f:
cfg = yaml.safe_load(f)
cfg = resolve_config(cfg)
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"]
fields = get_columns(table)
entity = table.replace("health_","")
ctx = {
"table":table,
"old_table": to_kebab(table),
"entity":entity,
"fields":build_fields(table)
}
render(
"api.ts.j2",
f"{API_DIR}/{entity}.ts",
ctx
)
render(
"index.vue.j2",
f"{VIEW_DIR}/{entity}/index.vue",
ctx
)
render(
"data.ts.j2",
f"{VIEW_DIR}/{entity}/data.ts",
ctx
)
render(
"form.ts.j2",
f"{VIEW_DIR}/{entity}/form.ts",
ctx
)
render(
"router.ts.j2",
f"{ROUTER_DIR}/{entity}.ts",
ctx
)
render(
"mock.ts.j2",
f"{MOCK_DIR}/{entity}.ts",
ctx
)
if __name__ == "__main__":
generate("health_user")