aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-registry/sources/file.lua
blob: 1d97bbe7168788c4f5f809c8d3b79357a566437f (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
local Optional = require "mason-core.optional"
local Result = require "mason-core.result"
local _ = require "mason-core.functional"
local a = require "mason-core.async"
local async_control = require "mason-core.async.control"
local async_uv = require "mason-core.async.uv"
local fs = require "mason-core.fs"
local log = require "mason-core.log"
local path = require "mason-core.path"
local spawn = require "mason-core.spawn"
local util = require "mason-registry.sources.util"

local Channel = async_control.Channel

---@class FileRegistrySourceSpec
---@field path string

---@class FileRegistrySource : RegistrySource
---@field spec FileRegistrySourceSpec
---@field root_dir string
---@field buffer { specs: RegistryPackageSpec[], instances: table<string, Package> }?
local FileRegistrySource = {}
FileRegistrySource.__index = FileRegistrySource

---@param spec FileRegistrySourceSpec
function FileRegistrySource.new(spec)
    return setmetatable({
        spec = spec,
    }, FileRegistrySource)
end

function FileRegistrySource:is_installed()
    return self.buffer ~= nil
end

---@return RegistryPackageSpec[]
function FileRegistrySource:get_all_package_specs()
    return _.filter_map(util.map_registry_spec, self:get_buffer().specs)
end

function FileRegistrySource:reload()
    if not self:is_installed() then
        return
    end
    self.buffer.instances = _.compose(
        _.index_by(_.prop "name"),
        _.map(util.hydrate_package(self.buffer.instances or {}))
    )(self:get_all_package_specs())
    return self.buffer
end

function FileRegistrySource:get_buffer()
    return self.buffer or {
        specs = {},
        instances = {},
    }
end

---@param pkg_name string
---@return Package?
function FileRegistrySource:get_package(pkg_name)
    return self:get_buffer().instances[pkg_name]
end

function FileRegistrySource:get_all_package_names()
    return _.map(_.prop "name", self:get_all_package_specs())
end

function FileRegistrySource:get_installer()
    return Optional.of(_.partial(self.install, self))
end

---@async
function FileRegistrySource:install()
    return Result.try(function(try)
        a.scheduler()
        if vim.fn.executable "yq" ~= 1 then
            return Result.failure "yq is not installed."
        end
        local yq = vim.fn.exepath "yq"

        local registry_dir = vim.fn.expand(self.spec.path) --[[@as string]]
        local packages_dir = path.concat { registry_dir, "packages" }
        if not fs.async.dir_exists(registry_dir) then
            return Result.failure(("Directory %s does not exist."):format(registry_dir))
        end

        if not fs.async.dir_exists(packages_dir) then
            return Result.failure "packages/ directory is missing."
        end

        ---@type ReaddirEntry[]
        local entries = _.filter(_.prop_eq("type", "directory"), fs.async.readdir(packages_dir))

        local channel = Channel.new()
        a.run(function()
            for _, entry in ipairs(entries) do
                channel:send(path.concat { packages_dir, entry.name, "package.yaml" })
            end
            channel:close()
        end, function() end)

        local CONSUMERS_COUNT = 10
        local consumers = {}
        for _ = 1, CONSUMERS_COUNT do
            table.insert(consumers, function()
                local specs = {}
                for package_file in channel:iter() do
                    local yaml_spec = fs.async.read_file(package_file)
                    local spec = vim.json.decode(spawn
                        [yq]({
                            "-o",
                            "json",
                            on_spawn = a.scope(function(_, stdio)
                                local stdin = stdio[1]
                                async_uv.write(stdin, yaml_spec)
                                async_uv.shutdown(stdin)
                                async_uv.close(stdin)
                            end),
                        })
                        :get_or_throw(("Failed to parse %s."):format(package_file)).stdout)

                    specs[#specs + 1] = spec
                end
                return specs
            end)
        end

        local specs = _.reduce(vim.list_extend, {}, _.table_pack(a.wait_all(consumers)))
        return specs
    end)
        :on_success(function(specs)
            self.buffer = _.assoc("specs", specs, self.buffer or {})
            self:reload()
        end)
        :on_failure(function(err)
            log.fmt_error("Failed to install registry %s. %s", self, err)
        end)
end

function FileRegistrySource:get_display_name()
    if self:is_installed() then
        return ("local: %s"):format(self.spec.path)
    else
        return ("local: %s [uninstalled]"):format(self.spec.path)
    end
end

function FileRegistrySource:__tostring()
    return ("FileRegistrySource(path=%s)"):format(self.spec.path)
end

return FileRegistrySource