aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/managers/powershell/init.lua
blob: c0d36f2e15886f05a6bbb5a97f8f65a658303097 (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
local spawn = require "mason-core.spawn"
local process = require "mason-core.process"

local M = {}

local PWSHOPT = {
    progress_preference = [[ $ProgressPreference = 'SilentlyContinue'; ]], -- https://stackoverflow.com/a/63301751
    security_protocol = [[ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; ]],
}

---@param script string
---@param opts JobSpawnOpts?
---@param custom_spawn JobSpawn?
function M.script(script, opts, custom_spawn)
    opts = opts or {}
    ---@type JobSpawn
    local spawner = custom_spawn or spawn
    return spawner.powershell(
        vim.tbl_extend("keep", {
            "-NoProfile",
            on_spawn = function(_, stdio)
                local stdin = stdio[1]
                stdin:write(PWSHOPT.progress_preference)
                stdin:write(PWSHOPT.security_protocol)
                stdin:write(script)
                stdin:close()
            end,
            env_raw = process.graft_env(opts.env or {}, { "PSMODULEPATH" }),
        }, opts) --[[@as JobSpawnOpts]]
    )
end

---@param command string
---@param opts JobSpawnOpts?
---@param custom_spawn JobSpawn?
function M.command(command, opts, custom_spawn)
    opts = opts or {}
    ---@type JobSpawn
    local spawner = custom_spawn or spawn
    return spawner.powershell(
        vim.tbl_extend("keep", {
            "-NoProfile",
            "-Command",
            PWSHOPT.progress_preference .. PWSHOPT.security_protocol .. command,
            env_raw = process.graft_env(opts.env or {}, { "PSMODULEPATH" }),
        }, opts) --[[@as JobSpawnOpts]]
    )
end

return M