|
|
|
|
|
local queries = {}
|
|
|
|
|
|
local vers = require("luarocks.core.vers")
|
|
|
local util = require("luarocks.util")
|
|
|
local cfg = require("luarocks.core.cfg")
|
|
|
|
|
|
local query_mt = {}
|
|
|
|
|
|
query_mt.__index = query_mt
|
|
|
|
|
|
function query_mt.type()
|
|
|
return "query"
|
|
|
end
|
|
|
|
|
|
|
|
|
query_mt.arch = {
|
|
|
src = true,
|
|
|
all = true,
|
|
|
rockspec = true,
|
|
|
installed = true,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
query_mt.substring = false
|
|
|
|
|
|
|
|
|
|
|
|
local function arch_to_table(input)
|
|
|
if type(input) == "table" then
|
|
|
return input
|
|
|
elseif type(input) == "string" then
|
|
|
local arch = {}
|
|
|
for a in input:gmatch("[%w_-]+") do
|
|
|
arch[a] = true
|
|
|
end
|
|
|
return arch
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function queries.new(ns_name, version, substring, arch, operator)
|
|
|
assert(type(ns_name) == "string")
|
|
|
assert(type(version) == "string" or not version)
|
|
|
assert(type(substring) == "boolean" or not substring)
|
|
|
assert(type(arch) == "string" or not arch)
|
|
|
assert(type(operator) == "string" or not operator)
|
|
|
|
|
|
operator = operator or "=="
|
|
|
|
|
|
local name, namespace = util.split_namespace(ns_name)
|
|
|
|
|
|
local self = {
|
|
|
name = name,
|
|
|
namespace = namespace,
|
|
|
constraints = {},
|
|
|
substring = substring,
|
|
|
arch = arch_to_table(arch),
|
|
|
}
|
|
|
if version then
|
|
|
table.insert(self.constraints, { op = operator, version = vers.parse_version(version)})
|
|
|
end
|
|
|
|
|
|
query_mt.arch[cfg.arch] = true
|
|
|
return setmetatable(self, query_mt)
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function queries.all(arch)
|
|
|
assert(type(arch) == "string" or not arch)
|
|
|
|
|
|
return queries.new("", nil, true, arch)
|
|
|
end
|
|
|
|
|
|
do
|
|
|
local parse_constraints
|
|
|
do
|
|
|
local parse_constraint
|
|
|
do
|
|
|
local operators = {
|
|
|
["=="] = "==",
|
|
|
["~="] = "~=",
|
|
|
[">"] = ">",
|
|
|
["<"] = "<",
|
|
|
[">="] = ">=",
|
|
|
["<="] = "<=",
|
|
|
["~>"] = "~>",
|
|
|
|
|
|
[""] = "==",
|
|
|
["="] = "==",
|
|
|
["!="] = "~="
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parse_constraint = function(input)
|
|
|
assert(type(input) == "string")
|
|
|
|
|
|
local no_upgrade, op, version, rest = input:match("^(@?)([<>=~!]*)%s*([%w%.%_%-]+)[%s,]*(.*)")
|
|
|
local _op = operators[op]
|
|
|
version = vers.parse_version(version)
|
|
|
if not _op then
|
|
|
return nil, "Encountered bad constraint operator: '"..tostring(op).."' in '"..input.."'"
|
|
|
end
|
|
|
if not version then
|
|
|
return nil, "Could not parse version from constraint: '"..input.."'"
|
|
|
end
|
|
|
return { op = _op, version = version, no_upgrade = no_upgrade=="@" and true or nil }, rest
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parse_constraints = function(input)
|
|
|
assert(type(input) == "string")
|
|
|
|
|
|
local constraints, oinput, constraint = {}, input
|
|
|
while #input > 0 do
|
|
|
constraint, input = parse_constraint(input)
|
|
|
if constraint then
|
|
|
table.insert(constraints, constraint)
|
|
|
else
|
|
|
return nil, "Failed to parse constraint '"..tostring(oinput).."' with error: ".. input
|
|
|
end
|
|
|
end
|
|
|
return constraints
|
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function queries.from_dep_string(depstr)
|
|
|
assert(type(depstr) == "string")
|
|
|
|
|
|
local ns_name, rest = depstr:match("^%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)")
|
|
|
if not ns_name then
|
|
|
return nil, "failed to extract dependency name from '"..depstr.."'"
|
|
|
end
|
|
|
|
|
|
local constraints, err = parse_constraints(rest)
|
|
|
if not constraints then
|
|
|
return nil, err
|
|
|
end
|
|
|
|
|
|
local name, namespace = util.split_namespace(ns_name)
|
|
|
|
|
|
local self = {
|
|
|
name = name,
|
|
|
namespace = namespace,
|
|
|
constraints = constraints,
|
|
|
}
|
|
|
|
|
|
query_mt.arch[cfg.arch] = true
|
|
|
return setmetatable(self, query_mt)
|
|
|
end
|
|
|
end
|
|
|
|
|
|
function queries.from_persisted_table(tbl)
|
|
|
query_mt.arch[cfg.arch] = true
|
|
|
return setmetatable(tbl, query_mt)
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function query_mt:__tostring()
|
|
|
local out = {}
|
|
|
if self.namespace then
|
|
|
table.insert(out, self.namespace)
|
|
|
table.insert(out, "/")
|
|
|
end
|
|
|
table.insert(out, self.name)
|
|
|
|
|
|
if #self.constraints > 0 then
|
|
|
local pretty = {}
|
|
|
for _, c in ipairs(self.constraints) do
|
|
|
local v = c.version.string
|
|
|
if c.op == "==" then
|
|
|
table.insert(pretty, v)
|
|
|
else
|
|
|
table.insert(pretty, c.op .. " " .. v)
|
|
|
end
|
|
|
end
|
|
|
table.insert(out, " ")
|
|
|
table.insert(out, table.concat(pretty, ", "))
|
|
|
end
|
|
|
|
|
|
return table.concat(out)
|
|
|
end
|
|
|
|
|
|
return queries
|
|
|
|