aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/pep440/init.lua
blob: 526bb87ef798248927d15dc558a2593fad254f01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local function split_version(version)
    local parts = {}
    for part in version:gmatch "[^.]+" do
        table.insert(parts, tonumber(part) or part)
    end
    return parts
end

local function compare_versions(version1, version2)
    local v1_parts = split_version(version1)
    local v2_parts = split_version(version2)
    local len = math.max(#v1_parts, #v2_parts)

    for i = 1, len do
        local v1_part = v1_parts[i] or 0
        local v2_part = v2_parts[i] or 0

        if v1_part < v2_part then
            return -1
        elseif v1_part > v2_part then
            return 1
        end
    end

    return 0
end

local function check_single_specifier(version, specifier)
    local operator, spec_version = specifier:match "^([<>=!~]+)%s*(.+)$"
    local comp_result = compare_versions(version, spec_version)

    if operator == "==" then
        return comp_result == 0
    elseif operator == "!=" then
        return comp_result ~= 0
    elseif operator == "<=" then
        return comp_result <= 0
    elseif operator == "<" then
        return comp_result < 0
    elseif operator == ">=" then
        return comp_result >= 0
    elseif operator == ">" then
        return comp_result > 0
    elseif operator == "~=" then
        if comp_result < 0 then
            return false
        end
        local spec_version_components = split_version(spec_version)
        local version_components = split_version(version)
        for i = 1, #spec_version_components - 1 do
            if spec_version_components[i] ~= version_components[i] then
                return false
            end
        end
        return true
    else
        error("Unknown operator in version specifier: " .. operator)
    end
end

local function check_version(version, specifiers)
    for specifier in specifiers:gmatch "[^,]+" do
        if not check_single_specifier(version, specifier:match "^%s*(.-)%s*$") then
            return false
        end
    end
    return true
end

return {
    check_version = check_version,
}