From 055cbe24255be0b376d840a92948c830c46e3adc Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Thu, 29 Feb 2024 16:38:35 +0100 Subject: [vis] add simple plugin to asynchronously run build commands --- config/vis/plugins/vis-build/.editorconfig | 11 ++++++ config/vis/plugins/vis-build/Makefile | 19 +++++++++ config/vis/plugins/vis-build/README.md | 24 ++++++++++++ config/vis/plugins/vis-build/init.lua | 52 +++++++++++++++++++++++++ config/vis/plugins/vis-build/tools/check-format | 8 ++++ 5 files changed, 114 insertions(+) create mode 100644 config/vis/plugins/vis-build/.editorconfig create mode 100644 config/vis/plugins/vis-build/Makefile create mode 100644 config/vis/plugins/vis-build/README.md create mode 100644 config/vis/plugins/vis-build/init.lua create mode 100755 config/vis/plugins/vis-build/tools/check-format (limited to 'config/vis/plugins') diff --git a/config/vis/plugins/vis-build/.editorconfig b/config/vis/plugins/vis-build/.editorconfig new file mode 100644 index 0000000..1635025 --- /dev/null +++ b/config/vis/plugins/vis-build/.editorconfig @@ -0,0 +1,11 @@ +# http://editorconfig.org + +root = true + +[*.lua] +indent_style = space +indent_size = 2 + +[.gitlab-ci.yml] +indent_style = space +indent_size = 2 diff --git a/config/vis/plugins/vis-build/Makefile b/config/vis/plugins/vis-build/Makefile new file mode 100644 index 0000000..144e229 --- /dev/null +++ b/config/vis/plugins/vis-build/Makefile @@ -0,0 +1,19 @@ +.PHONY: check format check-luacheck check-format + +LUA_FILES := $(shell find -name "*.lua" -not -path "./json.lua") + +TEST_FILES := $(shell find -name "*_test.lua") + +check: check-luacheck check-format + +check-luacheck: + luacheck --globals=vis -- $(LUA_FILES) + +check-format: + set -e; for lf in $(LUA_FILES); do tools/check-format "$${lf}"; done + +format: + lua-format -i $(LUA_FILES) + +test: + set -e; for tf in $(TEST_FILES); do "$$tf"; done diff --git a/config/vis/plugins/vis-build/README.md b/config/vis/plugins/vis-build/README.md new file mode 100644 index 0000000..23c4de0 --- /dev/null +++ b/config/vis/plugins/vis-build/README.md @@ -0,0 +1,24 @@ +# vis-build - asynchronously run (build) commands + +This is a simple `vis` plugin to asynchronously run any shell command using +the `vis:communicate` API. + +I use the plugin to run build commands while keeping my editor usable. + +## Configuration + +By default, the `build` will execute `make(1)` in the current directory. +The used build command can either be passed as first argument to `build` or +is retrieved by calling the function `build.get_default_build_cmd`. + +So in order to change the default build command to ninja add this snippet to +you `visrc.lua`: + +```lua +local build = require('plugins/vis-build') +build.get_default_build_cmd = function() return 'ninja' end +``` + +## Bindings + +`` is bound to the `build` command. diff --git a/config/vis/plugins/vis-build/init.lua b/config/vis/plugins/vis-build/init.lua new file mode 100644 index 0000000..0558c39 --- /dev/null +++ b/config/vis/plugins/vis-build/init.lua @@ -0,0 +1,52 @@ +local M = {} +M.get_default_build_cmd = function() return 'make' end + +local build_id = 0 +local builds = {} + +M.new_build = function(cmd) + build_id = build_id + 1 + local build_name = 'build' .. build_id + local build_fd = vis:communicate(build_name, cmd) + local build = {fd = build_fd, out = '', err = '', cmd = cmd} + builds[build_name] = build +end + +vis.events.subscribe(vis.events.PROCESS_RESPONSE, + function(name, event, code, msg) + local build = builds[name] + if not build then return end + + if event == 'EXIT' or event == 'SIGNAL' then + if code ~= 0 then + vis:message('build: ' .. name .. ' cmd: ' .. build.cmd) + if event == 'EXIT' then + vis:message('failed with: ' .. code) + else + vis:message('got signal: ' .. code) + end + vis:message('stdout:\n' .. build.out) + vis:message('stderr:\n' .. build.err) + else + vis:info(name .. ' done') + end + builds[name] = nil + end + + if event == 'STDOUT' then + build.out = build.out .. msg + elseif event == 'STDERR' then + build.err = build.err .. msg + end +end) + +vis:command_register('build', function(argv) + M.new_build(argv[1] or M.get_default_build_cmd()) +end, 'Asynchronously build the current file or project') + +vis:map(vis.modes.NORMAL, '', function() + vis:command('build') + return 0 +end, 'Asynchronously build the current file or project') + +return M diff --git a/config/vis/plugins/vis-build/tools/check-format b/config/vis/plugins/vis-build/tools/check-format new file mode 100755 index 0000000..392d023 --- /dev/null +++ b/config/vis/plugins/vis-build/tools/check-format @@ -0,0 +1,8 @@ +#!/bin/sh + +LUA_FILE=$1 +lua-format "${LUA_FILE}" > "${LUA_FILE}.fmt" +diff "${LUA_FILE}" "${LUA_FILE}.fmt" +RET=$? +rm "${LUA_FILE}.fmt" +exit ${RET} -- cgit v1.2.3