273 lines · 7.5 KB
---@brief [[
--- glaze.nvim update checker
--- Checks for newer versions of registered Go binaries
---@brief ]]

local M = {}

---@class GlazeUpdateInfo
---@field name string Binary name
---@field installed_version? string Currently installed version
---@field latest_version? string Latest available version
---@field has_update boolean Whether an update is available

---@type table<string, GlazeUpdateInfo>
M._update_info = {}

---@type boolean
M._checking = false

local STATE_FILE = vim.fn.stdpath("data") .. "/glaze/state.json"

---Read persisted state from disk.
---@return table
local function read_state()
  local ok, content = pcall(vim.fn.readfile, STATE_FILE)
  if not ok or #content == 0 then
    return {}
  end
  local decode_ok, data = pcall(vim.json.decode, table.concat(content, "\n"))
  if not decode_ok then
    return {}
  end
  return data or {}
end

---Write state to disk.
---@param state table
local function write_state(state)
  local dir = vim.fn.fnamemodify(STATE_FILE, ":h")
  vim.fn.mkdir(dir, "p")