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"));
|
}
|
|
|
}
|