feat(generator): 源项目内生成代码文件 (#125)

This commit is contained in:
luoqiz
2025-01-12 22:32:14 +08:00
committed by GitHub
parent 146f5b2169
commit 653802efbe
3 changed files with 46 additions and 5 deletions

View File

@@ -80,10 +80,17 @@ public interface GeneratorService {
List<GeneratePreviewResp> preview(List<String> tableNames);
/**
* 生成代码
* 生成下载代码
*
* @param tableNames 表名称列表
* @param response 响应对象
*/
void generate(List<String> tableNames, HttpServletResponse response);
void downloadCode(List<String> tableNames, HttpServletResponse response);
/**
* 生成下载代码
*
* @param tableNames 表名称列表
*/
void generateCode(List<String> tableNames);
}

View File

@@ -252,7 +252,7 @@ public class GeneratorServiceImpl implements GeneratorService {
}
@Override
public void generate(List<String> tableNames, HttpServletResponse response) {
public void downloadCode(List<String> tableNames, HttpServletResponse response) {
try {
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("代码生成失败,请手动清理生成文件");
}
}
/**
* 生成预览
*