feat: 项目整体命名修改cache-schema-checker
All checks were successful
缓存序列化结构检查 / cache-schema-check (push) Has been skipped
All checks were successful
缓存序列化结构检查 / cache-schema-check (push) Has been skipped
This commit is contained in:
66
src/main/java/com/codechecker/cache/analyze/FileScanner.java
vendored
Normal file
66
src/main/java/com/codechecker/cache/analyze/FileScanner.java
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.codechecker.cache.analyze;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* 扫描工作树中受包含/排除模块限制的 {@code src/main/java} 下的所有 Java 源文件。
|
||||
*/
|
||||
public class FileScanner {
|
||||
|
||||
private final Path repoRoot;
|
||||
private final List<String> includeModules;
|
||||
private final List<String> excludeModules;
|
||||
|
||||
public FileScanner(Path repoRoot, List<String> includeModules, List<String> excludeModules) {
|
||||
this.repoRoot = repoRoot;
|
||||
this.includeModules = includeModules;
|
||||
this.excludeModules = excludeModules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 相对仓库根(/ 分隔)-> 文件内容
|
||||
*/
|
||||
public Map<String, String> scan() {
|
||||
Map<String, String> result = new LinkedHashMap<>();
|
||||
try (Stream<Path> stream = Files.walk(repoRoot)) {
|
||||
stream.filter(Files::isRegularFile)
|
||||
.filter(p -> p.toString().endsWith(".java"))
|
||||
.forEach(p -> {
|
||||
String rel = repoRoot.relativize(p).toString().replace('\\', '/');
|
||||
if (!rel.contains("/src/main/java/")) {
|
||||
return;
|
||||
}
|
||||
if (!moduleAllowed(rel)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
result.put(rel, new String(Files.readAllBytes(p), StandardCharsets.UTF_8));
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
boolean moduleAllowed(String relPath) {
|
||||
String topModule = relPath.contains("/") ? relPath.substring(0, relPath.indexOf('/')) : relPath;
|
||||
if (excludeModules != null && excludeModules.contains(topModule)) {
|
||||
return false;
|
||||
}
|
||||
if (includeModules == null || includeModules.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return includeModules.contains(topModule);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user