Files
vern-nvim/lua/vern/actions/python.lua
T

119 lines
3.7 KiB
Lua

local M = {}
local utils = require("vern.utils")
local render = require("vern.templates")
local templates = require("vern.templates.python")
function M.create_project(opts)
opts = opts or {}
vim.ui.input({
prompt = "Enter project name: ",
default = "",
}, function(pname)
if not pname or vim.trim(pname) == "" then
return
end
pname = vim.trim(pname)
local configs = require("vern.configs")
local pconfigs = require("vern.configs.python")
local cfg =
vim.tbl_deep_extend("force", {}, configs, pconfigs, opts or {})
local project_root = vim.fs.joinpath(
vim.fn.expand(cfg.workspace_root),
cfg.python.root,
pname
)
local ctx = {
project_name = pname,
}
local dest = vim.tbl_get(cfg, "python", "templates", "project") or {}
vim.fn.mkdir(
vim.fs.joinpath(
project_root,
utils.fmt(cfg.python.source_root, ctx)
),
"p"
)
local function dump(dst, alt, tmpl)
local full_path = vim.fs.joinpath(
project_root,
utils.fmt(vim.tbl_get(dest, dst, "target") or alt, ctx)
)
utils.dump(full_path, tmpl)
return full_path
end
local bin =
dump("bin", "src/{project_name}/main.py", templates.main(pname))
dump("pkg", "src/{project_name}/__init__.py", templates.__init__(pname))
dump("spec", "pyproject.toml", templates.pyproject(pname))
vim.cmd.edit(vim.fn.fnameescape(bin))
end)
end
function M.create_file(opts)
opts = opts or {}
vim.ui.input({
prompt = "Enter file name: ",
default = "",
}, function(fname)
if fname ~= nil and vim.trim(fname) ~= "" then
local loc = vim.tbl_get(
opts,
"python",
"templates",
"file",
"module"
).target or {}
local context = {
project_name = utils.get_project_name(),
name = fname,
}
local rel_path = utils.fmt(loc, context)
local path = vim.fs.joinpath(utils.get_project_root(), rel_path)
utils.dump(path, templates.)
vim.cmd("edit " .. utils.get_project_root() .. "/" .. rel_path)
end
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.get_project_root() or "."
local project_name = vim.fs.basename(root)
local exe = opts.python.templates.project.bin.target or "src/%s/main.py"
-- local rel_path = exe:find("%%s") and string.format(exe, project_name) or exe
local rel_path = utils.fmt(exe, { project_name = project_name })
local full_path = root .. "/" .. rel_path
utils.terminal({ "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.terminal({ "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.terminal(
{ "pip install " .. vim.trim(pip_module) },
opts.terminal or {}
)
end)
end
return M