vue-pro/yudao-server/src/test/java/cn/iocoder/yudao/ProjectReactor.java

88 lines
4.3 KiB
Java
Raw Normal View History

package cn.iocoder.yudao;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
2022-03-30 10:08:55 +08:00
import java.util.regex.Matcher;
/**
* 项目修改器一键替换 Maven groupIdartifactId项目的 package
2022-03-30 10:08:55 +08:00
* <p>
* 通过修改 groupIdNewartifactIdNewprojectBaseDirNew 三个变量
*
* @author 芋道源码
*/
@Slf4j
public class ProjectReactor {
private static final String GROUP_ID = "cn.iocoder.boot";
private static final String ARTIFACT_ID = "yudao";
private static final String PACKAGE_NAME = "cn.iocoder.yudao";
2022-03-30 10:08:55 +08:00
private static final String TITLE = "欣辰装点猫管理系统";
public static void main(String[] args) {
2022-03-30 10:08:55 +08:00
long start = System.currentTimeMillis();
String projectBaseDir = getProjectBaseDir();
// ========== 配置,需要你手动修改 ==========
2022-03-30 10:08:55 +08:00
String groupIdNew = "cn.huwing.boot";
String artifactIdNew = "zdm";
String packageNameNew = "cn.huwing.zdm";
String titleNew = "欣辰装点猫管理系统";
String projectBaseDirNew = projectBaseDir + "-new"; // 一键改名后,“新”项目所在的目录
2022-03-30 10:08:55 +08:00
log.info("[main][新项目路径地址]projectBaseDirNew: " + projectBaseDirNew);
// 获得需要复制的文件
log.info("[main][开始获得需要重写的文件]");
Collection<File> files = listFiles(projectBaseDir);
log.info("[main][需要重写的文件数量:{},预计需要 5-10 秒]", files.size());
// 写入文件
files.forEach(file -> {
String content = replaceFileContent(file, groupIdNew, artifactIdNew, packageNameNew, titleNew);
writeFile(file, content, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
});
2022-03-30 10:08:55 +08:00
long end = System.currentTimeMillis();
log.info("[main][重写完成]共耗时:" + (end - start) / 1000 + "");
}
private static String getProjectBaseDir() {
2022-03-30 10:08:55 +08:00
return StrUtil.subBefore(new File(ProjectReactor.class.getClassLoader().getResource(File.separator).getFile()).getPath(), "\\yudao-server", false);
}
private static Collection<File> listFiles(String projectBaseDir) {
Collection<File> files = FileUtils.listFiles(new File(projectBaseDir), null, true);
files.removeIf(file -> file.getPath().contains("/target/"));
files.removeIf(file -> file.getPath().contains("/node_modules/"));
files.removeIf(file -> file.getPath().contains("/.idea/")); // 移除 IDEA 自身的文件
files.removeIf(file -> file.getPath().contains("/.git/")); // 移除 Git 自身的文件
2022-03-30 10:08:55 +08:00
files.removeIf(file -> file.getPath().contains("/.github/")); // 移除 GitHub 自身的文件
files.removeIf(file -> file.getPath().contains("/dist/")); // 移除 Node 编译出来的
return files;
}
private static String replaceFileContent(File file, String groupIdNew,
String artifactIdNew, String packageNameNew,
String titleNew) {
return FileUtil.readString(file, StandardCharsets.UTF_8)
.replaceAll(GROUP_ID, groupIdNew)
.replaceAll(PACKAGE_NAME, packageNameNew)
.replaceAll(ARTIFACT_ID, artifactIdNew) // 必须放在最后替换,因为 ARTIFACT_ID 太短!
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew))
.replaceAll(TITLE, titleNew);
}
private static void writeFile(File file, String fileContent, String projectBaseDir,
String projectBaseDirNew, String packageNameNew, String artifactIdNew) {
String newPath = file.getPath().replace(projectBaseDir, projectBaseDirNew) // 新目录
2022-03-30 10:08:55 +08:00
.replace(PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator)),
packageNameNew.replaceAll("\\.", Matcher.quoteReplacement(File.separator)))
.replace(ARTIFACT_ID, artifactIdNew) //
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew));
FileUtil.writeUtf8String(fileContent, newPath);
}
2022-03-30 10:08:55 +08:00
}