From 043448d16e33c8716244a409d4f0db1ecfc1c0f3 Mon Sep 17 00:00:00 2001 From: Denzel <> Date: Mon, 31 Aug 2026 11:20:31 +0200 Subject: [PATCH] refactor: do not use cmake for file operations (#398) --- lua/cmake-tools/utils.lua | 47 ++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/lua/cmake-tools/utils.lua b/lua/cmake-tools/utils.lua index 888b08f1..b06d8e34 100644 --- a/lua/cmake-tools/utils.lua +++ b/lua/cmake-tools/utils.lua @@ -4,6 +4,7 @@ local Result = require("cmake-tools.result") local Types = require("cmake-tools.types") local notification = require("cmake-tools.notification") local scratch = require("cmake-tools.scratch") +local log = require("cmake-tools.log") ---@alias executor_conf {name:string, opts:table} ---@alias runner_conf {name:string, opts:table} @@ -99,16 +100,29 @@ function utils.deepcopy(orig, copies) return copy end +---@param path string path of the file +---@return boolean success +local function create_parent_directory(path) + local dir = vim.fs.dirname(path) + local ok, err = pcall(utils.mkdir, dir) + if not ok then + log.error("Cannot create directory " .. dir .. ": " .. tostring(err)) + end + return ok +end + function utils.copyfile(src, target) - if utils.file_exists(src) then - -- if we don't always use terminal - local cmd = table.concat({ - "exec", - "'!cmake -E copy", - utils.shell_quote(src), - utils.shell_quote(target) .. "'", - }, " ") - vim.cmd(cmd) + if not utils.file_exists(src) then + return + end + + if not create_parent_directory(target) then + return + end + + local ok, err = vim.loop.fs_copyfile(src, target) + if not ok then + log.error("Cannot copy " .. src .. " to " .. target .. ": " .. tostring(err)) end end @@ -129,13 +143,14 @@ function utils.softlink(src, target) end end - local cmd = table.concat({ - "exec", - "'!cmake -E create_symlink", - utils.shell_quote(src), - utils.shell_quote(target) .. "'", - }, " ") - vim.cmd(cmd) + if not create_parent_directory(target) then + return + end + + local ok, err = vim.loop.fs_symlink(src, target) + if not ok then + log.error("Cannot link " .. target .. " to " .. src .. ": " .. tostring(err)) + end end function utils.shell_quote(str)