All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.huangxulin.inspector.Launcher Maven / Gradle / Ivy

There is a newer version: 2024.10.22
Show newest version
package cn.huangxulin.inspector;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;

/**
 * @author hxl
 */
public class Launcher {

    private static final Map PARAMS = new HashMap<>();

    private static final String WORK_DIR_KEY = "--work-dir";
    private static final String INPUT_KEY = "--input";
    private static final String OUTPUT_KEY = "--output";

    public static void main(String[] args) throws IOException {
        for (String arg : args) {
            String[] paramsInfo = StringUtils.split(arg, "=", 2);
            String paramValue = paramsInfo.length == 2 ? paramsInfo[1] : "";
            PARAMS.put(paramsInfo[0], paramValue);
        }
        String workDir = PARAMS.get(WORK_DIR_KEY);
        String input = PARAMS.get(INPUT_KEY);
        String output = PARAMS.get(OUTPUT_KEY);

        Path workspace;
        if (workDir == null) {
            workspace = Paths.get(System.getProperty("user.dir"));
        } else {
            workspace = Paths.get(workDir);
        }

        StringBuilder stringBuilder = new StringBuilder();
        Map checksumMap = computeChecksum(workspace);
        for (Map.Entry entry : checksumMap.entrySet()) {
            stringBuilder.append(entry.getValue()).append(" ").append(entry.getKey()).append("\n");
        }

        if (output == null) {
            System.out.println(stringBuilder);
            System.out.printf("\u001B[32mFileCount: %s\u001B[0m%n", checksumMap.size());
            System.out.printf("\u001B[32m Checksum: %s\u001B[0m%n%n", Md5Utils.md5(stringBuilder.toString()));
        } else {
            List originalChecksumLines;
            if (input == null) {
                originalChecksumLines = Collections.emptyList();
            } else {
                Path path = Paths.get(input);
                if (Files.exists(path)) {
                    originalChecksumLines = Files.readAllLines(path, StandardCharsets.UTF_8);
                } else {
                    originalChecksumLines = Collections.emptyList();
                }
            }
            Map originalChecksumMap = new LinkedHashMap<>(1024);
            for (String line : originalChecksumLines) {
                String[] parts = StringUtils.split(line, " ", 2);
                if (parts.length == 2) {
                    originalChecksumMap.put(parts[1], parts[0]);
                }
            }
            if (compareChecksum(originalChecksumMap, checksumMap)) {
                /* <此处直接写 IDEA 项目内已存在的文件时会报错: 请求的操作无法在使用用户映射区域打开的文件上执行> */
                Files.write(
                        Paths.get(output),
                        stringBuilder.toString().getBytes(StandardCharsets.UTF_8),
                        StandardOpenOption.CREATE,
                        StandardOpenOption.TRUNCATE_EXISTING
                );
                System.out.printf("[\u001B[34mINFO\u001B[0m] MD5 记录文件已更新%n");
            } else {
                System.out.printf("[\u001B[34mINFO\u001B[0m] 所有文件都匹配且无新文件或缺失文件%n");
            }
        }
    }

    private static boolean compareChecksum(
            Map originalChecksumMap, Map newChecksumMap) {
        boolean hasChanged = false;
        // 查找新增的依赖
        for (String fileName : newChecksumMap.keySet()) {
            if (!originalChecksumMap.containsKey(fileName)) {
                System.out.printf("[\u001B[33mWARNING\u001B[0m] \u001B[32mnew\u001B[0m %s%n", fileName);
                hasChanged = true;
            }
        }
        // 查找减少的依赖
        for (Map.Entry checksumInfo : originalChecksumMap.entrySet()) {
            String fileName = checksumInfo.getKey();
            String originalChecksum = checksumInfo.getValue();
            String newChecksum = newChecksumMap.get(fileName);
            if (newChecksum == null) {
                System.out.printf("[\u001B[33mWARNING\u001B[0m] \u001B[31mdel\u001B[0m %s%n", fileName);
                hasChanged = true;
            } else if (!originalChecksum.equals(newChecksum)) {
                // 查找修改的依赖
                System.out.printf(
                        "[\u001B[33mWARNING\u001B[0m] \u001B[36mmod\u001B[0m %s %s (实际) != %s (预期)%n",
                        fileName, newChecksum, originalChecksum
                );
                hasChanged = true;
            }
        }
        return hasChanged;
    }

    private static Map computeChecksum(Path workspace) throws IOException {
        Map checksumMap = new LinkedHashMap<>(1024);
        Files.walkFileTree(workspace, new SimpleFileVisitor() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Path relativePath = workspace.relativize(file);
                String fileName = relativePath.toString().replace('\\', '/');
                String fileMd5 = Md5Utils.md5(file.toFile());
                checksumMap.put(fileName, fileMd5);
                return FileVisitResult.CONTINUE;
            }
        });
        return checksumMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy