From b3f1985e6a741aadb6238ae5ae7c03d6e32941cb Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Mon, 10 Mar 2025 13:08:36 -0400 Subject: [PATCH] Update c.lua --- lua/Polydev/c.lua | 61 ++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/lua/Polydev/c.lua b/lua/Polydev/c.lua index b3f7c34..9f9e6b7 100644 --- a/lua/Polydev/c.lua +++ b/lua/Polydev/c.lua @@ -227,37 +227,44 @@ end function M.build() local current_dir = vim.fn.expand("%:p:h") - -- local compile_command = "mv " .. legacy_src_dir .. "/* " .. src_dir .. - -- " && mv " .. legacy_out_dir .. "/* " .. build_dir .. - -- " && rm -rd " .. project_root .. "/" .. "root/" + -- Try to detect the project root by looking for a known file or directory (e.g., CMakeLists.txt) + local project_root = vim.fn.findfile("CMakeLists.txt", current_dir .. ";") - vim.ui.input({ prompt = "Enter project name: " }, function(project_name) - if not project_name or project_name == "" then - print("Project creation canceled.") - return - end - local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name - local into_build = "cd " .. project_root .. "/build" - local into_build_status = vim.fn.system(into_build) - if vim.v.shell_error ~= 0 then - print("Could not go into build dir:\n" .. into_build_status) - end + if project_root == "" then + print("Could not find CMakeLists.txt. Are you in a project directory?") + return + end - -- local project_root_build = current_dir:match("(.*)/build") - -- if not project_root_build then - -- print("Error: Must be inside the 'build' directory.") - -- return - -- end + -- Get the directory where CMakeLists.txt is located (this is the project root) + project_root = vim.fn.fnamemodify(project_root, ":h") - local compile_command = "cmake .. && make" - local compile_status = vim.fn.system(compile_command) + -- Define the build directory path + local build_dir = project_root .. "/build" - if vim.v.shell_error ~= 0 then - print("Error during compilation:\n" .. compile_status) - else - print("Compilation successful!") - end - end) + -- Check if the build directory exists, create it if it doesn't + if vim.fn.isdirectory(build_dir) == 0 then + print("Build directory does not exist. Creating: " .. build_dir) + vim.fn.mkdir(build_dir, "p") + end + + -- Change to the build directory + local into_build = "cd " .. build_dir + local into_build_status = vim.fn.system(into_build) + + if vim.v.shell_error ~= 0 then + print("Could not go into build dir:\n" .. into_build_status) + return + end + + -- Run CMake and Make to build the project + local compile_command = "cmake .. && make" + local compile_status = vim.fn.system(compile_command) + + if vim.v.shell_error ~= 0 then + print("Error during compilation:\n" .. compile_status) + else + print("Compilation successful!") + end end