完成初始化基本模块

This commit is contained in:
userA
2023-04-20 21:39:09 +08:00
parent f301b4484f
commit d342c8167f
35 changed files with 412 additions and 3 deletions

View File

@@ -18,6 +18,11 @@
</properties>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
@@ -32,7 +37,6 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,15 @@
package org.example.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author Genius
* @date 2023/04/20 19:29
**/
@Data
@AllArgsConstructor
public class ModuleConfigSrc {
private String src;
}

View File

@@ -0,0 +1,99 @@
package org.example.init;
import org.example.bean.ModuleConfigSrc;
import org.example.util.FileUtil;
import org.example.util.JsonFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;
/**
* @author Genius
* @date 2023/04/20 18:34
**/
public class ModuleConfigSrcInit implements InitMachine {
private Logger logger = LoggerFactory.getLogger(ModuleConfigSrcInit.class);
private static final Map<String, ModuleConfigSrc> config;
static{
config = Map.of(
"account", new ModuleConfigSrc("./config/account"),
"section", new ModuleConfigSrc("./config/section"),
"barrage", new ModuleConfigSrc("./config/barrage"),
"creeper", new ModuleConfigSrc("./config/creeper"),
"videowork", new ModuleConfigSrc("./config/videowork"),
"hot", new ModuleConfigSrc("./config/hot"),
"publish", new ModuleConfigSrc("./config/publish")
);
}
private static final String CONFIG_SRC = "./config";
private static final String CONFIG_SRC_FILE = "moduleConfig.json";
// 初始化每个模块的配置文件夹
@Override
public boolean init() {
Path dir = Paths.get(CONFIG_SRC);
if (!createConfigDirectory(dir)) {
return false;
}
if (!createConfigFile(dir)) {
return false;
}
if (!createModuleDirectory()) {
return false;
}
return true;
}
private boolean createConfigDirectory(Path dir) {
try {
if (!Files.exists(dir)) {
Files.createDirectory(dir);
logger.info("创建 config 文件夹成功 √ ");
}
}catch (Exception e) {
logger.error("创建配置文件夹失败");
return false;
}
return true;
}
private boolean createConfigFile(Path dir) {
Path path = Paths.get(dir.toString(), CONFIG_SRC_FILE);
try {
if (!Files.exists(path)) {
JsonFileUtil.writeJsonFile(path.toString(),config);
logger.info("创建 {} 配置文件成功 √",CONFIG_SRC_FILE);
}
}catch (Exception e) {
logger.error("创建配置文件失败");
return false;
}
return true;
}
private boolean createModuleDirectory() {
for (Map.Entry<String, ModuleConfigSrc> stringModuleConfigSrcEntry : config.entrySet()) {
ModuleConfigSrc moduleConfigSrc = stringModuleConfigSrcEntry.getValue();
try {
if (!FileUtil.isFileExist(moduleConfigSrc.getSrc())) {
Files.createDirectory(Path.of(moduleConfigSrc.getSrc()));
logger.info("创建 {} 模块文件夹成功 √ ",moduleConfigSrc.getSrc());
}
}catch (Exception e) {
logger.error("创建 {} 模块文件夹失败 ×",moduleConfigSrc.getSrc());
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,15 @@
{
"age":18,
"hobby":[
"Coding",
"Reading",
"Playing"
],
"info":{
"QQ":"123456789",
"WeChat":"987654321"
},
"major":"CS",
"name":"Genius",
"school":"HUST"
}

View File

@@ -0,0 +1 @@
[{"age":18,"hobby":["Coding","Reading","Playing"],"info":{"QQ":"123456789","WeChat":"987654321"},"major":"CS","name":"Genius","school":"HUST"}]

View File

@@ -0,0 +1,19 @@
{
"module":{
"ui":"console-ui",
"main":"console",
"type":[
"Account",
"Creeper",
"File",
"Hot",
"Publish",
"Section",
"SectionWork",
"VideoSection"
]
},
"name":"ChopperBot",
"description":"A bot for the ChopperMC server",
"version":"1.0.0"
}

View File

@@ -3,6 +3,9 @@ package org.example.util;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author Genius
@@ -14,4 +17,9 @@ public class FileUtilTest {
public void testDelete() throws IOException {
FileUtil.deleteDirectory("E:\\Project\\ChopperBot\\FileModule\\src\\main\\resources\\trash");
}
@Test
public void testCreate() throws IOException {
Files.createDirectories(Paths.get("./Hello"));
}
}

View File

@@ -0,0 +1,15 @@
{
"age":18,
"hobby":[
"Coding",
"Reading",
"Playing"
],
"info":{
"QQ":"123456789",
"WeChat":"987654321"
},
"major":"CS",
"name":"Genius",
"school":"HUST"
}

View File

@@ -0,0 +1 @@
[{"age":18,"hobby":["Coding","Reading","Playing"],"info":{"QQ":"123456789","WeChat":"987654321"},"major":"CS","name":"Genius","school":"HUST"}]

View File

@@ -0,0 +1,10 @@
{
"name": "ChopperBot",
"description": "A bot for the ChopperMC server",
"version": "1.0.0",
"module": {
"type": ["Account","Creeper","File","Hot","Publish","Section","SectionWork","VideoSection"],
"main": "console",
"ui": "console-ui"
}
}

View File

@@ -0,0 +1,19 @@
{
"module":{
"ui":"console-ui",
"main":"console",
"type":[
"Account",
"Creeper",
"File",
"Hot",
"Publish",
"Section",
"SectionWork",
"VideoSection"
]
},
"name":"ChopperBot",
"description":"A bot for the ChopperMC server",
"version":"1.0.0"
}