Skip to content

Commit e134092

Browse files
committed
refactor: group installLuarcFiles functions
1 parent 58e3cf2 commit e134092

1 file changed

Lines changed: 135 additions & 136 deletions

File tree

src/luarocks/build/lls-addon.lua

Lines changed: 135 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -459,166 +459,165 @@ local function findLuarcFiles(projectDir, env)
459459
end
460460
M.findLuarcFiles = findLuarcFiles
461461

462-
---@param list unknown[]
463-
---@param value unknown[]
464-
---@return integer?
465-
local function tableFind(list, value)
466-
for i, v in ipairs(list) do
467-
if v == value then
468-
return i
462+
local installLuarcFiles
463+
do
464+
---@param list unknown[]
465+
---@param value unknown[]
466+
---@return integer?
467+
local function tableFind(list, value)
468+
for i, v in ipairs(list) do
469+
if v == value then
470+
return i
471+
end
469472
end
470-
end
471473

472-
return nil
473-
end
474-
475-
---@param pointsToWrongVersion fun(path: string): boolean
476-
---@param config { [string]: any }
477-
---@param configEntries lls-addon.config-entry[]
478-
---@param luarcType "vscode settings" | "luarc"
479-
local function applyConfigEntries(pointsToWrongVersion, config, configEntries, luarcType)
480-
local prefix = ""
481-
local nested ---@type boolean
482-
if luarcType == "vscode settings" then
483-
prefix = "Lua."
484-
nested = false
485-
elseif luarcType == "luarc" then
486-
nested = true
487-
else
488-
-- luacov: disable
489-
error("Unreachable: Unknown config file type " .. tostring(luarcType))
490-
-- luacov: enable
474+
return nil
491475
end
492476

493-
local primary, secondary ---@type lls-addon.path-getter, lls-addon.path-getter
494-
if nested then
495-
primary, secondary = nestedPath, unnestedPath
496-
else
497-
primary, secondary = unnestedPath, nestedPath
477+
---@param config { [string]: any }
478+
---@param key string
479+
---@param primary lls-addon.path-getter
480+
---@param secondary lls-addon.path-getter
481+
---@param nested boolean
482+
---@return any[]
483+
local function getConfigArray(config, key, primary, secondary, nested)
484+
local oldValue1 = primary.get(config, key)
485+
local oldValue1_isArray = json.isArray(oldValue1)
486+
local oldValue2 = secondary.get(config, key)
487+
local oldValue2_isArray = json.isArray(oldValue2)
488+
if oldValue1_isArray and oldValue2_isArray then
489+
secondary.set(config, key, nil)
490+
extend(nested, oldValue1, unnest2(oldValue2))
491+
return oldValue1
492+
elseif oldValue2_isArray then
493+
return oldValue2
494+
elseif oldValue1_isArray then
495+
return oldValue1
496+
else
497+
local list = json.array({})
498+
primary.set(config, key, list)
499+
return list
500+
end
498501
end
499502

500-
for _, entry in ipairs(configEntries) do
501-
local action = entry.action
502-
if action == "prepend" or action == "append" then
503-
---@cast entry lls-addon.config-entry.prepend
504-
local key, value = prefix .. entry.key, entry.value
505-
local list ---@type any[]
506-
local oldValue1 = primary.get(config, key)
507-
local oldValue1_isArray = json.isArray(oldValue1)
508-
local oldValue2 = secondary.get(config, key)
509-
local oldValue2_isArray = json.isArray(oldValue2)
510-
if oldValue1_isArray and oldValue2_isArray then
511-
secondary.set(config, key, nil)
512-
extend(nested, oldValue1, unnest2(oldValue2))
513-
list = oldValue1
514-
elseif oldValue2_isArray then
515-
list = oldValue2
516-
elseif oldValue1_isArray then
517-
list = oldValue1
518-
else
519-
list = json.array({})
520-
primary.set(config, key, list)
521-
end
503+
---@param pointsToWrongVersion fun(path: string): boolean
504+
---@param config { [string]: any }
505+
---@param configEntries lls-addon.config-entry[]
506+
---@param luarcType "vscode settings" | "luarc"
507+
local function applyConfigEntries(pointsToWrongVersion, config, configEntries, luarcType)
508+
local prefix ---@type string
509+
local nested ---@type boolean
510+
if luarcType == "vscode settings" then
511+
prefix = "Lua."
512+
nested = false
513+
elseif luarcType == "luarc" then
514+
prefix = ""
515+
nested = true
516+
else
517+
-- luacov: disable
518+
error("Unreachable: Unknown config file type " .. tostring(luarcType))
519+
-- luacov: enable
520+
end
521+
522+
local primary, secondary ---@type lls-addon.path-getter, lls-addon.path-getter
523+
if nested then
524+
primary, secondary = nestedPath, unnestedPath
525+
else
526+
primary, secondary = unnestedPath, nestedPath
527+
end
528+
529+
local function getConfigArray2(key)
530+
return getConfigArray(config, key, primary, secondary, nested)
531+
end
522532

