REFACTOR: Completely changed how project creation is done so I can make

other languages easier and hopefully faster
This commit is contained in:
2026-10-22 00:19:19 -04:00
parent df760783ba
commit 3bb97d833b
9 changed files with 347 additions and 105 deletions
+85
View File
@@ -0,0 +1,85 @@
local M = {}
local utils = require("vern.utils")
local templates = require("vern.templates")
local join, get = vim.fs.joinpath, vim.tbl_get
function M.create_project(opts)
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
pname = pname and vim.trim(pname)
if not pname or pname == "" then
return
end
local configs = require("vern.configs")
local pconfigs = require("vern.configs.python")
local cfg = vim.tbl_deep_extend("force", {}, configs, pconfigs, opts or {})
local ctx = { project_name = pname }
local py = cfg.python
local root = join(vim.fn.expand(cfg.workspace_root), py.root, pname)
vim.fn.mkdir(join(root, utils.fmt(py.source_root, ctx)), "p")
local tmpl = get(py, "templates", "project") or {}
local function dump(k, alt, id)
local path = join(root, utils.fmt(get(tmpl, k, "target") or alt, ctx))
utils.dump(path, templates.fetch("python", id, pname))
return path
end
local bin = dump("bin", py.source_root .. "/main.py", "main")
dump("pkg", py.source_root .. "/__init__.py", "__init__")
dump("cfg", "pyproject.toml", "pyproject")
utils.init_git(root, "python")
local venv_path = root .. "/venv"
vim.fn.system("python3 -m venv " .. venv_path)
vim.cmd.edit(vim.fn.fnameescape(bin))
end)
end
function M.create_file(opts)
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
fname = fname and vim.trim(fname)
if not fname or fname == "" then
return
end
local root = get(opts, "python", "templates", "file", "mod").target or {}
local context = { project_name = utils.proj_name(), name = fname }
local path = join(utils.proj_root(), utils.fmt(root, context))
utils.dump(path, templates.fetch("python", "module", fname))
vim.cmd.edit(vim.fn.expand(path))
end)
end
--- Goal is to add providers for uv and venv so its available to more users
---@param opts? table Takes a table for the runtime
function M.run(opts)
opts = opts or {}
local root = utils.proj_root() or "."
local pname = vim.fs.basename(root)
local exe = opts.python.templates.project.bin.target or "src/%s/main.py"
local full_path = join(vim.fn.expand(root), utils.fmt(exe, { project_name = pname }))
utils.term({ "python3 " .. full_path }, opts.terminal or {})
end
---@param cmd_args? table Takes a table for the runtime
function M.pip_install(opts, cmd_args)
local args = cmd_args and cmd_args.args and vim.trim(cmd_args.args) or ""
if args ~= "" then
utils.term({ "pip install " .. args }, opts.terminal or {})
return
end
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
if not pip_module or vim.trim(pip_module) == "" then
vim.notify("Pip Cancelled", vim.log.levels.WARN)
return
end
utils.term({ "pip install " .. vim.trim(pip_module) }, opts.terminal or {})
end)
end
return M
+36 -14
View File
@@ -1,5 +1,22 @@
local M = {}
-- -- Simple test function that pulls directly from the table and prints
-- function M.test_print()
-- local configs = require("vern.configs")
-- local pconfigs = require("vern.configs.python")
--
-- local cfg = vim.tbl_deep_extend("force", {}, configs, pconfigs)
-- -- Pulling straight from the python_config table
-- local raw_path = cfg.python.templates.project.bin.path
-- local dirs = cfg.python.dirs
-- local context = { project_name = "my_cool_app" }
--
-- local final_path = M.resolve_path(raw_path, dirs, context)
--
-- print("Raw path:", raw_path)
-- print("Resolved path:", final_path)
-- end
local utils = require("vern.utils")
local templates = require("vern.templates")
@@ -19,18 +36,19 @@ function M.create_project(opts)
local py = cfg.python
local root = join(vim.fn.expand(cfg.workspace_root), py.root, pname)
vim.fn.mkdir(join(root, utils.fmt(py.source_root, ctx)), "p")
local tmpl = get(py, "templates", "project") or {}
local function dump(k, alt, id)
local path = join(root, utils.fmt(get(tmpl, k, "target") or alt, ctx))
utils.dump(path, templates.fetch("python", id, pname))
return path
local bin
for tbl_name, tbl in pairs(py.templates.project) do
local raw_path = join(tbl.path, tbl.name)
local path = utils.resolve_path(raw_path, py.dirs, ctx)
if tbl_name == "bin" then
bin = join(root, path)
end
local abs_file_path = join(root, path)
local abs_dir_path = vim.fs.dirname(abs_file_path)
vim.fn.mkdir(abs_dir_path, "p")
utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname))
end
local bin = dump("bin", "src/{project_name}/main.py", "main")
dump("pkg", "src/{project_name}/__init__.py", "__init__")
dump("spec", "pyproject.toml", "pyproject")
utils.init_git(root, "python")
local venv_path = root .. "/venv"
@@ -46,9 +64,13 @@ function M.create_file(opts)
if not fname or fname == "" then
return
end
local root = get(opts, "python", "templates", "file", "module").target or {}
local py = get(opts, "python")
local cfg = get(py, "templates", "file", "mod") or {}
local raw_path = join(cfg.path, cfg.name)
local context = { project_name = utils.proj_name(), name = fname }
local path = join(utils.proj_root(), utils.fmt(root, context))
local path = utils.resolve_path(raw_path, py.dirs or {}, context)
utils.dump(path, templates.fetch("python", "module", fname))
vim.cmd.edit(vim.fn.expand(path))
end)
@@ -61,8 +83,8 @@ function M.run(opts)
local root = utils.proj_root() or "."
local pname = vim.fs.basename(root)
local exe = opts.python.templates.project.bin.target or "src/%s/main.py"
local full_path = vim.fn.expand(root, utils.fmt(exe, { project_name = pname }))
local exe = opts.python.templates.project.bin.target or "src/{project_name}/main.py"
local full_path = join(vim.fn.expand(root), utils.fmt(exe, { project_name = pname }))
utils.term({ "python3 " .. full_path }, opts.terminal or {})
end