91 lines
3.1 KiB
Lua
91 lines
3.1 KiB
Lua
local M = {}
|
|
|
|
local utils = require("vern.utils")
|
|
local templates = require("vern.templates")
|
|
local action_utils = require("vern.actions.utils")
|
|
|
|
local join = vim.fs.joinpath
|
|
|
|
---@param opts VernPythonConfig
|
|
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.python")
|
|
|
|
---@type VernPythonConfig
|
|
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
|
local root = join(vim.fn.expand(cfg.workspace_root), cfg.python.root, pname)
|
|
local bin = action_utils.create(cfg.python, pname, root)
|
|
|
|
utils.init_git(root, "python")
|
|
|
|
local venv_path = root .. "/venv"
|
|
vim.fn.system("python3 -m venv " .. venv_path)
|
|
|
|
vim.notify("Project: " .. pname .. " has been created!")
|
|
vim.cmd.edit(vim.fn.fnameescape(bin))
|
|
end)
|
|
end
|
|
|
|
---@param opts VernPythonConfig
|
|
function M.create_file(opts)
|
|
opts = opts or {}
|
|
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
|
|
fname = fname and vim.trim(fname)
|
|
if not fname or fname == "" then
|
|
return
|
|
end
|
|
local configs = require("vern.configs.python")
|
|
---@type VernPythonConfig
|
|
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
|
|
local mod = cfg.python.templates.file.mod or {}
|
|
|
|
local raw_path = join(mod.path, mod.name)
|
|
local context = { project_name = utils.proj_name(), name = fname }
|
|
local path = utils.resolve_path(raw_path, cfg.python.dirs or {}, context)
|
|
|
|
utils.dump(path, templates.fetch(mod.kind_path, mod.kind, fname))
|
|
vim.notify("File: " .. fname .. " has been created!")
|
|
vim.cmd.edit(vim.fn.expand(path))
|
|
end)
|
|
end
|
|
|
|
---@param opts VernPythonConfig 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 bin = opts.python.templates.project.primary
|
|
|
|
local exe = join(bin.path, bin.name) or "src/{project_name}/main.py"
|
|
local exe_path = utils.resolve_path(exe, opts.python.dirs, { project_name = pname })
|
|
local full_path = join(vim.fn.expand(root), exe_path)
|
|
utils.term({ "python3 " .. full_path }, opts.terminal or {}, false)
|
|
end
|
|
|
|
function M.pip_install()
|
|
vim.ui.input({ prompt = "Enter pip module: " }, function(mod)
|
|
mod = mod and vim.trim(mod)
|
|
if not mod or mod == "" then
|
|
vim.notify("Pip Cancelled", vim.log.levels.WARN)
|
|
return
|
|
end
|
|
vim.notify("Installing " .. mod, vim.log.levels.INFO)
|
|
vim.system({ "pip", "install", mod }, { text = true }, function(obj)
|
|
vim.schedule(function()
|
|
if obj.code ~= 0 then
|
|
vim.notify("Pip failed: " .. obj.stderr, vim.log.levels.WARN)
|
|
else
|
|
vim.notify("Pip installed " .. mod, vim.log.levels.INFO)
|
|
end
|
|
end)
|
|
end)
|
|
end)
|
|
end
|
|
|
|
return M
|