From f91293e2ffe934799582e736e07114aa6427a8ad Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Mon, 10 Mar 2025 13:13:50 -0400 Subject: [PATCH] Update c.lua --- lua/Polydev/c.lua | 62 +++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/lua/Polydev/c.lua b/lua/Polydev/c.lua index 53d29f8..d10fd59 100644 --- a/lua/Polydev/c.lua +++ b/lua/Polydev/c.lua @@ -227,44 +227,42 @@ end function M.build() local current_dir = vim.fn.expand("%:p:h") - -- Try to find CMakeLists.txt or another specific file within the current directory or its parents - local project_root = vim.fn.findfile("CMakeLists.txt", current_dir .. ";") + -- Prompt the user to input the project name + vim.ui.input({ prompt = "Enter project name: " }, function(project_name) + if not project_name or project_name == "" then + print("Project creation canceled.") + return + end - if project_root == "" then - print("Could not find CMakeLists.txt. Are you in a project directory?") - return - end + -- Define the project root path based on the entered project name + local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name + local build_dir = project_root .. "/build" - -- Get the directory of CMakeLists.txt, which is the root of the project - project_root = vim.fn.fnamemodify(project_root, ":h") + -- Check if the build directory exists, and 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 - -- Define the build directory path - local build_dir = project_root .. "/build" + -- Change to the build directory + local into_build = "cd " .. build_dir + local into_build_status = vim.fn.system(into_build) - -- 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 + if vim.v.shell_error ~= 0 then + print("Could not change into build dir:\n" .. into_build_status) + return + end - -- Change to the build directory - local into_build = "cd " .. build_dir - local into_build_status = vim.fn.system(into_build) + -- 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("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 + if vim.v.shell_error ~= 0 then + print("Error during compilation:\n" .. compile_status) + else + print("Compilation successful!") + end + end) end -- Run Java program in floating terminal