Skip to content

Commit

Permalink
feat(generator): 新增代码批量生成功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoofff authored and Charles7c committed Mar 23, 2024
1 parent c05584b commit 040f137
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public class TableResp implements Serializable {
*/
@Schema(description = "是否已配置", example = "true")
private Boolean isConfiged;

/**
* 是否禁用
*/
@Schema(description = "是否禁用", example = "true")
private Boolean disabled;

public Boolean getDisabled() {
return !isConfiged;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public interface GeneratorService {
/**
* 生成代码
*
* @param tableName 表名称
* @param request 请求对象
* @param response 响应对象
* @param tableNames 表明层
* @param request 请求对象
* @param response 响应对象
*/
void generate(String tableName, HttpServletRequest request, HttpServletResponse response);
void generate(List<String> tableNames, HttpServletRequest request, HttpServletResponse response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,50 +260,57 @@ public List<GeneratePreviewResp> preview(String tableName) {
}

@Override
public void generate(String tableName, HttpServletRequest request, HttpServletResponse response) {
public void generate(List<String> tableNames, HttpServletRequest request, HttpServletResponse response) {
try {
// 初始化配置及数据
List<GeneratePreviewResp> generatePreviewList = this.preview(tableName);
GenConfigDO genConfig = genConfigMapper.selectById(tableName);
// 生成后端代码
Map<Boolean, List<GeneratePreviewResp>> generatePreviewListMap = generatePreviewList.stream()
.collect(Collectors.groupingBy(GeneratePreviewResp::isBackend));
this.generateBackendCode(generatePreviewListMap.get(true), genConfig);
// 生成前端代码
List<GeneratePreviewResp> frontendGeneratePreviewList = generatePreviewListMap.get(false);
String packageName = genConfig.getPackageName();
String moduleName = StrUtil.subSuf(packageName, StrUtil
.lastIndexOfIgnoreCase(packageName, StringConstants.DOT) + 1);
String tempDir = SystemUtil.getUserInfo().getTempDir();
// 例如:continew-admin-ui/src
String frontendBasicPackagePath = tempDir + String.join(File.separator, projectProperties
.getAppName(), projectProperties.getAppName() + "-ui", "src");
// 1、生成 api 代码
GeneratePreviewResp apiGeneratePreview = frontendGeneratePreviewList.get(0);
// 例如:continew-admin-ui/src/src/api/system
String apiPath = String.join(File.separator, frontendBasicPackagePath, "api", moduleName);
// 例如:continew-admin-ui/src/api/system/user.ts
File apiFile = new File(apiPath, apiGeneratePreview.getFileName());
if (!apiFile.exists() || Boolean.TRUE.equals(genConfig.getIsOverride())) {
FileUtil.writeUtf8String(apiGeneratePreview.getContent(), apiFile);
}
// 2、生成 view 代码
GeneratePreviewResp viewGeneratePreview = frontendGeneratePreviewList.get(1);
// 例如:continew-admin-ui/src/views/system
String vuePath = String.join(File.separator, frontendBasicPackagePath, "views", moduleName, StrUtil
.lowerFirst(genConfig.getClassNamePrefix()));
// 例如:continew-admin-ui/src/views/system/user/index.vue
File vueFile = new File(vuePath, viewGeneratePreview.getFileName());
if (!vueFile.exists() || Boolean.TRUE.equals(genConfig.getIsOverride())) {
FileUtil.writeUtf8String(viewGeneratePreview.getContent(), vueFile);
}
// 删除旧代码
FileUtil.del(tempDir + projectProperties.getAppName());

tableNames.forEach(tableName -> {
// 初始化配置及数据
List<GeneratePreviewResp> generatePreviewList = this.preview(tableName);
GenConfigDO genConfig = genConfigMapper.selectById(tableName);
// 生成后端代码
Map<Boolean, List<GeneratePreviewResp>> generatePreviewListMap = generatePreviewList.stream()
.collect(Collectors.groupingBy(GeneratePreviewResp::isBackend));
this.generateBackendCode(generatePreviewListMap.get(true), genConfig);
// 生成前端代码
List<GeneratePreviewResp> frontendGeneratePreviewList = generatePreviewListMap.get(false);
String packageName = genConfig.getPackageName();
String moduleName = StrUtil.subSuf(packageName, StrUtil
.lastIndexOfIgnoreCase(packageName, StringConstants.DOT) + 1);

// 例如:continew-admin-ui/src
String frontendBasicPackagePath = tempDir + String.join(File.separator, projectProperties
.getAppName(), projectProperties.getAppName() + "-ui", "src");
// 1、生成 api 代码
GeneratePreviewResp apiGeneratePreview = frontendGeneratePreviewList.get(0);
// 例如:continew-admin-ui/src/src/api/system
String apiPath = String.join(File.separator, frontendBasicPackagePath, "api", moduleName);
// 例如:continew-admin-ui/src/api/system/user.ts
File apiFile = new File(apiPath, apiGeneratePreview.getFileName());
if (!apiFile.exists() || Boolean.TRUE.equals(genConfig.getIsOverride())) {
FileUtil.writeUtf8String(apiGeneratePreview.getContent(), apiFile);
}
// 2、生成 view 代码
GeneratePreviewResp viewGeneratePreview = frontendGeneratePreviewList.get(1);
// 例如:continew-admin-ui/src/views/system
String vuePath = String.join(File.separator, frontendBasicPackagePath, "views", moduleName, StrUtil
.lowerFirst(genConfig.getClassNamePrefix()));
// 例如:continew-admin-ui/src/views/system/user/index.vue
File vueFile = new File(vuePath, viewGeneratePreview.getFileName());
if (!vueFile.exists() || Boolean.TRUE.equals(genConfig.getIsOverride())) {
FileUtil.writeUtf8String(viewGeneratePreview.getContent(), vueFile);
}
});

// 打包下载
File tempDirFile = new File(tempDir, projectProperties.getAppName());
String zipFilePath = tempDirFile.getPath() + ".zip";
String zipFilePath = tempDirFile.getPath() + jodd.io.ZipUtil.ZIP_EXT;
ZipUtil.zip(tempDirFile.getPath(), zipFilePath);
FileUploadUtils.download(request, response, new File(zipFilePath), true);
} catch (Exception e) {
log.error("Generate code of table '{}' occurred an error. {}", tableName, e.getMessage(), 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 @@ -102,8 +102,10 @@ public R<List<GeneratePreviewResp>> preview(@PathVariable String tableName) {
@Operation(summary = "生成代码", description = "生成代码")
@Parameter(name = "tableName", description = "表名称", required = true, example = "sys_user", in = ParameterIn.PATH)
@SaCheckPermission("tool:generator:list")
@PostMapping("/{tableName}")
public void generate(@PathVariable String tableName, HttpServletRequest request, HttpServletResponse response) {
generatorService.generate(tableName, request, response);
@PostMapping("/{tableNames}")
public void generate(@PathVariable List<String> tableNames,
HttpServletRequest request,
HttpServletResponse response) {
generatorService.generate(tableNames, request, response);
}
}

0 comments on commit 040f137

Please sign in to comment.