rework: templates.files uses table instead of deprecated string format

rework: templates.files no longer requires the deprecated .polydev files
feat: added get_project_name to utils
This commit is contained in:
DarthMooMancer
2025-05-12 01:09:03 -04:00
parent 4ecaf54f82
commit 8584ed4bbf
2 changed files with 39 additions and 84 deletions
+20 -84
View File
@@ -2,20 +2,6 @@ local utils = require("Polydev.utils")
local M = {} local M = {}
local function init_git(path, gitignore_lines)
vim.fn.mkdir(path, "p")
-- Initialize Git
vim.fn.system({ "git", "-C", path, "init" })
-- Write .gitignore if provided
if gitignore_lines and #gitignore_lines > 0 then
local gitignore_path = { path, ".gitignore" }
local contents = table.concat(gitignore_lines, "\n")
utils.write_file(gitignore_path, contents)
end
end
M.projects = { M.projects = {
java = { java = {
run = function(project_name, project_root) run = function(project_name, project_root)
@@ -31,7 +17,7 @@ public class Main {
local gitignore = { local gitignore = {
"build/" "build/"
} }
init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
utils.write_file({ full_project_root, "src", "Main.java" }, content) utils.write_file({ full_project_root, "src", "Main.java" }, content)
end end
}, },
@@ -62,7 +48,7 @@ setup(
local gitignore = { local gitignore = {
"venv/" "venv/"
} }
init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
vim.cmd("edit " .. full_project_root .. "/main.py") vim.cmd("edit " .. full_project_root .. "/main.py")
local venv_path = full_project_root .. "/venv" local venv_path = full_project_root .. "/venv"
vim.fn.system("python3 -m venv " .. venv_path) vim.fn.system("python3 -m venv " .. venv_path)
@@ -82,7 +68,7 @@ local M = {}
return M return M
]]) ]])
local gitignore = { "" } local gitignore = { "" }
init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
vim.cmd("edit " .. full_project_root .. "/lua/" .. project_name .. "/init.lua") vim.cmd("edit " .. full_project_root .. "/lua/" .. project_name .. "/init.lua")
end end
}, },
@@ -112,7 +98,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
# target_link_libraries(Game PRIVATE SDL3::SDL3) # target_link_libraries(Game PRIVATE SDL3::SDL3)
]], project_name, project_name)) ]], project_name, project_name))
local gitignore = { "build/" } local gitignore = { "build/" }
init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
vim.fn.system(string.format("cd %s/build/ && cmake .. && cmake --build .", full_project_root)) vim.fn.system(string.format("cd %s/build/ && cmake .. && cmake --build .", full_project_root))
vim.cmd("edit " .. full_project_root .. "/src/main.c") vim.cmd("edit " .. full_project_root .. "/src/main.c")
end end
@@ -146,7 +132,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
]], project_name, project_name)) ]], project_name, project_name))
local gitignore = { "build/" } local gitignore = { "build/" }
init_git(full_project_root, gitignore) utils.init_git(full_project_root, gitignore)
vim.fn.system(string.format("cd %s/build/ && cmake .. && cmake --build .", full_project_root)) vim.fn.system(string.format("cd %s/build/ && cmake .. && cmake --build .", full_project_root))
vim.cmd("edit " .. full_project_root .. "/src/main.cpp") vim.cmd("edit " .. full_project_root .. "/src/main.cpp")
end end
@@ -183,96 +169,48 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
M.files = { M.files = {
java = { java = {
run = function (file_name) run = function (file_name)
local root_dir = vim.fn.expand("%:p:h"):match("(.*)/src") local root_dir = utils.get_project_root()
if not root_dir then return print("Error: src directory not found.") end utils.write_file({ root_dir, "src", file_name .. ".java" }, string.format([[
local java_content = string.format([[
public class %s { public class %s {
// New File // New File
} }
]], file_name) ]], file_name))
utils.write_file(root_dir .. "/src/" .. file_name .. ".java", java_content)
end end
}, },
python = { python = {
run = function (file_name) run = function (file_name)
if not file_name:match("%.py$") then file_name = file_name .. ".py" end local root_dir = utils.get_project_root()
local root_dir = M.get_project_root() utils.write_file({ root_dir, "include", file_name .. ".py" }, "")
if not root_dir then return print("Error: Project root not found.") end
local file_path = root_dir .. "/include/" .. file_name
utils.write_file(file_path, "")
end end
}, },
lua = { lua = {
run = function (file_name) run = function (file_name)
local function get_project_root() local root = utils.get_project_root()
local dir = vim.fn.expand("%:p:h") utils.write_file({ root, "lua", utils.get_project_name(), file_name .. ".lua"}, "")
while dir ~= "/" do
local polydev_file = vim.fn.glob(dir .. "/*.polydev")
if polydev_file ~= "" then return dir end
dir = vim.fn.fnamemodify(dir, ":h")
end
return nil
end
local function get_project_name() return vim.fn.glob((get_project_root() or "") .. "/*.polydev"):match("([^/]+)%.polydev$") end
if not get_project_root() or not get_project_name() then return print("Error: Project root not found.") end
utils.write_file(get_project_root() .. "/lua/" .. get_project_name() .. "/" .. file_name .. ".lua", "")
end end
}, },
c = { c = {
run = function (file_name) run = function (file_name)
local function get_project_root() local root_dir = utils.get_project_root()
local dir = vim.fn.expand("%:p:h") utils.write_file({ root_dir, "src", file_name .. ".c" }, "")
while dir ~= "/" do
if vim.fn.isdirectory(dir .. "/src") == 1 or vim.fn.filereadable(dir .. "/CMakeLists.txt") == 1 then
return dir
end
dir = vim.fn.fnamemodify(dir, ":h")
end
end
if not file_name or file_name == "" then return print("File creation canceled.") end
local root_dir = get_project_root()
if not root_dir then return print("Error: Project root not found.") end
utils.write_file(root_dir .. "/src/" .. file_name .. ".c", "")
end end
}, },
cpp = { cpp = {
run = function (file_name) run = function (file_name)
local function get_project_root() local root_dir = utils.get_project_root()
local dir = vim.fn.expand("%:p:h") utils.write_file({ root_dir, "src", file_name .. ".cpp" }, "")
while dir ~= "/" do
if vim.fn.isdirectory(dir .. "/src") == 1 or vim.fn.filereadable(dir .. "/CMakeLists.txt") == 1 then
return dir
end
dir = vim.fn.fnamemodify(dir, ":h")
end
end
local root_dir = get_project_root()
if not root_dir then return print("Error: Project root not found.") end
utils.write_file(root_dir .. "/src/" .. file_name .. ".cpp", "")
end end
}, },
rust = { rust = {
run = function (file_name) run = function (file_name)
local function get_project_root() local root_dir = utils.get_project_root()
local dir = vim.fn.expand("%:p:h") utils.write_file({ root_dir, "src", file_name .. ".rs" }, "")
while dir ~= "/" do
if vim.fn.isdirectory(dir .. "/src") == 1 then
return dir
end
dir = vim.fn.fnamemodify(dir, ":h")
end
end
local root_dir = get_project_root()
if not root_dir then return print("Error: Project root not found.") end
utils.write_file(root_dir .. "/src/" .. file_name .. ".rs", "")
end end
}, },
html = { html = {
run = function (file_name) run = function (file_name)
local html_content = string.format([[ utils.write_file({ ".", file_name .. ".html" }, string.format([[
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
@@ -285,9 +223,7 @@ public class %s {
</body> </body>
</html> </html>
]]) ]]))
utils.write_file("./" .. file_name .. ".html", html_content)
end end
} }
} }
+19
View File
@@ -24,6 +24,11 @@ function M.get_project_root()
return dir return dir
end end
function M.get_project_name()
local root = M.get_project_root()
return vim.fn.fnamemodify(root, ":t")
end
function M.write_file(path_segments, content) function M.write_file(path_segments, content)
local path = table.concat(path_segments, "/") local path = table.concat(path_segments, "/")
local file = assert(io.open(path, "w"), "Error creating file: " .. path) local file = assert(io.open(path, "w"), "Error creating file: " .. path)
@@ -32,6 +37,20 @@ function M.write_file(path_segments, content)
vim.cmd("edit " .. path) vim.cmd("edit " .. path)
end end
function M.init_git(path, gitignore_lines)
vim.fn.mkdir(path, "p")
-- Initialize Git
vim.fn.system({ "git", "-C", path, "init" })
-- Write .gitignore if provided
if gitignore_lines and #gitignore_lines > 0 then
local gitignore_path = { path, ".gitignore" }
local contents = table.concat(gitignore_lines, "\n")
M.write_file(gitignore_path, contents)
end
end
function M.open_float_terminal(cmd) function M.open_float_terminal(cmd)
local presets = require("Polydev.presets") local presets = require("Polydev.presets")
local presets_opts = {} local presets_opts = {}