admin
2022-03-31 36754ba47da7a3277d5be183a523c912a1dc4cef
src/main/java/com/ks/codegenerator/utils/ServiceBuilder.java
@@ -1,12 +1,19 @@
package com.ks.codegenerator.utils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.StringUtil;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@@ -22,6 +29,11 @@
    private String pks;
    private String subpks;
    private String cacheDir;
    private String projectChineseName;
    private String sqlTablePrefix;
    private String sqlDatabaseName;
    private String mongoDatabaseName;
    public ServiceBuilder setName(String name) {
@@ -41,6 +53,26 @@
    public ServiceBuilder setCacheDir(String cacheDir) {
        this.cacheDir = cacheDir;
        return this;
    }
    public ServiceBuilder setProjectChineseName(String projectChineseName) {
        this.projectChineseName = projectChineseName;
        return this;
    }
    public ServiceBuilder setSqlTablePrefix(String sqlTablePrefix) {
        this.sqlTablePrefix = sqlTablePrefix;
        return this;
    }
    public ServiceBuilder setSqlDatabaseName(String sqlDatabaseName) {
        this.sqlDatabaseName = sqlDatabaseName;
        return this;
    }
    public ServiceBuilder setMongoDatabaseName(String mongoDatabaseName) {
        this.mongoDatabaseName = mongoDatabaseName;
        return this;
    }
@@ -66,13 +98,14 @@
        //修改文件名称与文件夹名称
        renameDir(destPath);
        //重新命名包名
        renamePackage(destPath + "/app/src/main/java/com/ks/app");
        renamePackage(destPath + "/app/src/main/java/" + subpks.replace(".", "/"));
        replaceSettings(destPath);
        //压缩文件夹
        ZipUtil.compressToZip(destPath, new File(destPath).getParent(), new File(destPath).getName() + ".zip");
        File zip = new File(new File(destPath).getParent(), new File(destPath).getName() + ".zip");
        ZipUtil.compressToZip(destPath, zip.getParent(), zip.getName());
        //删除原来的文件夹
        FileUtil.deleteFileDir(new File(destPath));
        return destPath;
        return zip.getAbsolutePath();
    }
    private void replaceSettings(String path) throws Exception {
@@ -80,11 +113,62 @@
        FileUtils.replaceFileContent(path + "/pom.xml", "<groupId>com.ks</groupId>", String.format("<groupId>%s</groupId>", getGroupId(pks)));
        FileUtils.replaceFileContent(path + "/pom.xml", "<artifactId>demo</artifactId>", String.format("<artifactId>%s</artifactId>", getArtifactId(pks)));
        FileUtils.replaceFileContent(path + "/app/pom.xml", "<groupId>com.ks</groupId>", String.format("<groupId>%s</groupId>", getGroupId(subpks)));
        FileUtils.replaceFileContent(path + "/app/pom.xml", "<artifactId>demo</artifactId>", String.format("<artifactId>%s</artifactId>", getArtifactId(subpks)));
        //更改parent以及artifactId
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(path + "/app/pom.xml");
        NodeList list = doc.getElementsByTagName("project").item(0).getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i).getNodeName().equalsIgnoreCase("parent")) {
                NodeList parent = list.item(i).getChildNodes();
                for (int j = 0; j < parent.getLength(); j++) {
                    Node node = parent.item(j);
                    switch (node.getNodeName()) {
                        case "artifactId":
                            node.setTextContent(getArtifactId(pks));
                            break;
                        case "groupId":
                            node.setTextContent(getGroupId(pks));
                            break;
                    }
                }
            } else if (list.item(i).getNodeName().equalsIgnoreCase("artifactId")) {
                list.item(i).setTextContent(getArtifactId(subpks));
            }
        }
        //保存
        Transformer ts = TransformerFactory.newInstance().newTransformer();
        ts.transform(new DOMSource(doc), new StreamResult(path + "/app/pom.xml"));
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-dev.yml", "com.ks.app", subpks);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-dev.yml", "${mongoDataBaseName}", mongoDatabaseName);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-dev.yml", "${sqlDataBaseName}", sqlDatabaseName);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-dev.yml", "${projectName}", name);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-pro.yml", "com.ks.app", subpks);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-pro.yml", "${mongoDataBaseName}", mongoDatabaseName);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-pro.yml", "${sqlDataBaseName}", sqlDatabaseName);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-pro.yml", "${projectName}", name);
        //替换Mapper下的表前缀
        String mapperDir = path + "/app/src/main/resources/mapper";
        File mapperFiles = new File(mapperDir);
        for (File f : mapperFiles.listFiles()) {
            FileUtils.replaceFileContent(f.getAbsolutePath(), "com.ks.app", subpks);
            FileUtils.replaceFileContent(f.getAbsolutePath(), "lt_", sqlTablePrefix);
        }
        FileUtils.replaceFileContent(path + "/app/src/main/resources/mysql.sql", "${sqlDataBaseName}", sqlDatabaseName);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/mysql.sql", "${sqlTablePrefix}", sqlTablePrefix);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/static/index.html", "${projectChineseName}", projectChineseName);
        FileUtils.replaceFileContent(path + "/app/src/main/java/"+subpks.replace(".","/")+"/aop/AdminApiFilter.java", "com.ks.app", subpks);
        FileUtils.replaceFileContent(path + "/app/src/main/java/"+subpks.replace(".","/")+"/aop/SignValidate.java", "com.ks.app", subpks);
        FileUtils.replaceFileContent(path + "/app/src/main/java/"+subpks.replace(".","/")+"/aop/UserLoginValid.java", "com.ks.app", subpks);
        FileUtils.replaceFileContent(path + "/app/src/main/java/"+subpks.replace(".","/")+"/Application.java", "com.ks.app", subpks);
    }
    //重新命名文件夹与文件
@@ -92,7 +176,7 @@
        FileUtils.renameFile(path + "/service.iml", name + ".iml");
        //更改java文件的路径
        FileUtils.copyFileDir(path + "/app/src/main/java/com/ks/app", path + "/app/src/main/java/" + subpks.replace(".", "/"));
        new File(path + "/app/src/main/java/com/ks/app").delete();
        FileUtil.deleteFileDir(new File(path + "/app/src/main/java/com/ks/app"));
    }