提交 | 用户 | 时间
|
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 |
} catch (IOException e) { |
|
65 |
log.error("渲染模板失败,模型名称:" + entity.getPyName(), e); |
|
66 |
throw new RuntimeException("渲染模板失败,模型名称:" + entity.getPyName(), e); |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* 获取文件名 |
|
73 |
*/ |
|
74 |
public static String getFileName(String template, String moduleName, String pkgName, String pyModule) { |
|
75 |
// java |
|
76 |
if (template.equals("abstract.java.vm")) { |
|
77 |
return pkgName.replace(".", File.separator) + File.separator + moduleName + ".java"; |
|
78 |
} |
|
79 |
if (template.equals("impl.java.vm")) { |
|
80 |
return pkgName.replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + moduleName + "Impl.java"; |
|
81 |
} |
|
82 |
// c++ |
|
83 |
if (template.equals("cpp.vm")) { |
|
84 |
return pyModule.replace(".", File.separator) + File.separator + moduleName + ".cpp"; |
|
85 |
} |
|
86 |
if (template.equals("h.vm")) { |
|
87 |
return pyModule.replace(".", File.separator) + File.separator + moduleName + ".h"; |
|
88 |
} |
|
89 |
// Jni c++ |
|
90 |
if (template.equals("Jni.cpp.vm")) { |
|
91 |
return pyModule.replace(".", File.separator) + File.separator + MdkConstant.JNI + File.separator + moduleName + "Jni.cpp"; |
|
92 |
} |
|
93 |
if (template.equals("Jni.h.vm")) { |
|
94 |
return pyModule.replace(".", File.separator) + File.separator + MdkConstant.JNI + File.separator + moduleName + "Jni.h"; |
|
95 |
} |
|
96 |
return null; |
|
97 |
} |
|
98 |
|
|
99 |
/** |
|
100 |
* 渲染模板 |
|
101 |
**/ |
|
102 |
public static StringWriter drawTemplate(String template,Map<String, Object> map) { |
|
103 |
//模板数据 |
|
104 |
VelocityContext context = new VelocityContext(map); |
|
105 |
return drawTemplate(template,context); |
|
106 |
} |
|
107 |
|
|
108 |
public static StringWriter drawTemplate(String template,VelocityContext context) { |
|
109 |
Properties prop = new Properties(); |
|
110 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
111 |
Velocity.init(prop); |
|
112 |
|
|
113 |
StringWriter writer = new StringWriter(); |
|
114 |
Template tpl = Velocity.getTemplate("template/" + template, "UTF-8"); |
|
115 |
tpl.merge(context, writer); |
|
116 |
return writer; |
|
117 |
} |
|
118 |
|
|
119 |
public static void drawTemplate(String template,Map<String, Object> map,File toFile) throws IOException { |
|
120 |
VelocityContext context = new VelocityContext(map); |
|
121 |
drawTemplate(template,context,toFile); |
|
122 |
} |
|
123 |
|
|
124 |
public static void drawTemplate(String template,VelocityContext context,File toFile) throws IOException { |
|
125 |
StringWriter writer = drawTemplate(template,context); |
|
126 |
|
|
127 |
FileUtil.mkParentDirs(toFile); |
|
128 |
|
|
129 |
if (!toFile.exists()) { |
|
130 |
toFile.createNewFile(); |
|
131 |
} |
|
132 |
|
|
133 |
FileUtil.writeUtf8String(writer.toString(),toFile); |
f7932f
|
134 |
IOUtils.closeQuietly(writer); |
449017
|
135 |
} |
D |
136 |
} |