feat: 动态文件名

This commit is contained in:
bunny 2025-04-06 12:55:42 +08:00
parent 06f458bec2
commit 4d1a355cac
18 changed files with 248 additions and 5 deletions

View File

@ -3,16 +3,15 @@ package cn.bunny.controller;
import cn.bunny.dao.dto.VmsArgumentDto;
import cn.bunny.dao.result.Result;
import cn.bunny.dao.vo.GeneratorVo;
import cn.bunny.dao.vo.VmsPathVo;
import cn.bunny.service.VmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@Tag(name = "生成器", description = "代码生成器接口")
@RestController
@ -25,6 +24,13 @@ public class VmsController {
this.vmsService = vmsService;
}
@Operation(summary = "获取vms文件路径", description = "获取所有vms下的文件路径")
@GetMapping("getVmsPathList")
public Result<Map<String, List<VmsPathVo>>> getVmsPathList() {
Map<String, List<VmsPathVo>> list = vmsService.getVmsPathList();
return Result.success(list);
}
@Operation(summary = "生成控制器", description = "生成控制器代码")
@PostMapping("generator")
public Result<List<GeneratorVo>> generator(@Valid @RequestBody VmsArgumentDto dto) {

View File

@ -0,0 +1,23 @@
package cn.bunny.dao.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class VmsPathVo {
/* 路径名称 */
private String name;
/* 显示的label */
private String label;
/* 文件夹最上级目录名称 */
private String type;
}

View File

@ -2,8 +2,10 @@ package cn.bunny.service;
import cn.bunny.dao.dto.VmsArgumentDto;
import cn.bunny.dao.vo.GeneratorVo;
import cn.bunny.dao.vo.VmsPathVo;
import java.util.List;
import java.util.Map;
public interface VmsService {
/**
@ -13,4 +15,11 @@ public interface VmsService {
* @return 生成内容
*/
List<GeneratorVo> generator(VmsArgumentDto dto);
/**
* 获取vms文件路径
*
* @return vms下的文件路径
*/
Map<String, List<VmsPathVo>> getVmsPathList();
}

View File

@ -4,14 +4,19 @@ import cn.bunny.dao.dto.VmsArgumentDto;
import cn.bunny.dao.entity.ColumnMetaData;
import cn.bunny.dao.vo.GeneratorVo;
import cn.bunny.dao.vo.TableInfoVo;
import cn.bunny.dao.vo.VmsPathVo;
import cn.bunny.service.TableService;
import cn.bunny.service.VmsService;
import cn.bunny.utils.ResourceFileUtil;
import cn.bunny.utils.VmsUtil;
import lombok.SneakyThrows;
import org.apache.velocity.VelocityContext;
import org.springframework.stereotype.Service;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@ -58,7 +63,7 @@ public class VmsServiceImpl implements VmsService {
// 数据库sql列
context.put("baseColumnList", String.join(",", list));
VmsUtil.commonVms(writer, context, "vms/" + path + ".vm", dto);
VmsUtil.commonVms(writer, context, "vms/" + path, dto);
return GeneratorVo.builder()
.code(writer.toString())
@ -68,4 +73,24 @@ public class VmsServiceImpl implements VmsService {
.build();
}).toList();
}
/**
* 获取vms文件路径
*
* @return vms下的文件路径
*/
@SneakyThrows
@Override
public Map<String, List<VmsPathVo>> getVmsPathList() {
List<String> vmsRelativeFiles;
vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms");
return vmsRelativeFiles.stream().map(vmFile -> {
String[] filepathList = vmFile.split("/");
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
}).collect(Collectors.groupingBy(VmsPathVo::getType));
}
}

View File