523-
if not entry.dedup or not tableFind(list, value) then
524-
if action == "prepend" then
525-
table.insert(list, 1, value)
533+
for _, entry in ipairs(configEntries) do
534+
local action = entry.action
535+
if action == "prepend" or action == "append" then
536+
---@cast entry lls-addon.config-entry.prepend | lls-addon.config-entry.append
537+
local key, value = prefix .. entry.key, entry.value
538+
local list = getConfigArray2(key)
539+
540+
if not entry.dedup or not tableFind(list, value) then
541+
if action == "prepend" then
542+
table.insert(list, 1, value)
543+
else
544+
table.insert(list, value)
545+
end
546+
end
547+
elseif action == "merge" then
548+
---@cast entry lls-addon.config-entry.merge
549+
local value = unnest2(entry.value)
550+
if prefix ~= "" then
551+
local prefixedValue = json.object({})
552+
for k, v in pairs(value) do
553+
prefixedValue[prefix .. k] = v
554+
end
555+
extend(nested, config, prefixedValue)
526556
else
527-
table.insert(list, value)
557+
extend(nested, config, value)
528558
end
529-
end
530-
elseif action == "merge" then
531-
---@cast entry lls-addon.config-entry.merge
532-
local value = unnest2(entry.value)
533-
if prefix ~= "" then
534-
local prefixedValue = json.object({})
535-
for k, v in pairs(value) do
536-
prefixedValue[prefix .. k] = v
559+
elseif action == "remove-deleted-versions" then
560+
---@cast entry lls-addon.config-entry.remove-deleted-versions
561+
local key = prefix .. entry.key
562+
563+
local list = getConfigArray2(key) --[[@as unknown[] ]]
564+
for i = #list, 1, -1 do
565+
local v = list[i]
566+
if type(v) ~= "string" or pointsToWrongVersion(v) then
567+
table.remove(list, i)
568+
end
537569
end
538-
extend(nested, config, prefixedValue)
539-
else
540-
extend(nested, config, value)
541-
end
542-
elseif action == "remove-deleted-versions" then
543-
---@cast entry lls-addon.config-entry.remove-deleted-versions
544-
local key = prefix .. entry.key
545-
546-
local list ---@type string[]
547-
do
570+
elseif action == "set" then
571+
---@cast entry lls-addon.config-entry.set
572+
local key, value = prefix .. entry.key, entry.value
548573
local oldValue1 = primary.get(config, key)
549-
local oldValue1_isArray = json.isArray(oldValue1)
550574
local oldValue2 = secondary.get(config, key)
551-
local oldValue2_isArray = json.isArray(oldValue2)
552-
if oldValue1_isArray and oldValue2_isArray then
575+
if oldValue1 ~= nil and oldValue2 ~= nil then
553576
secondary.set(config, key, nil)
554-
extend(nested, oldValue1, unnest2(oldValue2))
555-
list = oldValue1
556-
elseif oldValue2_isArray then
557-
list = oldValue2
558-
elseif oldValue1_isArray then
559-
list = oldValue1
577+
primary.set(config, key, value)
578+
elseif oldValue2 ~= nil then
579+
secondary.set(config, key, value)
560580
else
561-
list = json.array({})
562-
primary.set(config, key, list)
581+
primary.set(config, key, value)
563582
end
564-
end
565-
566-
for i = #list, 1, -1 do
567-
local v = list[i]
568-
if pointsToWrongVersion(v) then
569-
table.remove(list, i)
570-
end
571-
end
572-
elseif action == "set" then
573-
---@cast entry lls-addon.config-entry.set
574-
local key, value = prefix .. entry.key, entry.value
575-
local oldValue1 = primary.get(config, key)
576-
local oldValue2 = secondary.get(config, key)
577-
if oldValue1 ~= nil and oldValue2 ~= nil then
578-
secondary.set(config, key, nil)
579-
primary.set(config, key, value)
580-
elseif oldValue2 ~= nil then
581-
secondary.set(config, key, value)
582583
else
583-
primary.set(config, key, value)
584+
-- luacov: disable
585+
error("Unreachable: unknown action " .. tostring(action))
586+
-- luacov: enable
584587
end
585-
else
586-
-- luacov: disable
587-
error("Unreachable: unknown action " .. tostring(action))
588-
-- luacov: enable
589588
end
590589
end
591-
end
592590

