Commit f07191437e4aa859ad642b0e61c95f3ab33feb26

Authored by YunaiV
1 parent 467170d3

改造代码生成器,支持多种前端模版

yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/enums/codegen/CodegenFrontTypeEnum.java 0 → 100644
  1 +package cn.iocoder.yudao.module.infra.enums.codegen;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Getter;
  5 +
  6 +/**
  7 + * 代码生成的前端类型枚举
  8 + *
  9 + * @author 芋道源码
  10 + */
  11 +@AllArgsConstructor
  12 +@Getter
  13 +public enum CodegenFrontTypeEnum {
  14 +
  15 + VUE2(10), // Vue2 Element UI 标准模版
  16 + VUE3(20), // Vue3 Element Plus 标准模版
  17 + VUE3_SCHEMA(21), // Vue3 Element Plus Schema 模版
  18 + VUE3_VXE(22), // Vue3 VXE 模版
  19 + ;
  20 +
  21 + /**
  22 + * 类型
  23 + */
  24 + private final Integer type;
  25 +
  26 +}
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/codegen/config/CodegenProperties.java
1 package cn.iocoder.yudao.module.infra.framework.codegen.config; 1 package cn.iocoder.yudao.module.infra.framework.codegen.config;
2 2
  3 +import cn.iocoder.yudao.module.infra.enums.codegen.CodegenFrontTypeEnum;
3 import lombok.Data; 4 import lombok.Data;
4 import org.springframework.boot.context.properties.ConfigurationProperties; 5 import org.springframework.boot.context.properties.ConfigurationProperties;
5 import org.springframework.validation.annotation.Validated; 6 import org.springframework.validation.annotation.Validated;
@@ -25,4 +26,12 @@ public class CodegenProperties { @@ -25,4 +26,12 @@ public class CodegenProperties {
25 @NotEmpty(message = "数据库不能为空") 26 @NotEmpty(message = "数据库不能为空")
26 private Collection<String> dbSchemas; 27 private Collection<String> dbSchemas;
27 28
  29 + /**
  30 + * 代码生成的前端类型
  31 + *
  32 + * 枚举 {@link CodegenFrontTypeEnum#getType()}
  33 + */
  34 + @NotNull(message = "代码生成的前端类型不能为空")
  35 + private Integer frontType;
  36 +
28 } 37 }
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java
@@ -23,9 +23,12 @@ import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; @@ -23,9 +23,12 @@ import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
23 import cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum; 23 import cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum;
24 import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenColumnDO; 24 import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenColumnDO;
25 import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO; 25 import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO;
  26 +import cn.iocoder.yudao.module.infra.enums.codegen.CodegenFrontTypeEnum;
26 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum; 27 import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
27 import cn.iocoder.yudao.module.infra.framework.codegen.config.CodegenProperties; 28 import cn.iocoder.yudao.module.infra.framework.codegen.config.CodegenProperties;
  29 +import com.google.common.collect.ImmutableTable;
28 import com.google.common.collect.Maps; 30 import com.google.common.collect.Maps;
  31 +import com.google.common.collect.Table;
