mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 04:39:00 +08:00
feat(代码生成): 支持模板通过github 动态更新检查
(cherry picked from commit e449f424e2dec78c61a2e3a5984f06230b91fca8)
This commit is contained in:
1 parent
9e5cbd4fb5
commit
5d75b8294f
7 files changed
+92
-20
No files matched your search
+1
-1
@@ -2397,7 +2397,7 @@ CREATE TABLE `gen_table` (
|
||||
`package_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '项目包名',
|
||||
`version` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '项目版本号',
|
||||
`i18n` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '是否生成带有i18n 0 不带有 1带有',
|
||||
`style` bigint DEFAULT '1' COMMENT '代码风格',
|
||||
`style` bigint DEFAULT NULL COMMENT '代码风格',
|
||||
`child_table_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '子表名称',
|
||||
`main_field` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '主表关联键',
|
||||
`child_field` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '子表关联键',
|
||||
|
||||
@@ -151,7 +151,7 @@ CREATE TABLE `gen_table` (
|
||||
`package_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '项目包名',
|
||||
`version` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '项目版本号',
|
||||
`i18n` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '是否生成带有i18n 0 不带有 1带有',
|
||||
`style` bigint DEFAULT '1' COMMENT '代码风格',
|
||||
`style` bigint DEFAULT NULL COMMENT '代码风格',
|
||||
`child_table_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '子表名称',
|
||||
`main_field` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '主表关联键',
|
||||
`child_field` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '子表关联键',
|
||||
|
||||
+10
@@ -167,5 +167,15 @@ public class GenTemplateController {
|
||||
return genTemplateService.onlineUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查版本
|
||||
* @return {@link R }
|
||||
*/
|
||||
@Operation(summary = "在线检查模板", description = "在线检查模板")
|
||||
@GetMapping("/checkVersion")
|
||||
@PreAuthorize("@pms.hasPermission('codegen_template_view')")
|
||||
public R checkVersion() {
|
||||
return genTemplateService.checkVersion();
|
||||
}
|
||||
|
||||
}
|
||||
+6
@@ -29,6 +29,12 @@ import com.pig4cloud.pigx.common.core.util.R;
|
||||
*/
|
||||
public interface GenTemplateService extends IService<GenTemplateEntity> {
|
||||
|
||||
/**
|
||||
* 检查版本
|
||||
* @return {@link R }
|
||||
*/
|
||||
R checkVersion();
|
||||
|
||||
|
||||
/**
|
||||
* 在线更新
|
||||
|
||||
+58
-7
@@ -25,6 +25,7 @@ import cn.hutool.json.JSONUtil;
|
||||
import cn.smallbun.screw.core.constant.DefaultConstants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.pig4cloud.pigx.codegen.config.PigxCodeGenDefaultProperties;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenGroupEntity;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenTemplateEntity;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenTemplateGroupEntity;
|
||||
@@ -40,7 +41,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -55,6 +58,8 @@ import java.util.Set;
|
||||
public class GenTemplateServiceImpl extends ServiceImpl<GenTemplateMapper, GenTemplateEntity>
|
||||
implements GenTemplateService {
|
||||
|
||||
private final PigxCodeGenDefaultProperties defaultProperties;
|
||||
|
||||
private final GenTemplateGroupMapper genTemplateGroupMapper;
|
||||
|
||||
private final GenGroupMapper genGroupMapper;
|
||||
@@ -68,12 +73,10 @@ public class GenTemplateServiceImpl extends ServiceImpl<GenTemplateMapper, GenTe
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public R onlineUpdate() {
|
||||
// 1. 获取 config.json + version 文件
|
||||
String configFile = getCGTMFile("config.json");
|
||||
String versionFile = getCGTMFile("VERSION");
|
||||
|
||||
// 解析 config.json
|
||||
JSONObject configJsonObj = JSONUtil.parseObj(configFile);
|
||||
// 获取 config.json 和 version 文件
|
||||
Map<String, Object> configAndVersion = getConfigAndVersion();
|
||||
JSONObject configJsonObj = (JSONObject) configAndVersion.get("configJsonObj");
|
||||
String versionFile = (String) configAndVersion.get("versionFile");
|
||||
|
||||
// 查询出全部的模板组名称
|
||||
Set<String> cgtmConfigGroupNames = configJsonObj.keySet();
|
||||
@@ -90,10 +93,58 @@ public class GenTemplateServiceImpl extends ServiceImpl<GenTemplateMapper, GenTe
|
||||
// 插入新的模板组(名称 + VERSION), 再解析 config.json group 里面的所有模板
|
||||
insertTemplateFiles(versionFile, configJsonObj, cgtmConfigGroupName);
|
||||
}
|
||||
|
||||
return R.ok("更新成功,版本号:" + versionFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查版本
|
||||
*
|
||||
* @return {@link R }
|
||||
*/
|
||||
public R checkVersion() {
|
||||
// 关闭在线更新提示
|
||||
if (!defaultProperties.isAutoCheckVersion()) {
|
||||
return R.ok(true);
|
||||
}
|
||||
|
||||
// 获取 config.json 和 version 文件
|
||||
Map<String, Object> configAndVersion = getConfigAndVersion();
|
||||
JSONObject configJsonObj = (JSONObject) configAndVersion.get("configJsonObj");
|
||||
String versionFile = (String) configAndVersion.get("versionFile");
|
||||
|
||||
// 查询出全部的模板组名称
|
||||
boolean exists = false;
|
||||
Set<String> cgtmConfigGroupNames = configJsonObj.keySet();
|
||||
for (String cgtmConfigGroupName : cgtmConfigGroupNames) {
|
||||
exists = genGroupMapper.exists(Wrappers.<GenGroupEntity>lambdaQuery()
|
||||
.eq(GenGroupEntity::getGroupName, cgtmConfigGroupName + versionFile));
|
||||
}
|
||||
|
||||
return R.ok(exists);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置和版本
|
||||
*
|
||||
* @return {@link Map }<{@link String }, {@link Object }>
|
||||
*/
|
||||
private Map<String, Object> getConfigAndVersion() {
|
||||
// 获取 config.json 和 version 文件
|
||||
String configFile = getCGTMFile("config.json");
|
||||
String versionFile = getCGTMFile("VERSION");
|
||||
|
||||
// 解析 config.json
|
||||
JSONObject configJsonObj = JSONUtil.parseObj(configFile);
|
||||
|
||||
// 将 configJsonObj 和 versionFile 放入 Map 中
|
||||
Map<String, Object> configAndVersion = new HashMap<>();
|
||||
configAndVersion.put("configJsonObj", configJsonObj);
|
||||
configAndVersion.put("versionFile", versionFile);
|
||||
|
||||
return configAndVersion;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 插入模板文件
|
||||
*
|
||||
|
||||
+14
-9
@@ -25,6 +25,7 @@ import cn.hutool.core.text.NamingCase;
|
||||
import cn.hutool.core.util.BooleanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.pig4cloud.pigx.codegen.config.PigxCodeGenDefaultProperties;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenFormConf;
|
||||
import com.pig4cloud.pigx.codegen.entity.GenTable;
|
||||
@@ -34,6 +35,7 @@ import com.pig4cloud.pigx.codegen.service.*;
|
||||
import com.pig4cloud.pigx.codegen.util.GeneratorStyleEnum;
|
||||
import com.pig4cloud.pigx.codegen.util.VelocityKit;
|
||||
import com.pig4cloud.pigx.codegen.util.vo.GroupVO;
|
||||
import com.pig4cloud.pigx.common.core.exception.CheckedException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -188,16 +190,17 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
Map<String, Object> dataModel = getDataModel(genTable.getId());
|
||||
|
||||
// 获取模板信息,Lambda 表达式简化代码
|
||||
GenTemplateEntity genTemplateEntity = Optional
|
||||
.ofNullable(genTemplateService.getById(GeneratorStyleEnum.VFORM_JSON.getTemplateId()))
|
||||
.orElseThrow(() -> new Exception("模板不存在"));
|
||||
|
||||
GenTemplateEntity genTemplateEntity = genTemplateService.getOneOpt(Wrappers.<GenTemplateEntity>lambdaQuery()
|
||||
.likeRight(GenTemplateEntity::getTemplateName, GeneratorStyleEnum.VFORM_JSON.getDesc())
|
||||
.orderByDesc(GenTemplateEntity::getCreateTime)
|
||||
)
|
||||
.orElseThrow(() -> new CheckedException("模板不存在"));
|
||||
// 渲染模板并返回结果
|
||||
return VelocityKit.renderStr(genTemplateEntity.getTemplateCode(), dataModel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表单设计器需要的 JSON 方法
|
||||
* 获取sfc vue
|
||||
*
|
||||
* @param id 表单配置 ID
|
||||
* @return JSON 字符串
|
||||
@@ -220,10 +223,12 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
// 遍历 widgetList
|
||||
dataModel.put("resultMap", resultMap);
|
||||
|
||||
// 获取模板信息
|
||||
GenTemplateEntity genTemplateEntity = Optional
|
||||
.ofNullable(genTemplateService.getById(GeneratorStyleEnum.VFORM_FORM.getTemplateId()))
|
||||
.orElseThrow(() -> new Exception("模板不存在"));
|
||||
// 获取模板信息 查询模板中最新的 vform.json 文件
|
||||
GenTemplateEntity genTemplateEntity = genTemplateService.getOneOpt(Wrappers.<GenTemplateEntity>lambdaQuery()
|
||||
.likeRight(GenTemplateEntity::getTemplateName, GeneratorStyleEnum.VFORM_VUE.getDesc())
|
||||
.orderByDesc(GenTemplateEntity::getCreateTime)
|
||||
)
|
||||
.orElseThrow(() -> new CheckedException("模板不存在"));
|
||||
|
||||
// 渲染模板并返回结果
|
||||
return VelocityKit.renderStr(genTemplateEntity.getTemplateCode(), dataModel);
|
||||
|
||||
+2
-2
@@ -12,9 +12,9 @@ import lombok.Getter;
|
||||
@AllArgsConstructor
|
||||
public enum GeneratorStyleEnum {
|
||||
|
||||
VFORM_JSON(1L, "element-plus 风格"),
|
||||
VFORM_JSON(1L, "vform.json"),
|
||||
|
||||
VFORM_FORM(2L, "uview 风格");
|
||||
VFORM_VUE(2L, "vform.vue");
|
||||
|
||||
/**
|
||||
* 对应模板ID
|
||||
|
||||
Reference in new issue
Block a user