From 67e4c9d6ae3abc01fff031b8231f155a3a6c9efa Mon Sep 17 00:00:00 2001 From: fanshuai <1141904845@qq.com> Date: Fri, 20 Sep 2024 14:43:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=8Advc=20pull?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ModelsServiceImpl.java | 5 +++++ .../service/impl/NewDatasetServiceImpl.java | 7 +++++-- .../com/ruoyi/platform/utils/DVCUtils.java | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java index 697ec04f..ec1342ab 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java @@ -599,6 +599,8 @@ public class ModelsServiceImpl implements ModelsService { String relatePath = ci4sUsername + "/model/" + gitlinIid + "/" + repositoryName+ "/model"; dvcUtils.gitClone(rootPath, projectUrl, branchName, gitLinkUsername, gitLinkPassword); + //干掉目标文件夹 + dvcUtils.deleteDirectory(modelPath); dvcUtils.moveFiles(sourcePath, modelPath); //拼接生产的元数据后写入yaml文件 @@ -701,7 +703,10 @@ public class ModelsServiceImpl implements ModelsService { // 创建本地分支 dvcUtils.createLocalBranchBasedOnMaster(rootPath, branchName); //dvc checkout + dvcUtils.dvcPull(rootPath); dvcUtils.dvcCheckout(rootPath); + //干掉目标文件夹 + dvcUtils.deleteDirectory(modelPath); dvcUtils.moveFiles(sourcePath, modelPath); //拼接生产的元数据后写入yaml文件 diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java index 48b0033d..4357f395 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/NewDatasetServiceImpl.java @@ -117,7 +117,8 @@ public class NewDatasetServiceImpl implements NewDatasetService { // 命令行操作 git clone 项目地址 dvcUtils.gitClone(localPath, projectUrl, branchName, gitLinkUsername, gitLinkPassword); String s3Path = "management-platform-files" + "/" + relatePath + "/" + branchName; - + //干掉目标文件夹 + dvcUtils.deleteDirectory(datasetPath); dvcUtils.moveFiles(sourcePath, datasetPath); // 拼接生产的元数据后写入yaml文件 datasetVo.setCreateBy(String.valueOf(StringUtils.isNotEmpty((String) userInfo.get("nickname")) ? userInfo.get("nickname") : userInfo.get("login"))); @@ -188,11 +189,13 @@ public class NewDatasetServiceImpl implements NewDatasetService { dvcUtils.createLocalBranchBasedOnMaster(localPath, branchName); //dvc checkout + dvcUtils.dvcPull(localPath); dvcUtils.dvcCheckout(localPath); // 准备数据 String s3Path = "management-platform-files" + "/" + relatePath + "/" + branchName; - + //干掉目标文件夹 + dvcUtils.deleteDirectory(datasetPath); dvcUtils.moveFiles(sourcePath, datasetPath); // 拼接生产的元数据后写入yaml文件 datasetVo.setCreateBy(String.valueOf(StringUtils.isNotEmpty((String) userInfo.get("nickname")) ? userInfo.get("nickname") : userInfo.get("login"))); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DVCUtils.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DVCUtils.java index 795a2b0a..52cfa450 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DVCUtils.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DVCUtils.java @@ -78,6 +78,26 @@ public class DVCUtils { Files.move(sourceDir, targetDir, StandardCopyOption.REPLACE_EXISTING); } + public void deleteDirectory(String dirPath) throws IOException { + Path directory = Paths.get(dirPath); + + // 检查是否目录存在 + if (Files.exists(directory)) { + // 使用Files.walk来删除目录及其内容 + Files.walk(directory) + .sorted((path1, path2) -> path2.compareTo(path1)) // 先删除子文件夹,再删除父文件夹 + .forEach(this::deletePath); + } + } + + private void deletePath(Path path) { + try { + Files.delete(path); + } catch (IOException e) { + log.error("Unable to delete: " + path + " " + e.getMessage()); + } + } + public void gitClone(String localPath, String repoUrl, String branch, String username, String password) throws GitAPIException { CloneCommand cloneCommand = Git.cloneRepository() .setURI(repoUrl) @@ -419,6 +439,7 @@ public class DVCUtils { // 切换分支 gitCheckoutBranch(localPath, branch); + dvcPull(localPath); dvcCheckout(localPath); }