|
@@ -1,17 +1,24 @@
|
|
|
package cn.iocoder.yudao;
|
|
|
|
|
|
+import cn.hutool.core.io.FileTypeUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static java.io.File.separator;
|
|
|
|
|
|
|
|
|
* 项目修改器,一键替换 Maven 的 groupId、artifactId,项目的 package 等
|
|
|
- *
|
|
|
+ * <p>
|
|
|
* 通过修改 groupIdNew、artifactIdNew、projectBaseDirNew 三个变量
|
|
|
*
|
|
|
* @author 芋道源码
|
|
@@ -24,8 +31,16 @@ public class ProjectReactor {
|
|
|
private static final String PACKAGE_NAME = "cn.iocoder.yudao";
|
|
|
private static final String TITLE = "芋道管理系统";
|
|
|
|
|
|
+
|
|
|
+ * 白名单文件,不进行重写,避免出问题
|
|
|
+ */
|
|
|
+ private static final Set<String> WHITE_FILE_TYPES = SetUtils.asSet("gif", "jpg", "svg", "png",
|
|
|
+ "eot", "woff2", "ttf", "woff");
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
String projectBaseDir = getProjectBaseDir();
|
|
|
+ log.info("[main][原项目路劲改地址 ({})]", projectBaseDir);
|
|
|
|
|
|
|
|
|
String groupIdNew = "cn.star.gg";
|
|
@@ -33,41 +48,61 @@ public class ProjectReactor {
|
|
|
String packageNameNew = "cn.start.pp";
|
|
|
String titleNew = "土豆管理系统";
|
|
|
String projectBaseDirNew = projectBaseDir + "-new";
|
|
|
-
|
|
|
+ log.info("[main][新项目路径地址 ({})]", projectBaseDirNew);
|
|
|
|
|
|
|
|
|
- log.info("[main][开始获得需要重写的文件]");
|
|
|
+ log.info("[main][开始获得需要重写的文件,预计需要 10-20 秒]");
|
|
|
Collection<File> files = listFiles(projectBaseDir);
|
|
|
- log.info("[main][需要重写的文件数量:{},预计需要 5-10 秒]", files.size());
|
|
|
+ log.info("[main][需要重写的文件数量:{},预计需要 15-30 秒]", files.size());
|
|
|
|
|
|
files.forEach(file -> {
|
|
|
+
|
|
|
+ String fileType = FileTypeUtil.getType(file);
|
|
|
+ if (WHITE_FILE_TYPES.contains(fileType)) {
|
|
|
+ copyFile(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
String content = replaceFileContent(file, groupIdNew, artifactIdNew, packageNameNew, titleNew);
|
|
|
writeFile(file, content, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
|
|
|
});
|
|
|
- log.info("[main][重写完成]");
|
|
|
+ log.info("[main][重写完成]共耗时:{} 秒", (System.currentTimeMillis() - start) / 1000);
|
|
|
}
|
|
|
|
|
|
private static String getProjectBaseDir() {
|
|
|
-
|
|
|
- return StrUtil.subBefore(ProjectReactor.class.getClassLoader().getResource("").getFile(),
|
|
|
- "/yudao-server", false);
|
|
|
+ String baseDir = System.getProperty("user.dir");
|
|
|
+ if (StrUtil.isEmpty(baseDir)) {
|
|
|
+ throw new NullPointerException("项目基础路径不存在");
|
|
|
+ }
|
|
|
+ return baseDir;
|
|
|
}
|
|
|
|
|
|
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/"));
|
|
|
- files.removeIf(file -> file.getPath().contains("/.git/"));
|
|
|
- files.removeIf(file -> file.getPath().contains("/dist/"));
|
|
|
+
|
|
|
+ files = files.stream()
|
|
|
+ .filter(file -> !file.getPath().contains(separator + "target" + separator)
|
|
|
+ && !file.getPath().contains(separator + "node_modules" + separator)
|
|
|
+ && !file.getPath().contains(separator + ".idea" + separator)
|
|
|
+ && !file.getPath().contains(separator + ".git" + separator)
|
|
|
+ && !file.getPath().contains(separator + "dist" + separator)
|
|
|
+ && !file.getPath().contains(".iml")
|
|
|
+ && !file.getPath().contains(".html.gz"))
|
|
|
+ .collect(Collectors.toList());
|
|
|
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)
|
|
|
+ String content = FileUtil.readString(file, StandardCharsets.UTF_8);
|
|
|
+
|
|
|
+ String fileType = FileTypeUtil.getType(file);
|
|
|
+ if (WHITE_FILE_TYPES.contains(fileType)) {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
+ return content.replaceAll(GROUP_ID, groupIdNew)
|
|
|
.replaceAll(PACKAGE_NAME, packageNameNew)
|
|
|
.replaceAll(ARTIFACT_ID, artifactIdNew)
|
|
|
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew))
|
|
@@ -76,12 +111,23 @@ public class ProjectReactor {
|
|
|
|
|
|
private static void writeFile(File file, String fileContent, String projectBaseDir,
|
|
|
String projectBaseDirNew, String packageNameNew, String artifactIdNew) {
|
|
|
- String newPath = file.getPath().replace(projectBaseDir, projectBaseDirNew)
|
|
|
- .replace(PACKAGE_NAME.replaceAll("\\.", File.separator),
|
|
|
- packageNameNew.replaceAll("\\.", File.separator))
|
|
|
+ String newPath = buildNewFilePath(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
|
|
|
+ FileUtil.writeUtf8String(fileContent, newPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void copyFile(File file, String projectBaseDir,
|
|
|
+ String projectBaseDirNew, String packageNameNew, String artifactIdNew) {
|
|
|
+ String newPath = buildNewFilePath(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
|
|
|
+ FileUtil.copyFile(file, new File(newPath));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String buildNewFilePath(File file, String projectBaseDir,
|
|
|
+ String projectBaseDirNew, String packageNameNew, String artifactIdNew) {
|
|
|
+ return file.getPath().replace(projectBaseDir, projectBaseDirNew)
|
|
|
+ .replace(PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(separator)),
|
|
|
+ packageNameNew.replaceAll("\\.", Matcher.quoteReplacement(separator)))
|
|
|
.replace(ARTIFACT_ID, artifactIdNew)
|
|
|
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew));
|
|
|
- FileUtil.writeUtf8String(fileContent, newPath);
|
|
|
}
|
|
|
|
|
|
}
|