Skip to content

Commit cac6368

Browse files
committed
feat: remove old versions of a plugin
1 parent 86ff76c commit cac6368

4 files changed

Lines changed: 112 additions & 17 deletions

File tree

spec/behavior_spec.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ end
160160
---@type "path/to/lls-addon-loader.lua"
161161
local FAKE_LOADER_SOURCE = path("path", "to", "lls-addon-loader.lua")
162162

163+
---@param projectDir string?
164+
local function stubFakeLoaderSource(projectDir)
165+
local oldIsFile = luarocks.fs.is_file
166+
local cd = projectDir and path(luarocks.fs.current_dir(), projectDir) or luarocks.fs.current_dir()
167+
local absoluteFakeLoaderSource =
168+
luarocks.fs.absolute_name(FAKE_LOADER_SOURCE, path(luarocks.fs.current_dir(), projectDir))
169+
stub(luarocks.fs, "is_file", function(filePath)
170+
return filePath == FAKE_LOADER_SOURCE or filePath == absoluteFakeLoaderSource or oldIsFile(filePath)
171+
end)
172+
end
173+
163174
---@param options? lls-addon.spec.makeProject.options
164175
local function makeProject(options)
165176
options = options or {}
@@ -214,6 +225,7 @@ local function setupProject(dir, options)
214225
assertNoRoot(dir)
215226
finally = upgradeFinally(finally)
216227
pushDir(dir)
228+
stubFakeLoaderSource(options and options.projectDir)
217229
makeProject(options)
218230
end
219231

@@ -439,7 +451,8 @@ describe("luarocks-build-lls-addon", function()
439451
assert.is_nil(mode(path(INSTALL_DIR, "library")))
440452
assert.is_nil(mode(".luarc.json"))
441453
assert.is_nil(mode(path(INSTALL_DIR, "config.json")))
442-
assert.is_nil(mode(path(LUA_DIR, "types.lua")))
454+
assert.is_nil(mode(path(INSTALL_DIR, "plugin.lua")))
455+
assert.is_nil(mode(path(INSTALL_DIR, "plugin")))
443456
end)
444457

