Adding migrate for older projects

This commit is contained in:
DarthMooMancer
2025-03-06 10:08:02 -05:00
parent 803b404e38
commit b6d23131e9
+39 -21
View File
@@ -60,27 +60,11 @@ function M.setup(opts)
vim.api.nvim_create_user_command("NewJavaFile", M.create_new_file, {}) vim.api.nvim_create_user_command("NewJavaFile", M.create_new_file, {})
vim.api.nvim_create_user_command("JavaBuild", M.build, {}) vim.api.nvim_create_user_command("JavaBuild", M.build, {})
vim.api.nvim_create_user_command("JavaRun", M.run, {}) vim.api.nvim_create_user_command("JavaRun", M.run, {})
vim.api.nvim_create_user_command("Migrate", M.migrate, {})
end end
-- -- Open floating terminal -- Open floating terminal
-- local function open_float_terminal(cmd) function M.open_float_terminal(cmd)
-- local ui = vim.api.nvim_list_uis()[1]
-- local width, height = math.floor(ui.width * 0.9), math.floor(ui.height * 0.9)
-- local row, col = math.floor(ui.height * 0.05), math.floor(ui.width * 0.05)
--
-- local buf = vim.api.nvim_create_buf(false, true)
-- local win = vim.api.nvim_open_win(buf, true, {
-- relative = "editor",
-- width = width - M.config.terminal.right_padding,
-- height = height - M.config.terminal.bottom_padding,
-- row = row + M.config.terminal.left_padding,
-- col = col + M.config.terminal.top_padding,
-- style = "minimal",
-- border = M.config.terminal.border and "rounded" or "none",
-- })
--
--
local function open_float_terminal(cmd)
local ui = vim.api.nvim_list_uis()[1] local ui = vim.api.nvim_list_uis()[1]
local term_cfg = M.config.terminal local term_cfg = M.config.terminal
@@ -139,6 +123,40 @@ end
return buf, win return buf, win
end end
-- Migrate old folder to new method
-- take project to convert
-- cd into project in project_root
-- mkdir src and build
-- move all contents of root/src to src
-- move all contents of root/out to build
-- delete root
function M.migrate()
vim.ui.input({ prompt = "Enter project name to migrate: "}, function(project_name)
if not project_name or project_name == "" then
print("Migration failed")
return
end
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
local legacy_src_dir = project_root .. "/root/src"
local legacy_out_dir = project_root .. "/root/out"
local src_dir = project_root .. "/src"
local build_dir = project_root .. "/build"
vim.fn.mkdir(src_dir, "p")
vim.fn.mkdir(build_dir, "p")
-- local compile_command = string.format("javac -d %s %s/*.java", out_dir, src_dir)
local compile_command = string.format("mv" .. legacy_src_dir .. "*" .. src_dir " && mv " .. legacy_out_dir .. "*" .. build_dir "&& rm -rd " .. project_root .. "/" .. "root/")
local compile_status = vim.fn.system(compile_command)
if vim.v.shell_error ~= 0 then
print("Error during migration:\n" .. compile_status)
else
print("Migration successful!")
end
end)
end
-- Create a new Java project -- Create a new Java project
function M.create_project() function M.create_project()
-- Use vim.ui.input for better handling of input -- Use vim.ui.input for better handling of input
@@ -189,7 +207,7 @@ function M.create_new_file()
end end
local current_dir = vim.fn.expand("%:p:h") local current_dir = vim.fn.expand("%:p:h")
local root_dir = current_dir:match("(.*)/root/src") local root_dir = current_dir:match("(.*)/src")
if not root_dir then if not root_dir then
print("Error: src directory not found.") print("Error: src directory not found.")
return return
@@ -247,7 +265,7 @@ function M.run()
end end
local out_dir = project_root .. "/build" local out_dir = project_root .. "/build"
open_float_terminal("java -cp " .. out_dir .. " Main") M.open_float_terminal("java -cp " .. out_dir .. " Main")
end end
return M return M