feat: núcleo del plugin NCS (build, flash, LSP)
Primera versión funcional del plugin para nRF Connect SDK en LazyVim. Incluye: - setup() con opciones por proyecto y API pública (build, flash) - detect: resolución en runtime de versión NCS, toolchain y board (sin hashes hardcodeados) - exec: wrapper async sobre vim.system + entorno NCS - west: construcción del comando build (--sysbuild, -DCONF_FILE, -DDTC_OVERLAY_FILE, -DCMAKE_EXPORT_COMPILE_COMMANDS=ON) - lsp: symlink de compile_commands.json desde build/<image>/ y LspRestart (clangd solo lo lee al arrancar) - flash: detección de placas vía nrfutil device list + selector - keymaps: <leader>nb (build), <leader>np (pristine), <leader>nf (flash)
This commit is contained in:
1 parent
03a43a686f
commit
a4a5bfe636
8 files changed
+297
No files matched your search
@@ -0,0 +1,9 @@
|
||||
vim.api.nvim_create_user_command("HegoBuild", function()
|
||||
require("hego").build(false)
|
||||
end, {})
|
||||
vim.api.nvim_create_user_command("HegoBuildPristine", function()
|
||||
require("hego").build(true)
|
||||
end, {})
|
||||
vim.api.nvim_create_user_command("HegoFlash", function()
|
||||
require("hego").flash()
|
||||
end, {})
|
||||
@@ -0,0 +1,52 @@
|
||||
local M = {}
|
||||
|
||||
local function stat(p)
|
||||
return vim.uv.fs_stat(p)
|
||||
end
|
||||
|
||||
--- Versión NCS más reciente instalada (p.ej. "v3.4.0").
|
||||
function M.ncs_version(root)
|
||||
local out = {}
|
||||
for _, d in ipairs(vim.fn.glob(root .. "/v*", false, true)) do
|
||||
if stat(d .. "/zephyr") then
|
||||
table.insert(out, vim.fn.fnamemodify(d, ":t"))
|
||||
end
|
||||
end
|
||||
table.sort(out)
|
||||
return out[#out]
|
||||
end
|
||||
|
||||
--- Toolchain más reciente: { dir, gcc }.
|
||||
function M.toolchain(root)
|
||||
local out = {}
|
||||
for _, d in ipairs(vim.fn.glob(root .. "/*", false, true)) do
|
||||
local gcc = d .. "/opt/zephyr-sdk/gnu/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc"
|
||||
local st = stat(gcc)
|
||||
if st then
|
||||
table.insert(out, { dir = d, gcc = gcc, mtime = st.mtime.sec })
|
||||
end
|
||||
end
|
||||
table.sort(out, function(a, b)
|
||||
return a.mtime > b.mtime
|
||||
end)
|
||||
return out[1]
|
||||
end
|
||||
|
||||
--- Board: lo lee del CMakeCache si ya hay build; si no, nil.
|
||||
function M.board(build_dir)
|
||||
local f = io.open(build_dir .. "/CMakeCache.txt", "r")
|
||||
if not f then
|
||||
return nil
|
||||
end
|
||||
for line in f:lines() do
|
||||
local b = line:match("^BOARD:STRING=(.+)$")
|
||||
if b then
|
||||
f:close()
|
||||
return b
|
||||
end
|
||||
end
|
||||
f:close()
|
||||
return nil
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,53 @@
|
||||
local M = {}
|
||||
|
||||
--- Envuelve un binario (west, nrfutil...) en el entorno NCS.
|
||||
function M.ncs(opts, cmd)
|
||||
local full = { "nrfutil", "toolchain-manager", "launch" }
|
||||
if opts.ncs_version then
|
||||
vim.list_extend(full, { "--ncs-version", opts.ncs_version })
|
||||
end
|
||||
vim.list_extend(full, { "--" })
|
||||
vim.list_extend(full, cmd)
|
||||
return full
|
||||
end
|
||||
|
||||
--- Ejecuta async y vuelca la salida a un buffer scratch (no bloquea nvim).
|
||||
function M.run_async(cmd, o)
|
||||
o = o or {}
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(buf, o.title or "hego://run")
|
||||
vim.cmd("botright split")
|
||||
vim.api.nvim_win_set_buf(0, buf)
|
||||
|
||||
local function append(data)
|
||||
if not data or data == "" then
|
||||
return
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(buf, -1, -1, false, vim.split(data, "\n", { plain = true }))
|
||||
vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(buf), 0 })
|
||||
end
|
||||
|
||||
return vim.system(cmd, {
|
||||
cwd = o.cwd,
|
||||
text = true,
|
||||
stdout = function(_, d)
|
||||
vim.schedule(function()
|
||||
append(d)
|
||||
end)
|
||||
end,
|
||||
stderr = function(_, d)
|
||||
vim.schedule(function()
|
||||
append(d)
|
||||
end)
|
||||
end,
|
||||
}, function(obj)
|
||||
vim.schedule(function()
|
||||
append("\n[hego] exit: " .. tostring(obj.code))
|
||||
if o.on_exit then
|
||||
o.on_exit(obj)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,49 @@
|
||||
local M = {}
|
||||
|
||||
function M.devices()
|
||||
local out = vim.fn.system({ "nrfutil", "device", "list", "--json" })
|
||||
local ok, data = pcall(vim.json.decode, out)
|
||||
if not ok or type(data) ~= "table" then
|
||||
return {}
|
||||
end
|
||||
local devs = {}
|
||||
for _, d in ipairs(data) do
|
||||
if d.serial_number then
|
||||
table.insert(devs, {
|
||||
sn = d.serial_number,
|
||||
name = (d.traits and d.traits.product) or "DK",
|
||||
})
|
||||
end
|
||||
end
|
||||
return devs
|
||||
end
|
||||
|
||||
function M.run(opts)
|
||||
local devs = M.devices()
|
||||
if #devs == 0 then
|
||||
vim.notify("[hego] no hay ninguna placa conectada", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local function go(sn)
|
||||
local exec = require("hego.exec")
|
||||
local cmd = exec.ncs(opts, { "west", "flash", "-d", opts.build_dir, "--dev-id", sn })
|
||||
exec.run_async(cmd, { title = "hego://flash" })
|
||||
end
|
||||
|
||||
if #devs == 1 then
|
||||
return go(devs[1].sn)
|
||||
end
|
||||
vim.ui.select(devs, {
|
||||
prompt = "¿A qué placa flasheo?",
|
||||
format_item = function(d)
|
||||
return d.name .. " (" .. d.sn .. ")"
|
||||
end,
|
||||
}, function(c)
|
||||
if c then
|
||||
go(c.sn)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,48 @@
|
||||
local M = {}
|
||||
|
||||
M.defaults = {
|
||||
ncs_root = vim.fn.expand("~/ncs"),
|
||||
toolchains_root = vim.fn.expand("~/ncs/toolchains"),
|
||||
ncs_version = nil, -- nil = autodetectar (p.ej. "v3.4.0")
|
||||
toolchain_dir = nil, -- nil = autodetectar
|
||||
board = nil, -- nil = leer de build/CMakeCache.txt
|
||||
build_dir = "build",
|
||||
conf_file = "prj.conf",
|
||||
sysbuild = true,
|
||||
overlays = {}, -- { "boards/nrf54l15dk_nrf54l15_cpuapp.overlay" }
|
||||
extra_cmake = {}, -- { "-DCONFIG_DEBUG_THREAD_INFO=y" }
|
||||
keys = { build = "<leader>nb", pristine = "<leader>np", flash = "<leader>nf" },
|
||||
}
|
||||
|
||||
M.options = {}
|
||||
|
||||
function M.setup(opts)
|
||||
local o = vim.tbl_deep_extend("force", {}, M.defaults, opts or {})
|
||||
local detect = require("hego.detect")
|
||||
|
||||
if not o.ncs_version then
|
||||
o.ncs_version = detect.ncs_version(o.ncs_root)
|
||||
end
|
||||
if not o.toolchain_dir then
|
||||
local tc = detect.toolchain(o.toolchains_root)
|
||||
o.toolchain_dir = tc and tc.dir or nil
|
||||
end
|
||||
if not o.board then
|
||||
o.board = detect.board(o.build_dir)
|
||||
end
|
||||
|
||||
M.options = o
|
||||
require("hego.keymaps").setup(o)
|
||||
require("hego.commands").setup(o)
|
||||
return o
|
||||
end
|
||||
|
||||
-- API que consumen keymaps y comandos de usuario
|
||||
function M.build(pristine)
|
||||
return require("hego.west").build(M.options, pristine)
|
||||
end
|
||||
function M.flash()
|
||||
return require("hego.flash").run(M.options)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,12 @@
|
||||
function M.setup(opts)
|
||||
local k, H = opts.keys, require("hego")
|
||||
vim.keymap.set("n", k.build, function()
|
||||
H.build(false)
|
||||
end, { desc = "NCS: Build" })
|
||||
vim.keymap.set("n", k.pristine, function()
|
||||
H.build(true)
|
||||
end, { desc = "NCS: Build (pristine)" })
|
||||
vim.keymap.set("n", k.flash, function()
|
||||
H.flash()
|
||||
end, { desc = "NCS: Flash" })
|
||||
end
|
||||
@@ -0,0 +1,39 @@
|
||||
local M = {}
|
||||
|
||||
local SKIP = { mcuboot = true, matter_factory_data = true }
|
||||
|
||||
function M.link(opts)
|
||||
local m = vim.fn.glob(opts.build_dir .. "/*/compile_commands.json", false, true)
|
||||
if #m == 0 then
|
||||
m = vim.fn.glob(opts.build_dir .. "/compile_commands.json", false, true) -- sin sysbuild
|
||||
end
|
||||
if #m == 0 then
|
||||
vim.notify("[hego] no encuentro compile_commands.json", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local chosen
|
||||
for _, p in ipairs(m) do
|
||||
local image = vim.fn.fnamemodify(vim.fn.fnamemodify(p, ":h"), ":t")
|
||||
if not SKIP[image] then
|
||||
chosen = p
|
||||
break
|
||||
end
|
||||
end
|
||||
chosen = chosen or m[1]
|
||||
|
||||
vim.fn.system({
|
||||
"ln",
|
||||
"-sfn",
|
||||
vim.fn.fnamemodify(chosen, ":p"),
|
||||
vim.fn.getcwd() .. "/compile_commands.json",
|
||||
})
|
||||
if vim.v.shell_error ~= 0 then
|
||||
vim.notify("[hego] fallo creando el symlink", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
vim.cmd("LspRestart") -- clangd solo lo lee al arrancar
|
||||
vim.notify("[hego] compile_commands.json -> " .. chosen, vim.log.levels.INFO)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,35 @@
|
||||
local M = {}
|
||||
|
||||
function M.build(opts, pristine)
|
||||
local a = { "build", "-d", opts.build_dir, vim.fn.getcwd(), "--board", opts.board }
|
||||
if opts.sysbuild then
|
||||
table.insert(a, "--sysbuild")
|
||||
end
|
||||
if pristine then
|
||||
table.insert(a, "--pristine")
|
||||
end
|
||||
table.insert(a, "--")
|
||||
table.insert(a, "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON")
|
||||
if opts.conf_file then
|
||||
table.insert(a, "-DCONF_FILE=" .. opts.conf_file)
|
||||
end
|
||||
if #opts.overlays > 0 then
|
||||
table.insert(a, "-DDTC_OVERLAY_FILE=" .. table.concat(opts.overlays, ";"))
|
||||
end
|
||||
for _, e in ipairs(opts.extra_cmake or {}) do
|
||||
table.insert(a, e)
|
||||
end
|
||||
|
||||
local exec = require("hego.exec")
|
||||
local cmd = exec.ncs(opts, vim.list_extend({ "west" }, a))
|
||||
return exec.run_async(cmd, {
|
||||
title = "hego://build",
|
||||
on_exit = function(obj)
|
||||
if obj.code == 0 then
|
||||
require("hego.lsp").link(opts)
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in new issue
Block a user