445458
it("installs to unique paths", function()

spec/lls-addon_spec.lua

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ describe("lls-addon", function()
137137
dedup = true,
138138
key = "workspace.library",
139139
value = path(ROCKS_DIR, INSTALL_DIR, "library"),
140-
} --[[@as lls-addon.config-entry.append]],
141-
}, configEntries)
140+
},
141+
{
142+
action = "remove-deleted-versions",
143+
key = "workspace.library",
144+
},
145+
} --[[@as lls-addon.config-entry[] ]], configEntries)
142146
assert.are_same({
143147
{
144148
type = "directory",
@@ -163,14 +167,18 @@ describe("lls-addon", function()
163167
dedup = true,
164168
key = "runtime.plugin",
165169
value = LOADER_SOURCE,
166-
} --[[@as lls-addon.config-entry.prepend]],
170+
},
167171
{
168172
action = "append",
169173
dedup = true,
170174
key = "runtime.plugin",
171175
value = path(ROCKS_DIR, installDir("lls-addon-types", VERSION), "plugin.lua"),
172-
} --[[@as lls-addon.config-entry.append]],
173-
}, configEntries)
176+
},
177+
{
178+
action = "remove-deleted-versions",
179+
key = "runtime.plugin",
180+
},
181+
} --[[@as lls-addon.config-entry[] ]], configEntries)
174182
assert.are_same({
175183
{
176184
type = "file",
@@ -196,14 +204,18 @@ describe("lls-addon", function()
196204
dedup = true,
197205
key = "runtime.plugin",
198206
value = LOADER_SOURCE,
199-
} --[[@as lls-addon.config-entry.prepend]],
207+
},
200208
{
201209
action = "append",
202210
dedup = true,
203211
key = "runtime.plugin",
204212
value = path(ROCKS_DIR, installDir("lls-addon-types", VERSION), "plugin.lua"),
205-
} --[[@as lls-addon.config-entry.append]],
206-
}, configEntries)
213+
},
214+
{
215+
action = "remove-deleted-versions",
216+
key = "runtime.plugin",
217+
},
218+
} --[[@as lls-addon.config-entry[] ]], configEntries)
207219
assert.are_same({
208220
{
209221
type = "file",
@@ -383,6 +395,10 @@ describe("lls-addon", function()
383395
key = "workspace.library",
384396
value = path(ROCKS_DIR, INSTALL_DIR, "library"),
385397
},
398+
{
399+
action = "remove-deleted-versions",
400+
key = "workspace.library",
401+
},
386402
{
387403
action = "prepend",
388404
dedup = true,
@@ -395,14 +411,18 @@ describe("lls-addon", function()
395411
key = "runtime.plugin",
396412
value = path(ROCKS_DIR, INSTALL_DIR, "plugin.lua"),
397413
},
414+
{
415+
action = "remove-deleted-versions",
416+
key = "runtime.plugin",
417+
},
398418
{
399419
action = "merge",
400420
value = {
401421
["workspace.library"] = { "anotherLibrary" },
402422
["runtime.plugin"] = "anotherPlugin.lua",
403423
},
404424
},
405-
}, configEntries)
425+
} --[[@as lls-addon.config-entry[] ]], configEntries)
406426
assert.are_equal(2, #installEntries)
407427
assert.contains({
408428
type = "directory",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- also does nothing

src/luarocks/build/lls-addon.lua

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,16 @@ end
245245
---@field action "merge"
246246
---@field value { [string]: unknown }
247247

248+
---@class lls-addon.config-entry.remove-deleted-versions
249+
---@field action "remove-deleted-versions"
250+
---@field key string
251+
248252
---@alias lls-addon.config-entry
249253
---| lls-addon.config-entry.append
250254
---| lls-addon.config-entry.prepend
251255
---| lls-addon.config-entry.set
252256
---| lls-addon.config-entry.merge
257+
---| lls-addon.config-entry.remove-deleted-versions
253258

254259
---@class lls-addon.install-entry
255260
---@field type "file" | "directory"
@@ -301,6 +306,11 @@ local function compileLuarc(rockspec, env)
301306
value = formattedLibraryDestination,
302307
} --[[@as lls-addon.config-entry.append]])
303308

309+
table.insert(configEntries, {
310+
action = "remove-deleted-versions",
311+
key = "workspace.library",
312+
} --[[@as lls-addon.config-entry.remove-deleted-versions]])
313+
304314
table.insert(installEntries, {
305315
type = "directory",
306316
source = librarySource,
@@ -338,6 +348,11 @@ local function compileLuarc(rockspec, env)
338348
value = formattedPluginDestination,
339349
} --[[@as lls-addon.config-entry.append]])
340350

351+
table.insert(configEntries, {
352+
action = "remove-deleted-versions",
353+
key = "runtime.plugin",
354+
} --[[@as lls-addon.config-entry.remove-deleted-versions]])
355+
341356
table.insert(installEntries, {
342357
type = "file",
343358
source = pluginSource,
@@ -450,13 +465,13 @@ local function tableFind(list, value)
450465
return nil
451466
end
452467

468+
---@param pointsToWrongVersion fun(path: string): boolean
453469
---@param config { [string]: any }
454470
---@param configEntries lls-addon.config-entry[]
455471
---@param luarcType "vscode settings" | "luarc"
456-
local function applyConfigEntries(config, configEntries, luarcType)
472+
local function applyConfigEntries(pointsToWrongVersion, config, configEntries, luarcType)
457473
local prefix = ""
458474
local nested ---@type boolean
459-
local transformMerge ---@type fun(config: { [string]: any }): { [string]: any }
460475
if luarcType == "vscode settings" then
461476
prefix = "Lua."
462477
nested = false
@@ -487,7 +502,7 @@ local function applyConfigEntries(config, configEntries, luarcType)
487502
local oldValue2_isArray = json.isArray(oldValue2)
488503
if oldValue1_isArray and oldValue2_isArray then
489504
secondary.set(config, key, nil)
490-
extend(nested, oldValue1, unnest2(transformMerge(oldValue2)))
505+
extend(nested, oldValue1, unnest2(oldValue2))
491506
list = oldValue1
492507
elseif oldValue2_isArray then
493508
list = oldValue2
@@ -517,6 +532,36 @@ local function applyConfigEntries(config, configEntries, luarcType)
517532
else
518533
extend(nested, config, value)
519534
end
535+
elseif action == "remove-deleted-versions" then
536+
---@cast entry lls-addon.config-entry.remove-deleted-versions
537+
local key = prefix .. entry.key
538+
539+
local list ---@type string[]
540+
do
541+
local oldValue1 = primary.get(config, key)
542+
local oldValue1_isArray = json.isArray(oldValue1)
543+
local oldValue2 = secondary.get(config, key)
544+
local oldValue2_isArray = json.isArray(oldValue2)
545+
if oldValue1_isArray and oldValue2_isArray then
546+
secondary.set(config, key, nil)
547+
extend(nested, oldValue1, unnest2(oldValue2))
548+
list = oldValue1
549+
elseif oldValue2_isArray then
550+
list = oldValue2
551+
elseif oldValue1_isArray then
552+
list = oldValue1
553+
else
554+
list = json.array({})
555+
primary.set(config, key, list)
556+
end
557+
end
558+
559+
for i = #list, 1, -1 do
560+
local v = list[i]
561+
if pointsToWrongVersion(v) then
562+
table.remove(list, i)
563+
end
564+
end
520565
elseif action == "set" then
521566
---@cast entry lls-addon.config-entry.set
522567
local key, value = prefix .. entry.key, entry.value
@@ -538,15 +583,31 @@ local function applyConfigEntries(config, configEntries, luarcType)
538583
end
539584
end
540585

586+
---@param rockspec luarocks.Rockspec
541587
---@param luarcFiles lls-addon.luarc-file[]
542588
---@param configEntries lls-addon.config-entry[]
543-
local function installLuarcFiles(luarcFiles, configEntries)
589+
local function installLuarcFiles(rockspec, luarcFiles, configEntries)
590+
local pointsToWrongVersion
591+
do
592+
local projectDir = getProjectDir()
593+
local packageDir = dir.path(path.rocks_dir(), rockspec.package)
594+
local packageDirLen = string.len(packageDir)
595+
local currentVersionDir = dir.path(packageDir, rockspec.version)
596+
local currentVersionDirLen = string.len(currentVersionDir)
597+
598+
function pointsToWrongVersion(installPath)
599+
local absPath = fs.absolute_name(installPath, projectDir)
600+
return string.sub(absPath, 1, packageDirLen) == packageDir
601+
and string.sub(absPath, 1, currentVersionDirLen) ~= currentVersionDir
602+
end
603+
end
604+
544605
for _, luarcFile in ipairs(luarcFiles) do
545606
local type = luarcFile.type
546607
local path = luarcFile.path
547608
log.info(string.format("writing to %s: %s", type, path))
548609
local oldConfig = readOrCreateLuarc(path)
549-
applyConfigEntries(oldConfig, configEntries, type)
610+
applyConfigEntries(pointsToWrongVersion, oldConfig, configEntries, type)
550611
json.write(path, oldConfig, { sortKeys = true })
551612
end
552613
end
@@ -577,10 +638,10 @@ local function installAddon(rockspec, env, noInstall)
577638
return
578639
end
579640

580-
installLuarcFiles(luarcFiles, configEntries)
581-
582641
-- for copying library, plugin.lua, and config.json
583642
installFiles(installEntries)
643+
644+
installLuarcFiles(rockspec, luarcFiles, configEntries)
584645
end
585646
M.installAddon = installAddon
586647

0 commit comments

Comments
 (0)