【update】注释代码,重命名变量

This commit is contained in:
mzc5649
2021-04-02 17:12:03 +08:00
parent e2159df971
commit 3e53429faf
3 changed files with 84 additions and 58 deletions
@@ -23,7 +23,7 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class BaseCodeGenerate {
protected static String charcterSet = "UTF-8";
protected static String characterSet = "UTF-8";
protected List<String> list = new ArrayList();
public BaseCodeGenerate() {
@@ -75,22 +75,23 @@ public class BaseCodeGenerate {
if (createFileConfig.getStylePath() != null && !"".equals(createFileConfig.getStylePath()) && !templateFilePath.replace(File.separator, ".").startsWith(createFileConfig.getStylePath())) {
return;
}
String var7 = a(configMap, templateFilePath, createFileConfig);
log.debug("-------outputFilepath--" + var7);
String var8;
if (var7.startsWith("java")) {
var8 = projectPath + File.separator + com.jero.codegenerate.properties.CodeConfigProperties.h.replace(".", File.separator);
var7 = var7.substring("java".length());
var7 = var8 + var7;
log.debug("-------java----outputFilepath--" + var7);
this.a(templateFilePath, var7, configMap, createFileConfig);
} else if (var7.startsWith("webapp")) {
var8 = projectPath + File.separator + com.jero.codegenerate.properties.CodeConfigProperties.i.replace(".", File.separator);
var7 = var7.substring("webapp".length());
var7 = var8 + var7;
log.debug("-------webapp---outputFilepath---" + var7);
this.a(templateFilePath, var7, configMap, createFileConfig);
//模板文件目录
String outputFilepath = getTemplatePath(configMap, templateFilePath, createFileConfig);
log.debug("-------outputFilepath--" + outputFilepath);
String packageDir;
if (outputFilepath.startsWith("java")) {
// 生成目录位置
packageDir = projectPath + File.separator + com.jero.codegenerate.properties.CodeConfigProperties.sourceRootPackage.replace(".", File.separator);
outputFilepath = outputFilepath.substring("java".length());
outputFilepath = packageDir + outputFilepath;
log.debug("-------java----outputFilepath--" + outputFilepath);
this.a(templateFilePath, outputFilepath, configMap, createFileConfig);
} else if (outputFilepath.startsWith("webapp")) {
packageDir = projectPath + File.separator + com.jero.codegenerate.properties.CodeConfigProperties.i.replace(".", File.separator);
outputFilepath = outputFilepath.substring("webapp".length());
outputFilepath = packageDir + outputFilepath;
log.debug("-------webapp---outputFilepath---" + outputFilepath);
this.a(templateFilePath, outputFilepath, configMap, createFileConfig);
}
} catch (Exception 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 {
if (var2.endsWith("i")) {
var2 = var2.substring(0, var2.length() - 1);
protected void a(String templateFilePath, String outputFilepath, Map<String, Object> configMap, CreateFileConfig createFileConfig) throws Exception {
if (outputFilepath.endsWith("i")) {
//将末尾的'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);
var5.setOutputEncoding(charcterSet);
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#");
if (this.a(file)) {
this.a(file, "#segment#");
}
}
protected Template a(String var1, CreateFileConfig var2) throws IOException {
return FreemarkerHelper.getConfiguration(var2.listTemplateRootDirs(), charcterSet, var1).getTemplate(var1);
/**
* 根据templateFilePath获得对应的模板类
* @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) {
@@ -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;
boolean flag = true;
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);
String var11 = createFileConfig.getStylePath();
if (var11 != null && var11 != "") {
templateFilePath1 = templateFilePath1.substring(var11.length() + 1);
String stylePath = createFileConfig.getStylePath();
if (stylePath != null && stylePath != "") {
templateFilePath1 = templateFilePath1.substring(stylePath.length() + 1);
}
String var7 = templateFilePath1.substring(templateFilePath1.lastIndexOf("."));
String var8 = templateFilePath1.substring(0, templateFilePath1.lastIndexOf(".")).replace(".", File.separator);
templateFilePath1 = var8 + var7;
// 后缀名
String suffix = templateFilePath1.substring(templateFilePath1.lastIndexOf("."));
String path = templateFilePath1.substring(0, templateFilePath1.lastIndexOf(".")).replace(".", File.separator);
templateFilePath1 = path + suffix;
return templateFilePath1;
}
@@ -28,11 +28,11 @@ public class FreemarkerHelper {
* 获取配置类
* @date 2021/4/2 15:47
* @param templateRootDirs 模板根目录集合
* @param charcterSet 字符集
* @param characterSet 字符集
* @param separator 分隔符
* @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();
log.debug(" FileTemplateLoader[] size " + templateRootDirs.size());
log.debug(" templateRootDirs templateName " + separator);
@@ -48,7 +48,7 @@ public class FreemarkerHelper {
configuration.setTemplateLoader(multiTemplateLoader);
configuration.setNumberFormat("###############");
configuration.setBooleanFormat("true,false");
configuration.setDefaultEncoding(charcterSet);
configuration.setDefaultEncoding(characterSet);
return configuration;
}
@@ -83,7 +83,7 @@ public class FreemarkerHelper {
}
}
/**
* 获取填充好的模板字符串
* 使用配置对象(map),对模板路径字符串进行填充并返回
* @date 2021/4/2 16:02
* @param templateFilePath
* @param configMap
@@ -103,11 +103,20 @@ public class FreemarkerHelper {
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));
var1.put("Format", new SimpleFormat());
var0.process(var1, var4);
var4.close();
/**
* 对根据模板生成的文件进行数据填充
* @date 2021/4/2 17:04
* @param template 模板
* @param configMap 数据配置
* @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();
}
}
@@ -28,7 +28,7 @@ public class CodeConfigProperties {
public static String password;
public static String projectPath;
public static String packageName;
public static String h;
public static String sourceRootPackage;
public static String i;
public static String templateUrl;
public static boolean dbFiledConvert;
@@ -98,7 +98,7 @@ public class CodeConfigProperties {
return configDatasourcePropties.getString("template_path");
}
public static final String g() {
public static final String getSourceRootPackage() {
return configDatasourcePropties.getString("source_root_package");
}
@@ -155,7 +155,7 @@ public class CodeConfigProperties {
password = "root";
projectPath = "c:/workspace/jero";
packageName = "com.jero";
h = "src";
sourceRootPackage = "src";
i = "WebRoot";
templateUrl = "/jero/code-template/";
dbFiledConvert = true;
@@ -166,7 +166,7 @@ public class CodeConfigProperties {
databaseUrl = getDatabaseUrl();
username = c();
password = d();
h = g();
sourceRootPackage = getSourceRootPackage();
i = h();
packageName = o();
templateUrl = getTemplateUrl();
@@ -191,7 +191,7 @@ public class CodeConfigProperties {
databaseType = "mysql";
}
h = h.replace(".", "/");
sourceRootPackage = sourceRootPackage.replace(".", "/");
i = i.replace(".", "/");
}
}