create-mekanism-modular/tools/live-edit.lua

63 lines
2.5 KiB
Lua

-- Live placement editing: save a file, watch it change in game.
--
-- Sourced on top of your own config with `-c luafile`, never with `-u`, so
-- nothing here sets an option or steals a mapping - it only adds the reload.
--
-- Saving any module JSON rebuilds the mod, copies it into whichever instance is
-- running, and tells that game to re-read the half of the jar the file belongs
-- to: `/reload` for module placement under `packs/`, F3+T for models and
-- textures under `assets/`. The build is async, so a save never blocks.
--
-- :Reload rebuild and reload now, without saving
-- :ReloadLog open the build log after a failure
-- :ReloadOff stop reloading on save (:ReloadOn to resume)
local REPO = '/home/themiro/code.dev/create-mekanism-modular'
local SCRIPT = REPO .. '/tools/live-reload.sh'
local running, enabled = false, true
local function reload(file)
if running then
vim.notify('[live] a reload is already in flight', vim.log.levels.WARN)
return
end
running = true
vim.notify('[live] building ' .. vim.fn.fnamemodify(file, ':t') .. ' ...')
local out = {}
local function grab(_, d)
for _, l in ipairs(d or {}) do if l ~= '' then table.insert(out, l) end end
end
vim.fn.jobstart({ SCRIPT, file }, {
stdout_buffered = true, stderr_buffered = true,
on_stdout = grab, on_stderr = grab,
on_exit = function(_, code)
running = false
local msg = '[live] ' .. table.concat(out, ' | ')
vim.notify(code == 0 and msg or (msg .. ' (:ReloadLog)'),
code == 0 and vim.log.levels.INFO or vim.log.levels.ERROR)
end,
})
end
-- Only the files that actually feed placement, so an unrelated save never
-- kicks off a build.
vim.api.nvim_create_autocmd('BufWritePost', {
group = vim.api.nvim_create_augroup('CmmodularLive', { clear = true }),
pattern = { REPO .. '/src/main/resources/packs/*.json',
REPO .. '/src/main/resources/assets/*.json' },
callback = function(a) if enabled then reload(a.match) end end,
})
vim.api.nvim_create_user_command('Reload', function()
reload(vim.api.nvim_buf_get_name(0))
end, { desc = 'rebuild, deploy, and reload in game' })
vim.api.nvim_create_user_command('ReloadLog', function()
vim.cmd('split /tmp/live-reload-build.log')
end, { desc = 'build log from the last live reload' })
vim.api.nvim_create_user_command('ReloadOff', function()
enabled = false; vim.notify('[live] reload-on-save off')
end, {})
vim.api.nvim_create_user_command('ReloadOn', function()
enabled = true; vim.notify('[live] reload-on-save on')
end, {})