admin
2022-01-07 60e97a582feba0526c64d823fcf74e1cb97fd4c1
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.ks.codegenerator.utils;
 
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.StringUtil;
 
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
/**
 * @author hxh
 * @title: ServiceUtil
 * @description: TODO
 * @date 2021/11/13 14:50
 */
public class AndroidBuilder {
 
 
    private String name;
    private String pks;
    private String cacheDir;
 
 
    public AndroidBuilder setName(String name) {
        this.name = name;
        return this;
    }
 
    public AndroidBuilder setPks(String pks) {
        this.pks = pks;
        return this;
    }
 
 
    public AndroidBuilder 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(AndroidBuilder.class.getClassLoader().getResource("code/android").getPath(), destPath);
 
 
        FileUtil.deleteFileDir(new File(destPath + "/.gradle"));
        FileUtil.deleteFileDir(new File(destPath + "/.idea"));
        Set<String> modules = getModules(destPath);
 
        //删除build
        for (String s : modules) {
            if (new File(s + "/build").exists()) {
                FileUtil.deleteFileDir(new File(s + "/build"));
            }
        }
 
        //修改文件名称与文件夹名称
        for (String s : modules) {
            renameFileAndDir(s);
        }
 
        //重新命名包名
        for (String s : modules) {
            if (new File(s + "/src/main/java/" + pks.replace(".", "/")).exists()) {
                //源代码位于src/main/java/下
                renamePackage(s + "/src/main/java/" + pks.replace(".", "/"));
            } else {
                //源代码直接位于src/下
                renamePackage(s + "/src/" + pks.replace(".", "/"));
            }
        }
 
        //重新命令设置文件
        for (String s : modules) {
            replaceSettings(s);
        }
        //压缩文件夹
        File zip = new File(new File(destPath).getParent(), "android.zip");
        ZipUtil.compressToZip(destPath, zip.getParent(), zip.getName());
        //删除原来的文件夹
        FileUtil.deleteFileDir(new File(destPath));
        return zip.getAbsolutePath();
    }
 
    private Set<String> getModules(String path) {
 
        Set<String> sets = new HashSet<>();
 
        File parent = new File(path);
        for (File f : parent.listFiles()) {
            if (f.isDirectory() && new File(f.getAbsolutePath() + "/build.gradle").exists()) {
                sets.add(f.getAbsolutePath());
            }
        }
        return sets;
    }
 
    private void replaceSettings(String path) throws Exception {
        FileUtils.replaceFileContent(path + "/build.gradle", "applicationId \"com.demo.android\"", "applicationId \"" + pks + "\"");
        FileUtils.replaceFileContent(path + "/src/main/AndroidManifest.xml", "com.demo.", pks + ".");
        FileUtils.replaceFileContent(path + "/src/main/res/values/strings.xml", "<string name=\"app_name\">Android</string>", "<string name=\"app_name\">" + name + "</string>");
        //布局文件
        String layoutPath = path + "/src/main/res/layout";
        if (!new File(layoutPath).exists()) {
            layoutPath = path + "/res/layout";
        }
        if (!new File(layoutPath).exists()) {
            return;
        }
        for (File file : new File(layoutPath).listFiles()) {
            FileUtils.replaceFileContent(file.getPath(), "<com.demo.", "<" + pks + ".");
        }
 
    }
 
    //重新命名文件夹与文件
    private void renameFileAndDir(String path) throws Exception {
        //更名代码目录
        if (new File(path + "/src/main/java/com/demo").exists()) {
            FileUtils.copyFileDir(path + "/src/main/java/com/demo", path + "/src/main/java/" + pks.replace(".", "/"));
            FileUtil.deleteFileDir(new File(path + "/src/main/java/com/demo"));
        } else if (new File(path + "/src/com/demo").exists()) {
            FileUtils.copyFileDir(path + "/src/com/demo", path + "/src/" + pks.replace(".", "/"));
            FileUtil.deleteFileDir(new File(path + "/src/com/demo"));
        }
 
    }
 
 
    //重新命名包名
    private void renamePackage(String path) throws Exception {
        if (!new File(path).exists()) {
            return;
        }
 
        if (new File(path).isDirectory()) {
            File[] fs = new File(path).listFiles();
            for (File f : fs) {
                if (f.isFile()) {
                    FileUtils.replaceFileContent(f.getPath(), "package com.demo", "package " + pks);
                    FileUtils.replaceFileContent(f.getPath(), "import com.demo.", "import " + pks + ".");
                } 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"));
    }
 
 
}