From 9112b473b6f079d70094c0fd8e3f4f2eb3b314ce Mon Sep 17 00:00:00 2001 From: luoyifan Date: Thu, 3 Jul 2025 13:18:12 +0800 Subject: [PATCH] =?UTF-8?q?Script=20=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yvan/workbench/controller/LccController.java | 20 ++++++++++ .../com/yvan/workbench/service/LccMapService.java | 46 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/servo/src/main/java/com/yvan/workbench/controller/LccController.java b/servo/src/main/java/com/yvan/workbench/controller/LccController.java index 703120b..9a9f8e8 100644 --- a/servo/src/main/java/com/yvan/workbench/controller/LccController.java +++ b/servo/src/main/java/com/yvan/workbench/controller/LccController.java @@ -11,6 +11,7 @@ import org.clever.data.jdbc.QueryDSL; import org.clever.data.jdbc.querydsl.utils.QueryDslUtils; import org.clever.web.mvc.annotation.RequestBody; +import java.util.List; import java.util.Map; import static com.galaxis.rcs.common.query.QLccBasContainer.lccBasContainer; @@ -126,4 +127,23 @@ public class LccController { return R.success(list); } + + public static R saveAndSyncScripts(@RequestBody Map params) { + String projectUuid = Conv.asString(params.get("projectUUID")); + Long envId = Conv.asLong(params.get("envId")); + List> scriptList = (List>) (params.get("scriptList")); + + if (Strings.isNullOrEmpty(projectUuid)) { + return R.fail("projectUUID Must not be empty"); + } + if (envId == null || envId < 0) { + return R.fail("envId Must not be empty"); + } + + // 保存脚本内容到文件系统 + var mapService = SpringContext.HOLDER.getBean(LccMapService.class); + var list = mapService.saveAndSyncScripts(projectUuid, scriptList); + + return R.success(list); + } } diff --git a/servo/src/main/java/com/yvan/workbench/service/LccMapService.java b/servo/src/main/java/com/yvan/workbench/service/LccMapService.java index af43734..554c62b 100644 --- a/servo/src/main/java/com/yvan/workbench/service/LccMapService.java +++ b/servo/src/main/java/com/yvan/workbench/service/LccMapService.java @@ -11,11 +11,13 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; +import org.clever.core.Conv; import org.clever.core.mapper.JacksonMapper; import java.io.File; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.Map; import java.util.Objects; @Slf4j @@ -266,4 +268,48 @@ public class LccMapService { } + public List> saveAndSyncScripts(String projectUuid, List> scriptList) { + var mapLoc = new File(this.config.getLocation()); + + // 扫描 mapLoc 目录下所有的文件夹,并且文件夹中包含有 project.json 文件存在,就讲她反序列化为 LccProject 对象 + if (!mapLoc.exists() || !mapLoc.isDirectory()) { + throw new RuntimeException( + String.format("LccMapService.getAllProjects() - mapLoc '%s' does not exist or is not a directory", mapLoc.getAbsolutePath()) + ); + } + + var scriptsDir = new File(mapLoc, projectUuid + "/scripts"); + if (!scriptsDir.exists()) { + scriptsDir.mkdirs(); + } + + // 保存脚本内容到文件系统 + for (Map script : scriptList) { + String name = Conv.asString(script.get("name")); + String content = Conv.asString(script.get("content")); + + if (Strings.isNullOrEmpty(name) || Strings.isNullOrEmpty(content)) { + continue; // 跳过无效的脚本 + } + + // 检查脚本名称是否符合规范 + if (!name.matches("^[a-zA-Z0-9_\\-]+$")) { + throw new RuntimeException("Script name '" + name + "' is not valid. It must be alphanumeric, underscores or hyphens and end with .js"); + } + + File scriptFile = new File(scriptsDir, name + ".ts"); + LccUtils.saveFile(scriptFile, content, "script"); + } + + // 读取脚本文件夹内所有文件,返回给前端 + List> scriptListNew = Lists.newArrayList(); + for (File scriptFile : Objects.requireNonNull(scriptsDir.listFiles())) { + if (scriptFile.isFile() && scriptFile.getName().endsWith(".ts")) { + String scriptName = FilenameUtils.getBaseName(scriptFile.getName()); + String scriptContent = LccUtils.readFile(scriptFile, "script"); + scriptListNew.add(Map.of("name", scriptName, "content", scriptContent)); + } + } + return scriptListNew; + } }