refactor: fixed deprecated write_file to use table instead of string for

each language in templates.projects
This commit is contained in:
DarthMooMancer
2025-05-12 00:41:05 -04:00
parent 677e5bbc67
commit 4ecaf54f82
7 changed files with 103 additions and 106 deletions
+1 -1
View File
@@ -16,5 +16,5 @@ if [[ "$DIR" == "/" ]]; then
exit 1 exit 1
fi fi
echo "✅ Project root found at: $DIR" echo "$DIR"
+2
View File
@@ -12,12 +12,14 @@ function M.setup(opts)
end end
vim.api.nvim_create_user_command("PolydevOpen", ui.open_project_manager, {}) vim.api.nvim_create_user_command("PolydevOpen", ui.open_project_manager, {})
vim.api.nvim_create_user_command("PolydevGet", require("Polydev.utils").get_project_root, {})
local keys = vim.tbl_deep_extend("force", {}, config.get("globals").keybinds, opts or {}) local keys = vim.tbl_deep_extend("force", {}, config.get("globals").keybinds, opts or {})
for key, command in pairs(keys) do for key, command in pairs(keys) do
vim.keymap.set("n", key, ":PolydevOpen<CR>", { silent = true }) vim.keymap.set("n", key, ":PolydevOpen<CR>", { silent = true })
break break
end end
vim.keymap.set("n", "<leader>tt", ":PolydevGet<CR>", { silent = true })
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd("FileType", {
pattern = "*", pattern = "*",
-25
View File
@@ -10,15 +10,11 @@ function M.setup(opts)
M.keybinds[command] = key M.keybinds[command] = key
end end
vim.api.nvim_create_user_command("NewCHeaderFile", M.create_new_header_file, {})
vim.api.nvim_create_user_command("CRun", M.run, {}) vim.api.nvim_create_user_command("CRun", M.run, {})
if M.keybinds.CRun then if M.keybinds.CRun then
vim.keymap.set("n", M.keybinds.CRun, ":CRun<CR>", { silent = true }) vim.keymap.set("n", M.keybinds.CRun, ":CRun<CR>", { silent = true })
end end
if M.keybinds.NewCHeaderFile then
vim.keymap.set("n", M.keybinds.NewCHeaderFile, ":NewCHeaderFile<CR>", { silent = true })
end
end end
function M.get_project_root() function M.get_project_root()
@@ -31,27 +27,6 @@ function M.get_project_root()
end end
end end
function M.create_new_header_file()
vim.ui.input({ prompt = "Enter header file name: " }, function(header_name)
if not header_name then return print("Header file creation canceled.") end
local root_dir = M.get_project_root()
if not root_dir then return print("Error: Project root not found.") end
local guard_macro = header_name:upper():gsub("[^A-Z0-9]", "_") .. "_H"
local content = string.format([[
#ifndef %s
#define %s
#include <stdio.h>
// Function prototypes
void example_function();
#endif // %s
]], guard_macro, guard_macro, guard_macro)
utils.write_file(root_dir .. "/include/" .. header_name .. ".h", content)
end)
end
function M.run() function M.run()
local root = M.get_project_root() local root = M.get_project_root()
if not root then return print("Error: Project root not found.") end if not root then return print("Error: Project root not found.") end
+55 -68
View File
@@ -2,19 +2,6 @@ local utils = require("Polydev.utils")
local M = {} local M = {}
---@class TemplateLanguage
---@field run fun(project_name: string, project_root: string): string
---@class Projects
---@field java TemplateLanguage
---@field python TemplateLanguage
---@field lua TemplateLanguage
---@field c TemplateLanguage
---@field cpp TemplateLanguage
---@field rust TemplateLanguage
---@field html TemplateLanguage
---@alias TemplatesLanguageName "java" | "python" | "lua" | "c" | "cpp" | "rust" | "html"
local function init_git(path, gitignore_lines) local function init_git(path, gitignore_lines)
vim.fn.mkdir(path, "p") vim.fn.mkdir(path, "p")
@@ -29,41 +16,39 @@ local function init_git(path, gitignore_lines)
end end
end end
---@type Projects
M.projects = { M.projects = {
java = { java = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build" }) do vim.fn.mkdir(full_project_root .. path, "p") end for _, path in ipairs({ "/src", "/build" }) do vim.fn.mkdir(full_project_root .. path, "p") end
local main_java_content = [[ local content = [[
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello, World!"); System.out.println("Hello, World!");
} }
} }
]] ]]
local java_gitignore = { local gitignore = {
"build/" "build/"
} }
init_git(full_project_root, java_gitignore) init_git(full_project_root, gitignore)
utils.write_file({full_project_root, "src", "Main.java"}, main_java_content) utils.write_file({ full_project_root, "src", "Main.java" }, content)
return full_project_root
end end
}, },
python = { python = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
vim.fn.mkdir(full_project_root .. "/include", "p") vim.fn.mkdir(full_project_root .. "/include", "p")
utils.write_file(full_project_root .. "/main.py", [[ utils.write_file({ full_project_root, "main.py" }, [[
def main(): def main():
print("Hello, World!") print("Hello, World!")
if __name__ == "__main__": if __name__ == "__main__":
main() main()
]]) ]])
utils.write_file(full_project_root .. "/include/__init__.py", "") utils.write_file({ full_project_root, "include/__init__.py" }, "")
utils.write_file(full_project_root .. "/requirements.txt", "") utils.write_file({ full_project_root, "requirements.txt" }, "")
utils.write_file(full_project_root .. "/setup.py", string.format([[ utils.write_file({ full_project_root, "setup.py" }, string.format([[
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup( setup(
@@ -74,6 +59,10 @@ setup(
python_requires=">=3.6", python_requires=">=3.6",
) )
]], project_name)) ]], project_name))
local gitignore = {
"venv/"
}
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)
@@ -81,38 +70,35 @@ setup(
vim.fn.setenv("VIRTUAL_ENV", venv_path) vim.fn.setenv("VIRTUAL_ENV", venv_path)
vim.fn.setenv("PATH", venv_path .. "/bin:" .. vim.fn.getenv("PATH")) vim.fn.setenv("PATH", venv_path .. "/bin:" .. vim.fn.getenv("PATH"))
print("Virtual environment activated for " .. project_name) print("Virtual environment activated for " .. project_name)
return full_project_root
end end
}, },
lua = { lua = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/lua/" .. project_name }) do vim.fn.mkdir(full_project_root .. path, "p") end vim.fn.mkdir(full_project_root .. "/lua/" .. project_name, "p")
utils.write_file(full_project_root .. "/lua/" .. project_name .. "/init.lua", [[ utils.write_file({ full_project_root, "lua", project_name, "init.lua" }, [[
local M = {} local M = {}
return M return M
]]) ]])
utils.write_file(full_project_root .. "/" .. project_name .. ".polydev", "[[DO NOT REMOVE THIS FILE]]") local gitignore = { "" }
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")
return full_project_root
end end
}, },
c = { c = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
for _, path in ipairs({ "/src", "/build", "/include" }) do vim.fn.mkdir(full_project_root .. path, "p") end for _, path in ipairs({ "/src", "/build", "/include" }) do vim.fn.mkdir(full_project_root .. path, "p") end
utils.write_file(full_project_root .. "/build/" .. project_name .. ".polydev", project_name) utils.write_file({ full_project_root, "src/main.c" }, [[
utils.write_file(full_project_root .. "/src/main.c", [[
#include <stdio.h> #include <stdio.h>
int int main() {
main() {
printf("%s", "Hello World"); printf("%s", "Hello World");
return 0; return 0;
} }
]]) ]])
utils.write_file(full_project_root .. "/CMakeLists.txt", string.format([[ utils.write_file({ full_project_root, "CMakeLists.txt" }, string.format([[
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(%s) project(%s)
include_directories(include) include_directories(include)
@@ -125,9 +111,10 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
# find_package(SDL3 CONFIG REQUIRED) # An example of how to import libraries from vcpkg # find_package(SDL3 CONFIG REQUIRED) # An example of how to import libraries from vcpkg
# target_link_libraries(Game PRIVATE SDL3::SDL3) # target_link_libraries(Game PRIVATE SDL3::SDL3)
]], project_name, project_name)) ]], project_name, project_name))
vim.cmd("edit " .. full_project_root .. "/src/main.c") local gitignore = { "build/" }
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))
return full_project_root vim.cmd("edit " .. full_project_root .. "/src/main.c")
end end
}, },
cpp = { cpp = {
@@ -136,8 +123,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
for _, path in ipairs({ "/src", "/build", "/include" }) do for _, path in ipairs({ "/src", "/build", "/include" }) do
vim.fn.mkdir(full_project_root .. path, "p") vim.fn.mkdir(full_project_root .. path, "p")
end end
utils.write_file(full_project_root .. "/build/" .. project_name .. ".polydev", project_name) utils.write_file({ full_project_root, "src/main.cpp" }, [[
utils.write_file(full_project_root .. "/src/main.cpp", [[
#include <iostream> #include <iostream>
int main() { int main() {
@@ -145,7 +131,7 @@ int main() {
return 0; return 0;
} }
]]) ]])
utils.write_file(full_project_root .. "/CMakeLists.txt", string.format([[ utils.write_file({ full_project_root, "CMakeLists.txt" }, string.format([[
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(%s) project(%s)
include_directories(include) include_directories(include)
@@ -159,12 +145,10 @@ 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))
utils.write_file(full_project_root .. "/.gitignore", string.format([[ local gitignore = { "build/" }
build/ 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")
return full_project_root
end end
}, },
rust = { rust = {
@@ -172,7 +156,6 @@ build/
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
vim.fn.system("cargo new " .. full_project_root) vim.fn.system("cargo new " .. full_project_root)
vim.cmd("edit " .. full_project_root .. "/src/main.rs") vim.cmd("edit " .. full_project_root .. "/src/main.rs")
return full_project_root
end end
}, },
html = { html = {
@@ -192,26 +175,11 @@ build/
</body> </body>
</html> </html>
]] ]]
utils.write_file(full_project_root .. "index.html", main_html_content) utils.write_file({ full_project_root, "index.html" }, main_html_content)
return full_project_root
end end
}, },
} }
---@class TemplateFile
---@field run fun(file_name: string): nil
---@class Files
---@field java TemplateFile
---@field python TemplateFile
---@field lua TemplateFile
---@field c TemplateFile
---@field cpp TemplateFile
---@field rust TemplateFile
---@field html TemplateFile
---@alias TemplatesFileName "java" | "python" | "lua" | "c" | "cpp" | "rust" | "html"
---@type Files
M.files = { M.files = {
java = { java = {
run = function (file_name) run = function (file_name)
@@ -324,25 +292,44 @@ public class %s {
} }
} }
---@type string M.aux_files = {
M.dir = "" c = {
run = function (file_name)
if not file_name then return print("Header file creation canceled.") end
local root_dir = utils.get_project_root()
local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. "_H"
local content = string.format([[
#ifndef %s
#define %s
// Add code here
#endif
]], guard_macro, guard_macro)
utils.write_file({ root_dir, "include", file_name .. ".h" }, content)
end
}
}
---@param lang TemplatesLanguageName
---@param project_root string
function M.create_project(lang, project_root) function M.create_project(lang, project_root)
vim.ui.input({ prompt = "Enter project name: " }, function(project_name) vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then return print("Project creation canceled.") end if not project_name or project_name == "" then return print("Project creation canceled") end
M.dir = M.projects[lang].run(project_name, project_root) M.projects[lang].run(project_name, project_root)
end) end)
end end
---@param lang TemplatesFileName
function M.create_new_file(lang) function M.create_new_file(lang)
vim.ui.input({ prompt = "Enter file name: " }, function(file_name) vim.ui.input({ prompt = "Enter file name: " }, function(file_name)
if not file_name or file_name == "" then return print("File creation canceled.") end if not file_name or file_name == "" then return print("File creation canceled") end
M.files[lang].run(file_name) M.files[lang].run(file_name)
end) end)
end end
---@return any function M.create_aux_file(lang)
vim.ui.input({ promt = "Enter file name: "}, function(file_name)
if not file_name or file_name == "" then return print("File creation canceled") end
M.aux_files[lang].run(file_name)
end)
end
return M return M
+20 -9
View File
@@ -287,15 +287,25 @@ function M.open_project_manager()
-- Creates a new file in project -- Creates a new file in project
popup:map("n", "%", function() popup:map("n", "%", function()
vim.ui.input({ prompt = "Enter language for new file: " }, function(lang) vim.ui.input({ prompt = "Enter language for new file: " }, function(lang)
if not lang or lang == "" then return print("File creation canceled.") end if not lang or lang == "" then return print("File creation canceled") end
if M.load_language_module(lang) and templates.files[lang] and templates.files[lang].run then if M.load_language_module(lang) and templates.files[lang] and templates.files[lang].run then
vim.ui.input({ prompt = "Enter file name: " }, function(file_name) popup:unmount()
if not file_name or file_name == "" then return print("File creation canceled.") end vim.schedule(function()
popup:unmount() templates.create_new_file(lang)
vim.schedule(function() end)
templates.files[lang].run(file_name) else
end) print("Error: No File creation method for " .. lang)
end
end)
end)
popup:map("n", "x", function()
vim.ui.input({ prompt = "Enter language for new auxilary file: "}, function(lang)
if not lang or lang == "" then return print("File create canceled") end
if M.load_language_module(lang) and templates.aux_files[lang] and templates.aux_files[lang].run then
popup:unmount()
vim.schedule(function()
templates.create_aux_file(lang)
end) end)
else else
print("Error: No File creation method for " .. lang) print("Error: No File creation method for " .. lang)
@@ -307,7 +317,7 @@ function M.open_project_manager()
popup:map("n", "d", function() popup:map("n", "d", function()
local opts = {} local opts = {}
vim.ui.input({ prompt = "Enter language for project: " }, function(lang) vim.ui.input({ prompt = "Enter language for project: " }, function(lang)
if not lang or lang == "" then return print("Project creation canceled.") end if not lang or lang == "" then return print("Project creation canceled") end
opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get(lang), opts or {}) opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get(lang), opts or {})
if M.load_language_module(lang) and templates.projects[lang] and templates.projects[lang].run then if M.load_language_module(lang) and templates.projects[lang] and templates.projects[lang].run then
popup:unmount() popup:unmount()
@@ -379,3 +389,4 @@ function M.open_project_manager()
end end
return M return M
+15 -2
View File
@@ -7,8 +7,21 @@ function M.setup(opts)
end end
function M.is_dir(path) function M.is_dir(path)
local stat = vim.loop.fs_stat(path) local stat = vim.loop.fs_stat(path)
return stat and stat.type == "directory" return stat and stat.type == "directory"
end
function M.get_project_root()
-- Get absolute path to this Lua file (or any file in your plugin)
local plugin_path = debug.getinfo(1, "S").source:sub(2)
local plugin_dir = vim.fn.fnamemodify(plugin_path, ":h:h:h")
-- Build the full path to your script
local script_path = plugin_dir .. "/bash_scripts/walkup.sh"
-- Run the script and get output
local dir = vim.fn.system(script_path):gsub("%s+$", "")
return dir
end end
function M.write_file(path_segments, content) function M.write_file(path_segments, content)
+9
View File
@@ -0,0 +1,9 @@
-- Get absolute path to this Lua file (or any file in your plugin)
local plugin_path = debug.getinfo(1, "S").source:sub(2)
local plugin_dir = vim.fn.fnamemodify(plugin_path, ":h")
-- Build the full path to your script
local script_path = plugin_dir .. "/bash_scripts/walkup.sh"
-- Run the script and get output
local dir = vim.fn.system(script_path):gsub("%s+$", "")