aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-registry/installer.lua
blob: 3fc4fb3104accfc017a993d0aa8c46362cf1d743 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
local a = require "mason-core.async"
local OneShotChannel = require("mason-core.async.control").OneShotChannel
local Result = require "mason-core.result"
local sources = require "mason-registry.sources"

local M = {}

---@type OneShotChannel?
local update_channel

---@async
function M.run()
    if not update_channel or update_channel:is_closed() then
        update_channel = OneShotChannel:new()
        a.run(function()
            update_channel:send(Result.try(function(try)
                local updated_sources = {}
                for source in sources.iter { include_uninstalled = true } do
                    source:get_installer():if_present(function(installer)
                        try(installer():map_err(function(err)
                            return ("%s failed to install: %s"):format(source, err)
                        end))
                        table.insert(updated_sources, source)
                    end)
                end
                return updated_sources
            end):on_success(function(updated_sources)
                if #updated_sources > 0 then
                    require("mason-registry"):emit("update", updated_sources)
                end
            end))
        end, function() end)
    end

    return update_channel:receive()
end

return M