local overseer = require("overseer") local parselib = require("overseer.parselib") -- Shared output parser (CMake + GCC/Clang) local function make_combined_parser() local cmake_error_match = parselib.make_lua_match_fn("^CMake Error at (%S+):(%d+)%s*%((.*)%)%s*:%s*(.*)$") local cmake_parse_fn = parselib.make_parse_fn(cmake_error_match, { "filename", "lnum", "type", "text" }) local cmake_parser = parselib.make_parser(cmake_parse_fn) local gcc_parser = parselib.parser_from_errorformat( "%f:%l:%c: %t%n %m," .. "%f:%l:%c: %t%*[^:]: %m," .. "%f:%l: %t%n %m," .. "%f:%l: %t%*[^:]: %m" ) return parselib.combine_parsers({ cmake_parser, gcc_parser }) end -- Core builder used by every variant local function build_task(params) local cwd = vim.fn.fnamemodify(params.cwd or vim.fn.getcwd(), ":p") local src = vim.fn.fnamemodify(params.src_dir, ":p") local build = vim.fn.fnamemodify(params.build_dir, ":p") local exts = {} for ext in params.extensions:gmatch("[^,]+") do local clean_ext = vim.trim(ext):gsub("^%.+", "") table.insert(exts, clean_ext) end if #exts == 0 then exts = { "cppm", "ixx", "cpp" } end local ext_pipe = table.concat(exts, "|") local ext_comma = table.concat(exts, ",") local script = string.format( [[ #!/bin/bash set -e cd "%s" SRC_DIR="%s" BUILD_DIR="%s" CMAKE="%s" CMAKE_ARGS="%s" BUILD_ARGS="%s" echo "[overseer] CWD: $(pwd)" echo "[overseer] SRC_DIR: $SRC_DIR" echo "[overseer] BUILD_DIR: $BUILD_DIR" echo "[overseer] Extensions: {%s}" if [ ! -d "$SRC_DIR" ]; then echo "[overseer] ERROR: Source directory does not exist: $SRC_DIR" exit 1 fi mkdir -p "$BUILD_DIR" rebuild() { echo "[overseer] >>> Change detected. Reconfiguring..." set -x $CMAKE -S . -B "$BUILD_DIR" $CMAKE_ARGS $CMAKE --build "$BUILD_DIR" $BUILD_ARGS set +x echo "[overseer] >>> Build complete. Watching..." } echo "[overseer] >>> Initial CMake configuration..." rebuild echo "[overseer] >>> Watching $SRC_DIR recursively for .{%s} files..." while true; do if command -v inotifywait >/dev/null 2>&1; then inotifywait -r -e create,modify,move,delete --include ".*\.(%s)$" -q "$SRC_DIR" 2>/dev/null || true rebuild elif command -v fswatch >/dev/null 2>&1; then fswatch -r -e ".*" -i "\.(%s)$" --event Created --event Updated --event Removed "$SRC_DIR" | head -n 1 >/dev/null rebuild else echo "[overseer] ERROR: Install inotifywait (Linux) or fswatch (macOS)" exit 1 fi done ]], cwd, src, build, params.cmake, params.cmake_args, params.build_args, ext_comma, ext_comma, ext_pipe, ext_pipe ) return { cmd = { "bash", "-c", script }, cwd = cwd, name = string.format("CMake Watcher [%s]", params.src_dir), components = { "default", { "on_output_parse", parser = make_combined_parser() }, { "on_output_quickfix", open = false, close = true }, "on_result_diagnostics", }, } end -- Parameter schema for the [Custom] variant local custom_params = { src_dir = { desc = "Source directory to watch recursively", type = "string", default = "src", order = 1, }, build_dir = { desc = "CMake build directory", type = "string", default = "build", order = 2, }, cmake = { desc = "CMake executable", type = "string", default = "cmake", order = 3, }, cmake_args = { desc = "CMake config args (space-separated)", type = "string", order = 4, }, build_args = { desc = "CMake build args (space-separated)", type = "string", default = "-j32", order = 5, }, extensions = { desc = "Extensions to watch (comma-separated)", type = "string", default = "cppm,ixx,cpp", order = 6, }, cwd = { desc = "Working directory", type = "string", default = vim.fn.getcwd(), order = 7, }, } ---@type overseer.TemplateFileProvider return { generator = function(search) local cwd = vim.fn.getcwd() -- Add/remove presets here as needed local presets = { { name = "cmake watcher [debug]", desc = "watch src/ with debug configuration", params = { src_dir = "src", build_dir = "build", cmake = "cmake", cmake_args = "-DCMAKE_BUILD_TYPE=Debug -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", build_args = "-j32", extensions = "cppm,ixx,cpp", cwd = cwd, }, }, { name = "cmake watcher [release]", desc = "watch src/ with release configuration", params = { src_dir = "src", build_dir = "build", cmake = "cmake", cmake_args = "-DCMAKE_BUILD_TYPE=Release -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", build_args = "-j32", extensions = "cppm,ixx,cpp", cwd = cwd, }, }, { name = "cmake watcher [relwithdebinfo]", desc = "watch src/ with relwithdebinfo configuration", params = { src_dir = "src", build_dir = "build", cmake = "cmake", cmake_args = "-DCMAKE_BUILD_TYPE=RelWithDebInfo -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", build_args = "-j32", extensions = "cppm,ixx,cpp", cwd = cwd, }, }, { name = "cmake watcher [debug, qt]", desc = "watch src/ with debug configuration", params = { src_dir = "src", build_dir = "build", cmake = "cmake", cmake_args = '-G Ninja -DCMAKE_CXX_COMPILER=/usr/bin/clang++ --install-prefix "$PWD/release" -DQT_DEPLOY_USE_PATCHELF=ON -DVLW_ENABLE_DEV=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON', build_args = "-j32", extensions = "cppm,ixx,cpp", cwd = cwd, }, }, { name = "cmake watcher [release,qt]", desc = "watch src/ with release configuration", params = { src_dir = "src", build_dir = "build", cmake = "cmake", cmake_args = '-G Ninja -DCMAKE_CXX_COMPILER=/usr/bin/clang++ --install-prefix "$PWD/release" -DQT_DEPLOY_USE_PATCHELF=ON -DVLW_ENABLE_DEV=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON', build_args = "-j32", extensions = "cppm,ixx,cpp", cwd = cwd, }, }, { name = "cmake watcher [relwithdebinfo,qt]", desc = "watch src/ with relwithdebinfo configuration", params = { src_dir = "src", build_dir = "build", cmake = "cmake", cmake_args = '-G Ninja -DCMAKE_CXX_COMPILER=/usr/bin/clang++ --install-prefix "$PWD/release" -DQT_DEPLOY_USE_PATCHELF=ON -DVLW_ENABLE_DEV=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_EXPORT_COMPILE_COMMANDS=ON', build_args = "-j32", extensions = "cppm,ixx,cpp", cwd = cwd, }, }, } local templates = {} -- Presets: hardcoded args, no prompt for _, preset in ipairs(presets) do table.insert(templates, { name = preset.name, desc = preset.desc, builder = function() return build_task(preset.params) end, tags = { overseer.TAG.BUILD }, }) end -- Custom: overseer will prompt for params before running table.insert(templates, { name = "CMake Watcher [Custom]", desc = "Watch with fully customizable parameters", builder = function(params) return build_task(params) end, params = custom_params, tags = { overseer.TAG.BUILD }, }) return templates end, condition = { callback = function(search) return vim.fn.filereadable(vim.fs.joinpath(search.dir, "CMakeLists.txt")) == 1 end, }, }