admin
2021-11-13 5ac194b4319c4f1e1e0e167243ffcc20d8924961
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
package com.ks.codegenerator.utils;
 
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.StringUtil;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * @author hxh
 * @title: ServiceUtil
 * @description: TODO
 * @date 2021/11/13 14:50
 */
public class ServiceBuilder {
 
 
    private String name;
    private String pks;
    private String subpks;
    private String cacheDir;
 
 
    public ServiceBuilder setName(String name) {
        this.name = name;
        return this;
    }
 
    public ServiceBuilder setPks(String pks) {
        this.pks = pks;
        return this;
    }
 
    public ServiceBuilder setSubpks(String subpks) {
        this.subpks = subpks;
        return this;
    }
 
    public ServiceBuilder setCacheDir(String cacheDir) {
        this.cacheDir = cacheDir;
        return this;
    }
 
    /**
     * @return void
     * @author hxh
     * @description 生成服务
     * @date 14:51 2021/11/13
     **/
    public String build() throws Exception {
        String destPath = String.format("%s/%s/%s", cacheDir, System.currentTimeMillis() + "", name);
        System.out.println(destPath);
        //copy文件夹
        FileUtils.copyFileDir(ServiceBuilder.class.getClassLoader().getResource("code/service").getPath(), destPath);
        //删除target
        if (new File(destPath + "/target").exists()) {
            FileUtil.deleteFileDir(new File(destPath + "/target"));
        }
        if (new File(destPath + "/app/target").exists()) {
            FileUtil.deleteFileDir(new File(destPath + "/app/target"));
        }
 
        //修改文件名称与文件夹名称
        renameDir(destPath);
        //重新命名包名
        renamePackage(destPath + "/app/src/main/java/com/ks/app");
        replaceSettings(destPath);
        //压缩文件夹
        ZipUtil.compressToZip(destPath, new File(destPath).getParent(), new File(destPath).getName() + ".zip");
        //删除原来的文件夹
        FileUtil.deleteFileDir(new File(destPath));
        return destPath;
    }
 
    private void replaceSettings(String path) throws Exception {
 
        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)));
 
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-dev.yml", "com.ks.app", subpks);
        FileUtils.replaceFileContent(path + "/app/src/main/resources/application-pro.yml", "com.ks.app", subpks);
    }
 
    //重新命名文件夹与文件
    private void renameDir(String path) throws Exception {
        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();
    }
 
 
    //重新命名包名
    private void renamePackage(String path) throws Exception {
        if (!new File(path).exists()) {
            throw new Exception("文件不存在");
        }
 
        if (new File(path).isDirectory()) {
            File[] fs = new File(path).listFiles();
            for (File f : fs) {
                if (f.isFile()) {
                    FileUtils.replaceFileContent(f.getPath(), "package com.ks.app", "package " + subpks);
                    FileUtils.replaceFileContent(f.getPath(), "import com.ks.app.", "import " + subpks + ".");
                } else {
                    renamePackage(f.getPath());
                }
            }
        }
    }
 
    private static String getGroupId(String pks) {
        String[] sts = pks.split("\\.");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < sts.length - 1; i++) {
            list.add(sts[i]);
        }
        return StringUtil.concat(list, ".");
    }
 
    private static String getArtifactId(String pks) {
        String[] sts = pks.split("\\.");
        return sts[sts.length - 1];
    }
 
    public static void main(String[] args) throws Exception {
        System.out.println(getGroupId("com.ks.app"));
    }
 
 
}