提交 | 用户 | 时间
|
449017
|
1 |
package com.iailab.module.model.mpk.service.impl; |
D |
2 |
|
|
3 |
import cn.hutool.core.io.FileUtil; |
|
4 |
import cn.hutool.core.util.RuntimeUtil; |
|
5 |
import cn.hutool.core.util.ZipUtil; |
|
6 |
import com.alibaba.fastjson.JSON; |
8b3ee3
|
7 |
import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
449017
|
8 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
D |
9 |
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
10 |
import com.iailab.framework.common.page.PageData; |
|
11 |
import com.iailab.framework.common.service.impl.BaseServiceImpl; |
|
12 |
import com.iailab.framework.common.util.date.DateUtils; |
|
13 |
import com.iailab.framework.common.util.object.ConvertUtils; |
|
14 |
import com.iailab.framework.security.core.util.SecurityFrameworkUtils; |
|
15 |
import com.iailab.module.infra.api.config.ConfigApi; |
|
16 |
import com.iailab.module.model.mpk.common.MdkConstant; |
|
17 |
import com.iailab.module.model.mpk.common.utils.GenUtils; |
|
18 |
import com.iailab.module.model.mpk.dao.MpkFileDao; |
1fea3e
|
19 |
import com.iailab.module.model.mpk.dto.GeneratorCodeHistoryDTO; |
D |
20 |
import com.iailab.module.model.mpk.dto.ModelMethodDTO; |
|
21 |
import com.iailab.module.model.mpk.dto.MpkFileDTO; |
|
22 |
import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO; |
|
23 |
import com.iailab.module.model.mpk.entity.GeneratorCodeHistoryEntity; |
|
24 |
import com.iailab.module.model.mpk.entity.MpkFileEntity; |
|
25 |
import com.iailab.module.model.mpk.entity.ProjectPackageHistoryModelEntity; |
449017
|
26 |
import com.iailab.module.model.mpk.service.*; |
D |
27 |
import lombok.extern.slf4j.Slf4j; |
|
28 |
import org.apache.commons.io.FileUtils; |
|
29 |
import org.apache.commons.lang3.StringUtils; |
|
30 |
import org.apache.velocity.VelocityContext; |
|
31 |
import org.apache.velocity.app.Velocity; |
|
32 |
import org.springframework.beans.factory.annotation.Autowired; |
a27351
|
33 |
import org.springframework.beans.factory.annotation.Value; |
449017
|
34 |
import org.springframework.stereotype.Service; |
D |
35 |
import org.springframework.util.CollectionUtils; |
|
36 |
import org.springframework.web.multipart.MultipartFile; |
|
37 |
|
f7932f
|
38 |
import java.io.*; |
912a1e
|
39 |
import java.nio.file.Files; |
449017
|
40 |
import java.util.*; |
D |
41 |
import java.util.stream.Collectors; |
|
42 |
|
|
43 |
/** |
|
44 |
* @author PanZhibao |
|
45 |
* @Description |
|
46 |
* @createTime 2024年08月14日 |
|
47 |
*/ |
|
48 |
@Slf4j |
|
49 |
@Service |
0255ea
|
50 |
public class MpkFileServiceImpl extends BaseServiceImpl<MpkFileDao, MpkFileEntity> implements MpkFileService { |
449017
|
51 |
|
D |
52 |
@Autowired |
|
53 |
private GeneratorCodeHistoryService generatorCodeHistoryService; |
8b3ee3
|
54 |
|
449017
|
55 |
@Autowired |
D |
56 |
private ProjectPackageHistoryService projectPackageHistoryService; |
8b3ee3
|
57 |
|
449017
|
58 |
@Autowired |
D |
59 |
private ModelMethodService modelMethodService; |
8b3ee3
|
60 |
|
449017
|
61 |
@Autowired |
D |
62 |
private ProjectPackageHistoryModelService projectPackageHistoryModelService; |
|
63 |
|
|
64 |
@Autowired |
|
65 |
private ConfigApi configApi; |
|
66 |
|
1fea3e
|
67 |
@Value("${mpk.bak-file-path}") |
a27351
|
68 |
private String mpkBakFilePath; |
1fea3e
|
69 |
|
D |
70 |
@Value("${mpk.bak-resources}") |
|
71 |
private String mpkResources; |
449017
|
72 |
|
38e87c
|
73 |
/*@PostConstruct |
449017
|
74 |
public void init() { |
D |
75 |
mpkBakFilePath = configApi.getConfigValueByKey("mpkBakFilePath").getCheckedData(); |
38e87c
|
76 |
}*/ |
449017
|
77 |
|
D |
78 |
@Override |
|
79 |
public PageData<MpkFileDTO> page(Map<String, Object> params) { |
|
80 |
IPage<MpkFileEntity> page = baseDao.selectPage( |
|
81 |
getPage(params, "create_date", false), |
|
82 |
getWrapper(params) |
|
83 |
); |
|
84 |
|
|
85 |
return getPageData(page, MpkFileDTO.class); |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public List<MpkFileDTO> list(Map<String, Object> params) { |
|
90 |
List<MpkFileEntity> entityList = baseDao.selectList(getWrapper(params).orderByDesc("create_date")); |
|
91 |
|
|
92 |
return ConvertUtils.sourceToTarget(entityList, MpkFileDTO.class); |
|
93 |
} |
|
94 |
|
8b3ee3
|
95 |
private QueryWrapper<MpkFileEntity> getWrapper(Map<String, Object> params) { |
449017
|
96 |
String pyName = (String) params.get("pyName"); |
D |
97 |
String pyType = (String) params.get("pyType"); |
|
98 |
String remark = (String) params.get("remark"); |
d8db4b
|
99 |
String label = (String) params.get("label"); |
449017
|
100 |
|
D |
101 |
QueryWrapper<MpkFileEntity> wrapper = new QueryWrapper<>(); |
|
102 |
wrapper.like(StringUtils.isNotBlank(pyName), "py_name", pyName) |
|
103 |
.eq(StringUtils.isNotBlank(pyType), "py_type", pyType) |
|
104 |
.like(StringUtils.isNotBlank(remark), "remark", remark); |
d8db4b
|
105 |
|
潘 |
106 |
if (StringUtils.isNotBlank(label)) { |
|
107 |
wrapper.and(w -> { |
|
108 |
w.eq(StringUtils.isNotBlank(label),"menu_name", label) |
|
109 |
.or().eq(StringUtils.isNotBlank(label),"group_name", label); |
|
110 |
}); |
|
111 |
} |
449017
|
112 |
return wrapper; |
D |
113 |
} |
|
114 |
|
|
115 |
@Override |
|
116 |
public MpkFileDTO get(String id) { |
|
117 |
MpkFileDTO entity = baseDao.get(id); |
|
118 |
|
|
119 |
return entity; |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
8b3ee3
|
123 |
@DSTransactional(rollbackFor = Exception.class) |
449017
|
124 |
public void save(MpkFileDTO dto) { |
D |
125 |
MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class); |
8b3ee3
|
126 |
entity.setId(UUID.randomUUID().toString()); |
ed8ba3
|
127 |
entity.setPkgName(dto.getPkgName().trim()); |
潘 |
128 |
entity.setFilePath(dto.getFilePath().trim()); |
449017
|
129 |
entity.setCreator(SecurityFrameworkUtils.getLoginUserId()); |
D |
130 |
entity.setCreateDate(new Date()); |
|
131 |
insert(entity); |
8b3ee3
|
132 |
modelMethodService.insertList(dto.getModelMethods(), entity.getId()); |
449017
|
133 |
} |
D |
134 |
|
|
135 |
@Override |
8b3ee3
|
136 |
@DSTransactional(rollbackFor = Exception.class) |
449017
|
137 |
public void update(MpkFileDTO dto) { |
D |
138 |
MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class); |
|
139 |
entity.setUpdater(SecurityFrameworkUtils.getLoginUserId()); |
|
140 |
entity.setUpdateDate(new Date()); |
|
141 |
updateById(entity); |
8b3ee3
|
142 |
modelMethodService.deleteModelMethod(entity.getId()); |
潘 |
143 |
modelMethodService.insertList(dto.getModelMethods(), entity.getId()); |
449017
|
144 |
} |
D |
145 |
|
|
146 |
@Override |
8b3ee3
|
147 |
@DSTransactional(rollbackFor = Exception.class) |
449017
|
148 |
public void delete(String id) { |
D |
149 |
|
|
150 |
//删除源文件 |
|
151 |
MpkFileEntity mpkFileEntity = selectById(id); |
|
152 |
if (StringUtils.isNoneBlank(mpkFileEntity.getFilePath())) { |
|
153 |
File mpkFile = new File(mpkFileEntity.getFilePath()); |
|
154 |
if (mpkFile.exists()) { |
|
155 |
mpkFile.delete(); |
|
156 |
log.info("删除源文件备份文件:" + mpkFileEntity.getFilePath()); |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
//删除备份文件 |
8b3ee3
|
161 |
Map<String, Object> map1 = new HashMap<>(); |
潘 |
162 |
map1.put("mdkId", id); |
449017
|
163 |
List<GeneratorCodeHistoryDTO> list = generatorCodeHistoryService.list(map1); |
D |
164 |
list.forEach(e -> { |
|
165 |
File file = new File(e.getFilePath()); |
|
166 |
if (file.exists()) { |
|
167 |
file.delete(); |
|
168 |
log.info("删除生成代码备份文件:" + e.getFilePath()); |
|
169 |
} |
|
170 |
}); |
|
171 |
|
0255ea
|
172 |
//删除 会级联删除掉关联表 |
D |
173 |
deleteById(id); |
449017
|
174 |
} |
D |
175 |
|
|
176 |
@Override |
|
177 |
public byte[] generatorCode(String id, String remark, String zipFileName) { |
|
178 |
MpkFileDTO entity = baseDao.get(id); |
|
179 |
//生成代码 |
1fea3e
|
180 |
//设置velocity资源加载器 |
D |
181 |
Properties prop = new Properties(); |
|
182 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
183 |
Velocity.init(prop); |
449017
|
184 |
|
1fea3e
|
185 |
//封装模板数据 |
D |
186 |
Map<String, Object> map = new HashMap<>(); |
|
187 |
map.put("pkgName",entity.getPkgName()); |
|
188 |
map.put("modelMethods",entity.getModelMethods()); |
|
189 |
map.put("pyName",entity.getPyName()); |
|
190 |
map.put("pyModule",entity.getPyModule()); |
|
191 |
|
|
192 |
VelocityContext dataContext = new VelocityContext(map); |
|
193 |
|
|
194 |
//临时文件夹 |
912a1e
|
195 |
File dirPath = null; |
D |
196 |
try { |
|
197 |
dirPath = Files.createTempDirectory("generatorCodeTmp").toFile(); |
|
198 |
log.info("生成临时文件夹," + dirPath.getAbsolutePath()); |
|
199 |
} catch (IOException e) { |
|
200 |
throw new RuntimeException("创建临时文件夹异常",e); |
|
201 |
} |
1fea3e
|
202 |
|
D |
203 |
List<String> javaFilePaths = new ArrayList<>(); |
|
204 |
List<String> cppFilePaths = new ArrayList<>(); |
|
205 |
|
|
206 |
//生成java文件 |
|
207 |
File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java"); |
|
208 |
GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile); |
|
209 |
javaFilePaths.add(javaFile.getAbsolutePath()); |
|
210 |
|
|
211 |
//生成Impl.java文件 |
|
212 |
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"); |
|
213 |
GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile); |
|
214 |
javaFilePaths.add(implJavaFile.getAbsolutePath()); |
|
215 |
|
|
216 |
//生成.cpp文件 |
|
217 |
File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp"); |
|
218 |
GenUtils.drawTemplate("cpp.vm", dataContext, cppFile); |
|
219 |
cppFilePaths.add(cppFile.getAbsolutePath()); |
|
220 |
|
|
221 |
//生成Jni.cpp文件 |
|
222 |
File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp"); |
|
223 |
GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile); |
|
224 |
cppFilePaths.add(jniCppFile.getAbsolutePath()); |
|
225 |
|
|
226 |
//生成.h文件 |
|
227 |
File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h"); |
|
228 |
GenUtils.drawTemplate("h.vm", dataContext, hFile); |
|
229 |
|
|
230 |
//生成Jni.h文件 |
|
231 |
File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h"); |
|
232 |
GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile); |
|
233 |
|
|
234 |
// 资源文件 |
|
235 |
FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); |
|
236 |
|
|
237 |
//生成dll文件 |
|
238 |
String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; |
|
239 |
createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); |
|
240 |
//备份dll文件,用于后续运行 |
|
241 |
String dllBakPath = mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll"; |
|
242 |
FileUtil.mkParentDirs(dllBakPath); |
|
243 |
FileUtil.copy(dllSavePath, dllBakPath, true); |
|
244 |
|
|
245 |
//utils + env java文件 |
|
246 |
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"); |
|
247 |
FileUtil.mkParentDirs(utilsJavaFile); |
|
248 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); |
|
249 |
javaFilePaths.add(utilsJavaFile.getAbsolutePath()); |
|
250 |
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"); |
|
251 |
FileUtil.mkParentDirs(envJavaFile); |
|
252 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); |
|
253 |
javaFilePaths.add(envJavaFile.getAbsolutePath()); |
|
254 |
// 生成class文件 |
|
255 |
createClassFile(javaFilePaths); |
|
256 |
// 删除java源文件 |
|
257 |
deleteJavaFile(javaFilePaths); |
|
258 |
// 打jar包 |
|
259 |
String jarSavePath = pkgJar(dirPath.getAbsolutePath()); |
|
260 |
//备份jar文件,用于后续运行 |
|
261 |
String jarBakPath = mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + entity.getPyName() + ".jar"; |
|
262 |
FileUtil.mkParentDirs(dllBakPath); |
|
263 |
FileUtil.copy(jarSavePath, jarBakPath, true); |
|
264 |
// 打zip包 |
|
265 |
String zipPath = mpkBakFilePath + File.separator + zipFileName; |
|
266 |
ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); |
|
267 |
byte[] bytes = FileUtil.readBytes(zipPath); |
449017
|
268 |
|
D |
269 |
//代码生成历史记录 |
|
270 |
GeneratorCodeHistoryEntity historyEntity = new GeneratorCodeHistoryEntity(); |
|
271 |
historyEntity.setId(UUID.randomUUID().toString()); |
|
272 |
historyEntity.setMdkId(id); |
1fea3e
|
273 |
historyEntity.setFileName(zipFileName); |
D |
274 |
historyEntity.setFilePath(zipPath); |
449017
|
275 |
historyEntity.setRemark(remark); |
D |
276 |
historyEntity.setCreateTime(new Date()); |
|
277 |
generatorCodeHistoryService.insert(historyEntity); |
|
278 |
|
1fea3e
|
279 |
// 删除临时文件 |
3009e2
|
280 |
try { |
D |
281 |
FileUtils.deleteDirectory(dirPath); |
|
282 |
} catch (IOException e) { |
912a1e
|
283 |
throw new RuntimeException("删除临时文件异常",e); |
3009e2
|
284 |
} |
1fea3e
|
285 |
return bytes; |
449017
|
286 |
} |
D |
287 |
|
|
288 |
@Override |
8b3ee3
|
289 |
@DSTransactional(rollbackFor = Exception.class) |
f7932f
|
290 |
public byte[] packageModel(List<String> ids, String projectId, String projectName, String zipFileName, String logs, String version) throws IOException, InterruptedException { |
449017
|
291 |
List<MpkFileDTO> entities = baseDao.selectByIds(ids); |
D |
292 |
|
|
293 |
//模板数据 |
1fea3e
|
294 |
// Map<String, Object> map = new HashMap<>(); |
D |
295 |
// map.put("entities", entities); |
|
296 |
// VelocityContext context = new VelocityContext(map); |
449017
|
297 |
|
D |
298 |
//临时文件夹 |
912a1e
|
299 |
File dirPath = null; |
D |
300 |
try { |
|
301 |
dirPath = Files.createTempDirectory("packageModelTmp").toFile(); |
|
302 |
log.info("生成临时文件夹," + dirPath.getAbsolutePath()); |
|
303 |
} catch (IOException e) { |
|
304 |
throw new RuntimeException("创建临时文件夹异常",e); |
|
305 |
} |
449017
|
306 |
|
D |
307 |
//设置velocity资源加载器 |
|
308 |
Properties prop = new Properties(); |
|
309 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
310 |
Velocity.init(prop); |
|
311 |
|
a27351
|
312 |
//生成menu.xml文件 |
2cc235
|
313 |
LinkedHashMap<String, LinkedHashMap<String, List<MpkFileDTO>>> collect = entities.stream().collect(Collectors.groupingBy(MpkFileDTO::getMenuName, LinkedHashMap::new, Collectors.groupingBy(e -> StringUtils.isNotBlank(e.getGroupName()) ? e.getGroupName() : "default_group",LinkedHashMap::new,Collectors.toList()))); |
a27351
|
314 |
Map<String, Object> map1 = new HashMap<>(); |
8b3ee3
|
315 |
map1.put("collects", collect); |
c7009e
|
316 |
File xmlFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "menu.xml"); |
8b3ee3
|
317 |
GenUtils.drawTemplate("menu.xml.vm", new VelocityContext(map1), xmlFile); |
449017
|
318 |
|
D |
319 |
List<String> javaFilePaths = new ArrayList<>(); |
f7932f
|
320 |
List<String> cppFilePaths = new ArrayList<>(); |
449017
|
321 |
|
D |
322 |
//生成java文件 |
|
323 |
for (MpkFileDTO entity : entities) { |
|
324 |
//封装模板数据 |
|
325 |
Map<String, Object> data = new HashMap<>(); |
8b3ee3
|
326 |
data.put("pkgName", entity.getPkgName()); |
潘 |
327 |
data.put("modelMethods", entity.getModelMethods()); |
|
328 |
data.put("pyName", entity.getPyName()); |
|
329 |
data.put("pyModule", entity.getPyModule()); |
449017
|
330 |
VelocityContext dataContext = new VelocityContext(data); |
D |
331 |
//生成java文件 |
|
332 |
File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java"); |
8b3ee3
|
333 |
GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile); |
449017
|
334 |
javaFilePaths.add(javaFile.getAbsolutePath()); |
D |
335 |
|
|
336 |
//生成Impl.java文件 |
|
337 |
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"); |
8b3ee3
|
338 |
GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile); |
449017
|
339 |
javaFilePaths.add(implJavaFile.getAbsolutePath()); |
D |
340 |
|
f7932f
|
341 |
//生成.cpp文件 |
D |
342 |
File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp"); |
|
343 |
GenUtils.drawTemplate("cpp.vm", dataContext, cppFile); |
|
344 |
cppFilePaths.add(cppFile.getAbsolutePath()); |
|
345 |
|
|
346 |
//生成Jni.cpp文件 |
|
347 |
File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp"); |
|
348 |
GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile); |
|
349 |
cppFilePaths.add(jniCppFile.getAbsolutePath()); |
|
350 |
|
|
351 |
//生成.h文件 |
|
352 |
File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h"); |
|
353 |
GenUtils.drawTemplate("h.vm", dataContext, hFile); |
|
354 |
|
|
355 |
//生成Jni.h文件 |
|
356 |
File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h"); |
|
357 |
GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile); |
|
358 |
|
449017
|
359 |
// 添加python源文件 |
c7009e
|
360 |
String pyFilePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.ALGS + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd"; |
449017
|
361 |
FileUtil.mkParentDirs(pyFilePath); |
8b3ee3
|
362 |
FileUtil.copy(entity.getFilePath(), pyFilePath, true); |
449017
|
363 |
} |
D |
364 |
|
1fea3e
|
365 |
// 资源文件 |
D |
366 |
FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); |
c7009e
|
367 |
|
f7932f
|
368 |
//生成dll文件 |
c7009e
|
369 |
String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; |
f7932f
|
370 |
createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); |
D |
371 |
|
449017
|
372 |
//utils + env java文件 |
c7009e
|
373 |
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"); |
449017
|
374 |
FileUtil.mkParentDirs(utilsJavaFile); |
1fea3e
|
375 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); |
449017
|
376 |
javaFilePaths.add(utilsJavaFile.getAbsolutePath()); |
c7009e
|
377 |
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"); |
449017
|
378 |
FileUtil.mkParentDirs(envJavaFile); |
1fea3e
|
379 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); |
449017
|
380 |
javaFilePaths.add(envJavaFile.getAbsolutePath()); |
D |
381 |
// 生成class文件 |
|
382 |
createClassFile(javaFilePaths); |
|
383 |
// 删除java源文件 |
|
384 |
deleteJavaFile(javaFilePaths); |
|
385 |
// 打jar包 |
|
386 |
pkgJar(dirPath.getAbsolutePath()); |
|
387 |
// 本次更新日志 |
|
388 |
ProjectPackageHistoryDTO dto = new ProjectPackageHistoryDTO(); |
|
389 |
String historyId = UUID.randomUUID().toString(); |
|
390 |
dto.setId(historyId); |
|
391 |
dto.setProjectId(projectId); |
|
392 |
dto.setFileName(zipFileName); |
f7932f
|
393 |
dto.setLog(logs); |
449017
|
394 |
dto.setVersion(version); |
D |
395 |
dto.setModelNames(entities.stream().map(MpkFileDTO::getPyName).collect(Collectors.joining(","))); |
|
396 |
dto.setCreateTime(new Date()); |
|
397 |
// 生成更新日志 |
8b3ee3
|
398 |
createLog(projectId, projectName, dirPath.getAbsolutePath(), dto, version); |
449017
|
399 |
// 打zip包 |
D |
400 |
String zipPath = mpkBakFilePath + File.separator + zipFileName; |
8b3ee3
|
401 |
ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); |
449017
|
402 |
byte[] bytes = FileUtil.readBytes(zipPath); |
D |
403 |
// 记录打包日志 |
|
404 |
dto.setFilePath(zipPath); |
|
405 |
projectPackageHistoryService.save(dto); |
|
406 |
// 插入打包历史-模型关联表 |
|
407 |
List<ProjectPackageHistoryModelEntity> historyModelList = new ArrayList<>(entities.size()); |
|
408 |
entities.forEach(e -> { |
|
409 |
ProjectPackageHistoryModelEntity entity = new ProjectPackageHistoryModelEntity(); |
|
410 |
entity.setId(UUID.randomUUID().toString()); |
|
411 |
entity.setProjectId(projectId); |
|
412 |
entity.setPackageHistoryId(historyId); |
|
413 |
entity.setPyName(e.getPyName()); |
067d7a
|
414 |
entity.setPyChineseName(e.getPyChineseName()); |
449017
|
415 |
entity.setPkgName(e.getPkgName()); |
D |
416 |
entity.setPyModule(e.getPyModule()); |
|
417 |
entity.setRemark(e.getRemark()); |
0255ea
|
418 |
List<ModelMethodDTO> methods = e.getModelMethods(); |
449017
|
419 |
if (!CollectionUtils.isEmpty(methods)) { |
D |
420 |
entity.setMethodInfo(JSON.toJSONString(methods)); |
|
421 |
} |
|
422 |
historyModelList.add(entity); |
|
423 |
}); |
|
424 |
projectPackageHistoryModelService.insertBatch(historyModelList); |
|
425 |
// 删除临时文件 |
|
426 |
FileUtils.deleteDirectory(dirPath); |
|
427 |
return bytes; |
|
428 |
} |
|
429 |
|
1fea3e
|
430 |
private void createDllFile(String dirPath, List<String> cppFilePaths, String dllSavePath) { |
D |
431 |
try { |
|
432 |
String paths = cppFilePaths.stream().collect(Collectors.joining(" ")); |
|
433 |
String command = "cl /LD " + paths + " /EHsc /o " + dllSavePath + " /link " + "IAIL.MDK.Mid.Windows.lib"; |
|
434 |
log.info("执行cmd命令生成dll:" + command); |
|
435 |
ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command); |
|
436 |
builder.directory(new File(dirPath + File.separator + MdkConstant.LIBS)); |
|
437 |
Process process = builder.start(); |
|
438 |
// 获取命令输出结果 |
|
439 |
InputStream inputStream = process.getInputStream(); |
|
440 |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK |
|
441 |
String line; |
|
442 |
while ((line = reader.readLine()) != null) { |
|
443 |
log.info(line); |
|
444 |
} |
|
445 |
// 等待命令执行完成 |
|
446 |
process.waitFor(); |
|
447 |
} catch (Exception e) { |
912a1e
|
448 |
throw new RuntimeException("执行cmd命令生成dll异常",e); |
f7932f
|
449 |
} |
D |
450 |
} |
|
451 |
|
449017
|
452 |
@Override |
8b3ee3
|
453 |
public Map<String, String> savePyFile(MultipartFile file) throws IOException { |
449017
|
454 |
File dir = new File(mpkBakFilePath); |
D |
455 |
if (!dir.exists()) { |
|
456 |
dir.mkdirs(); |
|
457 |
} |
|
458 |
String fileName = file.getOriginalFilename(); |
1fea3e
|
459 |
String fileSuffix = fileName.substring(fileName.lastIndexOf(".")); |
D |
460 |
File saveFile = new File(dir.getAbsolutePath() + File.separator + UUID.randomUUID() + fileSuffix); |
|
461 |
|
449017
|
462 |
saveFile.createNewFile(); |
D |
463 |
// 保存 |
|
464 |
file.transferTo(saveFile); |
|
465 |
|
8b3ee3
|
466 |
Map<String, String> result = new HashMap<>(2); |
潘 |
467 |
result.put("filePath", saveFile.getAbsolutePath()); |
449017
|
468 |
result.put("fileName", fileName); |
D |
469 |
|
|
470 |
return result; |
|
471 |
} |
|
472 |
|
8b3ee3
|
473 |
private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryDTO dto, String version) throws IOException { |
潘 |
474 |
Map<String, Object> map = new HashMap<>(); |
|
475 |
map.put("projectId", projectId); |
449017
|
476 |
List<ProjectPackageHistoryDTO> list = projectPackageHistoryService.list(map); |
D |
477 |
list.add(dto); |
|
478 |
// 按照日期分组再排序 |
|
479 |
HashMap<String, List<ProjectPackageHistoryDTO>> dataMap = list.stream().collect( |
|
480 |
Collectors.groupingBy(e -> DateUtils.format(e.getCreateTime(), DateUtils.DATE_PATTERN_POINT), |
8b3ee3
|
481 |
LinkedHashMap::new, |
潘 |
482 |
Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().sorted(Comparator.comparing(ProjectPackageHistoryDTO::getCreateTime)).collect(Collectors.toList())))); |
449017
|
483 |
|
D |
484 |
Map<String, Object> data = new HashMap<>(); |
8b3ee3
|
485 |
data.put("dataMap", dataMap); |
潘 |
486 |
data.put("projectName", projectName); |
|
487 |
data.put("version", version); |
|
488 |
data.put("now", DateUtils.format(new Date(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); |
449017
|
489 |
|
D |
490 |
File logFile = new File(dirPath + File.separator + "更新日志.txt"); |
8b3ee3
|
491 |
GenUtils.drawTemplate("log.txt.vm", data, logFile); |
449017
|
492 |
} |
D |
493 |
|
3009e2
|
494 |
private String pkgJar(String dirPath) { |
D |
495 |
try { |
|
496 |
String jarSavePath = dirPath + File.separator + MdkConstant.LIBS + File.separator + "IAILMDK.jar"; |
|
497 |
StringBuilder sb = new StringBuilder(); |
|
498 |
sb.append("jar -cvf"); |
|
499 |
sb.append(" ").append(jarSavePath); |
|
500 |
sb.append(" -C ").append(dirPath).append(File.separator).append("IAILMDK").append(File.separator).append(" ."); |
|
501 |
log.info("执行cmd命令打jar包:" + sb); |
|
502 |
Process process = RuntimeUtil.exec(sb.toString()); |
|
503 |
process.waitFor(); |
|
504 |
return jarSavePath; |
|
505 |
} catch (InterruptedException e) { |
912a1e
|
506 |
throw new RuntimeException("执行cmd命令打jar包异常",e); |
3009e2
|
507 |
} |
449017
|
508 |
} |
D |
509 |
|
|
510 |
private void deleteJavaFile(List<String> javaFilePaths) { |
|
511 |
for (String javaFilePath : javaFilePaths) { |
|
512 |
FileUtil.del(javaFilePath); |
|
513 |
} |
|
514 |
} |
|
515 |
|
3009e2
|
516 |
private void createClassFile(List<String> javaFilePaths){ |
D |
517 |
try { |
|
518 |
StringBuilder sb = new StringBuilder(); |
|
519 |
sb.append("javac -encoding utf-8"); |
|
520 |
for (String path : javaFilePaths) { |
|
521 |
sb.append(" ").append(path); |
|
522 |
} |
|
523 |
log.info("执行cmd命令生成class:" + sb); |
|
524 |
Process process = RuntimeUtil.exec(sb.toString()); |
|
525 |
process.waitFor(); |
|
526 |
} catch (InterruptedException e) { |
912a1e
|
527 |
throw new RuntimeException("执行cmd命令生成class异常",e); |
449017
|
528 |
} |
D |
529 |
} |
|
530 |
} |