|
|
@ -11,11 +11,13 @@ import lombok.SneakyThrows; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.apache.commons.io.FileUtils; |
|
|
import org.apache.commons.io.FileUtils; |
|
|
import org.apache.commons.io.FilenameUtils; |
|
|
import org.apache.commons.io.FilenameUtils; |
|
|
|
|
|
import org.clever.core.Conv; |
|
|
import org.clever.core.mapper.JacksonMapper; |
|
|
import org.clever.core.mapper.JacksonMapper; |
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
import java.io.File; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Map; |
|
|
import java.util.Objects; |
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@ -266,4 +268,48 @@ public class LccMapService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Map<String, String>> saveAndSyncScripts(String projectUuid, List<Map<String, Object>> 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<String, Object> 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<Map<String, String>> 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; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|