aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/installer
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2023-10-13 21:14:29 +0200
committerWilliam Boman <william@redwill.se>2025-02-19 09:23:19 +0100
commitae208dc380808ff1aef39929a0e897e881571d43 (patch)
treeb9044cec7223062db69998189c76f8163ac9d58b /lua/mason-core/installer
parentfix(location): use correct registry path (diff)
downloadmason-ae208dc380808ff1aef39929a0e897e881571d43.tar
mason-ae208dc380808ff1aef39929a0e897e881571d43.tar.gz
mason-ae208dc380808ff1aef39929a0e897e881571d43.tar.bz2
mason-ae208dc380808ff1aef39929a0e897e881571d43.tar.lz
mason-ae208dc380808ff1aef39929a0e897e881571d43.tar.xz
mason-ae208dc380808ff1aef39929a0e897e881571d43.tar.zst
mason-ae208dc380808ff1aef39929a0e897e881571d43.zip
refactor: standardize constructors and improve inheritance construction
Diffstat (limited to 'lua/mason-core/installer')
-rw-r--r--lua/mason-core/installer/context/cwd.lua14
-rw-r--r--lua/mason-core/installer/context/fs.lua8
-rw-r--r--lua/mason-core/installer/context/init.lua10
-rw-r--r--lua/mason-core/installer/context/spawn.lua10
-rw-r--r--lua/mason-core/installer/handle.lua46
-rw-r--r--lua/mason-core/installer/location.lua12
-rw-r--r--lua/mason-core/installer/runner.lua16
7 files changed, 69 insertions, 47 deletions
diff --git a/lua/mason-core/installer/context/cwd.lua b/lua/mason-core/installer/context/cwd.lua
index cb2e70ec..2b74bf55 100644
--- a/lua/mason-core/installer/context/cwd.lua
+++ b/lua/mason-core/installer/context/cwd.lua
@@ -11,13 +11,15 @@ InstallContextCwd.__index = InstallContextCwd
---@param handle InstallHandle
---@param location InstallLocation
-function InstallContextCwd.new(handle, location)
+function InstallContextCwd:new(handle, location)
assert(location, "location not provided")
- return setmetatable({
- location = location,
- handle = handle,
- cwd = nil,
- }, InstallContextCwd)
+ ---@type InstallContextCwd
+ local instance = {}
+ setmetatable(instance, self)
+ instance.location = location
+ instance.handle = handle
+ instance.cwd = nil
+ return instance
end
function InstallContextCwd:initialize()
diff --git a/lua/mason-core/installer/context/fs.lua b/lua/mason-core/installer/context/fs.lua
index 5c51fb56..93379017 100644
--- a/lua/mason-core/installer/context/fs.lua
+++ b/lua/mason-core/installer/context/fs.lua
@@ -8,8 +8,12 @@ local InstallContextFs = {}
InstallContextFs.__index = InstallContextFs
---@param cwd InstallContextCwd
-function InstallContextFs.new(cwd)
- return setmetatable({ cwd = cwd }, InstallContextFs)
+function InstallContextFs:new(cwd)
+ ---@type InstallContextFs
+ local instance = {}
+ setmetatable(instance, InstallContextFs)
+ instance.cwd = cwd
+ return instance
end
---@async
diff --git a/lua/mason-core/installer/context/init.lua b/lua/mason-core/installer/context/init.lua
index f2cedb42..425bf39c 100644
--- a/lua/mason-core/installer/context/init.lua
+++ b/lua/mason-core/installer/context/init.lua
@@ -26,10 +26,10 @@ InstallContext.__index = InstallContext
---@param handle InstallHandle
---@param location InstallLocation
---@param opts PackageInstallOpts
-function InstallContext.new(handle, location, opts)
- local cwd = InstallContextCwd.new(handle, location)
- local spawn = InstallContextSpawn.new(handle, cwd, false)
- local fs = InstallContextFs.new(cwd)
+function InstallContext:new(handle, location, opts)
+ local cwd = InstallContextCwd:new(handle, location)
+ local spawn = InstallContextSpawn:new(handle, cwd, false)
+ local fs = InstallContextFs:new(cwd)
return setmetatable({
cwd = cwd,
spawn = spawn,
@@ -37,7 +37,7 @@ function InstallContext.new(handle, location, opts)
location = location,
package = handle.package, -- for convenience
fs = fs,
- receipt = receipt.InstallReceiptBuilder.new(),
+ receipt = receipt.InstallReceiptBuilder:new(),
stdio_sink = handle.stdio.sink,
links = {
bin = {},
diff --git a/lua/mason-core/installer/context/spawn.lua b/lua/mason-core/installer/context/spawn.lua
index 0a73ff3a..f2ce8df2 100644
--- a/lua/mason-core/installer/context/spawn.lua
+++ b/lua/mason-core/installer/context/spawn.lua
@@ -10,8 +10,14 @@ local InstallContextSpawn = {}
---@param handle InstallHandle
---@param cwd InstallContextCwd
---@param strict_mode boolean
-function InstallContextSpawn.new(handle, cwd, strict_mode)
- return setmetatable({ cwd = cwd, handle = handle, strict_mode = strict_mode }, InstallContextSpawn)
+function InstallContextSpawn:new(handle, cwd, strict_mode)
+ ---@type InstallContextSpawn
+ local instance = {}
+ setmetatable(instance, self)
+ instance.cwd = cwd
+ instance.handle = handle
+ instance.strict_mode = strict_mode
+ return instance
end
---@param cmd string
diff --git a/lua/mason-core/installer/handle.lua b/lua/mason-core/installer/handle.lua
index 96acbdd1..62da5bae 100644
--- a/lua/mason-core/installer/handle.lua
+++ b/lua/mason-core/installer/handle.lua
@@ -23,9 +23,19 @@ local uv = vim.loop
local InstallHandleSpawnHandle = {}
InstallHandleSpawnHandle.__index = InstallHandleSpawnHandle
----@param fields InstallHandleSpawnHandle
-function InstallHandleSpawnHandle.new(fields)
- return setmetatable(fields, InstallHandleSpawnHandle)
+---@param luv_handle luv_handle
+---@param pid integer
+---@param cmd string
+---@param args string[]
+function InstallHandleSpawnHandle:new(luv_handle, pid, cmd, args)
+ ---@type InstallHandleSpawnHandle
+ local instance = {}
+ setmetatable(instance, InstallHandleSpawnHandle)
+ instance.uv_handle = luv_handle
+ instance.pid = pid
+ instance.cmd = cmd
+ instance.args = args
+ return instance
end
function InstallHandleSpawnHandle:__tostring()
@@ -38,8 +48,9 @@ end
---@field stdio { buffers: { stdout: string[], stderr: string[] }, sink: StdioSink }
---@field is_terminated boolean
---@field private spawn_handles InstallHandleSpawnHandle[]
-local InstallHandle = setmetatable({}, { __index = EventEmitter })
-local InstallHandleMt = { __index = InstallHandle }
+local InstallHandle = {}
+InstallHandle.__index = InstallHandle
+setmetatable(InstallHandle, { __index = EventEmitter })
---@param handle InstallHandle
local function new_sink(handle)
@@ -60,14 +71,14 @@ local function new_sink(handle)
end
---@param pkg Package
-function InstallHandle.new(pkg)
- local self = EventEmitter.init(setmetatable({}, InstallHandleMt))
- self.state = "IDLE"
- self.package = pkg
- self.spawn_handles = {}
- self.stdio = new_sink(self)
- self.is_terminated = false
- return self
+function InstallHandle:new(pkg)
+ local instance = EventEmitter.new(self)
+ instance.state = "IDLE"
+ instance.package = pkg
+ instance.spawn_handles = {}
+ instance.stdio = new_sink(instance)
+ instance.is_terminated = false
+ return instance
end
---@param luv_handle luv_handle
@@ -75,12 +86,7 @@ end
---@param cmd string
---@param args string[]
function InstallHandle:register_spawn_handle(luv_handle, pid, cmd, args)
- local spawn_handles = InstallHandleSpawnHandle.new {
- uv_handle = luv_handle,
- pid = pid,
- cmd = cmd,
- args = args,
- }
+ local spawn_handles = InstallHandleSpawnHandle:new(luv_handle, pid, cmd, args)
log.fmt_trace("Pushing spawn_handles stack for %s: %s (pid: %s)", self, spawn_handles, pid)
self.spawn_handles[#self.spawn_handles + 1] = spawn_handles
self:emit "spawn_handles:change"
@@ -211,7 +217,7 @@ function InstallHandle:close()
self:__clear_event_handlers()
end
-function InstallHandleMt:__tostring()
+function InstallHandle:__tostring()
return ("InstallHandle(package=%s, state=%s)"):format(self.package, self.state)
end
diff --git a/lua/mason-core/installer/location.lua b/lua/mason-core/installer/location.lua
index cc069cc2..9cdf097f 100644
--- a/lua/mason-core/installer/location.lua
+++ b/lua/mason-core/installer/location.lua
@@ -8,14 +8,16 @@ local InstallLocation = {}
InstallLocation.__index = InstallLocation
---@param dir string
-function InstallLocation.new(dir)
- return setmetatable({
- dir = dir,
- }, InstallLocation)
+function InstallLocation:new(dir)
+ ---@type InstallLocation
+ local instance = {}
+ setmetatable(instance, self)
+ instance.dir = dir
+ return instance
end
function InstallLocation.global()
- return InstallLocation.new(settings.current.install_root_dir)
+ return InstallLocation:new(settings.current.install_root_dir)
end
function InstallLocation:get_dir()
diff --git a/lua/mason-core/installer/runner.lua b/lua/mason-core/installer/runner.lua
index 3e3580e5..64aa605d 100644
--- a/lua/mason-core/installer/runner.lua
+++ b/lua/mason-core/installer/runner.lua
@@ -20,12 +20,14 @@ InstallRunner.__index = InstallRunner
---@param location InstallLocation
---@param handle InstallHandle
---@param semaphore Semaphore
-function InstallRunner.new(location, handle, semaphore)
- return setmetatable({
- location = location,
- semaphore = semaphore,
- handle = handle,
- }, InstallRunner)
+function InstallRunner:new(location, handle, semaphore)
+ ---@type InstallRunner
+ local instance = {}
+ setmetatable(instance, self)
+ instance.location = location
+ instance.semaphore = semaphore
+ instance.handle = handle
+ return instance
end
---@param opts PackageInstallOpts
@@ -34,7 +36,7 @@ function InstallRunner:execute(opts, callback)
local handle = self.handle
log.fmt_info("Executing installer for %s %s", handle.package, opts)
- local context = InstallContext.new(handle, self.location, opts)
+ local context = InstallContext:new(handle, self.location, opts)
local tailed_output = {}