Simplifying creating of new projects
This commit is contained in:
+37
-76
@@ -112,82 +112,6 @@ function M.open_float_terminal(cmd)
|
|||||||
return buf, win
|
return buf, win
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create a new Java project
|
|
||||||
function M.create_project()
|
|
||||||
-- Use vim.ui.input for better handling of input
|
|
||||||
vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
|
|
||||||
if not project_name or project_name == "" then
|
|
||||||
print("Project creation canceled.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Paths for the project
|
|
||||||
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
|
|
||||||
local src_dir = project_root .. "/src"
|
|
||||||
local build_dir = project_root .. "/build"
|
|
||||||
local include_dir = project_root .. "/include"
|
|
||||||
|
|
||||||
-- Create directories
|
|
||||||
vim.fn.mkdir(src_dir, "p")
|
|
||||||
vim.fn.mkdir(build_dir, "p")
|
|
||||||
vim.fn.mkdir(include_dir, "p")
|
|
||||||
|
|
||||||
-- Write project name to file
|
|
||||||
local project_name_file = io.open(build_dir .. "/" .. project_name .. ".polydev", "w")
|
|
||||||
if project_name_file then
|
|
||||||
project_name_file:write(project_name)
|
|
||||||
project_name_file:close()
|
|
||||||
else
|
|
||||||
print("Error creating project_name.polydev")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Write the Main.java file
|
|
||||||
local main_c_path = src_dir .. "/main.c"
|
|
||||||
local main_c_content = [[
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
printf("Hello World\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
]]
|
|
||||||
local Cmake_path = project_root .. "/" .. "CMakeLists.txt"
|
|
||||||
local Cmake_content = string.format([[
|
|
||||||
cmake_minimum_required(VERSION 3.10)
|
|
||||||
project(%s C)
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
|
||||||
include_directories(include)
|
|
||||||
|
|
||||||
set(SOURCES
|
|
||||||
src/main.c
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(%s ${SOURCES})
|
|
||||||
]], project_name, project_name)
|
|
||||||
|
|
||||||
-- Create the file and write content
|
|
||||||
local file = io.open(main_c_path, "w")
|
|
||||||
if file then
|
|
||||||
file:write(main_c_content)
|
|
||||||
file:close()
|
|
||||||
vim.cmd("edit " .. main_c_path)
|
|
||||||
print(" Project '" .. project_name .. "' created at " .. project_root)
|
|
||||||
else
|
|
||||||
print("Error creating main.c")
|
|
||||||
end
|
|
||||||
|
|
||||||
local cmake_file = io.open(Cmake_path, "w")
|
|
||||||
if cmake_file then
|
|
||||||
cmake_file:write(Cmake_content)
|
|
||||||
cmake_file:close()
|
|
||||||
vim.cmd("edit " .. project_root)
|
|
||||||
print(" Cmake created! '")
|
|
||||||
else
|
|
||||||
print("Error creating CMakeLists.txt")
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function write_file(path, content)
|
local function write_file(path, content)
|
||||||
local file = assert(io.open(path, "w"), "Error creating file: " .. path)
|
local file = assert(io.open(path, "w"), "Error creating file: " .. path)
|
||||||
file:write(content)
|
file:write(content)
|
||||||
@@ -196,6 +120,43 @@ local function write_file(path, content)
|
|||||||
print(path .. " created successfully!")
|
print(path .. " created successfully!")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Create a new Java project
|
||||||
|
function M.create_project()
|
||||||
|
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
|
||||||
|
for _, path in ipairs({ "/src", "/build", "/include" }) do
|
||||||
|
vim.fn.mkdir(project_root .. path, "p")
|
||||||
|
end
|
||||||
|
|
||||||
|
write_file(project_root .. "/build/" .. project_name .. ".polydev", project_name)
|
||||||
|
write_file(project_root .. "/src/main.c", [[
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("Hello World\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
]])
|
||||||
|
|
||||||
|
write_file(project_root .. "/CMakeLists.txt", string.format([[
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(%s C)
|
||||||
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
include_directories(include)
|
||||||
|
|
||||||
|
set(SOURCES src/main.c)
|
||||||
|
add_executable(%s ${SOURCES})
|
||||||
|
]], project_name, project_name))
|
||||||
|
|
||||||
|
vim.cmd("edit " .. project_root .. "/src/main.c")
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
function M.create_new_file()
|
function M.create_new_file()
|
||||||
vim.ui.input({ prompt = "Enter file name: " }, function(class_name)
|
vim.ui.input({ prompt = "Enter file name: " }, function(class_name)
|
||||||
if not class_name then return print("File creation canceled.") end
|
if not class_name then return print("File creation canceled.") end
|
||||||
|
|||||||
Reference in New Issue
Block a user