Skip to content

Commit 61c548f

Browse files
committed
feat(registry): add registry_cache setting for controlling cache behaviour
1 parent 8e921c2 commit 61c548f

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

lua/mason-registry/init.lua

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local EventEmitter = require "mason-core.EventEmitter"
22
local InstallLocation = require "mason-core.installer.InstallLocation"
33
local log = require "mason-core.log"
44
local path = require "mason-core.path"
5+
local settings = require "mason.settings"
56
local uv = vim.loop
67
local LazySourceCollection = require "mason-registry.sources"
78

@@ -165,18 +166,20 @@ function Registry.update(callback)
165166
a.run(update, callback or noop, Registry.sources)
166167
end
167168

168-
local REGISTRY_STORE_TTL = 86400 -- 24 hrs
169-
170169
---@param sources LazySourceCollection
171170
---@param callback? RegistryUpdateCallback
172171
local function refresh(sources, callback)
172+
if not settings.current.registry_cache.refresh then
173+
log.debug "Not performing a registry refresh as it's disabled in settings."
174+
return true, {}
175+
end
173176
local a = require "mason-core.async"
174177

175178
local state = sources:get_install_state()
176179
if state and sources:is_all_installed() then
177180
local registry_age = os.time() - state.timestamp
178181

179-
if registry_age <= REGISTRY_STORE_TTL and state.checksum == sources:checksum() then
182+
if registry_age <= settings.current.registry_cache.duration and state.checksum == sources:checksum() then
180183
log.fmt_debug(
181184
"Registry refresh is not necessary yet. Registry age=%d, checksum=%s",
182185
registry_age,
@@ -185,14 +188,14 @@ local function refresh(sources, callback)
185188
if callback then
186189
callback(true, {})
187190
end
188-
return
191+
return true, {}
189192
end
190193
end
191194

192195
if not callback then
193196
-- We don't want to error in the synchronous version because of how this function is recommended to be used in
194197
-- 3rd party code. If accessing the update result is required, users are recommended to pass a callback.
195-
pcall(a.run_blocking, update, sources)
198+
return pcall(a.run_blocking, update, sources)
196199
else
197200
a.run(update, callback, sources)
198201
end
@@ -201,13 +204,13 @@ end
201204
---@param callback? RegistryUpdateCallback
202205
function Registry.refresh(callback)
203206
log.debug "Refreshing the registry."
204-
refresh(Registry.sources, callback)
207+
return refresh(Registry.sources, callback)
205208
end
206209

207210
---@param callback? RegistryUpdateCallback
208211
function Registry.refresh_system(callback)
209212
log.debug "Refreshing the system registry."
210-
refresh(Registry.system_sources, callback)
213+
return refresh(Registry.system_sources, callback)
211214
end
212215

213216
return Registry

lua/mason/settings.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ local DEFAULT_SETTINGS = {
4242
"github:mason-org/mason-system-registry",
4343
},
4444

45+
registry_cache = {
46+
---@since 2.3.0
47+
-- [Advanced setting]
48+
-- Whether Mason should automatically refresh the registry when needed. If false, the registry will have to be
49+
-- updated manually via :MasonUpdate or the :Mason UI.
50+
refresh = true,
51+
52+
---@since 2.3.0
53+
-- Amount of seconds before the local registry cache is considered stale.
54+
-- Note that this setting has no effect if refresh is set to false.
55+
duration = 24 * 60 * 60, -- 24 hours
56+
},
57+
4558
---@since 1.0.0
4659
-- The provider implementations to use for resolving supplementary package metadata (e.g., all available versions).
4760
-- Accepts multiple entries, where later entries will be used as fallback should prior providers fail.

tests/mason-registry/registry_spec.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local Pkg = require "mason-core.package"
2+
local match = require "luassert.match"
23
local registry = require "mason-registry"
4+
local spy = require "luassert.spy"
35
local test_helpers = require "mason-test.helpers"
46

57
describe("mason-registry", function()
@@ -33,4 +35,32 @@ describe("mason-registry", function()
3335
test_helpers.sync_install(dummy)
3436
assert.is_true(registry.is_installed "dummy")
3537
end)
38+
39+
describe("refresh/update", function()
40+
local a = require "mason-core.async"
41+
42+
it("should refresh registry synchronously", function()
43+
local ok, updated_registries = registry.refresh()
44+
assert.is_true(ok)
45+
assert.same(updated_registries, {})
46+
end)
47+
48+
it("should call registry.refresh callback", function()
49+
local spy = spy.new()
50+
registry.refresh(spy)
51+
assert.wait(function()
52+
assert.spy(spy).was_called(1)
53+
assert.spy(spy).was_called_with(true, {})
54+
end)
55+
end)
56+
57+
it("should call registry.update callback", function()
58+
local spy = spy.new()
59+
registry.update(spy)
60+
assert.wait(function()
61+
assert.spy(spy).was_called(1)
62+
assert.spy(spy).was_called_with(true, match.is_table())
63+
end)
64+
end)
65+
end)
3666
end)

0 commit comments

Comments
 (0)