mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-10-26 20:57:11 +08:00
feat(generator): 源项目内生成代码文件 (#125)
This commit is contained in:
@@ -80,10 +80,17 @@ public interface GeneratorService {
|
|||||||
List<GeneratePreviewResp> preview(List<String> tableNames);
|
List<GeneratePreviewResp> preview(List<String> tableNames);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成代码
|
* 生成下载代码
|
||||||
*
|
*
|
||||||
* @param tableNames 表名称列表
|
* @param tableNames 表名称列表
|
||||||
* @param response 响应对象
|
* @param response 响应对象
|
||||||
*/
|
*/
|
||||||
void generate(List<String> tableNames, HttpServletResponse response);
|
void downloadCode(List<String> tableNames, HttpServletResponse response);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成下载代码
|
||||||
|
*
|
||||||
|
* @param tableNames 表名称列表
|
||||||
|
*/
|
||||||
|
void generateCode(List<String> tableNames);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generate(List<String> tableNames, HttpServletResponse response) {
|
public void downloadCode(List<String> tableNames, HttpServletResponse response) {
|
||||||
try {
|
try {
|
||||||
String tempDir = SystemUtil.getUserInfo().getTempDir();
|
String tempDir = SystemUtil.getUserInfo().getTempDir();
|
||||||
// 删除旧代码
|
// 删除旧代码
|
||||||
@@ -274,6 +274,32 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void generateCode(List<String> tableNames) {
|
||||||
|
try {
|
||||||
|
String projectPath = System.getProperty("user.dir");
|
||||||
|
tableNames.forEach(tableName -> {
|
||||||
|
// 初始化配置及数据
|
||||||
|
List<GeneratePreviewResp> generatePreviewList = this.preview(tableName);
|
||||||
|
// 生成代码
|
||||||
|
for (GeneratePreviewResp generatePreview : generatePreviewList) {
|
||||||
|
// 后端:continew-admin/continew-system/src/main/java/top/continew/admin/system/service/impl/XxxServiceImpl.java
|
||||||
|
// 前端:continew-admin/continew-admin-ui/src/views/system/user/index.vue
|
||||||
|
File file = new File(projectPath + generatePreview.getPath()
|
||||||
|
.replace("continew-admin\\continew-admin", ""), generatePreview.getFileName());
|
||||||
|
// 如果已经存在,且不允许覆盖,则跳过
|
||||||
|
if (!file.exists() || Boolean.TRUE.equals(genConfigMapper.selectById(tableName).getIsOverride())) {
|
||||||
|
FileUtil.writeUtf8String(generatePreview.getContent(), file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Generate code of table '{}' occurred an error. {}", tableNames, e.getMessage(), e);
|
||||||
|
throw new BusinessException("代码生成失败,请手动清理生成文件");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成预览
|
* 生成预览
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -96,12 +96,20 @@ public class GeneratorController {
|
|||||||
return baseService.preview(tableNames);
|
return baseService.preview(tableNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "生成下载代码", description = "生成下载代码")
|
||||||
|
@Parameter(name = "tableNames", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
|
||||||
|
@SaCheckPermission("code:generator:generate")
|
||||||
|
@PostMapping("/{tableNames}/download")
|
||||||
|
public void downloadCode(@PathVariable List<String> tableNames, HttpServletResponse response) {
|
||||||
|
baseService.downloadCode(tableNames, response);
|
||||||
|
}
|
||||||
|
|
||||||
@Operation(summary = "生成代码", description = "生成代码")
|
@Operation(summary = "生成代码", description = "生成代码")
|
||||||
@Parameter(name = "tableNames", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
|
@Parameter(name = "tableNames", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
|
||||||
@SaCheckPermission("code:generator:generate")
|
@SaCheckPermission("code:generator:generate")
|
||||||
@PostMapping("/{tableNames}")
|
@PostMapping("/{tableNames}")
|
||||||
public void generate(@PathVariable List<String> tableNames, HttpServletResponse response) {
|
public void generateCode(@PathVariable List<String> tableNames) {
|
||||||
baseService.generate(tableNames, response);
|
baseService.generateCode(tableNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "查询字典", description = "查询字典列表")
|
@Operation(summary = "查询字典", description = "查询字典列表")
|
||||||
|
|||||||
Reference in New Issue
Block a user