aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/installer
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2023-10-13 01:27:58 +0200
committerWilliam Boman <william@redwill.se>2025-02-19 09:23:19 +0100
commitc338fb2698ae276bc3b6edccdd3afaef92fc77bd (patch)
treeab9ec90f23e5ba1c48585fd41ef987cff8c3a693 /lua/mason-core/installer
parentrefactor: add InstallLocation.global() (diff)
downloadmason-c338fb2698ae276bc3b6edccdd3afaef92fc77bd.tar
mason-c338fb2698ae276bc3b6edccdd3afaef92fc77bd.tar.gz
mason-c338fb2698ae276bc3b6edccdd3afaef92fc77bd.tar.bz2
mason-c338fb2698ae276bc3b6edccdd3afaef92fc77bd.tar.lz
mason-c338fb2698ae276bc3b6edccdd3afaef92fc77bd.tar.xz
mason-c338fb2698ae276bc3b6edccdd3afaef92fc77bd.tar.zst
mason-c338fb2698ae276bc3b6edccdd3afaef92fc77bd.zip
refactor(installer): move initializations to InstallContext constructor
Diffstat (limited to 'lua/mason-core/installer')
-rw-r--r--lua/mason-core/installer/context/cwd.lua16
-rw-r--r--lua/mason-core/installer/context/init.lua14
-rw-r--r--lua/mason-core/installer/context/spawn.lua4
-rw-r--r--lua/mason-core/installer/runner.lua10
4 files changed, 23 insertions, 21 deletions
diff --git a/lua/mason-core/installer/context/cwd.lua b/lua/mason-core/installer/context/cwd.lua
index 4f645fbb..cb2e70ec 100644
--- a/lua/mason-core/installer/context/cwd.lua
+++ b/lua/mason-core/installer/context/cwd.lua
@@ -4,27 +4,29 @@ local path = require "mason-core.path"
---@class InstallContextCwd
---@field private location InstallLocation Defines the upper boundary for which paths are allowed as cwd.
+---@field private handle InstallHandle
---@field private cwd string?
local InstallContextCwd = {}
InstallContextCwd.__index = InstallContextCwd
+---@param handle InstallHandle
---@param location InstallLocation
-function InstallContextCwd.new(location)
+function InstallContextCwd.new(handle, location)
assert(location, "location not provided")
return setmetatable({
location = location,
+ handle = handle,
cwd = nil,
}, InstallContextCwd)
end
----@param handle InstallHandle
-function InstallContextCwd:initialize(handle)
+function InstallContextCwd:initialize()
return Result.try(function(try)
- local staging_dir = self.location:staging(handle.package.name)
- if fs.async.dir_exists(staging_dir) then
- try(Result.pcall(fs.async.rmrf, staging_dir))
+ local staging_dir = self.location:staging(self.handle.package.name)
+ if fs.sync.dir_exists(staging_dir) then
+ try(Result.pcall(fs.sync.rmrf, staging_dir))
end
- try(Result.pcall(fs.async.mkdirp, staging_dir))
+ try(Result.pcall(fs.sync.mkdirp, staging_dir))
self:set(staging_dir)
end)
end
diff --git a/lua/mason-core/installer/context/init.lua b/lua/mason-core/installer/context/init.lua
index 0d178c4e..f2cedb42 100644
--- a/lua/mason-core/installer/context/init.lua
+++ b/lua/mason-core/installer/context/init.lua
@@ -1,3 +1,6 @@
+local InstallContextCwd = require "mason-core.installer.context.cwd"
+local InstallContextFs = require "mason-core.installer.context.fs"
+local InstallContextSpawn = require "mason-core.installer.context.spawn"
local Result = require "mason-core.result"
local _ = require "mason-core.functional"
local fs = require "mason-core.fs"
@@ -9,6 +12,7 @@ local receipt = require "mason-core.receipt"
---@class InstallContext
---@field receipt InstallReceiptBuilder
---@field fs InstallContextFs
+---@field location InstallLocation
---@field spawn InstallContextSpawn
---@field handle InstallHandle
---@field package Package
@@ -20,15 +24,17 @@ local InstallContext = {}
InstallContext.__index = InstallContext
---@param handle InstallHandle
----@param cwd InstallContextCwd
----@param spawn InstallContextSpawn
----@param fs InstallContextFs
+---@param location InstallLocation
---@param opts PackageInstallOpts
-function InstallContext.new(handle, cwd, spawn, fs, opts)
+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,
handle = handle,
+ location = location,
package = handle.package, -- for convenience
fs = fs,
receipt = receipt.InstallReceiptBuilder.new(),
diff --git a/lua/mason-core/installer/context/spawn.lua b/lua/mason-core/installer/context/spawn.lua
index 6528c4b3..0a73ff3a 100644
--- a/lua/mason-core/installer/context/spawn.lua
+++ b/lua/mason-core/installer/context/spawn.lua
@@ -7,10 +7,10 @@ local spawn = require "mason-core.spawn"
---@field [string] async fun(opts: SpawnArgs): Result
local InstallContextSpawn = {}
----@param cwd InstallContextCwd
---@param handle InstallHandle
+---@param cwd InstallContextCwd
---@param strict_mode boolean
-function InstallContextSpawn.new(cwd, handle, strict_mode)
+function InstallContextSpawn.new(handle, cwd, strict_mode)
return setmetatable({ cwd = cwd, handle = handle, strict_mode = strict_mode }, InstallContextSpawn)
end
diff --git a/lua/mason-core/installer/runner.lua b/lua/mason-core/installer/runner.lua
index 175610d5..8a03b724 100644
--- a/lua/mason-core/installer/runner.lua
+++ b/lua/mason-core/installer/runner.lua
@@ -8,9 +8,6 @@ local log = require "mason-core.log"
local registry = require "mason-registry"
local InstallContext = require "mason-core.installer.context"
-local InstallContextCwd = require "mason-core.installer.context.cwd"
-local InstallContextFs = require "mason-core.installer.context.fs"
-local InstallContextSpawn = require "mason-core.installer.context.spawn"
---@class InstallRunner
---@field location InstallLocation
@@ -37,10 +34,7 @@ function InstallRunner:execute(opts, callback)
local handle = self.handle
log.fmt_info("Executing installer for %s %s", handle.package, opts)
- local context_cwd = InstallContextCwd.new(self.location)
- local context_spawn = InstallContextSpawn.new(context_cwd, handle, false)
- local context_fs = InstallContextFs.new(context_cwd)
- local context = InstallContext.new(handle, context_cwd, context_spawn, context_fs, opts)
+ local context = InstallContext.new(handle, self.location, opts)
local tailed_output = {}
@@ -107,7 +101,7 @@ function InstallRunner:execute(opts, callback)
context.receipt:with_start_time(vim.loop.gettimeofday())
-- 1. initialize working directory
- try(context_cwd:initialize(handle))
+ try(context.cwd:initialize())
-- 2. run installer
---@type async fun(ctx: InstallContext): Result