Skip to content

Commit

Permalink
feat(generator): 源项目内生成代码文件 (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
luoqiz authored Jan 12, 2025
1 parent 146f5b2 commit 653802e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private void setPreviewPath(GeneratePreviewResp generatePreview,
}

@Override
public void generate(List<String> tableNames, HttpServletResponse response) {
public void downloadCode(List<String> tableNames, HttpServletResponse response) {
try {
String tempDir = SystemUtil.getUserInfo().getTempDir();
// 删除旧代码
Expand All @@ -274,6 +274,32 @@ public void generate(List<String> tableNames, HttpServletResponse response) {
}
}

@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("代码生成失败,请手动清理生成文件");
}
}

/**
* 生成预览
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,20 @@ public List<GeneratePreviewResp> preview(@PathVariable List<String> 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 = "生成代码")
@Parameter(name = "tableNames", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
@SaCheckPermission("code:generator:generate")
@PostMapping("/{tableNames}")
public void generate(@PathVariable List<String> tableNames, HttpServletResponse response) {
baseService.generate(tableNames, response);
public void generateCode(@PathVariable List<String> tableNames) {
baseService.generateCode(tableNames);
}

@Operation(summary = "查询字典", description = "查询字典列表")
Expand Down

0 comments on commit 653802e

Please sign in to comment.