Bootstrapping Compilers - gpanders/nvim-moonwalk GitHub Wiki

You can use some hacky metatable tricks to ensure that your compilers are installed. Below is an example for Fennel, which can be modified for other compilers.

Add the block below and replace

require("fennel").compileString(src)

with

fennel.compileString(src)

in the compile function to add_loader.

local fennel = setmetatable({}, {
    __index = function(_, k)
        local ok, fennel = pcall(require, "fennel")
        if not ok then
            local install_path = vim.fn.stdpath("data") .. "/site/pack/fennel/start/fennel"
            local tag = "0.9.2"
            print("Installing fennel " .. tag .. " to " .. install_path .. "...")
            vim.fn.system({"git", "clone", "-b", tag, "https://git.sr.ht/~technomancy/fennel", install_path})
            vim.fn.system({"make", "-C", install_path})
            vim.fn.system({"mkdir", install_path .. "/lua"})
            vim.fn.system({"mv", install_path .. "/fennel.lua", install_path .. "/lua"})
            vim.api.nvim_command("redraw")
            fennel = require("fennel")
        end
        return fennel[k]
    end,
})