mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 10:57:13 +08:00 
			
		
		
		
	feat(generator): 源项目内生成代码文件 (#125)
This commit is contained in:
		| @@ -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); | ||||
| } | ||||
|   | ||||
| @@ -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("代码生成失败,请手动清理生成文件"); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 生成预览 | ||||
|      * | ||||
|   | ||||
		Reference in New Issue
	
	Block a user