-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathplatform.lua
More file actions
71 lines (63 loc) · 2.79 KB
/
Copy pathplatform.lua
File metadata and controls
71 lines (63 loc) · 2.79 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
-- platform.lua — the single home for the per-OS branch in the integration
-- layer. Before this, script-extension / hook-command / chmod logic was
-- duplicated across every backend installer and health.lua; centralising it
-- keeps the OS fork in one place as more backends go cross-platform.
-- See issue #46 / ADR-0008.
local M = {}
function M.is_windows()
return vim.fn.has("win32") == 1
end
-- Hook shims are per-OS: a .sh shim on Unix, a .ps1 shim on Windows.
function M.script_ext()
return M.is_windows() and ".ps1" or ".sh"
end
--- Build the command an agent should invoke for a hook entry.
--- On Windows the .ps1 is run through PowerShell explicitly (the file is not
--- directly executable); on Unix the .sh path runs directly. `args` (a string,
--- e.g. "claudecode pre") is appended so the generic hook-entry shim knows
--- which backend + event it is serving.
--- @param script_path string absolute path to the hook-entry shim
--- @param args string? space-separated args appended to the command
--- @return string
function M.hook_command(script_path, args)
local suffix = (args and args ~= "") and (" " .. args) or ""
if M.is_windows() then
return string.format(
'powershell -NoProfile -ExecutionPolicy Bypass -File "%s"%s',
script_path, suffix
)
end
return script_path .. suffix
end
--- Make a shim executable. chmod +x on Unix; a no-op on Windows, where there is
--- no executable bit and the interpreter is invoked explicitly.
--- @param path string
function M.make_executable(path)
if not M.is_windows() then
vim.fn.system({ "chmod", "+x", path })
end
end
--- True if `p` is an already-absolute path. Recognises Unix-rooted (`/…`),
--- Windows drive-letter (`C:\` or `C:/`), and Windows UNC (`\\server\share`)
--- forms. Deliberately OS-INDEPENDENT — it inspects the string's shape, so it
--- recognises Windows-absolute paths even on Unix. Callers use it to avoid
--- joining an already-absolute path onto cwd, which would double it
--- (`D:\proj\D:\proj\file` → fs_stat miss → file mis-marked "created", no diff).
--- Single source of truth shared by the path resolvers in pre_tool/normalisers,
--- pre_tool/shell_detect, and apply/patch.
--- @param p string
--- @return boolean
function M.is_absolute(p)
if not p or p == "" then return false end
return p:sub(1, 1) == "/" -- Unix absolute
or p:match("^%a:[/\\]") ~= nil -- Windows drive-letter (C:\ or C:/)
or p:sub(1, 2) == "\\\\" -- Windows UNC (\\server\share)
end
--- The external dependency each OS's shim relies on, for health reporting:
--- the Unix shims parse JSON with jq; the Windows shims use PowerShell's native
--- ConvertFrom-Json (so jq is irrelevant there).
--- @return string
function M.shim_dependency()
return M.is_windows() and "powershell" or "jq"
end
return M