Skip to content

Commit bac6ba2

Browse files
DenzellceWolf
authored andcommitted
fix: re-create softlink when target differs
1 parent e100f40 commit bac6ba2

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

lua/cmake-tools/utils.lua

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,40 @@ end
8686
function utils.copyfile(src, target)
8787
if utils.file_exists(src) then
8888
-- if we don't always use terminal
89-
local cmd = "exec "
90-
.. "'!cmake -E copy "
91-
.. utils.shell_quote(src)
92-
.. " "
93-
.. utils.shell_quote(target)
94-
.. "'"
89+
local cmd = table.concat({
90+
"exec",
91+
"'!cmake -E copy",
92+
utils.shell_quote(src),
93+
utils.shell_quote(target) .. "'",
94+
}, " ")
9595
vim.cmd(cmd)
9696
end
9797
end
9898

9999
function utils.softlink(src, target)
100-
if utils.file_exists(src) and not utils.file_exists(target) then
101-
-- if we don't always use terminal
102-
local cmd = "exec "
103-
.. "'!cmake -E create_symlink "
104-
.. utils.shell_quote(src)
105-
.. " "
106-
.. utils.shell_quote(target)
107-
.. "'"
108-
vim.cmd(cmd)
100+
if not utils.file_exists(src) then
101+
return
102+
end
103+
104+
local stat = vim.loop.fs_lstat(target)
105+
if stat then
106+
if stat.type == "link" then
107+
if vim.loop.fs_readlink(target) == src then
108+
return
109+
end
110+
else
111+
-- target is a regular file, remove it first
112+
os.remove(target)
113+
end
109114
end
115+
116+
local cmd = table.concat({
117+
"exec",
118+
"'!cmake -E create_symlink",
119+
utils.shell_quote(src),
120+
utils.shell_quote(target) .. "'",
121+
}, " ")
122+
vim.cmd(cmd)
110123
end
111124

112125
function utils.shell_quote(str)

0 commit comments

Comments
 (0)