Skip to content

Commit

Permalink
feat(core): new updater with stable and nightly channels
Browse files Browse the repository at this point in the history
Signed-off-by: Micah Halter <micah@balena.io>
  • Loading branch information
mehalter committed Jun 3, 2022
1 parent 4bcc8bc commit 95aaf8d
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 29 deletions.
49 changes: 49 additions & 0 deletions lua/core/utils/git.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local git = {}

function git.cmd(args, ...)
return astronvim.cmd("git -C " .. vim.fn.stdpath "config" .. " " .. args, ...)
end

function git.fetch_origin()
return git.cmd "fetch origin"
end

function git.checkout(branch)
return git.cmd("checkout " .. branch)
end

function git.branch_contains(branch, commit)
return git.cmd("merge-base --is-ancestor " .. commit .. " " .. branch) ~= nil
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(branch)
return astronvim.trim_or_nil(git.cmd("rev-list -n 1 origin/" .. branch))
end

function git.tag_commit(tag)
return astronvim.trim_or_nil(git.cmd("rev-list -n 1 " .. tag))
end

function git.get_versions()
local tags = git.cmd "git tag -l --sort=version:refname 'v*'"
return tags and vim.fn.split(tags, "\n") or {}
end

function git.latest_version()
local versions = git.get_versions()
return versions[#versions]
end

return git
98 changes: 69 additions & 29 deletions lua/core/utils.lua → lua/core/utils/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
_G.astronvim = {}
astronvim.git = require "core.utils.git"
local stdpath = vim.fn.stdpath
local tbl_insert = table.insert

Expand Down Expand Up @@ -50,6 +51,10 @@ function astronvim.conditional_func(func, condition, ...)
end
end

function astronvim.trim_or_nil(str)
return type(str) == "string" and vim.trim(str) or nil
end

local function user_setting_table(module)
local settings = astronvim.user_settings or {}
for tbl in string.gmatch(module, "([^%.]+)") do
Expand Down Expand Up @@ -244,38 +249,73 @@ function astronvim.toggle_url_match()
astronvim.set_url_match()
end

function astronvim.update()
(require "plenary.job")
:new({
command = "git",
args = { "pull", "--ff-only" },
cwd = stdpath "config",
on_exit = function(_, return_val)
if return_val == 0 then
vim.notify("Updated!", "info", astronvim.base_notification)
else
vim.notify("Update failed! Please try pulling manually.", "error", astronvim.base_notification)
end
end,
})
:sync()
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

function astronvim.version()
(require "plenary.job")
:new({
command = "git",
args = { "describe", "--tags" },
cwd = stdpath "config",
on_exit = function(out, return_val)
if return_val == 0 then
vim.notify("Version: " .. out:result()[1], "info", astronvim.base_notification)
else
vim.notify("Error retrieving version", "error", astronvim.base_notification)
end
end,
})
:start()
local version = astronvim.git.current_version()
if version then
vim.notify("Version: " .. version, "info", astronvim.base_notification)
end
end

function astronvim.update()
local settings = astronvim.user_plugin_opts("updater", {
-- possibly add custom remote in the future?
channel = "stable", -- "stable" or "nightly"
version = "latest", -- "latest" or tag name, must us "stable" for this to matter
branch = "main", -- branch name, must use "nightly" for this to matter
commit = nil, -- nil means the latest commit for branch or a specific commit hash
skip_prompts = false, -- true or false
show_changelog = true, -- true or false
})
local is_stable = settings.channel == "stable"
-- if stable, always use main branch
settings.branch = is_stable and "main" or settings.branch
-- fetch origin
astronvim.git.fetch_origin()
-- get branch
astronvim.git.checkout(settings.branch)
if astronvim.git.current_branch() ~= settings.branch then
vim.api.nvim_err_writeln("Error checking out branch: " .. settings.branch)
return
end
-- -- -- get current commit/latest remote commit, and set as current/target
local local_commit = astronvim.git.local_head()
local remote_commit
if is_stable then
-- -- if stable get tag remote commit
settings.version = settings.version == "latest" and astronvim.git.latest_version() or settings.version
remote_commit = astronvim.git.tag_commit(settings.version)
elseif settings.commit then
-- -- if nightly with specified commit get branch given commit
remote_commit = astronvim.git.branch_contains(settings.branch, settings.commit) and settings.commit or nil
else
-- -- if nightly get branch latest remote commit
remote_commit = astronvim.git.remote_head(settings.branch)
end
if local_commit and remote_commit then
if local_commit == remote_commit then
vim.notify("No updates available", "info", astronvim.base_notification)
else
vim.notify("Update available!\n" .. local_commit .. " to " .. remote_commit, "info", astronvim.base_notification)
-- check for breaking changes in commits between current/target (commits with !)
-- -- have ability to disable breaking change check and just accept everything
-- check for user modifications to base install
-- -- if there are prompt that we will reset them to continue
-- pull update and get commit log
-- if using stable
-- -- get the appropriate packersnapshot (this should be automatically selected by packer config)
-- run packer sync somehow?
-- display commit log in some way, window/buffer/quickfix/etc.
end
end
end

return astronvim

0 comments on commit 95aaf8d

Please sign in to comment.