From 7324229be6b961411038fab3c680929ae05b0496 Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期四, 12 十二月 2024 16:30:58 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 iailab-module-model/iailab-module-model-biz/db/mysql.sql                                                                   |    2 
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/MdkConstant.java              |    6 +
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java |   95 +++++++++++--------------------
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/CmdUtils.java           |   64 +++++++++++++++++++++
 4 files changed, 105 insertions(+), 62 deletions(-)

diff --git a/iailab-module-model/iailab-module-model-biz/db/mysql.sql b/iailab-module-model/iailab-module-model-biz/db/mysql.sql
index 6f8e5dd..1381cea 100644
--- a/iailab-module-model/iailab-module-model-biz/db/mysql.sql
+++ b/iailab-module-model/iailab-module-model-biz/db/mysql.sql
@@ -709,7 +709,7 @@
     `file_path`   varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '文件路径',
     `version`     varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '版本号',
     `log`         varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新日志',
-    `model_names` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '打包模型名称(“,”分割)',
+    `model_names` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '打包模型名称(“,”分割)',
     `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
     PRIMARY KEY (`id`, `project_id`) USING BTREE,
     INDEX         `del_package_history`(`project_id` ASC) USING BTREE,
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/MdkConstant.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/MdkConstant.java
index ca7d23d..ede977c 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/MdkConstant.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/MdkConstant.java
@@ -15,10 +15,14 @@
      */
     String IMPL = "impl";
     /**
-     * impl
+     * C++
      */
     String C = "C++";
     /**
+     * IAILMDK
+     */
+    String IAILMDK = "IAILMDK";
+    /**
      * Algs
      */
     String ALGS = "Algs";
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/CmdUtils.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/CmdUtils.java
new file mode 100644
index 0000000..3cfb09b
--- /dev/null
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/CmdUtils.java
@@ -0,0 +1,64 @@
+package com.iailab.module.model.mpk.common.utils;
+
+import com.iailab.module.model.mpk.common.MdkConstant;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+@Slf4j
+public class CmdUtils {
+
+    public static void exec(String cmd, String directory) {
+        Process process = null;
+        BufferedReader normalReader = null;
+        BufferedReader errorReader = null;
+        try {
+            log.info("执行cmd命令:" + cmd);
+            ProcessBuilder builder = new ProcessBuilder("cmd", "/c", cmd);
+            if (StringUtils.isNotBlank(directory)) {
+                builder.directory(new File(directory));
+            }
+            process = builder.start();
+            normalReader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
+            errorReader = new  BufferedReader(new  InputStreamReader(process.getErrorStream(),"GBK"));
+
+            String line = null;
+            while ((line = normalReader.readLine()) != null) {
+                log.info("脚本文件执行信息normalStream:{}", line);
+            }
+            String errorLine;
+            while ((errorLine = errorReader.readLine()) != null) {
+                log.warn("脚本文件执行信息ErrorStream:{}", errorLine);
+            }
+            process.waitFor();
+        } catch (Exception e) {
+            throw new RuntimeException("执行cmd命令异常,【" + cmd + "】",e);
+        } finally {
+            if (null != normalReader) {
+                try {
+                    normalReader.close();
+                } catch (IOException e) {
+                    log.error("流文件关闭异常:", e);
+                }
+            }
+            if (null != errorReader) {
+                try {
+                    errorReader.close();
+                } catch (IOException e) {
+                    log.error("流文件关闭异常:", e);
+                }
+            }
+            if (null != process) {
+                process.destroy();
+            }
+        }
+    }
+
+    public static void exec(String cmd) {
+        exec(cmd,null);
+    }
+}
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java
index ba86a21..51e677b 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java
@@ -1,7 +1,6 @@
 package com.iailab.module.model.mpk.service.impl;
 
 import cn.hutool.core.io.FileUtil;
-import cn.hutool.core.util.RuntimeUtil;
 import cn.hutool.core.util.ZipUtil;
 import com.alibaba.fastjson.JSON;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
@@ -16,6 +15,7 @@
 import com.iailab.framework.tenant.core.context.TenantContextHolder;
 import com.iailab.module.infra.api.config.ConfigApi;
 import com.iailab.module.model.mpk.common.MdkConstant;
+import com.iailab.module.model.mpk.common.utils.CmdUtils;
 import com.iailab.module.model.mpk.common.utils.DllUtils;
 import com.iailab.module.model.mpk.common.utils.GenUtils;
 import com.iailab.module.model.mpk.dao.MpkFileDao;
@@ -39,10 +39,12 @@
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
 import java.net.URLClassLoader;
 import java.nio.file.Files;
 import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -272,27 +274,24 @@
         }
 
         List<String> javaFilePaths = new ArrayList<>();
-        List<String> cppFilePaths = new ArrayList<>();
 
         //生成java文件
-        File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java");
+        File javaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java");
         GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile);
         javaFilePaths.add(javaFile.getAbsolutePath());
 
         //生成Impl.java文件
-        File implJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + entity.getPyName() + "Impl.java");
+        File implJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + entity.getPyName() + "Impl.java");
         GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile);
         javaFilePaths.add(implJavaFile.getAbsolutePath());
 
         //生成.cpp文件
         File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp");
         GenUtils.drawTemplate("cpp.vm", dataContext, cppFile);
-        cppFilePaths.add(cppFile.getAbsolutePath());
 
         //生成Jni.cpp文件
         File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp");
         GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile);
-        cppFilePaths.add(jniCppFile.getAbsolutePath());
 
         //生成.h文件
         File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h");
@@ -312,23 +311,23 @@
 
         //生成dll文件
         String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll";
-        createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath);
+        createDllFile(dirPath.getAbsolutePath(),dllSavePath);
         //备份dll文件,用于后续运行
         String dllBakPath = mpkTenantBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll";
         FileUtil.mkParentDirs(dllBakPath);
         FileUtil.copy(dllSavePath, dllBakPath, true);
 
         //utils + env java文件
-        File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java");
+        File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java");
         FileUtil.mkParentDirs(utilsJavaFile);
         FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true);
         javaFilePaths.add(utilsJavaFile.getAbsolutePath());
-        File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "common" + File.separator + "Environment.java");
+        File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "common" + File.separator + "Environment.java");
         FileUtil.mkParentDirs(envJavaFile);
         FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true);
         javaFilePaths.add(envJavaFile.getAbsolutePath());
         // 生成class文件
-        createClassFile(javaFilePaths);
+        createClassFile(dirPath.getAbsolutePath(),javaFilePaths);
         // 删除java源文件
         deleteJavaFile(javaFilePaths);
         // 打jar包
@@ -397,7 +396,6 @@
         GenUtils.drawTemplate("menu.xml.vm", new VelocityContext(map1), xmlFile);
 
         List<String> javaFilePaths = new ArrayList<>();
-        List<String> cppFilePaths = new ArrayList<>();
 
         //生成java文件
         for (MpkFileDTO entity : entities) {
@@ -409,24 +407,22 @@
             data.put("pyModule", entity.getPyModule());
             VelocityContext dataContext = new VelocityContext(data);
             //生成java文件
-            File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java");
+            File javaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java");
             GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile);
             javaFilePaths.add(javaFile.getAbsolutePath());
 
             //生成Impl.java文件
-            File implJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + entity.getPyName() + "Impl.java");
+            File implJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + entity.getPyName() + "Impl.java");
             GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile);
             javaFilePaths.add(implJavaFile.getAbsolutePath());
 
             //生成.cpp文件
             File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp");
             GenUtils.drawTemplate("cpp.vm", dataContext, cppFile);
-            cppFilePaths.add(cppFile.getAbsolutePath());
 
             //生成Jni.cpp文件
             File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp");
             GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile);
-            cppFilePaths.add(jniCppFile.getAbsolutePath());
 
             //生成.h文件
             File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h");
@@ -447,7 +443,7 @@
 
         //生成dll文件
         String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll";
-        createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath);
+        createDllFile(dirPath.getAbsolutePath(),dllSavePath);
         // 打包历史id
         String historyId = UUID.randomUUID().toString();
         //备份dll,发布时使用
@@ -460,16 +456,16 @@
         }
 
         //utils + env java文件
-        File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java");
+        File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java");
         FileUtil.mkParentDirs(utilsJavaFile);
         FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true);
         javaFilePaths.add(utilsJavaFile.getAbsolutePath());
-        File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "common" + File.separator + "Environment.java");
+        File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "common" + File.separator + "Environment.java");
         FileUtil.mkParentDirs(envJavaFile);
         FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true);
         javaFilePaths.add(envJavaFile.getAbsolutePath());
         // 生成class文件
-        createClassFile(javaFilePaths);
+        createClassFile(dirPath.getAbsolutePath(),javaFilePaths);
         // 删除java源文件
         deleteJavaFile(javaFilePaths);
         // 打jar包
@@ -525,23 +521,10 @@
         return bytes;
     }
 
-    private void createDllFile(String dirPath, List<String> cppFilePaths, String dllSavePath) {
+    private void createDllFile(String dirPath, String dllSavePath) {
         try {
-            String paths = cppFilePaths.stream().collect(Collectors.joining(" "));
-            String command = "cl /LD " + paths + " /EHsc /o " + dllSavePath + " /link " + "IAIL.MDK.Mid.Windows.lib";
-            log.info("执行cmd命令生成dll:" + command);
-            ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command);
-            builder.directory(new File(dirPath + File.separator + MdkConstant.LIBS));
-            Process process = builder.start();
-            // 获取命令输出结果
-            InputStream inputStream = process.getInputStream();
-            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK
-            String line;
-            while ((line = reader.readLine()) != null) {
-                log.info(line);
-            }
-            // 等待命令执行完成
-            process.waitFor();
+            String command = "cl /LD *.cpp /EHsc /o " + dllSavePath + " /link " + "IAIL.MDK.Mid.Windows.lib";
+            CmdUtils.exec(command,dirPath + File.separator + MdkConstant.C);
         } catch (Exception e) {
             throw new RuntimeException("执行cmd命令生成dll异常",e);
         }
@@ -607,19 +590,7 @@
         try {
             String command = "python setup.py build_ext --inplace";
             log.info("执行cmd命令编译pyd:" + command);
-            ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command);
-            builder.directory(new File(dir));  // 设置工作目录
-            builder.redirectErrorStream(true);  // 将错误输出和标准输出合并
-            Process process = builder.start();
-            // 获取命令输出结果
-            InputStream inputStream = process.getInputStream();
-            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK
-            String line;
-            while ((line = reader.readLine()) != null) {
-                log.info(line);
-            }
-            // 等待命令执行完成
-            process.waitFor();
+            CmdUtils.exec(command,dir);
         } catch (Exception e) {
             throw new RuntimeException("执行cmd命令生成dll异常", e);
         }
@@ -711,12 +682,10 @@
             StringBuilder sb = new StringBuilder();
             sb.append("jar -cvf");
             sb.append(" ").append(jarSavePath);
-            sb.append(" -C ").append(dirPath).append(File.separator).append("IAILMDK").append(File.separator).append(" .");
-            log.info("执行cmd命令打jar包:" + sb);
-            Process process = RuntimeUtil.exec(sb.toString());
-            process.waitFor();
+            sb.append(" -C ").append(dirPath).append(File.separator).append(MdkConstant.IAILMDK).append(File.separator).append(" .");
+            CmdUtils.exec(sb.toString());
             return jarSavePath;
-        } catch (InterruptedException e) {
+        } catch (Exception e) {
             throw new RuntimeException("执行cmd命令打jar包异常",e);
         }
     }
@@ -727,17 +696,23 @@
         }
     }
 
-    private void createClassFile(List<String> javaFilePaths){
+    private void createClassFile(String dirPath,List<String> javaFilePaths){
         try {
+            Set<String> pathSet = javaFilePaths.stream().map(path -> {
+                // 替换为相对路径
+                path = path.replace(dirPath + File.separator + MdkConstant.IAILMDK, ".");
+                // 删除文件名,替换为通配符*
+                path = path.substring(0, path.lastIndexOf("\\") + 1) + "*.java";
+                return path;
+            }).collect(Collectors.toSet());
+
             StringBuilder sb = new StringBuilder();
             sb.append("javac -encoding utf-8");
-            for (String path : javaFilePaths) {
+            for (String path : pathSet) {
                 sb.append(" ").append(path);
             }
-            log.info("执行cmd命令生成class:" + sb);
-            Process process = RuntimeUtil.exec(sb.toString());
-            process.waitFor();
-        } catch (InterruptedException e) {
+            CmdUtils.exec(sb.toString(),dirPath + File.separator + MdkConstant.IAILMDK);
+        } catch (Exception e) {
             throw new RuntimeException("执行cmd命令生成class异常",e);
         }
     }

--
Gitblit v1.9.3