实现 #2969: 启动前日志列出mods目录的jar文件 (#3125)

* Support #2969: 启动前日志列出mods目录的jar文件

* Support #2969: 启动前日志列出mods目录jar文件

* Support #2969: 启动前日志中使用字符画列出mods目录模组文件

* Support #2969: 启动前日志中使用字符画列出mods目录模组文件

* Support #2969: 启动前日志中使用字符画列出mods目录模组文件

* Support #2969: 日志列出mods目录时不再过滤文件后缀

* Support #2969: 启动时日志列出mods目录(代码优化)

* Support #2969: 启动时日志列出mods目录(不再列出Zip内容)

* Support #2969

* Fix: checkstyle

---------

Co-authored-by: YELANDAOKONG <yelandaokong@yldk.xyz>
This commit is contained in:
Burning_TNT
2024-11-16 23:29:53 +08:00
committed by GitHub
parent 5591d92e25
commit 9edfe29659
3 changed files with 117 additions and 10 deletions

View File

@@ -0,0 +1,99 @@
package org.jackhuang.hmcl.util.io;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
final class DirectoryStructurePrinter {
private DirectoryStructurePrinter() {
}
public static String list(Path path, int maxDepth) throws IOException {
StringBuilder output = new StringBuilder(128);
list(path, maxDepth, output);
output.setLength(output.length() - 1);
return output.toString();
}
private static void list(Path path, int maxDepth, StringBuilder output) throws IOException {
output.append("Filesystem structure of: ").append(path).append('\n');
if (!Files.exists(path)) {
pushMessage(output, "nonexistent path", 1);
return;
}
if (Files.isRegularFile(path)) {
pushMessage(output, "regular file path", 1);
return;
}
if (Files.isDirectory(path)) {
Files.walkFileTree(path, new FileVisitor<Path>() {
private boolean isFolderEmpty;
private int depth = 1;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
isFolderEmpty = true;
pushFile(output, dir, depth);
if (depth == maxDepth) {
pushMessage(output, "too deep", depth);
return FileVisitResult.SKIP_SUBTREE;
}
depth++;
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
isFolderEmpty = false;
pushFile(output, file, depth);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
visitFile(file, null);
pushMessage(output, exc.toString(), depth);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
if (isFolderEmpty) {
pushMessage(output, "empty directory", depth);
}
depth--;
return FileVisitResult.CONTINUE;
}
});
return;
}
pushMessage(output, "unknown file type", 1);
}
private static void pushFile(StringBuilder output, Path file, int depth) {
output.append("|");
for (int i = 1; i < depth; i++) {
output.append(" |");
}
output.append("-> ").append(FileUtils.getName(file)).append('\n');
}
private static void pushMessage(StringBuilder output, String message, int depth) {
output.append("| ");
for (int i = 1; i < depth; i++) {
output.append(" | ");
}
output.append('<').append(message).append(">\n");
}
}

View File

@@ -30,11 +30,7 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Predicate;
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -199,7 +195,7 @@ public final class FileUtils {
* It will create the file if it does not exist, or truncate the existing file to empty for rewriting.
* All bytes in byte array will be written into the file in binary format. Existing data will be erased.
*
* @param file the path to the file
* @param file the path to the file
* @param data the data being written to file
* @throws IOException if an I/O error occurs
*/
@@ -212,7 +208,7 @@ public final class FileUtils {
* It will create the file if it does not exist, or truncate the existing file to empty for rewriting.
* All bytes in byte array will be written into the file in binary format. Existing data will be erased.
*
* @param file the path to the file
* @param file the path to the file
* @param data the data being written to file
* @throws IOException if an I/O error occurs
*/
@@ -591,4 +587,8 @@ public final class FileUtils {
Files.move(tmpFile, file, StandardCopyOption.REPLACE_EXISTING);
}
public static String printFileStructure(Path path, int maxDepth) throws IOException {
return DirectoryStructurePrinter.list(path, maxDepth);
}
}