refactor(codegen): 收敛命名转换并统一模板注释
- 新增 NamingUtils,目录、路由与模板变量共用同一套命名规则 - 字段特性标记合并为集合,模板变量按常量循环生成 - 删除模板未引用的 entityComment - 权限前缀按模块映射配置 - 模板方法注释统一为多行
This commit is contained in:
1 parent
6915a01cb6
commit
fcd8381bd1
20 files changed
+376
-109
No files matched your search
@@ -85,7 +85,7 @@ public class CodegenController {
|
||||
@Operation(summary = "获取预览生成代码")
|
||||
@GetMapping("/{tableName}/preview")
|
||||
public Result<List<CodegenPreviewVO>> getTablePreviewData(@PathVariable String tableName,
|
||||
@RequestParam(value = "pageType", required = false, defaultValue = "classic") String pageType,
|
||||
@RequestParam(value = "pageType", required = false, defaultValue = "crud") String pageType,
|
||||
@RequestParam(value = "type", required = false, defaultValue = "ts") String type) {
|
||||
List<CodegenPreviewVO> list = codegenService.getCodegenPreviewData(tableName, pageType, type);
|
||||
return Result.success(list);
|
||||
@@ -95,7 +95,7 @@ public class CodegenController {
|
||||
@GetMapping("/{tableName}/download")
|
||||
@Log(module = LogModuleEnum.CODEGEN, value = ActionTypeEnum.DOWNLOAD)
|
||||
public void downloadZip(HttpServletResponse response, @PathVariable String tableName,
|
||||
@RequestParam(value = "pageType", required = false, defaultValue = "classic") String pageType,
|
||||
@RequestParam(value = "pageType", required = false, defaultValue = "crud") String pageType,
|
||||
@RequestParam(value = "type", required = false, defaultValue = "ts") String type) {
|
||||
String[] tableNames = tableName.split(",");
|
||||
byte[] data = codegenService.downloadCode(tableNames, pageType, type);
|
||||
|
||||
@@ -60,7 +60,7 @@ public class GenConfigForm {
|
||||
@Schema(description = "前端应用名")
|
||||
private String frontendAppName;
|
||||
|
||||
@Schema(description = "页面类型 classic|crud", example = "classic")
|
||||
@Schema(description = "页面类型 classic|crud", example = "crud")
|
||||
private String pageType;
|
||||
|
||||
@Schema(description = "要移除的表前缀,如: sys_", example = "sys_")
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.youlai.boot.codegen.property;
|
||||
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -22,11 +23,26 @@ public class CodegenProperties {
|
||||
|
||||
|
||||
/** 默认配置 */
|
||||
private DefaultConfig defaultConfig ;
|
||||
private DefaultConfig defaultConfig;
|
||||
|
||||
/** 模板配置 */
|
||||
private Map<String, TemplateConfig> templateConfigs = MapUtil.newHashMap(true);
|
||||
|
||||
/** 模块名 → 权限前缀,未配置时用模块名 */
|
||||
private Map<String, String> permissionNamespaces = MapUtil.newHashMap(true);
|
||||
|
||||
/**
|
||||
* 解析权限标识前缀
|
||||
*
|
||||
* @param moduleName 模块名
|
||||
* @param entityName 实体名
|
||||
* @return 权限标识前缀,如 sys:user
|
||||
*/
|
||||
public String resolvePermissionPrefix(String moduleName, String entityName) {
|
||||
String namespace = permissionNamespaces.getOrDefault(moduleName, moduleName);
|
||||
return namespace + ":" + StrUtil.toSymbolCase(entityName, '-');
|
||||
}
|
||||
|
||||
/** 后端应用名 */
|
||||
private String backendAppName;
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import com.youlai.boot.codegen.property.CodegenProperties;
|
||||
import com.youlai.boot.codegen.service.GenTableService;
|
||||
import com.youlai.boot.codegen.service.GenTableColumnService;
|
||||
import com.youlai.boot.codegen.service.CodegenService;
|
||||
import com.youlai.boot.codegen.util.NamingUtils;
|
||||
import com.youlai.boot.common.exception.BusinessException;
|
||||
import com.youlai.boot.codegen.mapper.DatabaseMapper;
|
||||
import com.youlai.boot.codegen.model.entity.GenTable;
|
||||
@@ -46,6 +47,9 @@ import java.util.zip.ZipOutputStream;
|
||||
@Slf4j
|
||||
public class CodegenServiceImpl implements CodegenService {
|
||||
|
||||
private static final List<String> FEATURES = List.of(
|
||||
"LocalDate", "LocalDateTime", "BigDecimal", "NotBlank", "NotNull", "Size");
|
||||
|
||||
private final DatabaseMapper databaseMapper;
|
||||
private final CodegenProperties codegenProperties;
|
||||
private final GenTableService genTableService;
|
||||
@@ -247,14 +251,14 @@ public class CodegenServiceImpl implements CodegenService {
|
||||
+ File.separator + "src"
|
||||
+ File.separator + "api"
|
||||
+ File.separator + moduleName
|
||||
+ File.separator + StrUtil.toSymbolCase(entityName, '-')
|
||||
+ File.separator + NamingUtils.kebab(entityName)
|
||||
);
|
||||
} else if ("API_TYPES".equals(templateName)) {
|
||||
path = (codegenProperties.getFrontendAppName()
|
||||
+ File.separator + "src"
|
||||
+ File.separator + "api"
|
||||
+ File.separator + moduleName
|
||||
+ File.separator + StrUtil.toSymbolCase(entityName, '-')
|
||||
+ File.separator + NamingUtils.kebab(entityName)
|
||||
);
|
||||
} else if ("VIEW".equals(templateName)) {
|
||||
// path = "src/views/system/user";
|
||||
@@ -262,7 +266,7 @@ public class CodegenServiceImpl implements CodegenService {
|
||||
+ File.separator + "src"
|
||||
+ File.separator + subPackageName
|
||||
+ File.separator + moduleName
|
||||
+ File.separator + StrUtil.toSymbolCase(entityName, '-')
|
||||
+ File.separator + NamingUtils.kebab(entityName)
|
||||
);
|
||||
} else {
|
||||
path = (codegenProperties.getBackendAppName()
|
||||
@@ -297,8 +301,8 @@ public class CodegenServiceImpl implements CodegenService {
|
||||
String pageType) {
|
||||
|
||||
Map<String, Object> bindMap = new HashMap<>();
|
||||
|
||||
String entityName = genTable.getEntityName();
|
||||
String entityKebab = NamingUtils.kebab(entityName);
|
||||
|
||||
bindMap.put("packageName", genTable.getPackageName());
|
||||
bindMap.put("moduleName", genTable.getModuleName());
|
||||
@@ -307,65 +311,41 @@ public class CodegenServiceImpl implements CodegenService {
|
||||
bindMap.put("entityName", entityName);
|
||||
bindMap.put("tableName", genTable.getTableName());
|
||||
bindMap.put("author", genTable.getAuthor());
|
||||
String entityLowerCamel = StrUtil.lowerFirst(entityName);
|
||||
String entityKebab = StrUtil.toSymbolCase(entityName, '-');
|
||||
String entityUpperSnake = StrUtil.toSymbolCase(entityName, '_').toUpperCase();
|
||||
bindMap.put("entityLowerCamel", entityLowerCamel);
|
||||
bindMap.put("entityLowerCamel", StrUtil.lowerFirst(entityName));
|
||||
bindMap.put("entityKebab", entityKebab);
|
||||
bindMap.put("entityUpperSnake", entityUpperSnake);
|
||||
bindMap.put("entityKebabPlural", NamingUtils.pluralKebab(entityKebab));
|
||||
bindMap.put("entityUpperSnake", NamingUtils.upperSnake(entityName));
|
||||
bindMap.put("permissionPrefix",
|
||||
codegenProperties.resolvePermissionPrefix(genTable.getModuleName(), entityName));
|
||||
bindMap.put("businessName", genTable.getBusinessName());
|
||||
bindMap.put("entityComment", genTable.getBusinessName());
|
||||
bindMap.put("fieldConfigs", fieldConfigs);
|
||||
|
||||
boolean hasLocalDate = false;
|
||||
boolean hasLocalDateTime = false;
|
||||
boolean hasBigDecimal = false;
|
||||
boolean hasNotBlank = false;
|
||||
boolean hasNotNull = false;
|
||||
boolean hasSize = false;
|
||||
|
||||
Set<String> features = new HashSet<>();
|
||||
for (GenTableColumn fieldConfig : fieldConfigs) {
|
||||
|
||||
if (StrUtil.isBlank(fieldConfig.getFieldType())) {
|
||||
fieldConfig.setFieldType(JavaTypeEnum.getJavaTypeByColumnType(fieldConfig.getColumnType()));
|
||||
}
|
||||
|
||||
if ("LocalDate".equals(fieldConfig.getFieldType())) {
|
||||
hasLocalDate = true;
|
||||
}
|
||||
if ("LocalDateTime".equals(fieldConfig.getFieldType())) {
|
||||
hasLocalDateTime = true;
|
||||
}
|
||||
if ("BigDecimal".equals(fieldConfig.getFieldType())) {
|
||||
hasBigDecimal = true;
|
||||
}
|
||||
String fieldType = fieldConfig.getFieldType();
|
||||
features.add(fieldType);
|
||||
if (ObjectUtil.equals(fieldConfig.getIsShowInForm(), 1) && !"id".equals(fieldConfig.getFieldName())) {
|
||||
boolean isString = "String".equals(fieldConfig.getFieldType());
|
||||
boolean isString = "String".equals(fieldType);
|
||||
if (ObjectUtil.equals(fieldConfig.getIsRequired(), 1)) {
|
||||
if (isString) {
|
||||
hasNotBlank = true;
|
||||
} else {
|
||||
hasNotNull = true;
|
||||
}
|
||||
features.add(isString ? "NotBlank" : "NotNull");
|
||||
}
|
||||
if (isString && fieldConfig.getMaxLength() != null && fieldConfig.getMaxLength() > 0) {
|
||||
hasSize = true;
|
||||
features.add("Size");
|
||||
}
|
||||
}
|
||||
fieldConfig.setTsType(JavaTypeEnum.getTsTypeByJavaType(fieldConfig.getFieldType()));
|
||||
fieldConfig.setTsType(JavaTypeEnum.getTsTypeByJavaType(fieldType));
|
||||
}
|
||||
for (String feature : FEATURES) {
|
||||
bindMap.put("has" + feature, features.contains(feature));
|
||||
}
|
||||
|
||||
bindMap.put("hasLocalDate", hasLocalDate);
|
||||
bindMap.put("hasLocalDateTime", hasLocalDateTime);
|
||||
bindMap.put("hasBigDecimal", hasBigDecimal);
|
||||
bindMap.put("hasNotBlank", hasNotBlank);
|
||||
bindMap.put("hasNotNull", hasNotNull);
|
||||
bindMap.put("hasSize", hasSize);
|
||||
|
||||
TemplateEngine templateEngine = TemplateUtil.createEngine(new TemplateConfig("codegen", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
// 根据 ui 选择不同的前端页面模板:默认 index.vue.vm;封装版使用 index.crud.vue.vm
|
||||
// classic 为兼容模板,其余情况统一使用 CrudPage 页面
|
||||
String path = templatePath;
|
||||
if ("crud".equalsIgnoreCase(pageType)) {
|
||||
if (!"classic".equalsIgnoreCase(pageType)) {
|
||||
if (path.endsWith("index.js.vue.vm")) {
|
||||
path = path.replace("index.js.vue.vm", "index.crud.js.vue.vm");
|
||||
} else if (path.endsWith("index.vue.vm")) {
|
||||
|
||||
@@ -198,16 +198,21 @@ public class GenTableServiceImpl extends ServiceImpl<GenTableMapper, GenTable> i
|
||||
@Override
|
||||
public void saveGenConfig(GenConfigForm formData) {
|
||||
GenTable genTable = codegenConverter.toGenTable(formData);
|
||||
if (StrUtil.isBlank(genTable.getPageType())) {
|
||||
genTable.setPageType("crud");
|
||||
}
|
||||
this.saveOrUpdate(genTable);
|
||||
|
||||
// 如果选择上级菜单且当前环境不是生产环境,则保存菜单
|
||||
Long parentMenuId = formData.getParentMenuId();
|
||||
if (parentMenuId != null && !EnvEnum.PROD.getValue().equals(springProfilesActive)) {
|
||||
String permissionPrefix = codegenProperties.resolvePermissionPrefix(
|
||||
genTable.getModuleName(), genTable.getEntityName());
|
||||
MenuCreateDTO dto = new MenuCreateDTO(
|
||||
genTable.getBusinessName(),
|
||||
genTable.getEntityName(),
|
||||
genTable.getModuleName(),
|
||||
genTable.getTableName()
|
||||
permissionPrefix
|
||||
);
|
||||
menuService.addMenuForCodegen(parentMenuId, dto);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.youlai.boot.codegen.util;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 命名转换,代码生成的目录、路由与权限标识使用同一套规则
|
||||
*
|
||||
* @author Ray.Hao
|
||||
* @since 4.14.1
|
||||
*/
|
||||
public final class NamingUtils {
|
||||
|
||||
private NamingUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转短横线命名,如 MenuDemo → menu-demo
|
||||
*/
|
||||
public static String kebab(String value) {
|
||||
return StrUtil.toSymbolCase(value, '-');
|
||||
}
|
||||
|
||||
/**
|
||||
* 转大写下划线命名,如 MenuDemo → MENU_DEMO
|
||||
*/
|
||||
public static String upperSnake(String value) {
|
||||
return StrUtil.toSymbolCase(value, '_').toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 短横线命名转英文复数,如 dict-item → dict-items、category → categories
|
||||
*/
|
||||
public static String pluralKebab(String kebab) {
|
||||
if (kebab.matches(".*(s|x|z|ch|sh)$")) {
|
||||
return kebab + "es";
|
||||
}
|
||||
if (kebab.matches(".*[^aeiou]y$")) {
|
||||
return kebab.substring(0, kebab.length() - 1) + "ies";
|
||||
}
|
||||
return kebab + "s";
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public record MenuCreateDTO(
|
||||
/** 前端组件所属模块名,如 system */
|
||||
String moduleName,
|
||||
|
||||
/** 按钮权限前缀使用的表名,如 sys_user */
|
||||
String tableName
|
||||
/** 按钮权限标识前缀,如 sys:user */
|
||||
String permissionPrefix
|
||||
) {
|
||||
}
|
||||
@@ -573,7 +573,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
||||
this.updateById(menu);
|
||||
|
||||
// 生成 CRUD 按钮权限
|
||||
saveCrudButtons(menu.getId(), dto.moduleName() + ":" + dto.tableName().replace("_", "-"), treePath);
|
||||
saveCrudButtons(menu.getId(), dto.permissionPrefix(), treePath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user