Files

187 lines
5.1 KiB
Lua

local M = {}
--- Ejecuta nrfutil tal cual. La gestión de toolchains NO se envuelve en
--- `toolchain-manager launch`: el manager se ejecuta a sí mismo.
local function nrfutil(args, title)
local exec = require("hego.exec")
local cmd = vim.list_extend({ "nrfutil" }, args)
return exec.run_async(cmd, {
title = title or "hego://nrfutil",
cwd = vim.fn.getcwd(),
})
end
M.nrfutil = nrfutil
--- Salida de `toolchain-manager list` (nrfutil 8.2.1):
--- Version Toolchain
--- v3.2.4 /home/unai/ncs/toolchains/2ac5840438
--- v3.4.0 /home/unai/ncs/toolchains/fbf7391cab
function M.list()
local out = vim.fn.system({ "nrfutil", "toolchain-manager", "list" })
if vim.v.shell_error ~= 0 then
return {}
end
local installed = {}
for _, line in ipairs(vim.split(out, "\n", { plain = true })) do
local version, dir = line:match("^%s*(v[%d%.]+)%s+(/%S+)")
if version and dir then
table.insert(installed, { version = version, dir = dir })
end
end
return installed
end
--- Salida de `toolchain-manager search` (nrfutil 8.2.1):
--- Version Status
--- v3.4.1 Not installed
--- v3.4.0 Installed
function M.search()
local out = vim.fn.system({ "nrfutil", "toolchain-manager", "search" })
if vim.v.shell_error ~= 0 then
return {}
end
local all = {}
for _, line in ipairs(vim.split(out, "\n", { plain = true })) do
local version, status = line:match("^%s*(v[%d%.]+)%s+(.+%S)%s*$")
if version and status then
table.insert(all, {
version = version,
installed = status:lower():find("^installed") ~= nil,
})
end
end
return all
end
--- Comando completo para abrir una shell con el entorno del toolchain.
--- ZEPHYR_BASE se inyecta porque `toolchain-manager launch` NO lo exporta.
--- Verificado en v3.4.0 Y en v3.4.1: solo expone ZEPHYR_SDK_INSTALL_DIR
--- y ZEPHYR_TOOLCHAIN_VARIANT, nunca ZEPHYR_BASE.
function M.env_command(opts, version)
local cmd = { "env" }
if opts.ncs_root and version then
local zephyr = opts.ncs_root .. "/" .. version .. "/zephyr"
if vim.fn.isdirectory(zephyr) == 1 then
table.insert(cmd, "ZEPHYR_BASE=" .. zephyr)
end
end
vim.list_extend(cmd, { "nrfutil", "toolchain-manager", "launch" })
if version then
vim.list_extend(cmd, { "--ncs-version", version })
end
vim.list_extend(cmd, { "--", opts.terminal_shell or "zsh" })
return cmd
end
function M.terminal(opts, version)
version = version or opts.ncs_version
local exec = require("hego.exec")
return exec.run_async(M.env_command(opts, version), {
title = "hego://terminal" .. (version and ("-" .. version) or ""),
cwd = vim.fn.getcwd(),
})
end
--- Instala el toolchain de una versión.
--- `keep_default` deja el toolchain por defecto como estaba (útil para
--- instalar 3.4.1 sin pasar a usarlo todavía).
function M.install(opts, version, keep_default)
version = version or opts.ncs_version
if not version or version == "" then
vim.notify("[hego] no sé qué versión de toolchain instalar", vim.log.levels.WARN)
return
end
local args = { "toolchain-manager", "install", "--ncs-version", version }
if keep_default then
table.insert(args, "--keep-old-toolchain-selection")
end
nrfutil(args, "hego://toolchain-install")
end
function M.install_prompt(opts)
vim.ui.input({
prompt = "Versión NCS para el toolchain (p.ej. v3.4.1): ",
default = opts.ncs_version or "",
}, function(input)
input = input and vim.trim(input) or ""
if input ~= "" then
M.install(opts, input)
end
end)
end
--- <leader>nN: picker de versiones disponibles; instala la elegida.
function M.search_pick(opts)
local all = M.search()
if #all == 0 then
vim.notify("[hego] no pude leer `toolchain-manager search`", vim.log.levels.WARN)
return
end
vim.ui.select(all, {
prompt = "Toolchain a instalar",
format_item = function(t)
return t.version .. (t.installed and " (instalado)" or "")
end,
}, function(choice)
if not choice then
return
end
if choice.installed then
vim.notify("[hego] " .. choice.version .. " ya está instalado", vim.log.levels.INFO)
return
end
M.install(opts, choice.version, true) -- no cambia el toolchain por defecto
end)
end
--- <leader>nl: picker de toolchains instalados -> elegir acción.
function M.pick(opts)
local installed = M.list()
if #installed == 0 then
vim.notify("[hego] ningún toolchain detectado", vim.log.levels.WARN)
return
end
vim.ui.select(installed, {
prompt = "Toolchain",
format_item = function(t)
return t.version .. " " .. t.dir
end,
}, function(choice)
if not choice then
return
end
local actions = {
{ label = "Terminal con este toolchain", act = "terminal" },
{ label = "Build con este toolchain", act = "build" },
}
vim.ui.select(actions, {
prompt = choice.version,
format_item = function(a)
return a.label
end,
}, function(a)
if not a then
return
end
if a.act == "terminal" then
M.terminal(opts, choice.version)
elseif a.act == "build" then
-- Sobrescribe la versión solo para este build, sin tocar la config.
local o = vim.tbl_deep_extend("force", opts, { ncs_version = choice.version })
require("hego.west").build(o, false)
end
end)
end)
end
return M