package com.yqzx.generator.engine.base;
|
|
import cn.hutool.core.io.FileUtil;
|
//import com.sun.javafx.projectUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.beetl.core.Configuration;
|
import org.beetl.core.GroupTemplate;
|
import org.beetl.core.Template;
|
import org.beetl.core.resource.ClasspathResourceLoader;
|
|
import java.io.File;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.Properties;
|
|
/**
|
* @description: ADI项目模板生成 引擎
|
* @author: chaoyapeng
|
* @time: 2020/8/18 16:03
|
*/
|
@Slf4j
|
public abstract class AbstractTemplateEngine extends BaseTemplateEngine {
|
|
private GroupTemplate groupTemplate;
|
|
public AbstractTemplateEngine() {
|
initBeetlEngine();
|
}
|
|
protected void initBeetlEngine() {
|
Properties properties = new Properties();
|
properties.put("RESOURCE.root", "");
|
properties.put("DELIMITER_STATEMENT_START", "<%");
|
properties.put("DELIMITER_STATEMENT_END", "%>");
|
properties.put("HTML_TAG_FLAG", "##");
|
Configuration cfg = null;
|
try {
|
cfg = new Configuration(properties);
|
} catch (IOException e) {
|
log.error(e.getMessage());
|
}
|
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
|
groupTemplate = new GroupTemplate(resourceLoader, cfg);
|
}
|
|
protected void configTemplate(Template template) {
|
template.binding("context", super.contextConfig);
|
template.binding("controller", super.controllerConfig);
|
template.binding("service", super.serviceConfig);
|
template.binding("mapper", super.mapperConfig);
|
template.binding("model", super.modelConfig);
|
template.binding("query", super.queryConfig);
|
template.binding("tableInfo", super.tableInfo);
|
template.binding("columnStr", super.columnStr);
|
// 绑定查询参数列表
|
template.binding("queryTableInfo",super.queryTableInfo);
|
}
|
|
protected void generateFile(String template, String filePath) {
|
Template pageTemplate = groupTemplate.getTemplate(template);
|
configTemplate(pageTemplate);
|
filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
|
//if (projectUtil.isWindows()) {
|
// filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
|
//} else {
|
// filePath = filePath.replaceAll("/+|\\\\+", "/");
|
//}
|
// File file = new File(filePath);
|
File file = FileUtil.touch(filePath);
|
|
File parentFile = file.getParentFile();
|
if (!parentFile.exists()) {
|
parentFile.mkdirs();
|
}
|
FileOutputStream fileOutputStream = null;
|
try {
|
fileOutputStream = new FileOutputStream(file);
|
pageTemplate.renderTo(fileOutputStream);
|
} catch (FileNotFoundException e) {
|
log.error(e.getMessage());
|
} finally {
|
try {
|
fileOutputStream.close();
|
} catch (IOException e) {
|
log.error(e.getMessage());
|
}
|
}
|
}
|
|
public void start() {
|
//配置之间的相互依赖
|
super.initConfig();
|
|
//生成模板
|
if (super.contextConfig.getControllerSwitch()) {
|
generateController();
|
}
|
if (super.contextConfig.getServiceSwitch()) {
|
generateService();
|
}
|
if (super.contextConfig.getMapperSwitch()) {
|
generateMapper();
|
}
|
if (super.contextConfig.getModelSwitch()) {
|
generateModel();
|
}
|
if (super.contextConfig.getQuerySwitch()) {
|
generateQuery();
|
}
|
}
|
|
|
/**
|
* 生成Controller
|
*/
|
protected abstract void generateController();
|
|
/**
|
* 生成Service
|
*/
|
protected abstract void generateService();
|
|
/**
|
* 生成Mapper
|
*/
|
protected abstract void generateMapper();
|
|
/**
|
* 生成Model
|
*/
|
protected abstract void generateModel();
|
|
/**
|
* 生成Query
|
*/
|
protected abstract void generateQuery();
|
|
}
|