| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.io.*; |
| | | import java.nio.file.Files; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | |
| | | String pyName = (String) params.get("pyName"); |
| | | String pyType = (String) params.get("pyType"); |
| | | String remark = (String) params.get("remark"); |
| | | String label = (String) params.get("label"); |
| | | |
| | | QueryWrapper<MpkFileEntity> wrapper = new QueryWrapper<>(); |
| | | wrapper.like(StringUtils.isNotBlank(pyName), "py_name", pyName) |
| | | .eq(StringUtils.isNotBlank(pyType), "py_type", pyType) |
| | | .like(StringUtils.isNotBlank(remark), "remark", remark); |
| | | |
| | | if (StringUtils.isNotBlank(label)) { |
| | | wrapper.and(w -> { |
| | | w.eq(StringUtils.isNotBlank(label),"menu_name", label) |
| | | .or().eq(StringUtils.isNotBlank(label),"group_name", label); |
| | | }); |
| | | } |
| | | return wrapper; |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | @DSTransactional(rollbackFor = Exception.class) |
| | | public byte[] packageModel(List<String> ids, String projectId, String projectName, String zipFileName, String log, String version) throws IOException, InterruptedException { |
| | | public byte[] packageModel(List<String> ids, String projectId, String projectName, String zipFileName, String logs, String version) throws IOException, InterruptedException { |
| | | List<MpkFileDTO> entities = baseDao.selectByIds(ids); |
| | | |
| | | //模板数据 |
| | |
| | | File xmlFile = new File(dirPath.getAbsolutePath() + File.separator + "menu.xml"); |
| | | GenUtils.drawTemplate("menu.xml.vm", new VelocityContext(map1), xmlFile); |
| | | |
| | | //生成cpp文件 |
| | | File cppFile = new File(dirPath.getAbsolutePath() + File.separator + UUID.randomUUID() + ".cpp"); |
| | | GenUtils.drawTemplate("pkg.cpp.vm", context, cppFile); |
| | | // C++源文件 |
| | | FileUtil.copy("bak/C++", dirPath.getAbsolutePath(), true); |
| | | |
| | | //生成Jni.cpp文件 |
| | | File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + UUID.randomUUID() + "Jni.cpp"); |
| | | GenUtils.drawTemplate("pkg.Jni.cpp.vm", context, jniCppFile); |
| | | |
| | | //生成dll文件 |
| | | String dllSavePath = dirPath.getAbsolutePath() + File.separator + "IAIL.MDK.Mid.Windows.dll"; |
| | | String cppFilePath = cppFile.getAbsolutePath(); |
| | | String jniCppFilePath = jniCppFile.getAbsolutePath(); |
| | | // String command = "cmd.exe /c cl -o " + dllSavePath + " /LD " + jniCppFilePath + " " + cppFilePath; |
| | | String command = "cmd.exe /c cl -o " + dllSavePath + " /LD D:\\work\\mdk\\code\\makeDll\\src\\main\\java\\org\\example\\MakeDll.c D:\\work\\mdk\\code\\makeDll\\src\\main\\java\\org\\example\\MakeDll2.c"; |
| | | Process process = RuntimeUtil.exec(command); |
| | | // 等待命令执行完成 |
| | | process.waitFor(); |
| | | List<String> javaFilePaths = new ArrayList<>(); |
| | | List<String> cppFilePaths = new ArrayList<>(); |
| | | |
| | | //生成java文件 |
| | | for (MpkFileDTO entity : entities) { |
| | |
| | | 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"); |
| | | GenUtils.drawTemplate("h.vm", dataContext, hFile); |
| | | |
| | | //生成Jni.h文件 |
| | | File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h"); |
| | | GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile); |
| | | |
| | | // 添加python源文件 |
| | | String pyFilePath = dirPath.getAbsolutePath() + File.separator + "py" + File.separator + entity.getPyName() + ".pyd"; |
| | | FileUtil.mkParentDirs(pyFilePath); |
| | | FileUtil.copy(entity.getFilePath(), pyFilePath, true); |
| | | } |
| | | |
| | | //生成dll文件 |
| | | String dllSavePath = dirPath.getAbsolutePath() + File.separator + "IAIL.MDK.Mid.Windows.dll"; |
| | | createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); |
| | | |
| | | //utils + env java文件 |
| | | File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "utils" + File.separator + "AlgsUtils.java"); |
| | | FileUtil.mkParentDirs(utilsJavaFile); |
| | | FileUtil.copy("bak/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); |
| | | FileUtil.copy("bak/IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); |
| | | javaFilePaths.add(utilsJavaFile.getAbsolutePath()); |
| | | File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "common" + File.separator + "Environment.java"); |
| | | FileUtil.mkParentDirs(envJavaFile); |
| | | FileUtil.copy("bak/Environment.java", envJavaFile.getAbsolutePath(), true); |
| | | FileUtil.copy("bak/IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); |
| | | javaFilePaths.add(envJavaFile.getAbsolutePath()); |
| | | // 生成class文件 |
| | | createClassFile(javaFilePaths); |
| | |
| | | dto.setId(historyId); |
| | | dto.setProjectId(projectId); |
| | | dto.setFileName(zipFileName); |
| | | dto.setLog(log); |
| | | dto.setLog(logs); |
| | | dto.setVersion(version); |
| | | dto.setModelNames(entities.stream().map(MpkFileDTO::getPyName).collect(Collectors.joining(","))); |
| | | dto.setCreateTime(new Date()); |
| | |
| | | return bytes; |
| | | } |
| | | |
| | | private void createDllFile(String dirPath, List<String> cppFilePaths, String dllSavePath) throws InterruptedException, IOException { |
| | | cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "pch.cpp"); |
| | | cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "dllmain.cpp"); |
| | | cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "Environment.cpp"); |
| | | cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "pyutils.cpp"); |
| | | cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "convertutils.cpp"); |
| | | String paths = cppFilePaths.stream().collect(Collectors.joining(" ")); |
| | | String command = "cl /LD " + paths + " /EHsc /o " + dllSavePath; |
| | | // String command = "cmd.exe /c cl -o " + dllSavePath + " /LD D:\\work\\mdk\\code\\makeDll\\src\\main\\java\\org\\example\\MakeDll.c D:\\work\\mdk\\code\\makeDll\\src\\main\\java\\org\\example\\MakeDll2.c"; |
| | | log.info("执行cmd命令生成dll:" + command); |
| | | ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command); |
| | | builder.directory(new File(dirPath + File.separator + "C++")); |
| | | Process process = builder.start(); |
| | | // 获取命令输出结果 |
| | | InputStream inputStream = process.getInputStream(); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK |
| | | String line; |
| | | while ((line = reader.readLine()) != null) { |
| | | System.out.println(line); |
| | | } |
| | | // 等待命令执行完成 |
| | | process.waitFor(); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, String> savePyFile(MultipartFile file) throws IOException { |
| | | File dir = new File(mpkBakFilePath); |