新增: 基于配置生成文件名
parent
f4e9276a83
commit
c5e34d2f54
@ -0,0 +1,66 @@
|
||||
package space.caoshd.otone.builder;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import space.caoshd.otone.entity.OutputInfo;
|
||||
import space.caoshd.otone.entity.TableInfo;
|
||||
import space.caoshd.otone.tool.PropTools;
|
||||
import space.caoshd.otone.util.CfgConsts;
|
||||
import space.caoshd.otone.util.FileUtils;
|
||||
import space.caoshd.otone.util.PathConsts;
|
||||
import space.caoshd.otone.util.VelocityUtils;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class OutputBuilder {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(OutputBuilder.class);
|
||||
|
||||
private static final PropTools CONFIG = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
|
||||
|
||||
private final OutputInfo outputInfo;
|
||||
|
||||
public OutputBuilder(OutputInfo outputInfo) {
|
||||
this.outputInfo = outputInfo;
|
||||
}
|
||||
|
||||
public void build() {
|
||||
// 循环表信息 创建代码文件
|
||||
for (TableInfo tableInfo : outputInfo.getTableInfoList()) {
|
||||
// 创建数据文件夹
|
||||
FileUtils.mkdir(outputInfo.getPackagePath());
|
||||
// 获取输出文件路径
|
||||
String outputPath = getOutputPath(tableInfo.getBeanName());
|
||||
// 获取模板文件路径
|
||||
String templatePath = outputInfo.getTemplatePath();
|
||||
// 判断输出文件是否已存在
|
||||
if (!FileUtils.exists(outputPath)) {
|
||||
// 输出文件不存在 直接生成
|
||||
VelocityUtils.render(CfgConsts.VALUE_TABLE, tableInfo, templatePath, outputPath);
|
||||
}
|
||||
// 输出文件存在 判断是否可以覆盖
|
||||
if (CONFIG.getBoolean(CfgConsts.OUTPUT_COVER_IF_EXIST)) {
|
||||
// 配置可以执行覆盖 覆盖生成
|
||||
VelocityUtils.render(CfgConsts.VALUE_TABLE, tableInfo, templatePath, outputPath);
|
||||
} else {
|
||||
// 配置不可以执行覆盖 提示错误消息
|
||||
LOGGER.warn(
|
||||
"output file already exists: {}, "
|
||||
+ "please remove it or enable auto cover exist file,"
|
||||
+ " config.properties should like [output.cover_if_exists=true]",
|
||||
outputPath
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getOutputPath(String beanName) {
|
||||
String filename = getOutputFilename(beanName);
|
||||
return Paths.get(outputInfo.getPackagePath(), filename).toString();
|
||||
}
|
||||
|
||||
private String getOutputFilename(String beanName) {
|
||||
return beanName + outputInfo.getNameSuffix() + ".java";
|
||||
}
|
||||
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package space.caoshd.otone.builder;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import space.caoshd.otone.entity.TableInfo;
|
||||
import space.caoshd.otone.tool.PropTools;
|
||||
import space.caoshd.otone.util.CfgConsts;
|
||||
import space.caoshd.otone.util.FileUtils;
|
||||
import space.caoshd.otone.util.PathConsts;
|
||||
import space.caoshd.otone.util.PathUtils;
|
||||
import space.caoshd.otone.util.VelocityUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PoBuilder {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PoBuilder.class);
|
||||
|
||||
private static final PropTools CONFIG = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
|
||||
|
||||
private final List<TableInfo> tableInfoList;
|
||||
|
||||
private String templatePath = PathConsts.PO_TEMPLATE_PATH;
|
||||
|
||||
public PoBuilder(List<TableInfo> tableInfoList) {
|
||||
this.tableInfoList = tableInfoList;
|
||||
}
|
||||
|
||||
public PoBuilder setTemplatePath(String templatePath) {
|
||||
this.templatePath = templatePath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void build() {
|
||||
for (TableInfo tableInfo : tableInfoList) {
|
||||
String beanName = tableInfo.getBeanName();
|
||||
String packagePath = PathUtils.getPoPackagePath();
|
||||
FileUtils.mkdir(packagePath);
|
||||
String outputPath = PathUtils.getPoJavaPath(beanName);
|
||||
|
||||
if (FileUtils.exists(outputPath)) {
|
||||
if (CONFIG.getBoolean(CfgConsts.OUTPUT_COVER_IF_EXIST)) {
|
||||
VelocityUtils.render(
|
||||
CfgConsts.VALUE_TABLE,
|
||||
tableInfo,
|
||||
templatePath,
|
||||
outputPath
|
||||
);
|
||||
} else {
|
||||
LOGGER.warn(
|
||||
"po file already exists: {}, "
|
||||
+ "please remove it or enable auto cover exist file,"
|
||||
+ " config.properties should like [output.cover_if_exists=true]",
|
||||
outputPath
|
||||
);
|
||||
}
|
||||
} else {
|
||||
VelocityUtils.render(CfgConsts.VALUE_TABLE, tableInfo, templatePath, outputPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package space.caoshd.otone.entity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OutputInfo {
|
||||
|
||||
private List<TableInfo> tableInfoList;
|
||||
|
||||
private String templatePath;
|
||||
|
||||
private String packagePath;
|
||||
|
||||
private String nameSuffix;
|
||||
|
||||
public List<TableInfo> getTableInfoList() {
|
||||
return tableInfoList;
|
||||
}
|
||||
|
||||
public void setTableInfoList(List<TableInfo> tableInfoList) {
|
||||
this.tableInfoList = tableInfoList;
|
||||
}
|
||||
|
||||
public String getTemplatePath() {
|
||||
return templatePath;
|
||||
}
|
||||
|
||||
public void setTemplatePath(String templatePath) {
|
||||
this.templatePath = templatePath;
|
||||
}
|
||||
|
||||
public String getPackagePath() {
|
||||
return packagePath;
|
||||
}
|
||||
|
||||
public void setPackagePath(String packagePath) {
|
||||
this.packagePath = packagePath;
|
||||
}
|
||||
|
||||
public String getNameSuffix() {
|
||||
return nameSuffix;
|
||||
}
|
||||
|
||||
public void setNameSuffix(String nameSuffix) {
|
||||
this.nameSuffix = nameSuffix;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package space.caoshd.otone.builder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import space.caoshd.otone.entity.OutputInfo;
|
||||
import space.caoshd.otone.entity.TableInfo;
|
||||
import space.caoshd.otone.util.PathConsts;
|
||||
import space.caoshd.otone.util.PathUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class OutputBuilderTest {
|
||||
|
||||
@Test
|
||||
void build() {
|
||||
String[] templates = {
|
||||
PathConsts.VIEW_TEMPLATE_PATH,
|
||||
PathConsts.FORM_TEMPLATE_PATH,
|
||||
PathConsts.QUERY_TEMPLATE_PATH,
|
||||
PathConsts.CONTROLLER_TEMPLATE_PATH,
|
||||
PathConsts.SERVICE_TEMPLATE_PATH,
|
||||
PathConsts.SERVICE_INTERFACE_TEMPLATE_PATH,
|
||||
PathConsts.MAPPER_TEMPLATE_PATH,
|
||||
PathConsts.MAPPER_XML_TEMPLATE_PATH,
|
||||
PathConsts.PO_TEMPLATE_PATH,
|
||||
};
|
||||
|
||||
TableBuilder tableBuilder = new TableBuilder();
|
||||
List<TableInfo> tableInfoList = tableBuilder.build();
|
||||
|
||||
// OutputInfo poOutputInfo = new OutputInfo();
|
||||
// poOutputInfo.setTableInfoList(tableInfoList);
|
||||
// poOutputInfo.setTemplatePath(PathConsts.PO_TEMPLATE_PATH);
|
||||
// poOutputInfo.setPackagePath(PathUtils.getPoPackagePath());
|
||||
// new OutputBuilder(poOutputInfo).build();
|
||||
|
||||
OutputInfo voOutputInfo = new OutputInfo();
|
||||
voOutputInfo.setTableInfoList(tableInfoList);
|
||||
voOutputInfo.setNameSuffix("View");
|
||||
voOutputInfo.setTemplatePath(PathConsts.VIEW_TEMPLATE_PATH);
|
||||
voOutputInfo.setPackagePath(PathUtils.getViewPackagePath());
|
||||
new OutputBuilder(voOutputInfo).build();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package space.caoshd.otone.builder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import space.caoshd.otone.entity.TableInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class PoBuilderTest {
|
||||
|
||||
@Test
|
||||
void build(){
|
||||
TableBuilder tableBuilder = new TableBuilder();
|
||||
List<TableInfo> tableInfoList = tableBuilder.build();
|
||||
PoBuilder poBuilder = new PoBuilder(tableInfoList);
|
||||
poBuilder.build();
|
||||
}
|
||||
|
||||
}
|
@ -1 +1 @@
|
||||
file.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
|
||||
resource.loader.file.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
|
@ -0,0 +1 @@
|
||||
${table.beanName}
|
Loading…
Reference in New Issue