REFACTOR: Completely changed how project creation is done so I can make
other languages easier and hopefully faster
This commit is contained in:
@@ -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
@@ -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
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
local actions = require("vern.actions.python")
|
||||
local config = require("vern.configs.python")
|
||||
|
||||
return function(bufnr, user_opts)
|
||||
local opts = vim.tbl_deep_extend("force", {}, config, user_opts or {})
|
||||
vim.api.nvim_buf_create_user_command(bufnr, "Pythonrun", function()
|
||||
if type(actions.run) == "function" then
|
||||
actions.run(opts)
|
||||
end
|
||||
end, { desc = "Run python project" })
|
||||
|
||||
vim.api.nvim_buf_create_user_command(bufnr, "Pythonpip", function(cmd_args)
|
||||
if actions.pip_install then
|
||||
actions.pip_install(opts, cmd_args)
|
||||
end
|
||||
end, { nargs = "*", desc = "Install python packages" })
|
||||
|
||||
for key, def in pairs(opts.python.keybinds or {}) do
|
||||
if type(def.action) == "function" then
|
||||
vim.keymap.set("n", key, function()
|
||||
def.action(opts)
|
||||
end, {
|
||||
buffer = bufnr,
|
||||
silent = true,
|
||||
desc = def.desc,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
local root = require("vern.utils").proj_root()
|
||||
if not root then
|
||||
return
|
||||
end
|
||||
local venv_dir = nil
|
||||
if vim.uv.fs_stat(root .. "/venv/bin/python") then
|
||||
venv_dir = root .. "/venv"
|
||||
elseif vim.uv.fs_stat(root .. "/.venv/bin/python") then
|
||||
venv_dir = root .. "/.venv"
|
||||
end
|
||||
if venv_dir then
|
||||
vim.env.VIRTUAL_ENV = venv_dir
|
||||
vim.env.PATH = venv_dir .. "/bin:" .. vim.env.PATH
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
return {
|
||||
c = {
|
||||
root = "C",
|
||||
-- source_root = "src/{project_name}",
|
||||
keybinds = {
|
||||
["<leader>pr"] = {
|
||||
desc = "Executes c code",
|
||||
action = require("vern.actions.c").run,
|
||||
},
|
||||
["<leader>px"] = {
|
||||
desc = "Creates header files",
|
||||
action = require("vern.actions.python").create_header,
|
||||
},
|
||||
},
|
||||
templates = {
|
||||
project = {
|
||||
bin = {
|
||||
target = "src/{project_name}/main.py",
|
||||
},
|
||||
pkg = {
|
||||
target = "src/{project_name}/__init__.py",
|
||||
},
|
||||
cfg = {
|
||||
target = "pyproject.toml",
|
||||
},
|
||||
},
|
||||
file = {
|
||||
mod = {
|
||||
target = "src/{project_name}/{name}.py",
|
||||
},
|
||||
},
|
||||
extras = {
|
||||
gitignore = {
|
||||
template = "vern.templates.extras.gitignore", --- needs to call the function in file
|
||||
target = ".gitignore",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
+49
-36
@@ -1,40 +1,53 @@
|
||||
return {
|
||||
python = {
|
||||
root = "Python",
|
||||
source_root = "src/{project_name}",
|
||||
keybinds = {
|
||||
["<leader>pr"] = {
|
||||
desc = "Executes python code",
|
||||
action = require("vern.actions.python").run,
|
||||
},
|
||||
["<leader>pb"] = {
|
||||
desc = "Asks for what python package to install",
|
||||
action = require("vern.actions.python").pip_install,
|
||||
},
|
||||
local M = {}
|
||||
|
||||
M.python = {
|
||||
root = "Python",
|
||||
dirs = {
|
||||
src = "src/{project_name}",
|
||||
},
|
||||
keybinds = {
|
||||
["<leader>pr"] = {
|
||||
desc = "Executes python code",
|
||||
action = require("vern.actions.python").run,
|
||||
},
|
||||
templates = {
|
||||
project = {
|
||||
["bin"] = {
|
||||
target = "src/{project_name}/main.py",
|
||||
},
|
||||
["pkg"] = {
|
||||
target = "src/{project_name}/__init__.py",
|
||||
},
|
||||
["spec"] = {
|
||||
target = "pyproject.toml",
|
||||
},
|
||||
},
|
||||
file = {
|
||||
["module"] = {
|
||||
target = "src/{project_name}/{name}.py",
|
||||
},
|
||||
},
|
||||
extras = {
|
||||
["gitignore"] = {
|
||||
template = "vern.templates.extras.gitignore", --- needs to call the function in file
|
||||
target = ".gitignore",
|
||||
},
|
||||
},
|
||||
["<leader>pb"] = {
|
||||
desc = "Asks for what python package to install",
|
||||
action = require("vern.actions.python").pip_install,
|
||||
},
|
||||
},
|
||||
templates = {
|
||||
project = {
|
||||
bin = {
|
||||
path = "{dirs.src}",
|
||||
-- path = "src/{project_name}",
|
||||
name = "main.py",
|
||||
kind = "main",
|
||||
kind_path = "vern.templates.python",
|
||||
},
|
||||
pkg = {
|
||||
path = "{dirs.src}",
|
||||
-- path = "src/{project_name}",
|
||||
name = "__init__.py",
|
||||
kind = "__init__",
|
||||
kind_path = "vern.templates.python",
|
||||
},
|
||||
cfg = {
|
||||
path = ".",
|
||||
name = "pyproject.toml",
|
||||
kind = "pyproject",
|
||||
kind_path = "vern.templates.python",
|
||||
},
|
||||
},
|
||||
file = {
|
||||
mod = {
|
||||
path = "{dirs.src}",
|
||||
-- path = "src/{project_name}",
|
||||
name = "{name}.py",
|
||||
kind = "module",
|
||||
kind_path = "vern.templates.python",
|
||||
},
|
||||
},
|
||||
extras = {},
|
||||
},
|
||||
}
|
||||
return M
|
||||
|
||||
@@ -221,45 +221,6 @@ M.languages = {
|
||||
end
|
||||
end,
|
||||
},
|
||||
python = {
|
||||
setup = function(self, user_opts)
|
||||
local opts = vim.tbl_deep_extend("force", {}, configs.get("python"), user_opts or {})
|
||||
vim.api.nvim_create_user_command("PythonPip", function()
|
||||
self:install_dependency()
|
||||
end, {})
|
||||
vim.api.nvim_create_user_command("PythonRun", function()
|
||||
self:run()
|
||||
end, {})
|
||||
for key, command in pairs(opts.keybinds) do
|
||||
if command == "PythonRun" then
|
||||
vim.keymap.set("n", key, ":PythonRun<CR>", { silent = true })
|
||||
end
|
||||
if command == "PythonPip" then
|
||||
vim.keymap.set("n", key, ":PythonPip<CR>", { silent = true })
|
||||
end
|
||||
end
|
||||
|
||||
local root = utils.get_project_root()
|
||||
if root then
|
||||
local venv_path = root .. "/venv/bin/activate"
|
||||
if vim.fn.filereadable(venv_path) == 1 then
|
||||
vim.env.VIRTUAL_ENV = venv_path
|
||||
vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH
|
||||
end
|
||||
end
|
||||
end,
|
||||
install_dependency = function()
|
||||
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
|
||||
if not pip_module or pip_module == "" then
|
||||
return print("Pip canceled")
|
||||
end
|
||||
utils.terminal({ "pip install " .. pip_module })
|
||||
end)
|
||||
end,
|
||||
run = function()
|
||||
utils.terminal({ "python3 " .. utils.get_project_root(), "main.py" })
|
||||
end,
|
||||
},
|
||||
lua = {
|
||||
setup = function(self, user_opts)
|
||||
local opts = vim.tbl_deep_extend("force", {}, configs.get("lua"), user_opts or {})
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
return {
|
||||
--[[ =====================================================================
|
||||
TEMPLATE: MAIN (Entrypoint)
|
||||
File: main.c
|
||||
Target: src/{project_name}/main.py
|
||||
===================================================================== --]]
|
||||
main = vim.trim([=[
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
char* str = "Hello World";
|
||||
printf("%s\n", str);
|
||||
return 0;
|
||||
}
|
||||
]=]) .. "\n",
|
||||
|
||||
--[[ =====================================================================
|
||||
TEMPLATE: INIT (Package Anchor)
|
||||
File: __init__.py
|
||||
Target: src/{project_name}/__init__.py
|
||||
===================================================================== --]]
|
||||
__init__ = vim.trim([=[
|
||||
# %s init
|
||||
|
||||
__all__: list[str] = []
|
||||
]=]) .. "\n",
|
||||
|
||||
--[[ =====================================================================
|
||||
TEMPLATE: PYPROJECT (Spec / Build Manifest)
|
||||
File: pyproject.toml
|
||||
Target: pyproject.toml
|
||||
===================================================================== --]]
|
||||
pyproject = vim.trim([=[
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "%s"
|
||||
version = "0.1.0"
|
||||
description = "Project %s"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = []
|
||||
|
||||
[project.scripts]
|
||||
%s = "%s.main:main"
|
||||
]=]) .. "\n",
|
||||
|
||||
--[[ =====================================================================
|
||||
TEMPLATE: MODULE (Standalone File)
|
||||
File: {name}.py
|
||||
Target: src/{project_name}/{name}.py
|
||||
===================================================================== --]]
|
||||
module = vim.trim([=[
|
||||
"""
|
||||
Module: %s
|
||||
"""
|
||||
]=]) .. "\n",
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
local M = {}
|
||||
|
||||
---@param ft string Filetype of template
|
||||
---@param template string Template in language table
|
||||
---@param path string Filetype of template
|
||||
---@param kind string Template in language table
|
||||
---@param name string A string to replace temp values in template
|
||||
---@return string
|
||||
function M.fetch(ft, template, name)
|
||||
ft, template, name = ft or "", template or "", name or ""
|
||||
if ft == "" or template == "" or name == "" then
|
||||
function M.fetch(kind_path, kind, name)
|
||||
kind_path, kind, name = kind_path or "", kind or "", name or ""
|
||||
if kind_path == "" or kind == "" or name == "" then
|
||||
return ""
|
||||
end
|
||||
local ok, mod = pcall(require, "vern.templates." .. ft)
|
||||
local ok, mod = pcall(require, kind_path)
|
||||
if ok and type(mod) == "table" then
|
||||
return (string.gsub(mod[template], "%%s", name))
|
||||
return (string.gsub(mod[kind], "%%s", name))
|
||||
end
|
||||
return ""
|
||||
end
|
||||
|
||||
+24
-9
@@ -26,6 +26,24 @@ function M.proj_root(bufnr)
|
||||
return vim.fs.root(bufnr, root_markers) or vim.uv.cwd()
|
||||
end
|
||||
|
||||
---@param raw_path string Combination of tbl.path and tbl.name to override
|
||||
---@param dir_tbl table<string, string> Strings to replace raw directories in {dirs.<>} with
|
||||
---@param ctx table<string, string> Strings to replace raw names like {project_name} with
|
||||
---@return string path Fully replaced path
|
||||
function M.resolve_path(raw_path, dir_tbl, ctx)
|
||||
--- Replace the {dirs.<key>}
|
||||
raw_path = raw_path:gsub("{dirs%.([%w_]+)}", function(dir_key)
|
||||
return dir_tbl and dir_tbl[dir_key] or dir_key
|
||||
end)
|
||||
|
||||
--- Replace the {<key>}
|
||||
local path = raw_path:gsub("{([%w_]+)}", function(key)
|
||||
return ctx and ctx[key] or ("{" .. key .. "}")
|
||||
end)
|
||||
|
||||
return path
|
||||
end
|
||||
|
||||
---@return string
|
||||
function M.proj_name()
|
||||
return vim.fn.fnamemodify(M.proj_root(), ":t")
|
||||
@@ -44,6 +62,12 @@ function M.init_git(path, ft)
|
||||
M.dump(vim.fs.joinpath(path, ".gitignore"), gitignore)
|
||||
end
|
||||
|
||||
---@param path string
|
||||
function M.init_readme(path)
|
||||
local readme = require("vern.templates.extras.readme")
|
||||
M.dump(vim.fs.joinpath(path, "README.md"), readme)
|
||||
end
|
||||
|
||||
---@param cmd table
|
||||
---@return integer, integer?
|
||||
function M.term(cmd, opts)
|
||||
@@ -116,12 +140,3 @@ function M.term(cmd, opts)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
-- function M.exists(path)
|
||||
-- return (io.open(path, "r") ~= nil)
|
||||
-- end
|
||||
--
|
||||
--
|
||||
--
|
||||
--
|
||||
-- return M
|
||||
|
||||
Reference in New Issue
Block a user