aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Melnichenko <mpeterval@gmail.com>2016-05-16 11:03:19 +0300
committerPeter Melnichenko <mpeterval@gmail.com>2016-05-16 11:03:19 +0300
commit699abc334895b5e737976d7aa6c1982a692d623b (patch)
tree4abec350f31e850685a79b24b0ca8ab8b016a36d
parent7f5095a307597cea30ab93b8b3173b06745ee715 (diff)
downloadluarocks-699abc334895b5e737976d7aa6c1982a692d623b.tar.gz
luarocks-699abc334895b5e737976d7aa6c1982a692d623b.tar.bz2
luarocks-699abc334895b5e737976d7aa6c1982a692d623b.zip
Refactor match_dep()
* Instead of applying blacklist and sorting versions in separate steps do a single pass over the list. * On success return just the version instead of {name = name, version = version} since rock name is stored inside dependency table already.
-rw-r--r--src/luarocks/deps.lua46
1 files changed, 15 insertions, 31 deletions
diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua
index 3c580623..536b6c51 100644
--- a/src/luarocks/deps.lua
+++ b/src/luarocks/deps.lua
@@ -322,48 +322,32 @@ end
322-- @param dep table: A dependency parsed in table format. 322-- @param dep table: A dependency parsed in table format.
323-- @param blacklist table: Versions that can't be accepted. Table where keys 323-- @param blacklist table: Versions that can't be accepted. Table where keys
324-- are program versions and values are 'true'. 324-- are program versions and values are 'true'.
325-- @return table or nil: A table containing fields 'name' and 'version' 325-- @return string or nil: latest installed version of the rock matching the dependency
326-- representing an installed rock which matches the given dependency,
327-- or nil if it could not be matched. 326-- or nil if it could not be matched.
328local function match_dep(dep, blacklist, deps_mode) 327local function match_dep(dep, blacklist, deps_mode)
329 assert(type(dep) == "table") 328 assert(type(dep) == "table")
330 329
331 local versions = cfg.rocks_provided[dep.name] 330 local versions
332 if cfg.rocks_provided[dep.name] then 331 if cfg.rocks_provided[dep.name] then
333 -- provided rocks have higher priority than manifest's rocks 332 -- provided rocks have higher priority than manifest's rocks
334 versions = { cfg.rocks_provided[dep.name] } 333 versions = { cfg.rocks_provided[dep.name] }
335 else 334 else
336 versions = manif_core.get_versions(dep.name, deps_mode) 335 versions = manif_core.get_versions(dep.name, deps_mode)
337 end 336 end
338 if not versions then 337
339 return nil 338 local latest_version
340 end
341 if blacklist then
342 local i = 1
343 while versions[i] do
344 if blacklist[versions[i]] then
345 table.remove(versions, i)
346 else
347 i = i + 1
348 end
349 end
350 end
351 local candidates = {}
352 for _, vstring in ipairs(versions) do 339 for _, vstring in ipairs(versions) do
353 local version = deps.parse_version(vstring) 340 if not blacklist or not blacklist[vstring] then
354 if deps.match_constraints(version, dep.constraints) then 341 local version = deps.parse_version(vstring)
355 table.insert(candidates, version) 342 if deps.match_constraints(version, dep.constraints) then
343 if not latest_version or version > latest_version then
344 latest_version = version
345 end
346 end
356 end 347 end
357 end 348 end
358 if #candidates == 0 then 349
359 return nil 350 return latest_version and latest_version.string
360 else
361 table.sort(candidates)
362 return {
363 name = dep.name,
364 version = candidates[#candidates].string
365 }
366 end
367end 351end
368 352
369--- Attempt to match dependencies of a rockspec to installed rocks. 353--- Attempt to match dependencies of a rockspec to installed rocks.
@@ -386,7 +370,7 @@ function deps.match_deps(rockspec, blacklist, deps_mode)
386 local found = match_dep(dep, blacklist and blacklist[dep.name] or nil, deps_mode) 370 local found = match_dep(dep, blacklist and blacklist[dep.name] or nil, deps_mode)
387 if found then 371 if found then
388 if not cfg.rocks_provided[dep.name] then 372 if not cfg.rocks_provided[dep.name] then
389 matched[dep] = found 373 matched[dep] = {name = dep.name, version = found}
390 end 374 end
391 else 375 else
392 if dep.constraints[1] and dep.constraints[1].no_upgrade then 376 if dep.constraints[1] and dep.constraints[1].no_upgrade then
@@ -414,7 +398,7 @@ local function rock_status(name, deps_mode)
414 local search = require("luarocks.search") 398 local search = require("luarocks.search")
415 local installed = match_dep(search.make_query(name), nil, deps_mode) 399 local installed = match_dep(search.make_query(name), nil, deps_mode)
416 local installation_type = cfg.rocks_provided[name] and "provided by VM" or "installed" 400 local installation_type = cfg.rocks_provided[name] and "provided by VM" or "installed"
417 return installed and installed.version.." "..installation_type or "not installed" 401 return installed and installed.." "..installation_type or "not installed"
418end 402end
419 403
420--- Check dependencies of a rock and attempt to install any missing ones. 404--- Check dependencies of a rock and attempt to install any missing ones.