duhuizhe
2024-04-19 64c4fd3c7067c7626dc960a70b4bc2c3662bc653
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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();
 
}