summaryrefslogtreecommitdiff
path: root/script/cli/check.lua
blob: 8b314f24499ec2f594d08157c7a99b41e9d3840d (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
local lang       = require 'language'
local platform   = require 'bee.platform'
local subprocess = require 'bee.subprocess'
local json       = require 'json'
local jsonb      = require 'json-beautify'
local util       = require 'utility'


local numThreads = tonumber(NUM_THREADS or 1)

local exe = arg[-1]
-- TODO: is this necessary? got it from the shell.lua helper in bee.lua tests
if platform.os == 'windows' and not exe:match('%.[eE][xX][eE]$') then
    exe = exe..'.exe'
end

local function logFileForThread(threadId)
    return LOGPATH .. '/check-partial-' .. threadId .. '.json'
end

local function buildArgs(threadId)
    local args = {exe}
    local skipNext = false
    for i = 1, #arg do
        local arg = arg[i]
        -- --check needs to be transformed into --check_worker
        if arg:lower():match('^%-%-check$') or arg:lower():match('^%-%-check=') then
            args[#args + 1] = arg:gsub('%-%-%w*', '--check_worker')
        -- --check_out_path needs to be removed if we have more than one thread
        elseif arg:lower():match('%-%-check_out_path') and numThreads > 1 then
            if not arg:match('%-%-%w*=') then
                skipNext = true
            end
        else
            if skipNext then
                skipNext = false
            else
                args[#args + 1] = arg
            end
        end
    end
    args[#args + 1] = '--thread_id'
    args[#args + 1] = tostring(threadId)
    if numThreads > 1 then
        args[#args + 1] = '--quiet'
        args[#args + 1] = '--check_out_path'
        args[#args + 1] = logFileForThread(threadId)
    end
    return args
end

if numThreads > 1 then
    print(lang.script('CLI_CHECK_MULTIPLE_WORKERS', numThreads))
end

local procs = {}
for i = 1, numThreads do
    local process, err = subprocess.spawn({buildArgs(i)})
    if err then
        print(err)
    end
    if process then
        procs[#procs + 1] = process
    end
end

for _, process in ipairs(procs) do
    process:wait()
end

local outpath = CHECK_OUT_PATH
if outpath == nil then
    outpath = LOGPATH .. '/check.json'
end

if numThreads > 1 then
    local mergedResults = {}
    local count = 0
    for i = 1, numThreads do
        local result = json.decode(util.loadFile(logFileForThread(i)) or '[]')
        for k, v in pairs(result) do
            local entries = mergedResults[k] or {}
            mergedResults[k] = entries
            for _, entry in ipairs(v) do
                entries[#entries + 1] = entry
                count = count + 1
            end
        end
    end
    util.saveFile(outpath, jsonb.beautify(mergedResults))
    if count == 0 then
        print(lang.script('CLI_CHECK_SUCCESS'))
    else
        print(lang.script('CLI_CHECK_RESULTS', count, outpath))
    end
end