Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base: deps_version_parser.lua #47

Closed
wants to merge 5 commits into from
Closed

Conversation

MoYingJi
Copy link
Collaborator

@MoYingJi MoYingJi commented Dec 8, 2024

这样大概可以了? #45

@MoYingJi MoYingJi requested a review from Sunrisepeak as a code owner December 8, 2024 07:50
@Sunrisepeak
Copy link
Member

在调整架构, 等待一起合入

@@ -1,5 +1,5 @@
-- 解析单个版本字符串到版本对象
function parse_single_version(version_str)
function parse_single_version_range(version_str)
local version = {}
if version_str == "any" or version_str == "*" then
Copy link
Collaborator

@lvyuemeng lvyuemeng Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local Version = {}
Version.__index = Version

function Version:new(str)
      local self = setmetatable({},Version}
      self:parse(str)
      return self
end

function Version:parse(str)
   local pattern = (...)
   local major,minor,patch,prerelease,build = str:match(pattern)
   self.major = tonumber(major) or 0
   self.minor = tonumber(minor) or 0
   self.patch = tonumber(patch) or 0
   self.prerelease = prerelease or ""
   self.build = build or "" 
end

function Version:compare(other)
    if self.major ~= other.major then
        return self.major - other.major
    end
    if self.minor ~= other.minor then
        return self.minor - other.minor
    end
    if self.patch ~= other.patch then
        return self.patch - other.patch
    end

    if self.prerelease ~= other.prerelease then
        if self.prerelease == "" then
            return 1
        elseif other.prerelease == "" then
            return -1
        else
            return self.prerelease < other.prerelease and 1 or -1
        end
    end

    return 0
end

function Version:__lt(other)
    return self:compare(other) < 0
end

function Version:__le(other)
    return self:compare(other) <= 0
end

function Version:__eq(other)
    return self:compare(other) == 0
end

function Version:__gt(other)
    return self:compare(other) > 0
end

function Version:__ge(other)
    return self:compare(other) >= 0
end

@@ -51,10 +51,10 @@ function parse_single_version(version_str)
end

-- 解析复合版本字符串到版本对象
function parse_version(version_str)
function parse_version_ranges(version_str)
Copy link
Collaborator

@lvyuemeng lvyuemeng Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local Constraint = {}
Constraint.__index = Constraint

function Constraint:new(str)
   local self = setmetatable({},Constraint)
   self:parse(str)
   return self
end

function Constraint:parse(str)
   local pattern = (...)
   local op,version_str = str:match(pattern)
   self.op = op
   self.version = Version:new(version_str)
end

function Constraint:include(version)
     if self.operator == "*" then
        return true  -- "*" means any version
    elseif self.operator == "<=" then
        return version <= self.version
    elseif self.operator == "<" then
        return version < self.version
    elseif self.operator == ">=" then
        return version >= self.version
    elseif self.operator == ">" then
        return version > self.version
    elseif self.operator == "=" then
        return version == self.version
    else
        return false
    end
end

Copy link
Collaborator

@lvyuemeng lvyuemeng Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local Constraints = {}
Constraints.__index = Constraints

function Constraints:new(str)
   local self = setmetatable({},Constraints)
   self:parse(str)
   return self
end

function Constraints:parse(str)
   self.constraints = {}
   for part in str:gmatch(...) do
        table.insert(self.constraints,Constraint:new(part))
    end
end

function Constraints:include(version)
    for _,constraint in ipairs(self.constraints) do
          if not constraint:include(verison) then
                 return false
          end
     end
     return true
end

@MoYingJi MoYingJi deleted the branch d2learn:main December 22, 2024 11:05
@MoYingJi MoYingJi closed this Dec 22, 2024
@MoYingJi MoYingJi deleted the main branch December 22, 2024 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants