增加 ProjectReactor 程序,实现一键改包名

This commit is contained in:
YunaiV 2022-03-09 00:27:26 +08:00
parent fc4b677b00
commit d6333fc353
8 changed files with 105 additions and 25 deletions

View File

@ -38,7 +38,7 @@ public class FileServiceTest extends BaseDbUnitTest {
public void testGetFilePage() {
// mock 数据
FileDO dbFile = randomPojo(FileDO.class, o -> { // 等会查询到
o.setId("yudao");
o.setId("yunai");
o.setType("jpg");
o.setCreateTime(buildTime(2021, 1, 15));
});
@ -47,17 +47,17 @@ public class FileServiceTest extends BaseDbUnitTest {
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setId("tudou")));
// 测试 type 不匹配
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
o.setId("yudao02");
o.setId("yunai02");
o.setType("png");
}));
// 测试 createTime 不匹配
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
o.setId("yudao03");
o.setId("yunai03");
o.setCreateTime(buildTime(2020, 1, 15));
}));
// 准备参数
FilePageReqVO reqVO = new FilePageReqVO();
reqVO.setId("yudao");
reqVO.setId("yunai");
reqVO.setType("jp");
reqVO.setBeginCreateTime(buildTime(2021, 1, 10));
reqVO.setEndCreateTime(buildTime(2021, 1, 20));

View File

@ -163,7 +163,7 @@ public class UserController {
public void importTemplate(HttpServletResponse response) throws IOException {
// 手动创建导出 demo
List<UserImportExcelVO> list = Arrays.asList(
UserImportExcelVO.builder().username("yudao").deptId(1L).email("yudao@iocoder.cn").mobile("15601691300")
UserImportExcelVO.builder().username("yunai").deptId(1L).email("yunai@iocoder.cn").mobile("15601691300")
.nickname("芋道").status(CommonStatusEnum.ENABLE.getStatus()).sex(SexEnum.MALE.getSex()).build(),
UserImportExcelVO.builder().username("yuanma").deptId(2L).email("yuanma@iocoder.cn").mobile("15601701300")
.nickname("源码").status(CommonStatusEnum.DISABLE.getStatus()).sex(SexEnum.FEMALE.getSex()).build()

View File

@ -92,9 +92,9 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
// 准备参数
ErrorCodePageReqVO reqVO = new ErrorCodePageReqVO();
reqVO.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType());
reqVO.setApplicationName("yudao");
reqVO.setApplicationName("tu");
reqVO.setCode(1);
reqVO.setMessage("yu");
reqVO.setMessage("ma");
reqVO.setBeginCreateTime(buildTime(2020, 11, 1));
reqVO.setEndCreateTime(buildTime(2020, 11, 30));
@ -112,16 +112,16 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
private ErrorCodeDO initGetErrorCodePage() {
ErrorCodeDO dbErrorCode = randomInfErrorCodeDO(o -> { // 等会查询到
o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType());
o.setApplicationName("yudaoyuanma");
o.setApplicationName("tudou");
o.setCode(1);
o.setMessage("yudao");
o.setMessage("yuanma");
o.setCreateTime(buildTime(2020, 11, 11));
});
errorCodeMapper.insert(dbErrorCode);
// 测试 type 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())));
// 测试 applicationName 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yunai")));
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan")));
// 测试 code 不匹配
errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCode(2)));
// 测试 message 不匹配
@ -138,9 +138,9 @@ public class ErrorCodeServiceTest extends BaseDbUnitTest {
// 准备参数
ErrorCodeExportReqVO reqVO = new ErrorCodeExportReqVO();
reqVO.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType());
reqVO.setApplicationName("yudao");
reqVO.setApplicationName("tu");
reqVO.setCode(1);
reqVO.setMessage("yu");
reqVO.setMessage("ma");
reqVO.setBeginCreateTime(buildTime(2020, 11, 1));
reqVO.setEndCreateTime(buildTime(2020, 11, 30));

View File

