Skip to content

Commit

Permalink
feat: better git tag -> version fetching (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Dec 9, 2024
1 parent e37c0c3 commit e84cf29
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 14 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake . -Lv
15 changes: 1 addition & 14 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,7 @@ jobs:
id: get-release
run: |
# Check for semver tags
TAGS="$(git for-each-ref --sort=authordate --format '%(refname)' refs/tags )"
if [[ -n "$TAGS" ]]; then
echo "Found tags:"
echo "$TAGS"
fi
TAG="$(git for-each-ref --sort=authordate --format '%(refname)' refs/tags | sed 's/refs\/tags\/\(.*\)/\1/' | grep -P '^[v]*[0-9]{1,}.[0-9]{1,}.[0-9]{1,}' | tail -n1)"
if [[ -z "$TAG" ]]; then
# Try without patch
TAG="$(git for-each-ref --sort=authordate --format '%(refname)' refs/tags | sed 's/refs\/tags\/\(.*\)/\1/' | grep -P '^[v]*[0-9]{1,}.[0-9]{1,}' | tail -n1)"
fi
if [[ -z "$TAG" ]]; then
# Try without minor
TAG="$(git for-each-ref --sort=authordate --format '%(refname)' refs/tags | sed 's/refs\/tags\/\(.*\)/\1/' | grep -P '^[v]*[0-9]{1,}' | tail -n1)"
fi
TAG="$(./scripts/echo_latest_tag_version.lua)"
if [[ -n "$TAG" ]]; then
echo "Found $TAG"
git checkout $TAG
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
chunks.json
.luarc.json
.direnv
12 changes: 12 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
globals = {
"_",
"vim",
"describe",
"it",
"assert",
"stub",
}

ignore = {
"631", -- max_line_length
}
1 change: 1 addition & 0 deletions .luarc.json
124 changes: 124 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
description = "devShell for Neovim Lua plugins";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
gen-luarc.url = "github:mrcjkb/nix-gen-luarc-json";
};

outputs = inputs @ {
self,
nixpkgs,
flake-parts,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
perSystem = {
config,
self',
inputs',
pkgs,
system,
...
}: let
pkgs = import nixpkgs {
inherit system;
overlays = [
inputs.gen-luarc.overlays.default
];
};
luarc = pkgs.mk-luarc {
nvim = pkgs.neovim-nightly;
};
in {
devShells.default = pkgs.mkShell {
name = "NURR devShell";
shellHook = ''
ln -fs ${pkgs.luarc-to-json luarc} .luarc.json
'';
buildInputs = with pkgs; [
lua-language-server
stylua
(lua5_1.withPackages (luaPkgs:
with luaPkgs; [
luarocks
luacheck
]))
];
};
};
};
}
89 changes: 89 additions & 0 deletions scripts/echo_latest_tag_version.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env -S nvim -u NONE -U NONE -N -i NONE -l

-- Copied over from rocks-git.nvim

---@param rev string?
---@return boolean
local function is_version(rev)
if not rev then
return false
end
if tonumber(rev) then
return true
end
local version_str = rev:gsub("v", "")
return vim.iter(vim.gsplit(version_str, ".", { plain = true })):all(function(str)
return tonumber(str) ~= nil
end)
end

---@param rev string?
---@return vim.Version?
local function get_version(rev)
if not is_version(rev) then
return
end
local ok, version = pcall(vim.version.parse, rev)
return ok and version or nil
end

---@param stdout string
---@return string | nil
local function parse_git_latest_semver_tag(stdout)
local latest_tag = nil
local latest_version = nil
for tag in stdout:gmatch("refs/tags/([^\n]+)") do
local version = get_version(tag)
if version and latest_version then
if version > latest_version then
latest_tag = tag
latest_version = version
end
elseif version then
latest_tag = tag
latest_version = version
end
end
return latest_tag
end

---@param args string[] git CLI arguments
---@param on_exit fun(sc: vim.SystemCompleted)|nil Called asynchronously when the git command exits.
---@param opts? vim.SystemOpts
---@return vim.SystemObj | nil
---@see vim.system
local function git_cli(args, on_exit, opts)
opts = opts or {}
local git_cmd = vim.list_extend({
"git",
}, args)
---@type boolean, vim.SystemObj | string
local ok, so_or_err = pcall(vim.system, git_cmd, opts, on_exit)
if ok then
---@cast so_or_err vim.SystemObj
return so_or_err
else
---@cast so_or_err string
---@type vim.SystemCompleted
local sc = {
code = 1,
signal = 0,
stderr = ("Failed to invoke git: %s"):format(so_or_err),
}
if on_exit then
on_exit(sc)
end
end
end

local function echo_latest_git_tag()
local sc = git_cli({ "for-each-ref", "--format", "%(refname)", "refs/tags" }):wait()
if sc.code == 0 and sc.stdout then
local latest_tag, _ = parse_git_latest_semver_tag(sc.stdout or "")
if latest_tag then
io.write(latest_tag)
end
end
end

echo_latest_git_tag()

0 comments on commit e84cf29

Please sign in to comment.