提交 | 用户 | 时间
|
449017
|
1 |
package com.iailab.module.model.mpk.common.utils; |
D |
2 |
|
|
3 |
import cn.hutool.core.io.FileUtil; |
|
4 |
import com.iailab.module.model.mpk.common.MdkConstant; |
|
5 |
import com.iailab.module.model.mpk.dto.MpkFileDTO; |
|
6 |
import lombok.extern.slf4j.Slf4j; |
|
7 |
import org.apache.commons.io.FileUtils; |
|
8 |
import org.apache.commons.io.IOUtils; |
|
9 |
import org.apache.velocity.Template; |
|
10 |
import org.apache.velocity.VelocityContext; |
|
11 |
import org.apache.velocity.app.Velocity; |
|
12 |
|
|
13 |
import java.io.File; |
|
14 |
import java.io.IOException; |
|
15 |
import java.io.OutputStream; |
|
16 |
import java.io.StringWriter; |
|
17 |
import java.nio.file.Files; |
|
18 |
import java.util.*; |
|
19 |
import java.util.zip.ZipEntry; |
|
20 |
import java.util.zip.ZipOutputStream; |
|
21 |
|
|
22 |
@Slf4j |
|
23 |
public class GenUtils { |
|
24 |
public static List<String> getTemplates(){ |
|
25 |
List<String> templates = new ArrayList<String>(); |
|
26 |
templates.add("abstract.java.vm"); |
|
27 |
templates.add("impl.java.vm"); |
|
28 |
templates.add("cpp.vm"); |
|
29 |
templates.add("Jni.cpp.vm"); |
|
30 |
templates.add("h.vm"); |
|
31 |
templates.add("Jni.h.vm"); |
|
32 |
return templates; |
|
33 |
} |
|
34 |
/** |
|
35 |
* 生成代码 |
|
36 |
*/ |
|
37 |
public static void generatorCode(MpkFileDTO entity, ZipOutputStream zip){ |
|
38 |
//设置velocity资源加载器 |
|
39 |
Properties prop = new Properties(); |
|
40 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
41 |
Velocity.init(prop); |
|
42 |
|
|
43 |
//封装模板数据 |
|
44 |
Map<String, Object> map = new HashMap<>(); |
|
45 |
map.put("pkgName",entity.getPkgName()); |
|
46 |
map.put("modelMethods",entity.getModelMethods()); |
|
47 |
map.put("pyName",entity.getPyName()); |
|
48 |
map.put("pyModule",entity.getPyModule()); |
|
49 |
|
|
50 |
VelocityContext context = new VelocityContext(map); |
|
51 |
|
|
52 |
//获取模板列表 |
|
53 |
List<String> templates = getTemplates(); |
|
54 |
for(String template : templates){ |
|
55 |
//渲染模板 |
|
56 |
StringWriter sw = drawTemplate(template,context); |
|
57 |
|
|
58 |
try { |
|
59 |
//添加到zip |
|
60 |
zip.putNextEntry(new ZipEntry(getFileName(template, entity.getPyName(),entity.getPkgName(),entity.getPyModule()))); |
|
61 |
IOUtils.write(sw.toString(), zip, "UTF-8"); |
|
62 |
IOUtils.closeQuietly(sw); |
|
63 |
zip.closeEntry(); |
|
64 |
|
|
65 |
if (template.equals("cpp.vm")) { |
|
66 |
File dirPath = new File("C:/DLUT/tmp/"); |
|
67 |
if (!dirPath.exists()) { |
|
68 |
dirPath.mkdirs(); |
|
69 |
} |
|
70 |
// 生成临时cpp文件 |
|
71 |
File cppFile = new File(dirPath.getAbsolutePath() + File.separator + entity.getPyName() + ".cpp"); |
|
72 |
cppFile.createNewFile(); |
|
73 |
OutputStream cppOutputStream = Files.newOutputStream(cppFile.toPath()); |
|
74 |
IOUtils.write(sw.toString(), cppOutputStream, "UTF-8"); |
|
75 |
IOUtils.closeQuietly(cppOutputStream); |
|
76 |
} |
|
77 |
|
|
78 |
if (template.equals("Jni.cpp.vm")) { |
|
79 |
File dirPath = new File("C:/DLUT/tmp/"); |
|
80 |
if (!dirPath.exists()) { |
|
81 |
dirPath.mkdirs(); |
|
82 |
} |
|
83 |
|
|
84 |
// 生成临时Jni.cpp文件 |
|
85 |
File cppFile = new File(dirPath.getAbsolutePath() + File.separator + entity.getPyName() + ".cpp"); |
|
86 |
File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + entity.getPyName() + "Jni.cpp"); |
|
87 |
jniCppFile.createNewFile(); |
|
88 |
OutputStream jniCppOutputStream = Files.newOutputStream(jniCppFile.toPath()); |
|
89 |
IOUtils.write(sw.toString(), jniCppOutputStream, "UTF-8"); |
|
90 |
IOUtils.closeQuietly(jniCppOutputStream); |
|
91 |
try { |
|
92 |
// 根据file 通过cmd命令 生成dll |
|
93 |
String dllSavePath = dirPath.getAbsolutePath() + File.separator + entity.getPyName() + ".dll"; |
|
94 |
String cppFilePath = cppFile.getAbsolutePath(); |
|
95 |
String jniCppFilePath = jniCppFile.getAbsolutePath(); |
|
96 |
// String command = "cmd.exe /c cl -o " + dllSavePath + " /LD " + jniCppFilePath + " " + cppFilePath; |
|
97 |
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"; |
|
98 |
Process process = Runtime.getRuntime().exec(command); |
|
99 |
// 等待命令执行完成 |
|
100 |
process.waitFor(); |
|
101 |
|
|
102 |
File dllFile = new File(dllSavePath); |
|
103 |
//添加到zip |
|
104 |
zip.putNextEntry(new ZipEntry("dll" + File.separator + entity.getPyName() + ".dll")); |
|
105 |
IOUtils.write(FileUtils.readFileToByteArray(dllFile), zip); |
|
106 |
zip.closeEntry(); |
|
107 |
|
|
108 |
FileUtils.deleteDirectory(dirPath); |
|
109 |
} catch (InterruptedException e) { |
|
110 |
log.error("DLL生成失败" + entity.getPyName(), e); |
|
111 |
throw new RuntimeException(e); |
|
112 |
} |
|
113 |
} |
|
114 |
} catch (IOException e) { |
|
115 |
log.error("渲染模板失败,模型名称:" + entity.getPyName(), e); |
|
116 |
throw new RuntimeException("渲染模板失败,模型名称:" + entity.getPyName(), e); |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* 获取文件名 |
|
123 |
*/ |
|
124 |
public static String getFileName(String template, String moduleName, String pkgName, String pyModule) { |
|
125 |
// java |
|
126 |
if (template.equals("abstract.java.vm")) { |
|
127 |
return pkgName.replace(".", File.separator) + File.separator + moduleName + ".java"; |
|
128 |
} |
|
129 |
if (template.equals("impl.java.vm")) { |
|
130 |
return pkgName.replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + moduleName + "Impl.java"; |
|
131 |
} |
|
132 |
// c++ |
|
133 |
if (template.equals("cpp.vm")) { |
|
134 |
return pyModule.replace(".", File.separator) + File.separator + moduleName + ".cpp"; |
|
135 |
} |
|
136 |
if (template.equals("h.vm")) { |
|
137 |
return pyModule.replace(".", File.separator) + File.separator + moduleName + ".h"; |
|
138 |
} |
|
139 |
// Jni c++ |
|
140 |
if (template.equals("Jni.cpp.vm")) { |
|
141 |
return pyModule.replace(".", File.separator) + File.separator + MdkConstant.JNI + File.separator + moduleName + "Jni.cpp"; |
|
142 |
} |
|
143 |
if (template.equals("Jni.h.vm")) { |
|
144 |
return pyModule.replace(".", File.separator) + File.separator + MdkConstant.JNI + File.separator + moduleName + "Jni.h"; |
|
145 |
} |
|
146 |
return null; |
|
147 |
} |
|
148 |
|
|
149 |
/** |
|
150 |
* 渲染模板 |
|
151 |
**/ |
|
152 |
public static StringWriter drawTemplate(String template,Map<String, Object> map) { |
|
153 |
//模板数据 |
|
154 |
VelocityContext context = new VelocityContext(map); |
|
155 |
return drawTemplate(template,context); |
|
156 |
} |
|
157 |
|
|
158 |
public static StringWriter drawTemplate(String template,VelocityContext context) { |
|
159 |
Properties prop = new Properties(); |
|
160 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
161 |
Velocity.init(prop); |
|
162 |
|
|
163 |
StringWriter writer = new StringWriter(); |
|
164 |
Template tpl = Velocity.getTemplate("template/" + template, "UTF-8"); |
|
165 |
tpl.merge(context, writer); |
|
166 |
return writer; |
|
167 |
} |
|
168 |
|
|
169 |
public static void drawTemplate(String template,Map<String, Object> map,File toFile) throws IOException { |
|
170 |
VelocityContext context = new VelocityContext(map); |
|
171 |
drawTemplate(template,context,toFile); |
|
172 |
} |
|
173 |
|
|
174 |
public static void drawTemplate(String template,VelocityContext context,File toFile) throws IOException { |
|
175 |
StringWriter writer = drawTemplate(template,context); |
|
176 |
|
|
177 |
FileUtil.mkParentDirs(toFile); |
|
178 |
|
|
179 |
if (!toFile.exists()) { |
|
180 |
toFile.createNewFile(); |
|
181 |
} |
|
182 |
|
|
183 |
FileUtil.writeUtf8String(writer.toString(),toFile); |
|
184 |
} |
|
185 |
} |