summaryrefslogtreecommitdiff
path: root/config/essentials/vis/build.lua
blob: 8a062cdc5332da7794dee822f06264828b9259d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Copyright (c) 2024 Florian Fischer. All rights reserved.
--
-- vis-build is free software: you can redistribute it and/or modify it under
-- the terms of the GNU General Public License as published by the Free Software
-- Foundation, either version 3 of the License, or (at your option) any later
-- version.
--
-- vis-build is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- vis-build found in the LICENSE file.
-- If not, see <https://www.gnu.org/licenses/>.
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:message(name .. ':\n' .. build.out)
        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, '<M-b>', function()
    vis:command('build')
    return 0
end, 'Asynchronously build the current file or project')

return M