【update】注释代码,重命名变量
This commit is contained in:
+60
-43
@@ -23,7 +23,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class BaseCodeGenerate {
|
public class BaseCodeGenerate {
|
||||||
|
|
||||||
protected static String charcterSet = "UTF-8";
|
protected static String characterSet = "UTF-8";
|
||||||
protected List<String> list = new ArrayList();
|
protected List<String> list = new ArrayList();
|
||||||
|
|
||||||
public BaseCodeGenerate() {
|
public BaseCodeGenerate() {
|
||||||
@@ -75,22 +75,23 @@ public class BaseCodeGenerate {
|
|||||||
if (createFileConfig.getStylePath() != null && !"".equals(createFileConfig.getStylePath()) && !templateFilePath.replace(File.separator, ".").startsWith(createFileConfig.getStylePath())) {
|
if (createFileConfig.getStylePath() != null && !"".equals(createFileConfig.getStylePath()) && !templateFilePath.replace(File.separator, ".").startsWith(createFileConfig.getStylePath())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
//模板文件目录
|
||||||
String var7 = a(configMap, templateFilePath, createFileConfig);
|
String outputFilepath = getTemplatePath(configMap, templateFilePath, createFileConfig);
|
||||||
log.debug("-------outputFilepath--" + var7);
|
log.debug("-------outputFilepath--" + outputFilepath);
|
||||||
String var8;
|
String packageDir;
|
||||||
if (var7.startsWith("java")) {
|
if (outputFilepath.startsWith("java")) {
|
||||||
var8 = projectPath + File.separator + com.jero.codegenerate.properties.CodeConfigProperties.h.replace(".", File.separator);
|
// 生成目录位置
|
||||||
var7 = var7.substring("java".length());
|
packageDir = projectPath + File.separator + com.jero.codegenerate.properties.CodeConfigProperties.sourceRootPackage.replace(".", File.separator);
|
||||||
var7 = var8 + var7;
|
outputFilepath = outputFilepath.substring("java".length());
|
||||||
log.debug("-------java----outputFilepath--" + var7);
|
outputFilepath = packageDir + outputFilepath;
|
||||||
this.a(templateFilePath, var7, configMap, createFileConfig);
|
log.debug("-------java----outputFilepath--" + outputFilepath);
|
||||||
} else if (var7.startsWith("webapp")) {
|
this.a(templateFilePath, outputFilepath, configMap, createFileConfig);
|
||||||
var8 = projectPath + File.separator + com.jero.codegenerate.properties.CodeConfigProperties.i.replace(".", File.separator);
|
} else if (outputFilepath.startsWith("webapp")) {
|
||||||
var7 = var7.substring("webapp".length());
|
packageDir = projectPath + File.separator + com.jero.codegenerate.properties.CodeConfigProperties.i.replace(".", File.separator);
|
||||||
var7 = var8 + var7;
|
outputFilepath = outputFilepath.substring("webapp".length());
|
||||||
log.debug("-------webapp---outputFilepath---" + var7);
|
outputFilepath = packageDir + outputFilepath;
|
||||||
this.a(templateFilePath, var7, configMap, createFileConfig);
|
log.debug("-------webapp---outputFilepath---" + outputFilepath);
|
||||||
|
this.a(templateFilePath, outputFilepath, configMap, createFileConfig);
|
||||||
}
|
}
|
||||||
} catch (Exception var10) {
|
} catch (Exception var10) {
|
||||||
log.error(var10.toString(), var10);
|
log.error(var10.toString(), var10);
|
||||||
@@ -98,28 +99,36 @@ public class BaseCodeGenerate {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void a(String var1, String var2, Map<String, Object> var3, CreateFileConfig var4) throws Exception {
|
protected void a(String templateFilePath, String outputFilepath, Map<String, Object> configMap, CreateFileConfig createFileConfig) throws Exception {
|
||||||
if (var2.endsWith("i")) {
|
if (outputFilepath.endsWith("i")) {
|
||||||
var2 = var2.substring(0, var2.length() - 1);
|
//将末尾的'i'去除
|
||||||
|
outputFilepath = outputFilepath.substring(0, outputFilepath.length() - 1);
|
||||||
|
}
|
||||||
|
// 根据模板文件路径获取指定模板
|
||||||
|
Template template = this.getTemplate(templateFilePath, createFileConfig);
|
||||||
|
template.setOutputEncoding(characterSet);
|
||||||
|
// 生成文件
|
||||||
|
File file = FileHelper.createFile(outputFilepath);
|
||||||
|
log.info("[generate]\t template:" + templateFilePath + " ==> " + outputFilepath);
|
||||||
|
FreemarkerHelper.fillDataIntoFileByTemplate(template, configMap, file, characterSet);
|
||||||
|
if (!this.a(file)) {
|
||||||
|
this.list.add("生成成功:" + outputFilepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
Template var5 = this.a(var1, var4);
|
if (this.a(file)) {
|
||||||
var5.setOutputEncoding(charcterSet);
|
this.a(file, "#segment#");
|
||||||
File var6 = FileHelper.createFile(var2);
|
|
||||||
log.info("[generate]\t template:" + var1 + " ==> " + var2);
|
|
||||||
FreemarkerHelper.a(var5, var3, var6, charcterSet);
|
|
||||||
if (!this.a(var6)) {
|
|
||||||
this.list.add("生成成功:" + var2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.a(var6)) {
|
|
||||||
this.a(var6, "#segment#");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
protected Template a(String var1, CreateFileConfig var2) throws IOException {
|
* 根据templateFilePath获得对应的模板类
|
||||||
return FreemarkerHelper.getConfiguration(var2.listTemplateRootDirs(), charcterSet, var1).getTemplate(var1);
|
* @date 2021/4/2 16:54
|
||||||
|
* @param templateFilePath 目标模板路径
|
||||||
|
* @param createFileConfig 生成文件配置
|
||||||
|
* @return freemarker.template.Template
|
||||||
|
*/
|
||||||
|
protected Template getTemplate(String templateFilePath, CreateFileConfig createFileConfig) throws IOException {
|
||||||
|
return FreemarkerHelper.getConfiguration(createFileConfig.listTemplateRootDirs(), characterSet, templateFilePath).getTemplate(templateFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean a(File var1) {
|
protected boolean a(File var1) {
|
||||||
@@ -269,7 +278,15 @@ public class BaseCodeGenerate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
protected static String a(Map<String, Object> configMap, String templateFilePath, CreateFileConfig createFileConfig) throws Exception {
|
/**
|
||||||
|
* 获取模板文件路径相对于模板根目录路径的路径
|
||||||
|
* @date 2021/4/2 16:28
|
||||||
|
* @param configMap 实体配置
|
||||||
|
* @param templateFilePath 模板文件路径
|
||||||
|
* @param createFileConfig 生成文件配置
|
||||||
|
* @return java.lang.String
|
||||||
|
*/
|
||||||
|
protected static String getTemplatePath(Map<String, Object> configMap, String templateFilePath, CreateFileConfig createFileConfig) throws Exception {
|
||||||
String templateFilePath1 = templateFilePath;
|
String templateFilePath1 = templateFilePath;
|
||||||
boolean flag = true;
|
boolean flag = true;
|
||||||
int i;
|
int i;
|
||||||
@@ -290,16 +307,16 @@ public class BaseCodeGenerate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Configuration configuration = FreemarkerHelper.getConfiguration(createFileConfig.listTemplateRootDirs(), charcterSet, "/");
|
Configuration configuration = FreemarkerHelper.getConfiguration(createFileConfig.listTemplateRootDirs(), characterSet, "/");
|
||||||
templateFilePath1 = FreemarkerHelper.getFillTemplateString(templateFilePath1, configMap, configuration);
|
templateFilePath1 = FreemarkerHelper.getFillTemplateString(templateFilePath1, configMap, configuration);
|
||||||
String var11 = createFileConfig.getStylePath();
|
String stylePath = createFileConfig.getStylePath();
|
||||||
if (var11 != null && var11 != "") {
|
if (stylePath != null && stylePath != "") {
|
||||||
templateFilePath1 = templateFilePath1.substring(var11.length() + 1);
|
templateFilePath1 = templateFilePath1.substring(stylePath.length() + 1);
|
||||||
}
|
}
|
||||||
|
// 后缀名
|
||||||
String var7 = templateFilePath1.substring(templateFilePath1.lastIndexOf("."));
|
String suffix = templateFilePath1.substring(templateFilePath1.lastIndexOf("."));
|
||||||
String var8 = templateFilePath1.substring(0, templateFilePath1.lastIndexOf(".")).replace(".", File.separator);
|
String path = templateFilePath1.substring(0, templateFilePath1.lastIndexOf(".")).replace(".", File.separator);
|
||||||
templateFilePath1 = var8 + var7;
|
templateFilePath1 = path + suffix;
|
||||||
return templateFilePath1;
|
return templateFilePath1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-10
@@ -28,11 +28,11 @@ public class FreemarkerHelper {
|
|||||||
* 获取配置类
|
* 获取配置类
|
||||||
* @date 2021/4/2 15:47
|
* @date 2021/4/2 15:47
|
||||||
* @param templateRootDirs 模板根目录集合
|
* @param templateRootDirs 模板根目录集合
|
||||||
* @param charcterSet 字符集
|
* @param characterSet 字符集
|
||||||
* @param separator 分隔符
|
* @param separator 分隔符
|
||||||
* @return freemarker.template.Configuration
|
* @return freemarker.template.Configuration
|
||||||
*/
|
*/
|
||||||
public static Configuration getConfiguration(List<File> templateRootDirs, String charcterSet, String separator) throws IOException {
|
public static Configuration getConfiguration(List<File> templateRootDirs, String characterSet, String separator) throws IOException {
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
log.debug(" FileTemplateLoader[] size " + templateRootDirs.size());
|
log.debug(" FileTemplateLoader[] size " + templateRootDirs.size());
|
||||||
log.debug(" templateRootDirs templateName " + separator);
|
log.debug(" templateRootDirs templateName " + separator);
|
||||||
@@ -48,7 +48,7 @@ public class FreemarkerHelper {
|
|||||||
configuration.setTemplateLoader(multiTemplateLoader);
|
configuration.setTemplateLoader(multiTemplateLoader);
|
||||||
configuration.setNumberFormat("###############");
|
configuration.setNumberFormat("###############");
|
||||||
configuration.setBooleanFormat("true,false");
|
configuration.setBooleanFormat("true,false");
|
||||||
configuration.setDefaultEncoding(charcterSet);
|
configuration.setDefaultEncoding(characterSet);
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ public class FreemarkerHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 获取填充好的模板字符串
|
* 使用配置对象(map),对模板路径字符串进行填充并返回
|
||||||
* @date 2021/4/2 16:02
|
* @date 2021/4/2 16:02
|
||||||
* @param templateFilePath
|
* @param templateFilePath
|
||||||
* @param configMap
|
* @param configMap
|
||||||
@@ -103,11 +103,20 @@ public class FreemarkerHelper {
|
|||||||
throw new IllegalStateException("cannot process templateString:" + templateFilePath + " cause:" + e, e);
|
throw new IllegalStateException("cannot process templateString:" + templateFilePath + " cause:" + e, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
public static void a(Template var0, Map<String, Object> var1, File var2, String var3) throws IOException, TemplateException {
|
* 对根据模板生成的文件进行数据填充
|
||||||
BufferedWriter var4 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(var2), var3));
|
* @date 2021/4/2 17:04
|
||||||
var1.put("Format", new SimpleFormat());
|
* @param template 模板
|
||||||
var0.process(var1, var4);
|
* @param configMap 数据配置
|
||||||
var4.close();
|
* @param file 输出文件
|
||||||
|
* @param characterSet 字符集
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static void fillDataIntoFileByTemplate(Template template, Map<String, Object> configMap, File file, String characterSet) throws IOException, TemplateException {
|
||||||
|
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), characterSet));
|
||||||
|
configMap.put("Format", new SimpleFormat());
|
||||||
|
//填充文件
|
||||||
|
template.process(configMap, bufferedWriter);
|
||||||
|
bufferedWriter.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -28,7 +28,7 @@ public class CodeConfigProperties {
|
|||||||
public static String password;
|
public static String password;
|
||||||
public static String projectPath;
|
public static String projectPath;
|
||||||
public static String packageName;
|
public static String packageName;
|
||||||
public static String h;
|
public static String sourceRootPackage;
|
||||||
public static String i;
|
public static String i;
|
||||||
public static String templateUrl;
|
public static String templateUrl;
|
||||||
public static boolean dbFiledConvert;
|
public static boolean dbFiledConvert;
|
||||||
@@ -98,7 +98,7 @@ public class CodeConfigProperties {
|
|||||||
return configDatasourcePropties.getString("template_path");
|
return configDatasourcePropties.getString("template_path");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final String g() {
|
public static final String getSourceRootPackage() {
|
||||||
return configDatasourcePropties.getString("source_root_package");
|
return configDatasourcePropties.getString("source_root_package");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,7 +155,7 @@ public class CodeConfigProperties {
|
|||||||
password = "root";
|
password = "root";
|
||||||
projectPath = "c:/workspace/jero";
|
projectPath = "c:/workspace/jero";
|
||||||
packageName = "com.jero";
|
packageName = "com.jero";
|
||||||
h = "src";
|
sourceRootPackage = "src";
|
||||||
i = "WebRoot";
|
i = "WebRoot";
|
||||||
templateUrl = "/jero/code-template/";
|
templateUrl = "/jero/code-template/";
|
||||||
dbFiledConvert = true;
|
dbFiledConvert = true;
|
||||||
@@ -166,7 +166,7 @@ public class CodeConfigProperties {
|
|||||||
databaseUrl = getDatabaseUrl();
|
databaseUrl = getDatabaseUrl();
|
||||||
username = c();
|
username = c();
|
||||||
password = d();
|
password = d();
|
||||||
h = g();
|
sourceRootPackage = getSourceRootPackage();
|
||||||
i = h();
|
i = h();
|
||||||
packageName = o();
|
packageName = o();
|
||||||
templateUrl = getTemplateUrl();
|
templateUrl = getTemplateUrl();
|
||||||
@@ -191,7 +191,7 @@ public class CodeConfigProperties {
|
|||||||
databaseType = "mysql";
|
databaseType = "mysql";
|
||||||
}
|
}
|
||||||
|
|
||||||
h = h.replace(".", "/");
|
sourceRootPackage = sourceRootPackage.replace(".", "/");
|
||||||
i = i.replace(".", "/");
|
i = i.replace(".", "/");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user