179 lines
6.0 KiB
Lua
179 lines
6.0 KiB
Lua
local M = {}
|
|
|
|
local utils = require("vern.utils")
|
|
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 context = {
|
|
project_name = pname,
|
|
}
|
|
|
|
local bin = vim.tbl_get(
|
|
cfg,
|
|
"python",
|
|
"templates",
|
|
"project",
|
|
"bin",
|
|
"target"
|
|
) or ""
|
|
-- bin = bin["bin"].target or "src/{project_name}/main.py"
|
|
|
|
local bin_path = utils.interpolate(bin, context)
|
|
vim.notify(bin_path, vim.log.levels.INFO)
|
|
vim.fn.mkdir(
|
|
vim.fs.dirname(vim.fs.joinpath(project_root, bin_path)),
|
|
"p"
|
|
)
|
|
utils.write_file(
|
|
vim.fs.joinpath(project_root, bin_path),
|
|
templates.main(pname)
|
|
)
|
|
vim.notify(
|
|
string.format("Created project %s at %s", pname, project_root),
|
|
vim.log.levels.INFO
|
|
)
|
|
-- vim.cmd("edit " .. utils.get_project_root() .. "/" .. rel_path)
|
|
-- vim.fn.mkdir(full_project_root .. "/include", "p")
|
|
-- utils.write_file(
|
|
-- { full_project_root, "main.py" },
|
|
-- [[
|
|
-- def main():
|
|
-- print("Hello, World!")
|
|
--
|
|
-- if __name__ == "__main__":
|
|
-- main()
|
|
-- ]]
|
|
-- )
|
|
-- utils.write_file({ full_project_root, "include/__init__.py" }, "")
|
|
-- utils.write_file({ full_project_root, "requirements.txt" }, "")
|
|
-- utils.write_file(
|
|
-- { full_project_root, "setup.py" },
|
|
-- string.format(
|
|
-- [[
|
|
-- from setuptools import setup, find_packages
|
|
--
|
|
-- setup(
|
|
-- name="%s",
|
|
-- version="0.1.0",
|
|
-- packages=find_packages(),
|
|
-- install_requires=[],
|
|
-- python_requires=">=3.6",
|
|
-- )
|
|
-- ]],
|
|
-- project_name
|
|
-- )
|
|
-- )
|
|
-- local gitignore = {
|
|
-- "venv/",
|
|
-- }
|
|
-- utils.init_git(full_project_root, gitignore)
|
|
-- vim.cmd("edit " .. full_project_root .. "/main.py")
|
|
-- local venv_path = full_project_root .. "/venv"
|
|
-- vim.fn.system("python3 -m venv " .. venv_path)
|
|
-- vim.g.python3_host_prog = venv_path .. "/bin/python"
|
|
-- vim.fn.setenv("VIRTUAL_ENV", venv_path)
|
|
-- vim.fn.setenv("PATH", venv_path .. "/bin:" .. vim.fn.getenv("PATH"))
|
|
-- print("Virtual environment activated for " .. project_name)
|
|
-- end
|
|
--
|
|
-- local context = {
|
|
-- project_name = utils.get_project_name(),
|
|
-- name = pname,
|
|
-- }
|
|
-- -- local exe = opts.python.templates.project["executable"].target
|
|
-- local init = opts.python.templates.project["init"].target
|
|
-- local manifest = opts.python.templates.project["manifest"].target
|
|
--
|
|
-- local exe = (
|
|
-- opts.python.templates.project["executable"].target:gsub(
|
|
-- "{(%w+)}",
|
|
-- context
|
|
-- )
|
|
-- )
|
|
--
|
|
-- -- local loc = opts.python.templates.file["module"].target
|
|
-- -- local rel_path = (loc:gsub("{(%w+)}", context))
|
|
-- local path = vim.fs.joinpath(utils.get_project_root(), exe)
|
|
-- utils.write_file(path, templates.main(pname))
|
|
|
|
-- vim.cmd("edit " .. utils.get_project_root() .. "/" .. exe)
|
|
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 = opts.python.templates.file["module"].target
|
|
local context = {
|
|
project_name = utils.get_project_name(),
|
|
name = fname,
|
|
}
|
|
local rel_path = (loc:gsub("{(%w+)}", context))
|
|
local path = vim.fs.joinpath(utils.get_project_root(), rel_path)
|
|
utils.write_file(path, templates.module(fname))
|
|
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.executable.target
|
|
or "src/%s/main.py"
|
|
-- local rel_path = exe:find("%%s") and string.format(exe, project_name) or exe
|
|
local rel_path = string.gsub(exe, "%%s", 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
|