Commit 946735ac246a27f5fb9fe8a878bc43e31cbfd2b7
1 parent
ceb76e71
优化一键改码的实现,解决 win 和 mac 的兼容性问题,修复 png、jpg 等文件的编码问题
Showing
1 changed file
with
45 additions
and
7 deletions
yudao-server/src/test/java/cn/iocoder/yudao/ProjectReactor.java
| 1 | package cn.iocoder.yudao; | 1 | package cn.iocoder.yudao; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.io.FileTypeUtil; | ||
| 3 | import cn.hutool.core.io.FileUtil; | 4 | import cn.hutool.core.io.FileUtil; |
| 4 | import cn.hutool.core.util.StrUtil; | 5 | import cn.hutool.core.util.StrUtil; |
| 6 | +import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | ||
| 5 | import lombok.extern.slf4j.Slf4j; | 7 | import lombok.extern.slf4j.Slf4j; |
| 6 | import org.apache.commons.io.FileUtils; | 8 | import org.apache.commons.io.FileUtils; |
| 7 | 9 | ||
| 8 | import java.io.File; | 10 | import java.io.File; |
| 9 | import java.nio.charset.StandardCharsets; | 11 | import java.nio.charset.StandardCharsets; |
| 10 | import java.util.Collection; | 12 | import java.util.Collection; |
| 13 | +import java.util.Set; | ||
| 11 | import java.util.regex.Matcher; | 14 | import java.util.regex.Matcher; |
| 12 | import java.util.stream.Collectors; | 15 | import java.util.stream.Collectors; |
| 13 | 16 | ||
| @@ -28,9 +31,16 @@ public class ProjectReactor { | @@ -28,9 +31,16 @@ public class ProjectReactor { | ||
| 28 | private static final String PACKAGE_NAME = "cn.iocoder.yudao"; | 31 | private static final String PACKAGE_NAME = "cn.iocoder.yudao"; |
| 29 | private static final String TITLE = "芋道管理系统"; | 32 | private static final String TITLE = "芋道管理系统"; |
| 30 | 33 | ||
| 34 | + /** | ||
| 35 | + * 白名单文件,不进行重写,避免出问题 | ||
| 36 | + */ | ||
| 37 | + private static final Set<String> WHITE_FILE_TYPES = SetUtils.asSet("gif", "jpg", "svg", "png", // 图片 | ||
| 38 | + "eot", "woff2", "ttf", "woff"); // 字体 | ||
| 39 | + | ||
| 31 | public static void main(String[] args) { | 40 | public static void main(String[] args) { |
| 32 | long start = System.currentTimeMillis(); | 41 | long start = System.currentTimeMillis(); |
| 33 | String projectBaseDir = getProjectBaseDir(); | 42 | String projectBaseDir = getProjectBaseDir(); |
| 43 | + log.info("[main][原项目路劲改地址 ({})]", projectBaseDir); | ||
| 34 | 44 | ||
| 35 | // ========== 配置,需要你手动修改 ========== | 45 | // ========== 配置,需要你手动修改 ========== |
| 36 | String groupIdNew = "cn.star.gg"; | 46 | String groupIdNew = "cn.star.gg"; |
| @@ -38,7 +48,7 @@ public class ProjectReactor { | @@ -38,7 +48,7 @@ public class ProjectReactor { | ||
| 38 | String packageNameNew = "cn.start.pp"; | 48 | String packageNameNew = "cn.start.pp"; |
| 39 | String titleNew = "土豆管理系统"; | 49 | String titleNew = "土豆管理系统"; |
| 40 | String projectBaseDirNew = projectBaseDir + "-new"; // 一键改名后,“新”项目所在的目录 | 50 | String projectBaseDirNew = projectBaseDir + "-new"; // 一键改名后,“新”项目所在的目录 |
| 41 | - log.info("[main][新项目路径地址]projectBaseDirNew: " + projectBaseDirNew); | 51 | + log.info("[main][新项目路径地址 ({})]", projectBaseDirNew); |
| 42 | 52 | ||
| 43 | // 获得需要复制的文件 | 53 | // 获得需要复制的文件 |
| 44 | log.info("[main][开始获得需要重写的文件,预计需要 10-20 秒]"); | 54 | log.info("[main][开始获得需要重写的文件,预计需要 10-20 秒]"); |
| @@ -46,6 +56,13 @@ public class ProjectReactor { | @@ -46,6 +56,13 @@ public class ProjectReactor { | ||
| 46 | log.info("[main][需要重写的文件数量:{},预计需要 15-30 秒]", files.size()); | 56 | log.info("[main][需要重写的文件数量:{},预计需要 15-30 秒]", files.size()); |
| 47 | // 写入文件 | 57 | // 写入文件 |
| 48 | files.forEach(file -> { | 58 | files.forEach(file -> { |
| 59 | + // 如果是白名单的文件类型,不进行重写,直接拷贝 | ||
| 60 | + String fileType = FileTypeUtil.getType(file); | ||
| 61 | + if (WHITE_FILE_TYPES.contains(fileType)) { | ||
| 62 | + copyFile(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); | ||
| 63 | + return; | ||
| 64 | + } | ||
| 65 | + // 如果非白名单的文件类型,重写内容,在生成文件 | ||
| 49 | String content = replaceFileContent(file, groupIdNew, artifactIdNew, packageNameNew, titleNew); | 66 | String content = replaceFileContent(file, groupIdNew, artifactIdNew, packageNameNew, titleNew); |
| 50 | writeFile(file, content, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); | 67 | writeFile(file, content, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); |
| 51 | }); | 68 | }); |
| @@ -53,8 +70,11 @@ public class ProjectReactor { | @@ -53,8 +70,11 @@ public class ProjectReactor { | ||
| 53 | } | 70 | } |
| 54 | 71 | ||
| 55 | private static String getProjectBaseDir() { | 72 | private static String getProjectBaseDir() { |
| 56 | - return StrUtil.subBefore(ProjectReactor.class.getClassLoader().getResource("").getFile(), | ||
| 57 | - separator + "yudao-server", false); | 73 | + String baseDir = System.getProperty("user.dir"); |
| 74 | + if (StrUtil.isEmpty(baseDir)) { | ||
| 75 | + throw new NullPointerException("项目基础路径不存在"); | ||
| 76 | + } | ||
| 77 | + return baseDir; | ||
| 58 | } | 78 | } |
| 59 | 79 | ||
| 60 | private static Collection<File> listFiles(String projectBaseDir) { | 80 | private static Collection<File> listFiles(String projectBaseDir) { |
| @@ -75,8 +95,14 @@ public class ProjectReactor { | @@ -75,8 +95,14 @@ public class ProjectReactor { | ||
| 75 | private static String replaceFileContent(File file, String groupIdNew, | 95 | private static String replaceFileContent(File file, String groupIdNew, |
| 76 | String artifactIdNew, String packageNameNew, | 96 | String artifactIdNew, String packageNameNew, |
| 77 | String titleNew) { | 97 | String titleNew) { |
| 78 | - return FileUtil.readString(file, StandardCharsets.UTF_8) | ||
| 79 | - .replaceAll(GROUP_ID, groupIdNew) | 98 | + String content = FileUtil.readString(file, StandardCharsets.UTF_8); |
| 99 | + // 如果是白名单的文件类型,不进行重写 | ||
| 100 | + String fileType = FileTypeUtil.getType(file); | ||
| 101 | + if (WHITE_FILE_TYPES.contains(fileType)) { | ||
| 102 | + return content; | ||
| 103 | + } | ||
| 104 | + // 执行文件内容都重写 | ||
| 105 | + return content.replaceAll(GROUP_ID, groupIdNew) | ||
| 80 | .replaceAll(PACKAGE_NAME, packageNameNew) | 106 | .replaceAll(PACKAGE_NAME, packageNameNew) |
| 81 | .replaceAll(ARTIFACT_ID, artifactIdNew) // 必须放在最后替换,因为 ARTIFACT_ID 太短! | 107 | .replaceAll(ARTIFACT_ID, artifactIdNew) // 必须放在最后替换,因为 ARTIFACT_ID 太短! |
| 82 | .replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew)) | 108 | .replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew)) |
| @@ -85,11 +111,23 @@ public class ProjectReactor { | @@ -85,11 +111,23 @@ public class ProjectReactor { | ||
| 85 | 111 | ||
| 86 | private static void writeFile(File file, String fileContent, String projectBaseDir, | 112 | private static void writeFile(File file, String fileContent, String projectBaseDir, |
| 87 | String projectBaseDirNew, String packageNameNew, String artifactIdNew) { | 113 | String projectBaseDirNew, String packageNameNew, String artifactIdNew) { |
| 88 | - String newPath = file.getPath().replace(projectBaseDir, projectBaseDirNew) // 新目录 | 114 | + String newPath = buildNewFilePath(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); |
| 115 | + FileUtil.writeUtf8String(fileContent, newPath); | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + private static void copyFile(File file, String projectBaseDir, | ||
| 119 | + String projectBaseDirNew, String packageNameNew, String artifactIdNew) { | ||
| 120 | + String newPath = buildNewFilePath(file, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew); | ||
| 121 | + FileUtil.copyFile(file, new File(newPath)); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + private static String buildNewFilePath(File file, String projectBaseDir, | ||
| 125 | + String projectBaseDirNew, String packageNameNew, String artifactIdNew) { | ||
| 126 | + return file.getPath().replace(projectBaseDir, projectBaseDirNew) // 新目录 | ||
| 89 | .replace(PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(separator)), | 127 | .replace(PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(separator)), |
| 90 | packageNameNew.replaceAll("\\.", Matcher.quoteReplacement(separator))) | 128 | packageNameNew.replaceAll("\\.", Matcher.quoteReplacement(separator))) |
| 91 | .replace(ARTIFACT_ID, artifactIdNew) // | 129 | .replace(ARTIFACT_ID, artifactIdNew) // |
| 92 | .replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew)); | 130 | .replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew)); |
| 93 | - FileUtil.writeUtf8String(fileContent, newPath); | ||
| 94 | } | 131 | } |
| 132 | + | ||
| 95 | } | 133 | } |