Skip to content

Commit

Permalink
Refactor modules (#5)
Browse files Browse the repository at this point in the history
* Refactor modules

* Fix missing identifiers
  • Loading branch information
nizamiza authored May 14, 2024
1 parent 80bc407 commit e5bffa3
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 159 deletions.
64 changes: 28 additions & 36 deletions lua/rpm/autocomplete.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local commands = {
local M = {}

M.commands = {
help = {
desc = "Get help for a command",
nargs = "?"
Expand Down Expand Up @@ -45,20 +47,20 @@ local commands = {
},
}

local command_names = {}
local name_padding = 0
M.command_names = {}
M.name_padding = 0

for command, _ in pairs(commands) do
table.insert(command_names, command)
for command, _ in pairs(M.commands) do
table.insert(M.command_names, command)

if #command > name_padding then
name_padding = #command
if #command > M.name_padding then
M.name_padding = #command
end
end

local indent = string.rep(" ", name_padding + 3)
local indent = string.rep(" ", M.name_padding + 3)

local function get_command_args_info(command)
function M.get_command_args_info(command)
if command == nil then
return {
has_args = false,
Expand All @@ -70,11 +72,11 @@ local function get_command_args_info(command)
local has_args = type(command.nargs) == "string" or command.nargs > 0

local max_args = command.nargs == "?" and 1
or (command.nargs == "+" or commands.nargs == "*") and math.huge
or command.nargs
or (command.nargs == "+" or M.commands.nargs == "*") and math.huge
or command.nargs

local min_args = (command.nargs == "?" or command.nargs == "*") and 0
or max_args
or max_args

return {
has_args = has_args,
Expand All @@ -83,24 +85,23 @@ local function get_command_args_info(command)
}
end

local function get_command_help(name)
local command = commands[name]
function M.get_command_help(name)
local command = M.commands[name]

if not command then
return "Command " .. name .. " not found."
end

local padded_name = name .. string.rep(" ", name_padding - #name)
local padded_name = name .. string.rep(" ", M.name_padding - #name)

local has_args = get_command_args_info(command).has_args
local has_args = M.get_command_args_info(command).has_args

return padded_name .. " - " .. command.desc .. "\n" ..
indent .. "Required arguments: " .. command.nargs .. "\n" ..
indent .. "Usage: `:Rpm " .. name .. (has_args and " <args>" or "") .. "`"
indent .. "Required arguments: " .. command.nargs .. "\n" ..
indent .. "Usage: `:Rpm " .. name .. (has_args and " <args>" or "") .. "`"
end


local function narrow_options(options, arg_lead)
function M.narrow_options(options, arg_lead)
local matches = {}
local arg_lead_lower = arg_lead:lower()

Expand All @@ -113,18 +114,18 @@ local function narrow_options(options, arg_lead)
return matches
end

local function create_autocomplete(plugin_list)
function M.create(plugin_list)
local function autocomplete(arg_lead, cmd_line)
local args = vim.split(cmd_line, " ")

if #args == 1 then
return command_names
return M.command_names
end

local cmd = args[2]:lower()
local command = commands[cmd]
local command = M.commands[cmd]

local args_info = get_command_args_info(command)
local args_info = M.get_command_args_info(command)
local has_args = args_info.has_args
local max_args = args_info.max_args
local is_over_max_args = #args - 2 > max_args
Expand All @@ -138,20 +139,11 @@ local function create_autocomplete(plugin_list)
local is_help = cmd == "help"

return has_args and not is_help and
narrow_options(plugin_list, arg_lead) or
narrow_options(command_names, arg_lead)
M.narrow_options(plugin_list, arg_lead) or
M.narrow_options(M.command_names, arg_lead)
end

return autocomplete
end

local Autocomplete = {
create = create_autocomplete,
commands = commands,
command_names = command_names,
get_command_help = get_command_help,
get_command_args_info = get_command_args_info,
narrow_options = narrow_options
}

return Autocomplete
return M
100 changes: 47 additions & 53 deletions lua/rpm/core.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
local function parse_input_answer(answer)
local M = {}

function M.parse_input_answer(answer)
if answer:lower():match("^y") then
return true
end

return false
end

local function get_plugin_version(install_path)
function M.get_plugin_version(install_path)
return vim.fn.isdirectory(install_path) == 0 and "Not installed" or
vim.fn.system({ "git", "-C", install_path, "describe", "--tags" }):gsub("\n", "")
vim.fn.system({ "git", "-C", install_path, "describe", "--tags" }):gsub("\n", "")
end

local function coerce_to_table(value)
function M.coerce_to_table(value)
if type(value) == "table" then
return value
end

return { value }
end

local silent_print = function(message, silent)
function M.silent_print(message, silent)
silent = silent or false

if not silent then
print(message)
end
end

local function get_plugin_info(path)
local paths = coerce_to_table(path)
function M.get_plugin_info(path)
local paths = M.coerce_to_table(path)

-- Consider the last path as the main dependency
local last_path = paths[#paths]

local name = last_path:match("([^/]+)$"):gsub("%.[a-zA-Z0-9]+", "")
local install_path = vim.fn.stdpath("config") .. "/pack/plugins/start/" .. name

local version = get_plugin_version(install_path)
local version = M.get_plugin_version(install_path)

return {
install_path = install_path,
Expand All @@ -46,136 +48,128 @@ local function get_plugin_info(path)
}
end

local function is_plugin_installed(path, silent)
local paths = coerce_to_table(path)
function M.is_plugin_installed(path, silent)
local paths = M.coerce_to_table(path)

for _, p in ipairs(paths) do
local info = get_plugin_info(p)
local info = M.get_plugin_info(p)

if vim.fn.isdirectory(info.install_path) == 0 then
silent_print("Dependency " .. info.name .. " is not installed.", silent)
M.silent_print("Dependency " .. info.name .. " is not installed.", silent)
return false
end
end

return true
end

local function generate_helptags(path, silent)
local info = get_plugin_info(path)
function M.generate_helptags(path, silent)
local info = M.get_plugin_info(path)
local doc_dir = info.install_path .. "/doc"

if vim.fn.isdirectory(doc_dir) == 1 then
silent_print("Generating help tags for " .. info.name .. "...", silent)
M.silent_print("Generating help tags for " .. info.name .. "...", silent)

vim.cmd("helptags " .. doc_dir)
silent_print("Help tags for " .. info.name .. " have been generated!", silent)

M.silent_print("Help tags for " .. info.name .. " have been generated!", silent)
end
end

local function install_plugin(path, silent)
local paths = coerce_to_table(path)
function M.install_plugin(path, silent)
local paths = M.coerce_to_table(path)

for _, p in ipairs(paths) do
if is_plugin_installed(p, true) then
silent_print("Dependency " .. p .. " is already installed.\n", silent)
if M.is_plugin_installed(p, true) then
M.silent_print("Dependency " .. p .. " is already installed.\n", silent)
goto continue
end

local url = p:match("^http") and p or "https://github.com/" .. p
local info = get_plugin_info(p)
local info = M.get_plugin_info(p)

silent_print("Cloning " .. url .. " to " .. info.install_path .. "...", silent)
M.silent_print("Cloning " .. url .. " to " .. info.install_path .. "...", silent)

vim.fn.system({ "git", "clone", url, info.install_path })

generate_helptags(p, silent)
M.generate_helptags(p, silent)

silent_print("Dependency " .. info.name .. " has been installed!", silent)
M.silent_print("Dependency " .. info.name .. " has been installed!", silent)
::continue::
end
end
end

local function update_plugin(path, silent)
local paths = coerce_to_table(path)
function M.update_plugin(path, silent)
local paths = M.coerce_to_table(path)

for _, p in ipairs(paths) do
if not is_plugin_installed(p) then
silent_print("Dependency " .. p .. " is not installed.", silent)
if not M.is_plugin_installed(p) then
M.silent_print("Dependency " .. p .. " is not installed.", silent)

local answer = "n"

if not silent then
answer = vim.fn.input("Would you like to install it? (y/n): ")
end

if not parse_input_answer(answer) then
if not M.parse_input_answer(answer) then
goto continue
end

if not silent then
print("\n")
end

install_plugin(p, silent)
M.install_plugin(p, silent)
goto continue
end

local info = get_plugin_info(p)
local info = M.get_plugin_info(p)

silent_print("Updating dependency " .. info.name .. "...", silent)
M.silent_print("Updating dependency " .. info.name .. "...", silent)

vim.fn.system({ "git", "-C", info.install_path, "pull" })

local new_version = get_plugin_version(info.install_path)
local new_version = M.get_plugin_version(info.install_path)

if info.version == new_version then
silent_print("Dependency " .. info.name .. " is already up to date.", silent)
M.silent_print("Dependency " .. info.name .. " is already up to date.", silent)
else
silent_print("Dependency " .. info.name .. " has been updated to " .. new_version .. "!", silent)
M.silent_print("Dependency " .. info.name .. " has been updated to " .. new_version .. "!", silent)
end

::continue::
end
end

local function delete_plugin(path, silent)
local paths = coerce_to_table(path)
function M.delete_plugin(path, silent)
local paths = M.coerce_to_table(path)

for _, p in ipairs(paths) do
local info = get_plugin_info(p)
local info = M.get_plugin_info(p)

if vim.fn.isdirectory(info.install_path) == 0 then
silent_print("Dependency " .. info.name .. " is not installed.\n", silent)
M.silent_print("Dependency " .. info.name .. " is not installed.\n", silent)
goto continue
end

if not silent then
local answer = vim.fn.input("Are you sure you want to delete " .. info.name .. "? (y/n): ")

if not parse_input_answer(answer) then
if not M.parse_input_answer(answer) then
goto continue
end

print("\n")
end

silent_print("Deleting " .. info.name .. "...", silent)
M.silent_print("Deleting " .. info.name .. "...", silent)

vim.fn.system({ "rm", "-rf", info.install_path })

silent_print("Dependency " .. info.name .. " has been deleted.", silent)
M.silent_print("Dependency " .. info.name .. " has been deleted.", silent)
::continue::
end
end

return {
delete_plugin = delete_plugin,
get_plugin_info = get_plugin_info,
install_plugin = install_plugin,
is_plugin_installed = is_plugin_installed,
parse_input_answer = parse_input_answer,
generate_helptags = generate_helptags,
update_plugin = update_plugin
}
return M
Loading

0 comments on commit e5bffa3

Please sign in to comment.