@ -201,7 +201,7 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest {
SmsTemplateDO dbSmsTemplate = randomPojo(SmsTemplateDO.class, o -> { // 等会查询到
o.setType(SmsTemplateTypeEnum.PROMOTION.getType());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCode("yudaoyuanma");
o.setCode("tudou");
o.setContent("芋道源码");
o.setApiTemplateId("yunai");
o.setChannelId(1L);
@ -226,7 +226,7 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest {
SmsTemplatePageReqVO reqVO = new SmsTemplatePageReqVO();
reqVO.setType(SmsTemplateTypeEnum.PROMOTION.getType());
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCode("yudao");
reqVO.setCode("tu");
reqVO.setContent("芋道");
reqVO.setApiTemplateId("yu");
reqVO.setChannelId(1L);
@ -247,7 +247,7 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest {
SmsTemplateDO dbSmsTemplate = randomPojo(SmsTemplateDO.class, o -> { // 等会查询到
o.setType(SmsTemplateTypeEnum.PROMOTION.getType());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCode("yudaoyuanma");
o.setCode("tudou");
o.setContent("芋道源码");
o.setApiTemplateId("yunai");
o.setChannelId(1L);
@ -272,7 +272,7 @@ public class SmsTemplateServiceTest extends BaseDbUnitTest {
SmsTemplateExportReqVO reqVO = new SmsTemplateExportReqVO();
reqVO.setType(SmsTemplateTypeEnum.PROMOTION.getType());
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCode("yudao");
reqVO.setCode("tu");
reqVO.setContent("芋道");
reqVO.setApiTemplateId("yu");
reqVO.setChannelId(1L);

View File

@ -176,7 +176,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
}), eq(RoleTypeEnum.SYSTEM.getType()))).thenReturn(200L);
// mock 用户 300L
when(userService.createUser(argThat(user -> {
assertEquals("yudao", user.getUsername());
assertEquals("yunai", user.getUsername());
assertEquals("yuanma", user.getPassword());
assertEquals("芋道", user.getNickname());
assertEquals("15601691300", user.getMobile());
@ -190,7 +190,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
o.setPackageId(100L);
o.setStatus(randomCommonStatus());
o.setDomain("https://www.iocoder.cn");
o.setUsername("yudao");
o.setUsername("yunai");
o.setPassword("yuanma");
});

View File

@ -175,12 +175,12 @@ public class UserServiceImplTest extends BaseDbUnitTest {
@Test
public void testUpdateUserPassword_success() {
// mock 数据
AdminUserDO dbUser = randomAdminUserDO(o -> o.setPassword("encode:yudao"));
AdminUserDO dbUser = randomAdminUserDO(o -> o.setPassword("encode:tudou"));
userMapper.insert(dbUser);
// 准备参数
Long userId = dbUser.getId();
UserProfileUpdatePasswordReqVO reqVO = randomPojo(UserProfileUpdatePasswordReqVO.class, o -> {
o.setOldPassword("yudao");
o.setOldPassword("tudou");
o.setNewPassword("yuanma");
});
// mock 方法
@ -272,7 +272,7 @@ public class UserServiceImplTest extends BaseDbUnitTest {
AdminUserDO dbUser = initGetUserPageData();
// 准备参数
UserPageReqVO reqVO = new UserPageReqVO();
reqVO.setUsername("yudao");
reqVO.setUsername("tu");
reqVO.setMobile("1560");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setBeginTime(buildTime(2020, 12, 1));
@ -296,7 +296,7 @@ public class UserServiceImplTest extends BaseDbUnitTest {
AdminUserDO dbUser = initGetUserPageData();
// 准备参数
UserExportReqVO reqVO = new UserExportReqVO();
reqVO.setUsername("yudao");
reqVO.setUsername("tu");
reqVO.setMobile("1560");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setBeginTime(buildTime(2020, 12, 1));
@ -319,7 +319,7 @@ public class UserServiceImplTest extends BaseDbUnitTest {
private AdminUserDO initGetUserPageData() {
// mock 数据
AdminUserDO dbUser = randomAdminUserDO(o -> { // 等会查询到
o.setUsername("yudaoyuanma");
o.setUsername("tudou");
o.setMobile("15601691300");
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCreateTime(buildTime(2020, 12, 12));
@ -327,7 +327,7 @@ public class UserServiceImplTest extends BaseDbUnitTest {
});
userMapper.insert(dbUser);
// 测试 username 不匹配
userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setUsername("yuanma")));
userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setUsername("dou")));
// 测试 mobile 不匹配
userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setMobile("18818260888")));
// 测试 status 不匹配

View File

@ -0,0 +1,81 @@
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;
/**
* 项目修改器一键替换 Maven groupIdartifactId项目的 package
*
* 通过修改 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";
public static void main(String[] args) {
String projectBaseDir = getProjectBaseDir();
// ========== 配置需要你手动修改 ==========
String groupIdNew = "cn.star.gg";
String artifactIdNew = "star";
String packageNameNew = "cn.start.pp";
String projectBaseDirNew = projectBaseDir + "-new";
// ========== ==========
// 获得需要复制的文件
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);
writeFile(file, content, projectBaseDir, projectBaseDirNew, packageNameNew, artifactIdNew);
});
log.info("[main][重写完成]");
}
private static String getProjectBaseDir() {
// noinspection ConstantConditions
return StrUtil.subBefore(ProjectReactor.class.getClassLoader().getResource("").getFile(),
"/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 自身的文件
files.removeIf(file -> file.getPath().contains("/dist/")); // 移除 Node 编译出来的
return files;
}
private static String replaceFileContent(File file, String groupIdNew,
String artifactIdNew, String packageNameNew) {
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));
}
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("\\.", "/"), packageNameNew.replaceAll("\\.", "/"))
.replace(ARTIFACT_ID, artifactIdNew) //
.replaceAll(StrUtil.upperFirst(ARTIFACT_ID), StrUtil.upperFirst(artifactIdNew));
FileUtil.writeUtf8String(fileContent, newPath);
}
}