From a7ea9189f6698db65556cc576093528bf055f351 Mon Sep 17 00:00:00 2001 From: unai_71 Date: Mon, 12 Oct 2026 12:37:40 +0200 Subject: [PATCH] feat: add sdk and toolchain management tools --- lua/hego/commands.lua | 14 ++++ lua/hego/init.lua | 27 ++++++ lua/hego/keymaps.lua | 15 ++++ lua/hego/sdk.lua | 102 ++++++++++++++++++++++ lua/hego/toolchain.lua | 186 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 344 insertions(+) create mode 100644 lua/hego/sdk.lua create mode 100644 lua/hego/toolchain.lua diff --git a/lua/hego/commands.lua b/lua/hego/commands.lua index 45ff90b..9da7b2c 100644 --- a/lua/hego/commands.lua +++ b/lua/hego/commands.lua @@ -37,6 +37,20 @@ function M.setup(_) cmd("HegoBuildConfig", function() H.build_from_config() end, {}) + cmd("HegoTerminal", function() + H.terminal() + end, {}) + cmd("HegoToolchain", function() + H.toolchain_pick() + end, {}) + cmd("HegoSDK", function() + H.sdk_pick() + end, {}) + + -- Con argumentos: :HegoTerminal v3.4.1 + vim.api.nvim_create_user_command("HegoTerminalAt", function(args) + require("hego.toolchain").terminal(require("hego").options, args.args) + end, { nargs = 1 }) end return M diff --git a/lua/hego/init.lua b/lua/hego/init.lua index 4dbbbb5..8685670 100644 --- a/lua/hego/init.lua +++ b/lua/hego/init.lua @@ -11,6 +11,7 @@ M.defaults = { sysbuild = true, overlays = {}, extra_cmake = {}, + terminal_shell = "zsh", keys = { build = "nb", pristine = "np", @@ -23,6 +24,11 @@ M.defaults = { config = "nc", build_config = "nB", monitor = "nt", + terminal = "nT", + toolchain = "nl", + toolchain_install = "ni", + toolchain_search = "nN", + sdk = "ns", }, } @@ -74,6 +80,27 @@ function M.monitor() return require("hego.monitor").run(M.options) end +--- Terminal con el entorno NCS activo. +function M.terminal(version) + return require("hego.toolchain").terminal(M.options, version) +end + +function M.toolchain_pick() + return require("hego.toolchain").pick(M.options) +end + +function M.toolchain_install() + return require("hego.toolchain").install_prompt(M.options) +end + +function M.toolchain_search() + return require("hego.toolchain").search_pick(M.options) +end + +function M.sdk_pick() + return require("hego.sdk").pick(M.options) +end + function M.edit_config() local Config = require("hego.config") diff --git a/lua/hego/keymaps.lua b/lua/hego/keymaps.lua index 5cb456d..e03d529 100644 --- a/lua/hego/keymaps.lua +++ b/lua/hego/keymaps.lua @@ -37,6 +37,21 @@ function M.setup(opts) vim.keymap.set("n", k.monitor, function() H.monitor() end, { desc = "NCS: Monitor serie" }) + vim.keymap.set("n", k.terminal, function() + H.terminal() + end, { desc = "NCS: Terminal (entorno NCS)" }) + vim.keymap.set("n", k.toolchain, function() + H.toolchain_pick() + end, { desc = "NCS: Toolchains" }) + vim.keymap.set("n", k.toolchain_install, function() + H.toolchain_install() + end, { desc = "NCS: Instalar toolchain" }) + vim.keymap.set("n", k.toolchain_search, function() + H.toolchain_search() + end, { desc = "NCS: Buscar toolchains" }) + vim.keymap.set("n", k.sdk, function() + H.sdk_pick() + end, { desc = "NCS: SDK manager" }) end return M diff --git a/lua/hego/sdk.lua b/lua/hego/sdk.lua new file mode 100644 index 0000000..77123a4 --- /dev/null +++ b/lua/hego/sdk.lua @@ -0,0 +1,102 @@ +local M = {} + +local function sdk(args, title) + local exec = require("hego.exec") + local cmd = vim.list_extend({ "nrfutil", "sdk-manager" }, args) + return exec.run_async(cmd, { + title = title or "hego://sdk-manager", + cwd = vim.fn.getcwd(), + }) +end + +--- Salida de `sdk-manager list` (nrfutil 8.2.1): +--- SDK Type SDK Version SDK Status Toolchain Status +--- nrf v3.4.1 Not installed Installed +function M.list() + local out = vim.fn.system({ "nrfutil", "sdk-manager", "list" }) + if vim.v.shell_error ~= 0 then + return {} + end + + local sdks = {} + for _, line in ipairs(vim.split(out, "\n", { plain = true })) do + local typ, version, status, tc = + line:match("^(%S+)%s+(v[%d%.]+)%s+(Not installed|Installed)%s+(Not installed|Installed)%s*$") + if typ and version then + table.insert(sdks, { + type = typ, + version = version, + installed = status == "Installed", + toolchain = tc == "Installed", + }) + end + end + return sdks +end + +function M.search() + sdk({ "search" }, "hego://sdk-search") +end + +--- `install` toma la versión POSICIONAL (verificado: , con 'v'). +function M.install(version) + version = version and vim.trim(version) or "" + if version == "" then + return + end + sdk({ "install", version }, "hego://sdk-install") +end + +function M.install_prompt(opts) + vim.ui.input({ + prompt = "Versión de SDK a instalar (p.ej. v3.4.1): ", + default = opts.ncs_version or "", + }, function(input) + if input and vim.trim(input) ~= "" then + M.install(vim.trim(input)) + end + end) +end + +--- ns: picker con el estado real de SDKs y toolchains. +function M.pick(opts) + local sdks = M.list() + + if #sdks == 0 then + -- Sin parseo posible: cae al modo volcado. + return sdk({ "list" }, "hego://sdk-list") + end + + vim.ui.select(sdks, { + prompt = "SDK (fuente)", + format_item = function(s) + return string.format( + "%s %-8s sdk:%-14s toolchain:%s", + s.type, + s.version, + s.installed and "installed" or "not installed", + s.toolchain and "installed" or "not installed" + ) + end, + }, function(choice) + if not choice then + return + end + + if choice.installed then + vim.notify("[hego] SDK " .. choice.version .. " ya instalado", vim.log.levels.INFO) + return + end + + local ok = vim.fn.confirm( + ("Instalar el SDK %s (árbol de fuentes, ~15 GB).\n¿Seguir?"):format(choice.version), + "&Sí\n&No", + 2 + ) + if ok == 1 then + M.install(choice.version) + end + end) +end + +return M diff --git a/lua/hego/toolchain.lua b/lua/hego/toolchain.lua new file mode 100644 index 0000000..72cff55 --- /dev/null +++ b/lua/hego/toolchain.lua @@ -0,0 +1,186 @@ +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 + +--- 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 + +--- 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