Skip to content

Commit

Permalink
chore: update health.lua file
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Feb 10, 2024
1 parent ba2b9a5 commit 88ffbb1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
80 changes: 56 additions & 24 deletions lua/codecompanion/health.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
local health = vim.health or require("health")
local ok = health.ok or health.report_ok
local warn = health.warn or health.report_warn
local error = health.error or health.report_error
local config = require("codecompanion.config")

local start = vim.health.start or vim.health.report_start
local ok = vim.health.ok or vim.health.report_ok
local info = vim.health.info or vim.health.report_info
local warn = vim.health.warn or vim.health.report_warn
local error = vim.health.error or vim.health.report_error

local fmt = string.format

local M = {}

M.plugins = {
{
name = "nvim-treesitter/nvim-treesitter",
name = "nvim-treesitter",
},
{
name = "nvim-lua/plenary.nvim",
name = "plenary",
},
{
name = "stevearc/dressing.nvim",
name = "dressing",
optional = true,
},
{
name = "folke/edgy.nvim",
name = "edgy",
optional = true,
},
}
Expand All @@ -28,45 +31,74 @@ M.libraries = {
"curl",
}

local function library_available(cmd)
if vim.fn.executable(cmd) == 1 then
M.env_vars = {
{
name = config.options.api_key,
},
{
name = config.options.org_api_key,
optional = true,
},
}

local function plugin_available(name)
local check, _ = pcall(require, name)
return check
end

local function lib_available(lib)
if vim.fn.executable(lib) == 1 then
return true
end

return false
end

local function plugin_available(name)
local check, _ = pcall(require, name)
return check
local function env_available(env)
if os.getenv(env) ~= nil then
return true
end
return false
end

function M.check()
if vim.fn.has("nvim-0.9") == 0 then
health.error("CodeCompanion.nvim requires Neovim 0.9.0+")
error("codecompanion.nvim requires Neovim 0.9.0+")
end

health.start("Checking neovim plugin dependencies")
start("codecompanion.nvim report")

local log = require("codecompanion.utils.log")
info(fmt("Log file: %s", log.get_logfile()))

for _, plugin in ipairs(M.plugins) do
if plugin_available(plugin.name) then
ok(plugin.name .. " installed.")
ok(fmt("%s installed", plugin.name))
else
if plugin.optional then
warn(fmt("Optional dependency '%s' not found.", plugin.name))
warn(fmt("Optional %s dependency not found", plugin.name))
else
error(fmt("Dependency '%s' not found!", plugin.name))
error(fmt("Dependency %s not found", plugin.name))
end
end
end

health.start("Checking library dependencies")

for _, library in ipairs(M.libraries) do
if library_available(library) then
ok(fmt("%s installed.", library))
if lib_available(library) then
ok(fmt("%s installed", library))
else
error(fmt("%s not installed!", library))
error(fmt("%s not installed", library))
end
end

for _, env in ipairs(M.env_vars) do
if env_available(env.name) then
ok(fmt("%s key found", env.name))
else
if env.optional then
warn(fmt("Optional %s key not found", env.name))
else
error(fmt("Key %s not found", env.name))
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lua/codecompanion/utils/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ local M = {}

M.new = Logger.new

M.get_logfile = function()
local ok, stdpath = pcall(vim.fn.stdpath, "log")
if not ok then
stdpath = vim.fn.stdpath("cache")
end

return files.join(stdpath, "codecompanion.log")
end

---@param logger codecompanion.Logger
M.set_root = function(logger)
root = logger
Expand Down

0 comments on commit 88ffbb1

Please sign in to comment.