mirror of
https://github.com/Keyitdev/dotfiles.git
synced 2026-09-24 01:52:09 +00:00
New version: created v3
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
local git = { url = "https://github.com/" }
|
||||
|
||||
function git.cmd(args, ...)
|
||||
return astronvim.cmd("git -C " .. astronvim.install.home .. " " .. args, ...)
|
||||
end
|
||||
|
||||
function git.is_repo()
|
||||
return git.cmd("rev-parse --is-inside-work-tree", false)
|
||||
end
|
||||
|
||||
function git.fetch(remote, ...)
|
||||
return git.cmd("fetch " .. remote, ...)
|
||||
end
|
||||
|
||||
function git.pull(...)
|
||||
return git.cmd("pull --rebase", ...)
|
||||
end
|
||||
|
||||
function git.checkout(dest, ...)
|
||||
return git.cmd("checkout " .. dest, ...)
|
||||
end
|
||||
|
||||
function git.hard_reset(dest, ...)
|
||||
return git.cmd("reset --hard " .. dest, ...)
|
||||
end
|
||||
|
||||
function git.branch_contains(remote, branch, commit, ...)
|
||||
return git.cmd("merge-base --is-ancestor " .. commit .. " " .. remote .. "/" .. branch, ...) ~= nil
|
||||
end
|
||||
|
||||
function git.remote_add(remote, url, ...)
|
||||
return git.cmd("remote add " .. remote .. " " .. url, ...)
|
||||
end
|
||||
|
||||
function git.remote_update(remote, url, ...)
|
||||
return git.cmd("remote set-url " .. remote .. " " .. url, ...)
|
||||
end
|
||||
|
||||
function git.remote_url(remote, ...)
|
||||
return astronvim.trim_or_nil(git.cmd("remote get-url " .. remote, ...))
|
||||
end
|
||||
|
||||
function git.current_version(...)
|
||||
return astronvim.trim_or_nil(git.cmd("describe --tags", ...))
|
||||
end
|
||||
|
||||
function git.current_branch(...)
|
||||
return astronvim.trim_or_nil(git.cmd("rev-parse --abbrev-ref HEAD", ...))
|
||||
end
|
||||
|
||||
function git.local_head(...)
|
||||
return astronvim.trim_or_nil(git.cmd("rev-parse HEAD", ...))
|
||||
end
|
||||
|
||||
function git.remote_head(remote, branch, ...)
|
||||
return astronvim.trim_or_nil(git.cmd("rev-list -n 1 " .. remote .. "/" .. branch, ...))
|
||||
end
|
||||
|
||||
function git.tag_commit(tag, ...)
|
||||
return astronvim.trim_or_nil(git.cmd("rev-list -n 1 " .. tag, ...))
|
||||
end
|
||||
|
||||
function git.get_commit_range(start_hash, end_hash, ...)
|
||||
local log = git.cmd("log --no-merges --pretty='format:[%h] %s' " .. start_hash .. ".." .. end_hash, ...)
|
||||
return log and vim.fn.split(log, "\n") or {}
|
||||
end
|
||||
|
||||
function git.get_versions(search, ...)
|
||||
local tags = git.cmd("tag -l --sort=version:refname '" .. (search == "latest" and "v*" or search) .. "'", ...)
|
||||
return tags and vim.fn.split(tags, "\n") or {}
|
||||
end
|
||||
|
||||
function git.latest_version(versions, ...)
|
||||
versions = versions and versions or git.get_versions(...)
|
||||
return versions[#versions]
|
||||
end
|
||||
|
||||
function git.parse_remote_url(str)
|
||||
return vim.fn.match(str, astronvim.url_matcher) == -1
|
||||
and git.url .. str .. (vim.fn.match(str, "/") == -1 and "/AstroNvim.git" or ".git")
|
||||
or str
|
||||
end
|
||||
|
||||
function git.breaking_changes(commits)
|
||||
return vim.tbl_filter(function(v)
|
||||
return vim.fn.match(v, "\\[.*\\]\\s\\+\\w\\+\\((\\w\\+)\\)\\?!:") ~= -1
|
||||
end, commits)
|
||||
end
|
||||
|
||||
function git.pretty_changelog(commits)
|
||||
local changelog = {}
|
||||
for _, commit in ipairs(commits) do
|
||||
local hash, type, msg = commit:match "(%[.*%])(.*:)(.*)"
|
||||
if hash and type and msg then
|
||||
vim.list_extend(changelog, { { hash, "DiffText" }, { type, "Typedef" }, { msg }, { "\n" } })
|
||||
end
|
||||
end
|
||||
return changelog
|
||||
end
|
||||
|
||||
return git
|
||||
@@ -0,0 +1,299 @@
|
||||
_G.astronvim = {}
|
||||
local stdpath = vim.fn.stdpath
|
||||
local tbl_insert = table.insert
|
||||
local map = vim.keymap.set
|
||||
|
||||
astronvim.install = astronvim_installation or { home = stdpath "config" }
|
||||
|
||||
local astronvim_config = stdpath("config"):gsub("nvim$", "astronvim")
|
||||
vim.opt.rtp:append(astronvim_config)
|
||||
local supported_configs = { astronvim.install.home, astronvim_config }
|
||||
|
||||
local function load_module_file(module)
|
||||
local found_module = nil
|
||||
for _, config_path in ipairs(supported_configs) do
|
||||
local module_path = config_path .. "/lua/" .. module:gsub("%.", "/") .. ".lua"
|
||||
if vim.fn.filereadable(module_path) == 1 then
|
||||
found_module = module_path
|
||||
end
|
||||
end
|
||||
if found_module then
|
||||
local status_ok, loaded_module = pcall(require, module)
|
||||
if status_ok then
|
||||
found_module = loaded_module
|
||||
else
|
||||
astronvim.notify("Error loading " .. found_module, "error")
|
||||
end
|
||||
end
|
||||
return found_module
|
||||
end
|
||||
|
||||
astronvim.user_settings = load_module_file "user.init"
|
||||
astronvim.default_compile_path = stdpath "data" .. "/packer_compiled.lua"
|
||||
astronvim.user_terminals = {}
|
||||
astronvim.url_matcher =
|
||||
"\\v\\c%(%(h?ttps?|ftp|file|ssh|git)://|[a-z]+[@][a-z]+[.][a-z]+:)%([&:#*@~%_\\-=?!+;/0-9a-z]+%(%([.;/?]|[.][.]+)[&:#*@~%_\\-=?!+/0-9a-z]+|:\\d+|,%(%(%(h?ttps?|ftp|file|ssh|git)://|[a-z]+[@][a-z]+[.][a-z]+:)@![0-9a-z]+))*|\\([&:#*@~%_\\-=?!+;/.0-9a-z]*\\)|\\[[&:#*@~%_\\-=?!+;/.0-9a-z]*\\]|\\{%([&:#*@~%_\\-=?!+;/.0-9a-z]*|\\{[&:#*@~%_\\-=?!+;/.0-9a-z]*})\\})+"
|
||||
|
||||
local function func_or_extend(overrides, default, extend)
|
||||
if extend then
|
||||
if type(overrides) == "table" then
|
||||
default = vim.tbl_deep_extend("force", default, overrides)
|
||||
elseif type(overrides) == "function" then
|
||||
default = overrides(default)
|
||||
end
|
||||
elseif overrides ~= nil then
|
||||
default = overrides
|
||||
end
|
||||
return default
|
||||
end
|
||||
|
||||
function astronvim.conditional_func(func, condition, ...)
|
||||
if (condition == nil and true or condition) and type(func) == "function" then
|
||||
return func(...)
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.trim_or_nil(str)
|
||||
return type(str) == "string" and vim.trim(str) or nil
|
||||
end
|
||||
|
||||
function astronvim.notify(msg, type, opts)
|
||||
vim.notify(msg, type, vim.tbl_deep_extend("force", { title = "AstroNvim" }, opts or {}))
|
||||
end
|
||||
|
||||
function astronvim.echo(messages)
|
||||
messages = messages or { { "\n" } }
|
||||
if type(messages) == "table" then
|
||||
vim.api.nvim_echo(messages, false, {})
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.confirm_prompt(messages)
|
||||
if messages then
|
||||
astronvim.echo(messages)
|
||||
end
|
||||
local confirmed = string.lower(vim.fn.input "(y/n) ") == "y"
|
||||
astronvim.echo()
|
||||
astronvim.echo()
|
||||
return confirmed
|
||||
end
|
||||
|
||||
local function user_setting_table(module)
|
||||
local settings = astronvim.user_settings or {}
|
||||
for tbl in string.gmatch(module, "([^%.]+)") do
|
||||
settings = settings[tbl]
|
||||
if settings == nil then
|
||||
break
|
||||
end
|
||||
end
|
||||
return settings
|
||||
end
|
||||
|
||||
function astronvim.initialize_packer()
|
||||
local packer_avail, packer = pcall(require, "packer")
|
||||
if not packer_avail then
|
||||
local packer_path = stdpath "data" .. "/site/pack/packer/start/packer.nvim"
|
||||
vim.fn.delete(packer_path, "rf")
|
||||
vim.fn.system {
|
||||
"git",
|
||||
"clone",
|
||||
"--depth",
|
||||
"1",
|
||||
"https://github.com/wbthomason/packer.nvim",
|
||||
packer_path,
|
||||
}
|
||||
astronvim.echo { { "Initializing Packer...\n\n" } }
|
||||
vim.cmd "packadd packer.nvim"
|
||||
packer_avail, packer = pcall(require, "packer")
|
||||
if not packer_avail then
|
||||
vim.api.nvim_err_writeln("Failed to load packer at:" .. packer_path .. "\n\n" .. packer)
|
||||
end
|
||||
end
|
||||
return packer
|
||||
end
|
||||
|
||||
function astronvim.vim_opts(options)
|
||||
for scope, table in pairs(options) do
|
||||
for setting, value in pairs(table) do
|
||||
vim[scope][setting] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.user_plugin_opts(module, default, extend, prefix)
|
||||
if extend == nil then
|
||||
extend = true
|
||||
end
|
||||
default = default or {}
|
||||
local user_settings = load_module_file((prefix or "user") .. "." .. module)
|
||||
if user_settings == nil and prefix == nil then
|
||||
user_settings = user_setting_table(module)
|
||||
end
|
||||
if user_settings ~= nil then
|
||||
default = func_or_extend(user_settings, default, extend)
|
||||
end
|
||||
return default
|
||||
end
|
||||
|
||||
function astronvim.compiled()
|
||||
local run_me, _ = loadfile(
|
||||
astronvim.user_plugin_opts("plugins.packer", { compile_path = astronvim.default_compile_path }).compile_path
|
||||
)
|
||||
if run_me then
|
||||
run_me()
|
||||
else
|
||||
astronvim.echo { { "Please run " }, { ":PackerSync", "Title" } }
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.url_opener()
|
||||
if vim.fn.has "mac" == 1 then
|
||||
vim.fn.jobstart({ "open", vim.fn.expand "<cfile>" }, { detach = true })
|
||||
elseif vim.fn.has "unix" == 1 then
|
||||
vim.fn.jobstart({ "xdg-open", vim.fn.expand "<cfile>" }, { detach = true })
|
||||
else
|
||||
astronvim.notify("gx is not supported on this OS!", "error")
|
||||
end
|
||||
end
|
||||
|
||||
-- term_details can be either a string for just a command or
|
||||
-- a complete table to provide full access to configuration when calling Terminal:new()
|
||||
function astronvim.toggle_term_cmd(term_details)
|
||||
if type(term_details) == "string" then
|
||||
term_details = { cmd = term_details, hidden = true }
|
||||
end
|
||||
local term_key = term_details.cmd
|
||||
if vim.v.count > 0 and term_details.count == nil then
|
||||
term_details.count = vim.v.count
|
||||
term_key = term_key .. vim.v.count
|
||||
end
|
||||
if astronvim.user_terminals[term_key] == nil then
|
||||
astronvim.user_terminals[term_key] = require("toggleterm.terminal").Terminal:new(term_details)
|
||||
end
|
||||
astronvim.user_terminals[term_key]:toggle()
|
||||
end
|
||||
|
||||
function astronvim.add_cmp_source(source)
|
||||
local cmp_avail, cmp = pcall(require, "cmp")
|
||||
if cmp_avail then
|
||||
local config = cmp.get_config()
|
||||
tbl_insert(config.sources, source)
|
||||
cmp.setup(config)
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.get_user_cmp_source(source)
|
||||
source = type(source) == "string" and { name = source } or source
|
||||
local priority = astronvim.user_plugin_opts("cmp.source_priority", {
|
||||
nvim_lsp = 1000,
|
||||
luasnip = 750,
|
||||
buffer = 500,
|
||||
path = 250,
|
||||
})[source.name]
|
||||
if priority then
|
||||
source.priority = priority
|
||||
end
|
||||
return source
|
||||
end
|
||||
|
||||
function astronvim.add_user_cmp_source(source)
|
||||
astronvim.add_cmp_source(astronvim.get_user_cmp_source(source))
|
||||
end
|
||||
|
||||
function astronvim.null_ls_providers(filetype)
|
||||
local registered = {}
|
||||
local sources_avail, sources = pcall(require, "null-ls.sources")
|
||||
if sources_avail then
|
||||
for _, source in ipairs(sources.get_available(filetype)) do
|
||||
for method in pairs(source.methods) do
|
||||
registered[method] = registered[method] or {}
|
||||
tbl_insert(registered[method], source.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
return registered
|
||||
end
|
||||
|
||||
function astronvim.null_ls_sources(filetype, source)
|
||||
local methods_avail, methods = pcall(require, "null-ls.methods")
|
||||
return methods_avail and astronvim.null_ls_providers(filetype)[methods.internal[source]] or {}
|
||||
end
|
||||
|
||||
function astronvim.alpha_button(sc, txt)
|
||||
local sc_ = sc:gsub("%s", ""):gsub("LDR", "<leader>")
|
||||
if vim.g.mapleader then
|
||||
sc = sc:gsub("LDR", vim.g.mapleader == " " and "SPC" or vim.g.mapleader)
|
||||
end
|
||||
return {
|
||||
type = "button",
|
||||
val = txt,
|
||||
on_press = function()
|
||||
local key = vim.api.nvim_replace_termcodes(sc_, true, false, true)
|
||||
vim.api.nvim_feedkeys(key, "normal", false)
|
||||
end,
|
||||
opts = {
|
||||
position = "center",
|
||||
text = txt,
|
||||
shortcut = sc,
|
||||
cursor = 5,
|
||||
width = 36,
|
||||
align_shortcut = "right",
|
||||
hl = "DashboardCenter",
|
||||
hl_shortcut = "DashboardShortcut",
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
function astronvim.is_available(plugin)
|
||||
return packer_plugins ~= nil and packer_plugins[plugin] ~= nil
|
||||
end
|
||||
|
||||
function astronvim.set_mappings(map_table, base)
|
||||
for mode, maps in pairs(map_table) do
|
||||
for keymap, options in pairs(maps) do
|
||||
if options then
|
||||
local cmd = options
|
||||
if type(options) == "table" then
|
||||
cmd = options[1]
|
||||
options[1] = nil
|
||||
else
|
||||
options = {}
|
||||
end
|
||||
map(mode, keymap, cmd, vim.tbl_deep_extend("force", options, base or {}))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.delete_url_match()
|
||||
for _, match in ipairs(vim.fn.getmatches()) do
|
||||
if match.group == "HighlightURL" then
|
||||
vim.fn.matchdelete(match.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.set_url_match()
|
||||
astronvim.delete_url_match()
|
||||
if vim.g.highlighturl_enabled then
|
||||
vim.fn.matchadd("HighlightURL", astronvim.url_matcher, 15)
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.toggle_url_match()
|
||||
vim.g.highlighturl_enabled = not vim.g.highlighturl_enabled
|
||||
astronvim.set_url_match()
|
||||
end
|
||||
|
||||
function astronvim.cmd(cmd, show_error)
|
||||
local result = vim.fn.system(cmd)
|
||||
local success = vim.api.nvim_get_vvar "shell_error" == 0
|
||||
if not success and (show_error == nil and true or show_error) then
|
||||
vim.api.nvim_err_writeln("Error running command: " .. cmd .. "\nError message:\n" .. result)
|
||||
end
|
||||
return success and result or nil
|
||||
end
|
||||
|
||||
require "core.utils.updater"
|
||||
|
||||
return astronvim
|
||||
@@ -0,0 +1,160 @@
|
||||
local fn = vim.fn
|
||||
local git = require "core.utils.git"
|
||||
local options = astronvim.user_plugin_opts(
|
||||
"updater",
|
||||
{ remote = "origin", branch = "main", channel = "nightly", show_changelog = true }
|
||||
)
|
||||
|
||||
if astronvim.install.is_stable ~= nil then
|
||||
options.channel = astronvim.install.is_stable and "stable" or "nightly"
|
||||
end
|
||||
|
||||
astronvim.updater = { options = options }
|
||||
if options.pin_plugins == nil and options.channel == "stable" or options.pin_plugins then
|
||||
local loaded, snapshot = pcall(fn.readfile, astronvim.install.home .. "/packer_snapshot")
|
||||
if loaded then
|
||||
loaded, snapshot = pcall(fn.json_decode, snapshot)
|
||||
astronvim.updater.snapshot = type(snapshot) == "table" and snapshot or nil
|
||||
end
|
||||
if not loaded then
|
||||
vim.api.nvim_err_writeln "Error loading packer snapshot"
|
||||
end
|
||||
end
|
||||
|
||||
function astronvim.updater.version()
|
||||
local version = astronvim.install.version or git.current_version(false)
|
||||
if version then
|
||||
astronvim.notify("Version: " .. version)
|
||||
end
|
||||
end
|
||||
|
||||
local function attempt_update(target)
|
||||
if options.channel == "stable" or options.commit then
|
||||
return git.checkout(target, false)
|
||||
else
|
||||
return git.pull(false)
|
||||
end
|
||||
end
|
||||
|
||||
local cancelled_message = { { "Update cancelled", "WarningMsg" } }
|
||||
|
||||
function astronvim.updater.update()
|
||||
if not git.is_repo() then
|
||||
astronvim.notify("Updater not available for non-git installations", "error")
|
||||
return
|
||||
end
|
||||
for remote, entry in pairs(options.remotes and options.remotes or {}) do
|
||||
local url = git.parse_remote_url(entry)
|
||||
local current_url = git.remote_url(remote, false)
|
||||
local check_needed = false
|
||||
if not current_url then
|
||||
git.remote_add(remote, url)
|
||||
check_needed = true
|
||||
elseif
|
||||
current_url ~= url
|
||||
and astronvim.confirm_prompt {
|
||||
{ "Remote " },
|
||||
{ remote, "Title" },
|
||||
{ " is currently set to " },
|
||||
{ current_url, "WarningMsg" },
|
||||
{ "\nWould you like us to set it to " },
|
||||
{ url, "String" },
|
||||
{ "?" },
|
||||
}
|
||||
then
|
||||
git.remote_update(remote, url)
|
||||
check_needed = true
|
||||
end
|
||||
if check_needed and git.remote_url(remote, false) ~= url then
|
||||
vim.api.nvim_err_writeln("Error setting up remote " .. remote .. " to " .. url)
|
||||
return
|
||||
end
|
||||
end
|
||||
local is_stable = options.channel == "stable"
|
||||
options.branch = is_stable and "main" or options.branch
|
||||
if not git.fetch(options.remote) then
|
||||
vim.api.nvim_err_writeln("Error fetching remote: " .. options.remote)
|
||||
return
|
||||
end
|
||||
local local_branch = (options.remote == "origin" and "" or (options.remote .. "_")) .. options.branch
|
||||
if git.current_branch() ~= local_branch then
|
||||
astronvim.echo {
|
||||
{ "Switching to branch: " },
|
||||
{ options.remote .. "/" .. options.branch .. "\n\n", "String" },
|
||||
}
|
||||
if not git.checkout(local_branch, false) then
|
||||
git.checkout("-b " .. local_branch .. " " .. options.remote .. "/" .. options.branch, false)
|
||||
end
|
||||
end
|
||||
if git.current_branch() ~= local_branch then
|
||||
vim.api.nvim_err_writeln("Error checking out branch: " .. options.remote .. "/" .. options.branch)
|
||||
return
|
||||
end
|
||||
local source = git.local_head() -- calculate current commit
|
||||
local target -- calculate target commit
|
||||
if is_stable then -- if stable get tag commit
|
||||
options.version = git.latest_version(git.get_versions(options.version or "latest"))
|
||||
target = git.tag_commit(options.version)
|
||||
elseif options.commit then -- if commit specified use it
|
||||
target = git.branch_contains(options.remote, options.branch, options.commit) and options.commit or nil
|
||||
else -- get most recent commit
|
||||
target = git.remote_head(options.remote, options.branch)
|
||||
end
|
||||
if not source or not target then -- continue if current and target commits were found
|
||||
vim.api.nvim_err_writeln "Error checking for updates"
|
||||
return
|
||||
elseif source == target then
|
||||
astronvim.echo { { "No updates available", "String" } }
|
||||
return
|
||||
elseif -- prompt user if they want to accept update
|
||||
not options.skip_prompts
|
||||
and not astronvim.confirm_prompt {
|
||||
{ "Update available to ", "Title" },
|
||||
{ is_stable and options.version or target, "String" },
|
||||
{ "\nContinue?" },
|
||||
}
|
||||
then
|
||||
astronvim.echo(cancelled_message)
|
||||
return
|
||||
else -- perform update
|
||||
local changelog = git.get_commit_range(source, target)
|
||||
local breaking = git.breaking_changes(changelog)
|
||||
local breaking_prompt = { { "Update contains the following breaking changes:\n", "WarningMsg" } }
|
||||
vim.list_extend(breaking_prompt, git.pretty_changelog(breaking))
|
||||
vim.list_extend(breaking_prompt, { { "\nWould you like to continue?" } })
|
||||
if #breaking > 0 and not options.skip_prompts and not astronvim.confirm_prompt(breaking_prompt) then
|
||||
astronvim.echo(cancelled_message)
|
||||
return
|
||||
end
|
||||
local updated = attempt_update(target)
|
||||
if
|
||||
not updated
|
||||
and not options.skip_prompts
|
||||
and not astronvim.confirm_prompt {
|
||||
{ "Unable to pull due to local modifications to base files.\n", "ErrorMsg" },
|
||||
{ "Reset local files and continue?" },
|
||||
}
|
||||
then
|
||||
astronvim.echo(cancelled_message)
|
||||
return
|
||||
elseif not updated then
|
||||
git.hard_reset(source)
|
||||
updated = attempt_update(target)
|
||||
end
|
||||
if not updated then
|
||||
vim.api.nvim_err_writeln "Error ocurred performing update"
|
||||
return
|
||||
end
|
||||
local summary = {
|
||||
{ "AstroNvim updated successfully to ", "Title" },
|
||||
{ git.current_version(), "String" },
|
||||
{ "!\n", "Title" },
|
||||
{ "Please restart and run :PackerSync.\n\n", "WarningMsg" },
|
||||
}
|
||||
if options.show_changelog and #changelog > 0 then
|
||||
vim.list_extend(summary, { { "Changelog:\n", "Title" } })
|
||||
vim.list_extend(summary, git.pretty_changelog(changelog))
|
||||
end
|
||||
astronvim.echo(summary)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user