提交 | 用户 | 时间
|
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()); |
449017
|
127 |
entity.setCreator(SecurityFrameworkUtils.getLoginUserId()); |
D |
128 |
entity.setCreateDate(new Date()); |
|
129 |
insert(entity); |
8b3ee3
|
130 |
modelMethodService.insertList(dto.getModelMethods(), entity.getId()); |
449017
|
131 |
} |
D |
132 |
|
|
133 |
@Override |
8b3ee3
|
134 |
@DSTransactional(rollbackFor = Exception.class) |
449017
|
135 |
public void update(MpkFileDTO dto) { |
D |
136 |
MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class); |
|
137 |
entity.setUpdater(SecurityFrameworkUtils.getLoginUserId()); |
|
138 |
entity.setUpdateDate(new Date()); |
|
139 |
updateById(entity); |
8b3ee3
|
140 |
modelMethodService.deleteModelMethod(entity.getId()); |
潘 |
141 |
modelMethodService.insertList(dto.getModelMethods(), entity.getId()); |
449017
|
142 |
} |
D |
143 |
|
|
144 |
@Override |
8b3ee3
|
145 |
@DSTransactional(rollbackFor = Exception.class) |
449017
|
146 |
public void delete(String id) { |
D |
147 |
|
|
148 |
//删除源文件 |
|
149 |
MpkFileEntity mpkFileEntity = selectById(id); |
|
150 |
if (StringUtils.isNoneBlank(mpkFileEntity.getFilePath())) { |
|
151 |
File mpkFile = new File(mpkFileEntity.getFilePath()); |
|
152 |
if (mpkFile.exists()) { |
|
153 |
mpkFile.delete(); |
|
154 |
log.info("删除源文件备份文件:" + mpkFileEntity.getFilePath()); |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 |
//删除备份文件 |
8b3ee3
|
159 |
Map<String, Object> map1 = new HashMap<>(); |
潘 |
160 |
map1.put("mdkId", id); |
449017
|
161 |
List<GeneratorCodeHistoryDTO> list = generatorCodeHistoryService.list(map1); |
D |
162 |
list.forEach(e -> { |
|
163 |
File file = new File(e.getFilePath()); |
|
164 |
if (file.exists()) { |
|
165 |
file.delete(); |
|
166 |
log.info("删除生成代码备份文件:" + e.getFilePath()); |
|
167 |
} |
|
168 |
}); |
|
169 |
|
0255ea
|
170 |
//删除 会级联删除掉关联表 |
D |
171 |
deleteById(id); |
449017
|
172 |
} |
D |
173 |
|
|
174 |
@Override |
|
175 |
public byte[] generatorCode(String id, String remark, String zipFileName) { |
|
176 |
MpkFileDTO entity = baseDao.get(id); |
|
177 |
//生成代码 |
1fea3e
|
178 |
//设置velocity资源加载器 |
D |
179 |
Properties prop = new Properties(); |
|
180 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
181 |
Velocity.init(prop); |
449017
|
182 |
|
1fea3e
|
183 |
//封装模板数据 |
D |
184 |
Map<String, Object> map = new HashMap<>(); |
|
185 |
map.put("pkgName",entity.getPkgName()); |
|
186 |
map.put("modelMethods",entity.getModelMethods()); |
|
187 |
map.put("pyName",entity.getPyName()); |
|
188 |
map.put("pyModule",entity.getPyModule()); |
|
189 |
|
|
190 |
VelocityContext dataContext = new VelocityContext(map); |
|
191 |
|
|
192 |
//临时文件夹 |
912a1e
|
193 |
File dirPath = null; |
D |
194 |
try { |
|
195 |
dirPath = Files.createTempDirectory("generatorCodeTmp").toFile(); |
|
196 |
log.info("生成临时文件夹," + dirPath.getAbsolutePath()); |
|
197 |
} catch (IOException e) { |
|
198 |
throw new RuntimeException("创建临时文件夹异常",e); |
|
199 |
} |
1fea3e
|
200 |
|
D |
201 |
List<String> javaFilePaths = new ArrayList<>(); |
|
202 |
List<String> cppFilePaths = new ArrayList<>(); |
|
203 |
|
|
204 |
//生成java文件 |
|
205 |
File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java"); |
|
206 |
GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile); |
|
207 |
javaFilePaths.add(javaFile.getAbsolutePath()); |
|
208 |
|
|
209 |
//生成Impl.java文件 |
|
210 |
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"); |
|
211 |
GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile); |
|
212 |
javaFilePaths.add(implJavaFile.getAbsolutePath()); |
|
213 |
|
|
214 |
//生成.cpp文件 |
|
215 |
File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp"); |
|
216 |
GenUtils.drawTemplate("cpp.vm", dataContext, cppFile); |
|
217 |
cppFilePaths.add(cppFile.getAbsolutePath()); |
|
218 |
|
|
219 |
//生成Jni.cpp文件 |
|
220 |
File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp"); |
|
221 |
GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile); |
|
222 |
cppFilePaths.add(jniCppFile.getAbsolutePath()); |
|
223 |
|
|
224 |
//生成.h文件 |
|
225 |
File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h"); |
|
226 |
GenUtils.drawTemplate("h.vm", dataContext, hFile); |
|
227 |
|
|
228 |
//生成Jni.h文件 |
|
229 |
File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h"); |
|
230 |
GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile); |
|
231 |
|
|
232 |
// 资源文件 |
|
233 |
FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); |
|
234 |
|
|
235 |
//生成dll文件 |
|
236 |
String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; |
|
237 |
createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); |
|
238 |
//备份dll文件,用于后续运行 |
|
239 |
String dllBakPath = mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll"; |
|
240 |
FileUtil.mkParentDirs(dllBakPath); |
|
241 |
FileUtil.copy(dllSavePath, dllBakPath, true); |
|
242 |
|
|
243 |
//utils + env java文件 |
|
244 |
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"); |
|
245 |
FileUtil.mkParentDirs(utilsJavaFile); |
|
246 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); |
|
247 |
javaFilePaths.add(utilsJavaFile.getAbsolutePath()); |
|
248 |
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"); |
|
249 |
FileUtil.mkParentDirs(envJavaFile); |
|
250 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); |
|
251 |
javaFilePaths.add(envJavaFile.getAbsolutePath()); |
|
252 |
// 生成class文件 |
|
253 |
createClassFile(javaFilePaths); |
|
254 |
// 删除java源文件 |
|
255 |
deleteJavaFile(javaFilePaths); |
|
256 |
// 打jar包 |
|
257 |
String jarSavePath = pkgJar(dirPath.getAbsolutePath()); |
|
258 |
//备份jar文件,用于后续运行 |
|
259 |
String jarBakPath = mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + entity.getPyName() + ".jar"; |
|
260 |
FileUtil.mkParentDirs(dllBakPath); |
|
261 |
FileUtil.copy(jarSavePath, jarBakPath, true); |
|
262 |
// 打zip包 |
|
263 |
String zipPath = mpkBakFilePath + File.separator + zipFileName; |
|
264 |
ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); |
|
265 |
byte[] bytes = FileUtil.readBytes(zipPath); |
449017
|
266 |
|
D |
267 |
//代码生成历史记录 |
|
268 |
GeneratorCodeHistoryEntity historyEntity = new GeneratorCodeHistoryEntity(); |
|
269 |
historyEntity.setId(UUID.randomUUID().toString()); |
|
270 |
historyEntity.setMdkId(id); |
1fea3e
|
271 |
historyEntity.setFileName(zipFileName); |
D |
272 |
historyEntity.setFilePath(zipPath); |
449017
|
273 |
historyEntity.setRemark(remark); |
D |
274 |
historyEntity.setCreateTime(new Date()); |
|
275 |
generatorCodeHistoryService.insert(historyEntity); |
|
276 |
|
1fea3e
|
277 |
// 删除临时文件 |
3009e2
|
278 |
try { |
D |
279 |
FileUtils.deleteDirectory(dirPath); |
|
280 |
} catch (IOException e) { |
912a1e
|
281 |
throw new RuntimeException("删除临时文件异常",e); |
3009e2
|
282 |
} |
1fea3e
|
283 |
return bytes; |
449017
|
284 |
} |
D |
285 |
|
|
286 |
@Override |
8b3ee3
|
287 |
@DSTransactional(rollbackFor = Exception.class) |
f7932f
|
288 |
public byte[] packageModel(List<String> ids, String projectId, String projectName, String zipFileName, String logs, String version) throws IOException, InterruptedException { |
449017
|
289 |
List<MpkFileDTO> entities = baseDao.selectByIds(ids); |
D |
290 |
|
|
291 |
//模板数据 |
1fea3e
|
292 |
// Map<String, Object> map = new HashMap<>(); |
D |
293 |
// map.put("entities", entities); |
|
294 |
// VelocityContext context = new VelocityContext(map); |
449017
|
295 |
|
D |
296 |
//临时文件夹 |
912a1e
|
297 |
File dirPath = null; |
D |
298 |
try { |
|
299 |
dirPath = Files.createTempDirectory("packageModelTmp").toFile(); |
|
300 |
log.info("生成临时文件夹," + dirPath.getAbsolutePath()); |
|
301 |
} catch (IOException e) { |
|
302 |
throw new RuntimeException("创建临时文件夹异常",e); |
|
303 |
} |
449017
|
304 |
|
D |
305 |
//设置velocity资源加载器 |
|
306 |
Properties prop = new Properties(); |
|
307 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
308 |
Velocity.init(prop); |
|
309 |
|
a27351
|
310 |
//生成menu.xml文件 |
2cc235
|
311 |
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
|
312 |
Map<String, Object> map1 = new HashMap<>(); |
8b3ee3
|
313 |
map1.put("collects", collect); |
c7009e
|
314 |
File xmlFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "menu.xml"); |
8b3ee3
|
315 |
GenUtils.drawTemplate("menu.xml.vm", new VelocityContext(map1), xmlFile); |
449017
|
316 |
|
D |
317 |
List<String> javaFilePaths = new ArrayList<>(); |
f7932f
|
318 |
List<String> cppFilePaths = new ArrayList<>(); |
449017
|
319 |
|
D |
320 |
//生成java文件 |
|
321 |
for (MpkFileDTO entity : entities) { |
|
322 |
//封装模板数据 |
|
323 |
Map<String, Object> data = new HashMap<>(); |
8b3ee3
|
324 |
data.put("pkgName", entity.getPkgName()); |
潘 |
325 |
data.put("modelMethods", entity.getModelMethods()); |
|
326 |
data.put("pyName", entity.getPyName()); |
|
327 |
data.put("pyModule", entity.getPyModule()); |
449017
|
328 |
VelocityContext dataContext = new VelocityContext(data); |
D |
329 |
//生成java文件 |
|
330 |
File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java"); |
8b3ee3
|
331 |
GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile); |
449017
|
332 |
javaFilePaths.add(javaFile.getAbsolutePath()); |
D |
333 |
|
|
334 |
//生成Impl.java文件 |
|
335 |
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
|
336 |
GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile); |
449017
|
337 |
javaFilePaths.add(implJavaFile.getAbsolutePath()); |
D |
338 |
|
f7932f
|
339 |
//生成.cpp文件 |
D |
340 |
File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp"); |
|
341 |
GenUtils.drawTemplate("cpp.vm", dataContext, cppFile); |
|
342 |
cppFilePaths.add(cppFile.getAbsolutePath()); |
|
343 |
|
|
344 |
//生成Jni.cpp文件 |
|
345 |
File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp"); |
|
346 |
GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile); |
|
347 |
cppFilePaths.add(jniCppFile.getAbsolutePath()); |
|
348 |
|
|
349 |
//生成.h文件 |
|
350 |
File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h"); |
|
351 |
GenUtils.drawTemplate("h.vm", dataContext, hFile); |
|
352 |
|
|
353 |
//生成Jni.h文件 |
|
354 |
File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h"); |
|
355 |
GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile); |
|
356 |
|
449017
|
357 |
// 添加python源文件 |
c7009e
|
358 |
String pyFilePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.ALGS + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd"; |
449017
|
359 |
FileUtil.mkParentDirs(pyFilePath); |
8b3ee3
|
360 |
FileUtil.copy(entity.getFilePath(), pyFilePath, true); |
449017
|
361 |
} |
D |
362 |
|
1fea3e
|
363 |
// 资源文件 |
D |
364 |
FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); |
c7009e
|
365 |
|
f7932f
|
366 |
//生成dll文件 |
c7009e
|
367 |
String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; |
f7932f
|
368 |
createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); |
D |
369 |
|
449017
|
370 |
//utils + env java文件 |
c7009e
|
371 |
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
|
372 |
FileUtil.mkParentDirs(utilsJavaFile); |
1fea3e
|
373 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); |
449017
|
374 |
javaFilePaths.add(utilsJavaFile.getAbsolutePath()); |
c7009e
|
375 |
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
|
376 |
FileUtil.mkParentDirs(envJavaFile); |
1fea3e
|
377 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); |
449017
|
378 |
javaFilePaths.add(envJavaFile.getAbsolutePath()); |
D |
379 |
// 生成class文件 |
|
380 |
createClassFile(javaFilePaths); |
|
381 |
// 删除java源文件 |
|
382 |
deleteJavaFile(javaFilePaths); |
|
383 |
// 打jar包 |
|
384 |
pkgJar(dirPath.getAbsolutePath()); |
|
385 |
// 本次更新日志 |
|
386 |
ProjectPackageHistoryDTO dto = new ProjectPackageHistoryDTO(); |
|
387 |
String historyId = UUID.randomUUID().toString(); |
|
388 |
dto.setId(historyId); |
|
389 |
dto.setProjectId(projectId); |
|
390 |
dto.setFileName(zipFileName); |
f7932f
|
391 |
dto.setLog(logs); |
449017
|
392 |
dto.setVersion(version); |
D |
393 |
dto.setModelNames(entities.stream().map(MpkFileDTO::getPyName).collect(Collectors.joining(","))); |
|
394 |
dto.setCreateTime(new Date()); |
|
395 |
// 生成更新日志 |
8b3ee3
|
396 |
createLog(projectId, projectName, dirPath.getAbsolutePath(), dto, version); |
449017
|
397 |
// 打zip包 |
D |
398 |
String zipPath = mpkBakFilePath + File.separator + zipFileName; |
8b3ee3
|
399 |
ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); |
449017
|
400 |
byte[] bytes = FileUtil.readBytes(zipPath); |
D |
401 |
// 记录打包日志 |
|
402 |
dto.setFilePath(zipPath); |
|
403 |
projectPackageHistoryService.save(dto); |
|
404 |
// 插入打包历史-模型关联表 |
|
405 |
List<ProjectPackageHistoryModelEntity> historyModelList = new ArrayList<>(entities.size()); |
|
406 |
entities.forEach(e -> { |
|
407 |
ProjectPackageHistoryModelEntity entity = new ProjectPackageHistoryModelEntity(); |
|
408 |
entity.setId(UUID.randomUUID().toString()); |
|
409 |
entity.setProjectId(projectId); |
|
410 |
entity.setPackageHistoryId(historyId); |
|
411 |
entity.setPyName(e.getPyName()); |
067d7a
|
412 |
entity.setPyChineseName(e.getPyChineseName()); |
449017
|
413 |
entity.setPkgName(e.getPkgName()); |
D |
414 |
entity.setPyModule(e.getPyModule()); |
|
415 |
entity.setRemark(e.getRemark()); |
0255ea
|
416 |
List<ModelMethodDTO> methods = e.getModelMethods(); |
449017
|
417 |
if (!CollectionUtils.isEmpty(methods)) { |
D |
418 |
entity.setMethodInfo(JSON.toJSONString(methods)); |
|
419 |
} |
|
420 |
historyModelList.add(entity); |
|
421 |
}); |
|
422 |
projectPackageHistoryModelService.insertBatch(historyModelList); |
|
423 |
// 删除临时文件 |
|
424 |
FileUtils.deleteDirectory(dirPath); |
|
425 |
return bytes; |
|
426 |
} |
|
427 |
|
1fea3e
|
428 |
private void createDllFile(String dirPath, List<String> cppFilePaths, String dllSavePath) { |
D |
429 |
try { |
|
430 |
String paths = cppFilePaths.stream().collect(Collectors.joining(" ")); |
|
431 |
String command = "cl /LD " + paths + " /EHsc /o " + dllSavePath + " /link " + "IAIL.MDK.Mid.Windows.lib"; |
|
432 |
log.info("执行cmd命令生成dll:" + command); |
|
433 |
ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command); |
|
434 |
builder.directory(new File(dirPath + File.separator + MdkConstant.LIBS)); |
|
435 |
Process process = builder.start(); |
|
436 |
// 获取命令输出结果 |
|
437 |
InputStream inputStream = process.getInputStream(); |
|
438 |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK |
|
439 |
String line; |
|
440 |
while ((line = reader.readLine()) != null) { |
|
441 |
log.info(line); |
|
442 |
} |
|
443 |
// 等待命令执行完成 |
|
444 |
process.waitFor(); |
|
445 |
} catch (Exception e) { |
912a1e
|
446 |
throw new RuntimeException("执行cmd命令生成dll异常",e); |
f7932f
|
447 |
} |
D |
448 |
} |
|
449 |
|
449017
|
450 |
@Override |
8b3ee3
|
451 |
public Map<String, String> savePyFile(MultipartFile file) throws IOException { |
449017
|
452 |
File dir = new File(mpkBakFilePath); |
D |
453 |
if (!dir.exists()) { |
|
454 |
dir.mkdirs(); |
|
455 |
} |
|
456 |
String fileName = file.getOriginalFilename(); |
1fea3e
|
457 |
String fileSuffix = fileName.substring(fileName.lastIndexOf(".")); |
D |
458 |
File saveFile = new File(dir.getAbsolutePath() + File.separator + UUID.randomUUID() + fileSuffix); |
|
459 |
|
449017
|
460 |
saveFile.createNewFile(); |
D |
461 |
// 保存 |
|
462 |
file.transferTo(saveFile); |
|
463 |
|
8b3ee3
|
464 |
Map<String, String> result = new HashMap<>(2); |
潘 |
465 |
result.put("filePath", saveFile.getAbsolutePath()); |
449017
|
466 |
result.put("fileName", fileName); |
D |
467 |
|
|
468 |
return result; |
|
469 |
} |
|
470 |
|
8b3ee3
|
471 |
private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryDTO dto, String version) throws IOException { |
潘 |
472 |
Map<String, Object> map = new HashMap<>(); |
|
473 |
map.put("projectId", projectId); |
449017
|
474 |
List<ProjectPackageHistoryDTO> list = projectPackageHistoryService.list(map); |
D |
475 |
list.add(dto); |
|
476 |
// 按照日期分组再排序 |
|
477 |
HashMap<String, List<ProjectPackageHistoryDTO>> dataMap = list.stream().collect( |
|
478 |
Collectors.groupingBy(e -> DateUtils.format(e.getCreateTime(), DateUtils.DATE_PATTERN_POINT), |
8b3ee3
|
479 |
LinkedHashMap::new, |
潘 |
480 |
Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().sorted(Comparator.comparing(ProjectPackageHistoryDTO::getCreateTime)).collect(Collectors.toList())))); |
449017
|
481 |
|
D |
482 |
Map<String, Object> data = new HashMap<>(); |
8b3ee3
|
483 |
data.put("dataMap", dataMap); |
潘 |
484 |
data.put("projectName", projectName); |
|
485 |
data.put("version", version); |
|
486 |
data.put("now", DateUtils.format(new Date(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); |
449017
|
487 |
|
D |
488 |
File logFile = new File(dirPath + File.separator + "更新日志.txt"); |
8b3ee3
|
489 |
GenUtils.drawTemplate("log.txt.vm", data, logFile); |
449017
|
490 |
} |
D |
491 |
|
3009e2
|
492 |
private String pkgJar(String dirPath) { |
D |
493 |
try { |
|
494 |
String jarSavePath = dirPath + File.separator + MdkConstant.LIBS + File.separator + "IAILMDK.jar"; |
|
495 |
StringBuilder sb = new StringBuilder(); |
|
496 |
sb.append("jar -cvf"); |
|
497 |
sb.append(" ").append(jarSavePath); |
|
498 |
sb.append(" -C ").append(dirPath).append(File.separator).append("IAILMDK").append(File.separator).append(" ."); |
|
499 |
log.info("执行cmd命令打jar包:" + sb); |
|
500 |
Process process = RuntimeUtil.exec(sb.toString()); |
|
501 |
process.waitFor(); |
|
502 |
return jarSavePath; |
|
503 |
} catch (InterruptedException e) { |
912a1e
|
504 |
throw new RuntimeException("执行cmd命令打jar包异常",e); |
3009e2
|
505 |
} |
449017
|
506 |
} |
D |
507 |
|
|
508 |
private void deleteJavaFile(List<String> javaFilePaths) { |
|
509 |
for (String javaFilePath : javaFilePaths) { |
|
510 |
FileUtil.del(javaFilePath); |
|
511 |
} |
|
512 |
} |
|
513 |
|
3009e2
|
514 |
private void createClassFile(List<String> javaFilePaths){ |
D |
515 |
try { |
|
516 |
StringBuilder sb = new StringBuilder(); |
|
517 |
sb.append("javac -encoding utf-8"); |
|
518 |
for (String path : javaFilePaths) { |
|
519 |
sb.append(" ").append(path); |
|
520 |
} |
|
521 |
log.info("执行cmd命令生成class:" + sb); |
|
522 |
Process process = RuntimeUtil.exec(sb.toString()); |
|
523 |
process.waitFor(); |
|
524 |
} catch (InterruptedException e) { |
912a1e
|
525 |
throw new RuntimeException("执行cmd命令生成class异常",e); |
449017
|
526 |
} |
D |
527 |
} |
|
528 |
} |