228 lines
6.4 KiB
Lua
228 lines
6.4 KiB
Lua
local M = {}
|
|
|
|
---@module "vern-nvim.core.fs"
|
|
local fs = require("vern-nvim.core.fs")
|
|
|
|
---@module "vern-nvim.core.template"
|
|
local templates = require("vern-nvim.core.template")
|
|
|
|
---@module "vern-nvim.providers.web"
|
|
local providers = require("vern-nvim.providers.web")
|
|
|
|
---Imitate create_file so you can skip over and use for each filetype
|
|
|
|
M.create_project = function(opts)
|
|
opts = opts or {}
|
|
local ft = ""
|
|
local items = {
|
|
"cpp",
|
|
"c",
|
|
"rust",
|
|
"python",
|
|
"go",
|
|
"lua",
|
|
"java",
|
|
"web - not yet implemented",
|
|
}
|
|
vim.ui.select(items, {
|
|
prompt = "Select available language:",
|
|
format_item = function(item) return item end,
|
|
}, function(choice, _)
|
|
if choice then ft = vim.trim(choice) end
|
|
local ok, langs = pcall(require, "vern-nvim.langs." .. ft)
|
|
if not ok or type(langs) ~= "table" then
|
|
-- vim.notify("Empty or nil ft", vim.log.levels.WARN, {
|
|
-- title = "Vern Project",
|
|
-- })
|
|
return
|
|
end
|
|
local args = vim.tbl_deep_extend("force", {}, langs, opts or {})
|
|
if not args[ft] or type(args[ft]) ~= "table" then return end
|
|
if ft == "web" then
|
|
if type(langs.create_project) == "function" then
|
|
langs.create_project()
|
|
end
|
|
return
|
|
else
|
|
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
|
|
pname = pname and vim.trim(pname)
|
|
if not pname or pname == "" then return end
|
|
local root = vim.fs.joinpath(
|
|
vim.fn.expand(args.workspace_root),
|
|
args[ft].root,
|
|
pname
|
|
)
|
|
|
|
local ctx = {
|
|
project_name = pname,
|
|
company_name = args.env.author,
|
|
file_name = "",
|
|
}
|
|
|
|
vim.fn.mkdir(root, "p")
|
|
local dirs = args[ft].dirs
|
|
if type(dirs) == "table" and next(dirs) then
|
|
for _, dir in pairs(dirs) do
|
|
local path = vim.fs.normalize(
|
|
vim.fs.joinpath(root, fs.fmt_path(dir, nil, ctx))
|
|
)
|
|
vim.fn.mkdir(path, "p")
|
|
end
|
|
end
|
|
|
|
local function write(tpl)
|
|
if type(tpl) == "table" and next(tpl) then
|
|
for _, tbl in pairs(tpl) do
|
|
local raw_path = vim.fs.joinpath(tbl.path, tbl.name)
|
|
local path = fs.fmt_path(raw_path, args[ft].dirs, ctx)
|
|
if tbl.enabled ~= false and tbl.kind then
|
|
local abs_file_path =
|
|
vim.fs.normalize(vim.fs.joinpath(root, path))
|
|
local abs_dir_path = vim.fs.dirname(abs_file_path)
|
|
if
|
|
abs_dir_path ~= root
|
|
and not vim.startswith(root, abs_dir_path)
|
|
then
|
|
vim.fn.mkdir(abs_dir_path, "p")
|
|
end
|
|
fs.write(
|
|
abs_file_path,
|
|
templates.fetch(tbl.kind_path, tbl.kind, ctx)
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local tpl = args[ft].templates or {}
|
|
|
|
local bin
|
|
local project = tpl.project
|
|
if type(project) == "table" and next(project) then
|
|
for tbl_name, tbl in pairs(project) do
|
|
if tbl_name == "primary" then
|
|
bin = vim.fs.joinpath(
|
|
root,
|
|
fs.fmt_path(
|
|
vim.fs.joinpath(tbl.path, tbl.name),
|
|
args[ft].dirs,
|
|
ctx
|
|
)
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
write(project)
|
|
write(tpl.extras)
|
|
|
|
if type(langs.create_project) == "function" then
|
|
langs.create_project(root)
|
|
end
|
|
|
|
--- Init git
|
|
local gitignore =
|
|
require("vern-nvim.templates.extras.gitignore").template(ft)
|
|
local obj = vim.system({ "git", "-C", root, "init" }):wait()
|
|
if obj.code ~= 0 then
|
|
vim.notify(
|
|
"Git init failed " .. obj.stderr,
|
|
vim.log.levels.ERROR,
|
|
{ title = "Vern Git" }
|
|
)
|
|
end
|
|
fs.write(vim.fs.joinpath(root, ".gitignore"), gitignore)
|
|
|
|
--- Init Readme
|
|
local readme = require("vern-nvim.templates.extras.readme")
|
|
fs.write(vim.fs.joinpath(root, "README.md"), readme)
|
|
|
|
if bin then vim.cmd.edit(vim.fn.fnameescape(bin)) end
|
|
vim.notify(
|
|
"Project: " .. pname .. " has been created!",
|
|
vim.log.levels.INFO,
|
|
{ title = "Vern Project" }
|
|
)
|
|
end)
|
|
end
|
|
end)
|
|
end
|
|
|
|
M.create_file = function(opts)
|
|
opts = opts or {}
|
|
local filetype = vim.bo.filetype
|
|
|
|
local ft_ok, config = pcall(require, "vern-nvim.langs." .. filetype)
|
|
if not filetype or filetype == "" or not ft_ok or type(config) ~= "table" then
|
|
vim.notify("Unknown Filetype: " .. filetype, vim.log.levels.WARN, {
|
|
title = "Vern File",
|
|
})
|
|
return
|
|
else
|
|
local args = vim.tbl_deep_extend("force", {}, config, opts or {})
|
|
---@type VernCreateFile
|
|
local cf = args.create_file
|
|
local function function_write(mod, fname)
|
|
local raw_path = vim.fs.joinpath(mod.path, mod.name)
|
|
local context = {
|
|
project_name = fs.name(),
|
|
company_name = args.env.author,
|
|
file_name = fname,
|
|
}
|
|
local path = fs.fmt_path(raw_path, args[filetype].dirs or {}, context)
|
|
local full_path = vim.fs.joinpath(fs.root(), path)
|
|
fs.write(full_path, templates.fetch(mod.kind_path, mod.kind, context))
|
|
vim.cmd.edit(vim.fn.fnameescape(full_path))
|
|
vim.notify(
|
|
fname .. " has been created!",
|
|
vim.log.levels.INFO,
|
|
{ title = "Vern File" }
|
|
)
|
|
end
|
|
|
|
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
|
|
fname = fname and vim.trim(fname)
|
|
if not fname or fname == "" then return end
|
|
|
|
local done = function(mod)
|
|
if not mod or vim.tbl_isempty(mod) then return end
|
|
function_write(mod, fname)
|
|
end
|
|
|
|
if cf and type(cf) == "function" then
|
|
cf(args, done)
|
|
else
|
|
done(args[filetype].templates.file.source)
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
|
|
M.run = function(opts)
|
|
opts = opts or {}
|
|
local filetype = vim.bo.filetype
|
|
|
|
local ft_ok, action = pcall(require, "vern-nvim.langs." .. filetype)
|
|
if
|
|
not filetype
|
|
or filetype == ""
|
|
or not ft_ok
|
|
or type(action.run) ~= "function"
|
|
then
|
|
vim.notify("Unknown Filetype: " .. filetype, vim.log.levels.WARN, {
|
|
title = "Vern Run",
|
|
})
|
|
return
|
|
else
|
|
local args = vim.tbl_deep_extend(
|
|
"force",
|
|
{},
|
|
opts,
|
|
require("vern-nvim.langs." .. filetype)
|
|
)
|
|
action.run(args)
|
|
end
|
|
end
|
|
|
|
return M
|