-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathpowershell.lua
More file actions
72 lines (60 loc) · 2.15 KB
/
powershell.lua
File metadata and controls
72 lines (60 loc) · 2.15 KB
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
local class = require('java-core.utils.class')
local log = require('java-core.utils.log2')
local err_util = require('java-core.utils.errors')
local path = require('java-core.utils.path')
---@class java-core.PowerShell
---@field url string
---@field dest string
---@field retry_count number
---@field timeout number
local PowerShell = class()
---@class java-core.PowerShellOpts
---@field url string URL to download
---@field dest? string Destination path (optional, uses temp if not provided)
---@field retry_count? number Retry count (optional, defaults to 5)
---@field timeout? number Timeout in seconds (optional, defaults to 30)
---@param opts java-core.PowerShellOpts
function PowerShell:_init(opts)
self.url = opts.url
if not opts.dest then
local filename = vim.fs.basename(opts.url)
local tmp_dir = vim.fn.tempname()
vim.fn.mkdir(tmp_dir, 'p')
self.dest = path.join(tmp_dir, filename)
log.debug('Using temp destination:', self.dest)
else
self.dest = opts.dest
log.debug('Using provided destination:', self.dest)
end
self.retry_count = opts.retry_count or 5
self.timeout = opts.timeout or 30
end
---Download file using PowerShell
---@return string # Path to downloaded file
function PowerShell:download()
local pwsh = vim.fn.executable('pwsh') == 1 and 'pwsh' or 'powershell'
log.debug('PowerShell downloading:', self.url, 'to', self.dest)
log.debug('Using PowerShell binary:', pwsh)
local pwsh_cmd = string.format(
'iwr -TimeoutSec %d -UseBasicParsing -Method "GET" -Uri %q -OutFile %q;',
self.timeout,
self.url,
self.dest
)
local cmd = string.format(
-- luacheck: ignore
"%s -NoProfile -NonInteractive -Command \"$ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; %s\"",
pwsh,
pwsh_cmd
)
log.debug('PowerShell command:', cmd)
local result = vim.fn.system(cmd)
local exit_code = vim.v.shell_error
if exit_code ~= 0 then
local err = string.format('PowerShell download failed (exit %d): %s', exit_code, result)
err_util.throw(err)
end
log.debug('PowerShell download completed:', self.dest)
return self.dest
end
return PowerShell