Skip to content

Commit

Permalink
feat(api/command): add --debug flag to :MasonInstall (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamboman committed Oct 19, 2022
1 parent f22f51a commit 38c3c7f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions lua/mason-core/functional/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ _.flatten = list.flatten
_.sort_by = list.sort_by
_.uniq_by = list.uniq_by
_.join = list.join
_.partition = list.partition

---@module "mason-core.functional.relation"
local relation = lazy_require "mason-core.functional.relation"
Expand Down
12 changes: 12 additions & 0 deletions lua/mason-core/functional/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,16 @@ _.uniq_by = fun.curryN(function(id, list)
return result
end, 2)

---@generic T
---@param predicate fun(item: T): boolean
---@param list T[]
---@return T[][] # [T[], T[]]
_.partition = fun.curryN(function(predicate, list)
local partitions = { {}, {} }
for _, item in ipairs(list) do
table.insert(partitions[predicate(item) and 1 or 2], item)
end
return partitions
end, 2)

return _
1 change: 1 addition & 0 deletions lua/mason-core/installer/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ InstallContext.__index = InstallContext

---@class InstallContextOpts
---@field requested_version string?
---@field debug boolean?

---@param handle InstallHandle
---@param opts InstallContextOpts
Expand Down
14 changes: 10 additions & 4 deletions lua/mason-core/installer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@ function M.execute(handle, opts)
context.stdio_sink.stderr(tostring(failure))
context.stdio_sink.stderr "\n"

-- clean up installation dir
pcall(function()
fs.async.rmrf(context.cwd:get())
end)
if not opts.debug then
-- clean up installation dir
pcall(function()
fs.async.rmrf(context.cwd:get())
end)
else
context.stdio_sink.stdout(
("[debug] Installation directory retained at %q.\n"):format(context.cwd:get())
)
end

-- unlink linked executables (in the rare occasion an error occurs after linking)
linker.unlink(context.package, context.receipt.links)
Expand Down
3 changes: 2 additions & 1 deletion lua/mason-core/package/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function Package:new_handle()
return handle
end

---@param opts { version: string? }?
---@param opts? { version: string?, debug: boolean? }
---@return InstallHandle
function Package:install(opts)
opts = opts or {}
Expand Down Expand Up @@ -125,6 +125,7 @@ function Package:install(opts)
handle,
{
requested_version = opts.version,
debug = opts.debug,
}
)
return handle
Expand Down
17 changes: 14 additions & 3 deletions lua/mason/api/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ local function join_handles(handles)
end

---@param package_specifiers string[]
local function MasonInstall(package_specifiers)
---@param opts? { debug: boolean }
local function MasonInstall(package_specifiers, opts)
opts = opts or {}
local Package = require "mason-core.package"
local registry = require "mason-registry"
local valid_packages = filter_valid_packages(package_specifiers)
Expand All @@ -96,7 +98,7 @@ local function MasonInstall(package_specifiers)
local handles = _.map(function(pkg_specifier)
local package_name, version = Package.Parse(pkg_specifier)
local pkg = registry.get_package(package_name)
return pkg:install { version = version }
return pkg:install { version = version, debug = opts.debug }
end, valid_packages)

if is_headless then
Expand All @@ -110,8 +112,17 @@ local function MasonInstall(package_specifiers)
end
end

---@param args string[]
---@return table<string, true> opts, string[] args
local function parse_args(args)
local opts_list, args = unpack(_.partition(_.starts_with "--", args))
local opts = _.set_of(_.map(_.gsub("^%-%-", ""), opts_list))
return opts, args
end

vim.api.nvim_create_user_command("MasonInstall", function(opts)
MasonInstall(opts.fargs)
local command_opts, packages = parse_args(opts.fargs)
MasonInstall(packages, command_opts)
end, {
desc = "Install one or more packages.",
nargs = "+",
Expand Down
8 changes: 8 additions & 0 deletions tests/mason-core/functional/list_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,12 @@ describe("functional: list", function()
local list = { "Person.", "Woman.", "Man.", "Person.", "Woman.", "Camera.", "TV." }
assert.same({ "Person.", "Woman.", "Man.", "Camera.", "TV." }, _.uniq_by(_.identity, list))
end)

it("should partition lists", function()
local words = { "person", "Woman", "Man", "camera", "TV" }
assert.same({
{ "Woman", "Man", "TV" },
{ "person", "camera" },
}, _.partition(_.matches "%u", words))
end)
end)
13 changes: 13 additions & 0 deletions tests/mason/api/command_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ describe(":MasonInstall", function()
end)
)

it(
"should install provided packages in debug mode",
async_test(function()
local dummy = registry.get_package "dummy"
local dummy2 = registry.get_package "dummy2"
spy.on(Pkg, "install")
vim.cmd [[MasonInstall --debug dummy dummy2]]
assert.spy(Pkg.install).was_called(2)
assert.spy(Pkg.install).was_called_with(match.is_ref(dummy), { version = nil, debug = true })
assert.spy(Pkg.install).was_called_with(match.is_ref(dummy2), { version = nil, debug = true })
end)
)

it(
"should open the UI window",
async_test(function()
Expand Down

0 comments on commit 38c3c7f

Please sign in to comment.