@ -0,0 +1,130 @@
package cn.bunny.utils;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Stream;
public class ResourceFileUtil {
/**
* 获取目标文件夹下所有文件完整路径
*
* @param dirname 文件夹名称
* @return 目标文件完整路径
* @throws IOException IOException
*/
public static List<String> getAbsoluteFiles(String dirname) throws IOException {
List<String> fileNames = new ArrayList<>();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Enumeration<URL> urls = classLoader.getResources(dirname);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
if (url.getProtocol().equals("file")) {
// 文件系统
File file = new File(url.getFile());
if (file.isDirectory()) {
addFullFilesFromDirectory(file, fileNames);
}
} else if (url.getProtocol().equals("jar")) {
// JAR文件
String jarPath = url.getPath().substring(5, url.getPath().indexOf("!"));
try (JarFile jar = new JarFile(jarPath)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if (name.startsWith(dirname + "/") && !name.endsWith("/")) {
fileNames.add(name);
}
}
}
}
}
return fileNames;
}
/**
* 添加文件
* 获取目标文件夹下所有文件完整路径
*
* @param directory 文件夹
* @param fileNames 文件名称
*/
private static void addFullFilesFromDirectory(File directory, List<String> fileNames) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
fileNames.add(file.getPath());
} else if (file.isDirectory()) {
addFullFilesFromDirectory(file, fileNames);
}
}
}
}
/**
* 获取相对文件夹路径
*
* @return 相对当前的文件夹路径
* @throws IOException IOException
* @throws URISyntaxException URISyntaxException
*/
public static List<String> getRelativeFiles(String dirname) throws IOException, URISyntaxException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource(dirname);
if (resource == null) return Collections.emptyList();
// 处理JAR包内的情况
if (resource.getProtocol().equals("jar")) {
return getFilesFromJar(resource, dirname);
} else {
// 处理文件系统情况
return getFilesFromFileSystem(resource);
}
}
private static List<String> getFilesFromJar(URL jarUrl, String dirname) throws IOException {
List<String> fileNames = new ArrayList<>();
String jarPath = jarUrl.getPath().substring(5, jarUrl.getPath().indexOf("!"));
try (JarFile jar = new JarFile(jarPath)) {
jar.entries().asIterator()
.forEachRemaining(entry -> {
String name = entry.getName();
String prefix = dirname + "/";
if (name.startsWith(prefix) && !name.endsWith("/")) {
fileNames.add(name.substring(prefix.length()));
}
});
}
return fileNames;
}
private static List<String> getFilesFromFileSystem(URL resource) throws IOException, URISyntaxException {
Path filepath = Paths.get(resource.toURI());
try (Stream<Path> paths = Files.walk(filepath)) {
return paths.filter(Files::isRegularFile)
.map(filepath::relativize)
.map(Path::toString)
// 统一使用/作为分隔符
.map(s -> s.replace('\\', '/'))
.toList();
}
}
}

View File

@ -0,0 +1,50 @@
package cn.bunny.service.impl;
import cn.bunny.dao.vo.VmsPathVo;
import cn.bunny.utils.ResourceFileUtil;
import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class VmsServiceImplTest {
@Test
void getVmsPathList() throws IOException, URISyntaxException {
List<String> vmsFiles = ResourceFileUtil.getAbsoluteFiles("vms");
System.out.println(vmsFiles);
System.out.println("--------------------------------------------------------------");
List<String> vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms");
System.out.println(vmsRelativeFiles);
System.out.println("--------------------------集合对象模式------------------------------------");
Map<String, List<VmsPathVo>> map = vmsRelativeFiles.stream().map(vmFile -> {
String[] filepathList = vmFile.split("/");
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
}).collect(Collectors.groupingBy(VmsPathVo::getType));
System.out.println(JSON.toJSONString(map));
System.out.println("----------------------------二维数组格式----------------------------------");
List<List<VmsPathVo>> listMap = vmsRelativeFiles.stream().map(vmFile -> {
String[] filepathList = vmFile.split("/");
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
})
.collect(Collectors.groupingBy(VmsPathVo::getType))
.values().stream().toList();
System.out.println(JSON.toJSONString(listMap));
}
}