593-
---@param rockspec luarocks.Rockspec
594-
---@param luarcFiles lls-addon.luarc-file[]
595-
---@param configEntries lls-addon.config-entry[]
596-
local function installLuarcFiles(rockspec, luarcFiles, configEntries)
597-
local pointsToWrongVersion
598-
do
599-
local projectDir = getProjectDir()
600-
local packageDir = dir.path(path.rocks_dir(), rockspec.package)
601-
local packageDirLen = string.len(packageDir)
602-
local currentVersionDir = dir.path(packageDir, rockspec.version)
603-
local currentVersionDirLen = string.len(currentVersionDir)
604-
605-
function pointsToWrongVersion(installPath)
606-
local absPath = fs.absolute_name(installPath, projectDir)
607-
return string.sub(absPath, 1, packageDirLen) == packageDir
608-
and string.sub(absPath, 1, currentVersionDirLen) ~= currentVersionDir
591+
---@param rockspec luarocks.Rockspec
592+
---@param luarcFiles lls-addon.luarc-file[]
593+
---@param configEntries lls-addon.config-entry[]
594+
function installLuarcFiles(rockspec, luarcFiles, configEntries)
595+
local pointsToWrongVersion
596+
do
597+
local projectDir = getProjectDir()
598+
local packageDir = dir.path(path.rocks_dir(), rockspec.package)
599+
local packageDirLen = string.len(packageDir)
600+
local currentVersionDir = dir.path(packageDir, rockspec.version)
601+
local currentVersionDirLen = string.len(currentVersionDir)
602+
603+
function pointsToWrongVersion(installPath)
604+
local absPath = fs.absolute_name(installPath, projectDir)
605+
return string.sub(absPath, 1, packageDirLen) == packageDir
606+
and string.sub(absPath, 1, currentVersionDirLen) ~= currentVersionDir
607+
end
609608
end
610-
end
611609

612-
for _, luarcFile in ipairs(luarcFiles) do
613-
local type = luarcFile.type
614-
local path = luarcFile.path
615-
log.info(string.format("writing to %s: %s", type, path))
616-
local oldConfig = readOrCreateLuarc(path)
617-
applyConfigEntries(pointsToWrongVersion, oldConfig, configEntries, type)
618-
json.write(path, oldConfig, { sortKeys = true })
610+
for _, luarcFile in ipairs(luarcFiles) do
611+
local type = luarcFile.type
612+
local path = luarcFile.path
613+
log.info(string.format("writing to %s: %s", type, path))
614+
local oldConfig = readOrCreateLuarc(path)
615+
applyConfigEntries(pointsToWrongVersion, oldConfig, configEntries, type)
616+
json.write(path, oldConfig, { sortKeys = true })
617+
end
619618
end
619+
M.installLuarcFiles = installLuarcFiles
620620
end
621-
M.installLuarcFiles = installLuarcFiles
622621

623622
---does two things:
624623
---- copies the library/, config.json and plugin.lua into the rock's install

0 commit comments

Comments
 (0)