29 import org.springframework.stereotype.Component; 32 import org.springframework.stereotype.Component;
30 33
31 import javax.annotation.PostConstruct; 34 import javax.annotation.PostConstruct;
@@ -50,11 +53,12 @@ import static cn.hutool.core.text.CharSequenceUtil.*; @@ -50,11 +53,12 @@ import static cn.hutool.core.text.CharSequenceUtil.*;
50 public class CodegenEngine { 53 public class CodegenEngine {
51 54
52 /** 55 /**
53 - * 模板配置 56 + * 后端的模板配置
  57 + *
54 * key:模板在 resources 的地址 58 * key:模板在 resources 的地址
55 * value:生成的路径 59 * value:生成的路径
56 */ 60 */
57 - private static final Map<String, String> TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序 61 + private static final Map<String, String> SERVER_TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序
58 // Java module-biz Main 62 // Java module-biz Main
59 .put(javaTemplatePath("controller/vo/baseVO"), javaModuleImplVOFilePath("BaseVO")) 63 .put(javaTemplatePath("controller/vo/baseVO"), javaModuleImplVOFilePath("BaseVO"))
60 .put(javaTemplatePath("controller/vo/createReqVO"), javaModuleImplVOFilePath("CreateReqVO")) 64 .put(javaTemplatePath("controller/vo/createReqVO"), javaModuleImplVOFilePath("CreateReqVO"))
@@ -80,21 +84,31 @@ public class CodegenEngine { @@ -80,21 +84,31 @@ public class CodegenEngine {
80 javaModuleImplTestFilePath("service/${table.businessName}/${table.className}ServiceImplTest")) 84 javaModuleImplTestFilePath("service/${table.businessName}/${table.className}ServiceImplTest"))
81 // Java module-api Main 85 // Java module-api Main
82 .put(javaTemplatePath("enums/errorcode"), javaModuleApiMainFilePath("enums/ErrorCodeConstants_手动操作")) 86 .put(javaTemplatePath("enums/errorcode"), javaModuleApiMainFilePath("enums/ErrorCodeConstants_手动操作"))
  87 + // SQL
  88 + .put("codegen/sql/sql.vm", "sql/sql.sql")
  89 + .put("codegen/sql/h2.vm", "sql/h2.sql")
  90 + .build();
  91 +
  92 + /**
  93 + * 后端的配置模版
  94 + *
  95 + * key1:UI 模版的类型 {@link CodegenFrontTypeEnum#getType()}
  96 + * key2:模板在 resources 的地址
  97 + * value:生成的路径
  98 + */
  99 + private static final Table<Integer, String, String> FRONT_TEMPLATES = ImmutableTable.<Integer, String, String>builder()
83 // Vue2 100 // Vue2
84 - .put(vueTemplatePath("views/index.vue"), 101 + .put(CodegenFrontTypeEnum.VUE2.getType(), vueTemplatePath("views/index.vue"),
85 vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue")) 102 vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue"))
86 - .put(vueTemplatePath("api/api.js"), 103 + .put(CodegenFrontTypeEnum.VUE2.getType(), vueTemplatePath("api/api.js"),
87 vueFilePath("api/${table.moduleName}/${classNameVar}.js")) 104 vueFilePath("api/${table.moduleName}/${classNameVar}.js"))
88 // Vue3 105 // Vue3
89 - .put(vue3TemplatePath("views/index.vue"), 106 + .put(CodegenFrontTypeEnum.VUE3.getType(), vue3TemplatePath("views/index.vue"),
90 vue3FilePath("views/${table.moduleName}/${classNameVar}/index.vue")) 107 vue3FilePath("views/${table.moduleName}/${classNameVar}/index.vue"))
91 - .put(vue3TemplatePath("views/data.ts"), 108 + .put(CodegenFrontTypeEnum.VUE3.getType(), vue3TemplatePath("views/data.ts"),
92 vue3FilePath("views/${table.moduleName}/${classNameVar}/${classNameVar}.data.ts")) 109 vue3FilePath("views/${table.moduleName}/${classNameVar}/${classNameVar}.data.ts"))
93 - .put(vue3TemplatePath("api/api.ts"), 110 + .put(CodegenFrontTypeEnum.VUE3.getType(), vue3TemplatePath("api/api.ts"),
94 vue3FilePath("api/${table.moduleName}/${classNameVar}/index.ts")) 111 vue3FilePath("api/${table.moduleName}/${classNameVar}/index.ts"))
95 - // SQL  
96 - .put("codegen/sql/sql.vm", "sql/sql.sql")  
97 - .put("codegen/sql/h2.vm", "sql/h2.sql")  
98 .build(); 112 .build();
99 113
100 @Resource 114 @Resource
@@ -165,8 +179,9 @@ public class CodegenEngine { @@ -165,8 +179,9 @@ public class CodegenEngine {
165 bindingMap.put("permissionPrefix", table.getModuleName() + ":" + simpleClassNameStrikeCase); 179 bindingMap.put("permissionPrefix", table.getModuleName() + ":" + simpleClassNameStrikeCase);
166 180
167 // 执行生成 181 // 执行生成
168 - final Map<String, String> result = Maps.newLinkedHashMapWithExpectedSize(TEMPLATES.size()); // 有序  
169 - TEMPLATES.forEach((vmPath, filePath) -> { 182 + Map<String, String> templates = getTemplates();
  183 + Map<String, String> result = Maps.newLinkedHashMapWithExpectedSize(templates.size()); // 有序
  184 + templates.forEach((vmPath, filePath) -> {
170 filePath = formatFilePath(filePath, bindingMap); 185 filePath = formatFilePath(filePath, bindingMap);
171 String content = templateEngine.getTemplate(vmPath).render(bindingMap); 186 String content = templateEngine.getTemplate(vmPath).render(bindingMap);
172 result.put(filePath, content); 187 result.put(filePath, content);
@@ -174,6 +189,13 @@ public class CodegenEngine { @@ -174,6 +189,13 @@ public class CodegenEngine {
174 return result; 189 return result;
175 } 190 }
176 191
  192 + private Map<String, String> getTemplates() {
  193 + Map<String, String> templates = new LinkedHashMap<>();
  194 + templates.putAll(SERVER_TEMPLATES);
  195 + templates.putAll(FRONT_TEMPLATES.row(codegenProperties.getFrontType()));
  196 + return templates;
  197 + }
  198 +
177 private String formatFilePath(String filePath, Map<String, Object> bindingMap) { 199 private String formatFilePath(String filePath, Map<String, Object> bindingMap) {
178 filePath = StrUtil.replace(filePath, "${basePackage}", 200 filePath = StrUtil.replace(filePath, "${basePackage}",
179 getStr(bindingMap, "basePackage").replaceAll("\\.", "/")); 201 getStr(bindingMap, "basePackage").replaceAll("\\.", "/"));
yudao-server/src/main/resources/application.yaml
@@ -125,6 +125,7 @@ yudao: @@ -125,6 +125,7 @@ yudao:
125 codegen: 125 codegen:
126 base-package: ${yudao.info.base-package} 126 base-package: ${yudao.info.base-package}
127 db-schemas: ${spring.datasource.dynamic.datasource.master.name} 127 db-schemas: ${spring.datasource.dynamic.datasource.master.name}
  128 + front-type: 20
128 error-code: # 错误码相关配置项 129 error-code: # 错误码相关配置项
129 constants-class-list: 130 constants-class-list:
130 - cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants 131 - cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants