package xtools.boot.core.utils;

import lombok.extern.slf4j.Slf4j;
import xtools.core.CollectionUtils;
import xtools.core.sys.SysBaseInfoUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>Title : JarUtils</p>
 * <p>Description : JarUtils</p>
 * <p>DevelopTools : Idea_x64_v2026.1</p>
 * <p>DevelopSystem : macOS Sequoia 15.7.5</p>
 * <p>Company : org.xujun</p>
 *
 * @author : XuJun
 * @version : 5.0.0
 * @date : 2026/2/18 20:57
 */
@Slf4j
public class JarUtils implements Serializable {

    /**
     * 获取所有jar名称
     *
     * @return jar名称
     */
    public static List<String> getJarName() {
        String end = "jar";
        List<String> mfPath = null;
        try {
            mfPath = SysBaseInfoUtils.getMfPath();
        } catch (Exception e) {
            log.error("获取[MANIFEST.MF]文件路径失败", e);
        }
        if (CollectionUtils.isEmpty(mfPath)) {
            return null;
        }
        List<String> jarList = new ArrayList<>();
        mfPath.stream().filter(Objects::nonNull).forEach(item -> {
            item = item.replaceAll("!/META-INF/MANIFEST.MF", "");
            item = item.substring(item.lastIndexOf("/") + 1);
            if (item.endsWith(end)) {
                jarList.add(item);
            }
        });
        return jarList.stream().sorted().toList();
    }

}
