Skip to content

Commit 2a7cc44

Browse files
committed
refactor(cli/check): outline some functions and add some comments
1 parent 5275a75 commit 2a7cc44

File tree

2 files changed

+116
-91
lines changed

2 files changed

+116
-91
lines changed

script/cli/check.lua

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,41 @@ local util = require 'utility'
77

88
local export = {}
99

10+
local function logFileForThread(threadId)
11+
return LOGPATH .. '/check-partial-' .. threadId .. '.json'
12+
end
13+
14+
local function buildArgs(exe, numThreads, threadId)
15+
local args = {exe}
16+
local skipNext = false
17+
for i = 1, #arg do
18+
local arg = arg[i]
19+
-- --check needs to be transformed into --check_worker
20+
if arg:lower():match('^%-%-check$') or arg:lower():match('^%-%-check=') then
21+
args[#args + 1] = arg:gsub('%-%-%w*', '--check_worker')
22+
-- --check_out_path needs to be removed if we have more than one thread
23+
elseif arg:lower():match('%-%-check_out_path') and numThreads > 1 then
24+
if not arg:match('%-%-%w*=') then
25+
skipNext = true
26+
end
27+
else
28+
if skipNext then
29+
skipNext = false
30+
else
31+
args[#args + 1] = arg
32+
end
33+
end
34+
end
35+
args[#args + 1] = '--thread_id'
36+
args[#args + 1] = tostring(threadId)
37+
if numThreads > 1 then
38+
args[#args + 1] = '--quiet'
39+
args[#args + 1] = '--check_out_path'
40+
args[#args + 1] = logFileForThread(threadId)
41+
end
42+
return args
43+
end
44+
1045
function export.runCLI()
1146
local numThreads = tonumber(NUM_THREADS or 1)
1247

@@ -21,48 +56,13 @@ function export.runCLI()
2156
exe = exe..'.exe'
2257
end
2358

24-
local function logFileForThread(threadId)
25-
return LOGPATH .. '/check-partial-' .. threadId .. '.json'
26-
end
27-
28-
local function buildArgs(threadId)
29-
local args = {exe}
30-
local skipNext = false
31-
for i = 1, #arg do
32-
local arg = arg[i]
33-
-- --check needs to be transformed into --check_worker
34-
if arg:lower():match('^%-%-check$') or arg:lower():match('^%-%-check=') then
35-
args[#args + 1] = arg:gsub('%-%-%w*', '--check_worker')
36-
-- --check_out_path needs to be removed if we have more than one thread
37-
elseif arg:lower():match('%-%-check_out_path') and numThreads > 1 then
38-
if not arg:match('%-%-%w*=') then
39-
skipNext = true
40-
end
41-
else
42-
if skipNext then
43-
skipNext = false
44-
else
45-
args[#args + 1] = arg
46-
end
47-
end
48-
end
49-
args[#args + 1] = '--thread_id'
50-
args[#args + 1] = tostring(threadId)
51-
if numThreads > 1 then
52-
args[#args + 1] = '--quiet'
53-
args[#args + 1] = '--check_out_path'
54-
args[#args + 1] = logFileForThread(threadId)
55-
end
56-
return args
57-
end
58-
59-
if numThreads > 1 then
59+
if not QUIET and numThreads > 1 then
6060
print(lang.script('CLI_CHECK_MULTIPLE_WORKERS', numThreads))
6161
end
6262

6363
local procs = {}
6464
for i = 1, numThreads do
65-
local process, err = subprocess.spawn({buildArgs(i)})
65+
local process, err = subprocess.spawn({buildArgs(exe, numThreads, i)})
6666
if err then
6767
print(err)
6868
end

script/cli/check_worker.lua

Lines changed: 79 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,70 @@ require 'vm'
1717

1818
local export = {}
1919

20+
local function clear_line()
21+
-- Write out empty space to ensure that the previous lien is cleared.
22+
io.write('\x0D', (' '):rep(80), '\x0D')
23+
end
24+
25+
--- @param i integer
26+
--- @param max integer
27+
--- @param results table<string, table[]>
28+
local function report_progress(i, max, results)
29+
local filesWithErrors = 0
30+
local errors = 0
31+
for _, diags in pairs(results) do
32+
filesWithErrors = filesWithErrors + 1
33+
errors = errors + #diags
34+
end
35+
36+
clear_line()
37+
io.write(
38+
('>'):rep(math.ceil(i / max * 20)),
39+
('='):rep(20 - math.ceil(i / max * 20)),
40+
' ',
41+
('0'):rep(#tostring(max) - #tostring(i)),
42+
tostring(i),
43+
'/',
44+
tostring(max)
45+
)
46+
if errors > 0 then
47+
io.write(' [', lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors), ']')
48+
end
49+
io.flush()
50+
end
51+
52+
--- @param uri string
53+
--- @param checkLevel integer
54+
local function apply_check_level(uri, checkLevel)
55+
local config_disables = util.arrayToHash(config.get(uri, 'Lua.diagnostics.disable'))
56+
local config_severities = config.get(uri, 'Lua.diagnostics.severity')
57+
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
58+
serverity = config_severities[name] or serverity
59+
if serverity:sub(-1) == '!' then
60+
serverity = serverity:sub(1, -2)
61+
end
62+
if define.DiagnosticSeverity[serverity] > checkLevel then
63+
config_disables[name] = true
64+
end
65+
end
66+
config.set(uri, 'Lua.diagnostics.disable', util.getTableKeys(config_disables, true))
67+
end
68+
69+
local function downgrade_checks_to_opened(uri)
70+
local diagStatus = config.get(uri, 'Lua.diagnostics.neededFileStatus')
71+
for d, status in pairs(diagStatus) do
72+
if status == 'Any' or status == 'Any!' then
73+
diagStatus[d] = 'Opened!'
74+
end
75+
end
76+
for d, status in pairs(protoDiag.getDefaultStatus()) do
77+
if status == 'Any' or status == 'Any!' then
78+
diagStatus[d] = 'Opened!'
79+
end
80+
end
81+
config.set(uri, 'Lua.diagnostics.neededFileStatus', diagStatus)
82+
end
83+
2084
function export.runCLI()
2185
lang(LOCALE)
2286

@@ -36,18 +100,16 @@ function export.runCLI()
36100
end
37101
rootUri = rootUri:gsub("/$", "")
38102

39-
if CHECKLEVEL then
40-
if not define.DiagnosticSeverity[CHECKLEVEL] then
41-
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
42-
return
43-
end
103+
if CHECKLEVEL and not define.DiagnosticSeverity[CHECKLEVEL] then
104+
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
105+
return
44106
end
45107
local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSeverity.Warning
46108

47109
util.enableCloseFunction()
48110

49111
local lastClock = os.clock()
50-
local results = {}
112+
local results = {} --- @type table<string, table[]>
51113

52114
local function errorhandler(err)
53115
print(err)
@@ -75,31 +137,12 @@ function export.runCLI()
75137

76138
ws.awaitReady(rootUri)
77139

78-
local disables = util.arrayToHash(config.get(rootUri, 'Lua.diagnostics.disable'))
79-
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
80-
serverity = config.get(rootUri, 'Lua.diagnostics.severity')[name] or serverity
81-
if serverity:sub(-1) == '!' then
82-
serverity = serverity:sub(1, -2)
83-
end
84-
if define.DiagnosticSeverity[serverity] > checkLevel then
85-
disables[name] = true
86-
end
87-
end
88-
config.set(rootUri, 'Lua.diagnostics.disable', util.getTableKeys(disables, true))
140+
-- Disable any diagnostics that are above the check level
141+
apply_check_level(rootUri, checkLevel)
89142

90-
-- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
91-
local diagStatus = config.get(rootUri, 'Lua.diagnostics.neededFileStatus')
92-
for diag, status in pairs(diagStatus) do
93-
if status == 'Any' or status == 'Any!' then
94-
diagStatus[diag] = 'Opened!'
95-
end
96-
end
97-
for diag, status in pairs(protoDiag.getDefaultStatus()) do
98-
if status == 'Any' or status == 'Any!' then
99-
diagStatus[diag] = 'Opened!'
100-
end
101-
end
102-
config.set(rootUri, 'Lua.diagnostics.neededFileStatus', diagStatus)
143+
-- Downgrade file opened status to Opened for everything to avoid
144+
-- reporting during compilation on files that do not belong to this thread
145+
downgrade_checks_to_opened(rootUri)
103146

104147
local uris = files.getChildFiles(rootUri)
105148
local max = #uris
@@ -108,28 +151,12 @@ function export.runCLI()
108151
if (i % numThreads + 1) == threadId then
109152
files.open(uri)
110153
diag.doDiagnostic(uri, true)
111-
-- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
112-
if (os.clock() - lastClock > 0.2 or i == #uris) and not QUIET then
154+
-- Print regularly but always print the last entry to ensure
155+
-- that logs written to files don't look incomplete.
156+
if not QUIET and (os.clock() - lastClock > 0.2 or i == #uris) then
113157
lastClock = os.clock()
114158
client:update()
115-
local output = '\x0D'
116-
.. ('>'):rep(math.ceil(i / max * 20))
117-
.. ('='):rep(20 - math.ceil(i / max * 20))
118-
.. ' '
119-
.. ('0'):rep(#tostring(max) - #tostring(i))
120-
.. tostring(i) .. '/' .. tostring(max)
121-
io.write(output)
122-
local filesWithErrors = 0
123-
local errors = 0
124-
for _, diags in pairs(results) do
125-
filesWithErrors = filesWithErrors + 1
126-
errors = errors + #diags
127-
end
128-
if errors > 0 then
129-
local errorDetails = ' [' .. lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors) .. ']'
130-
io.write(errorDetails)
131-
end
132-
io.flush()
159+
report_progress(i, max, results)
133160
end
134161
end
135162
end
@@ -146,10 +173,8 @@ function export.runCLI()
146173
end
147174
end
148175

149-
local outpath = CHECK_OUT_PATH
150-
if outpath == nil then
151-
outpath = LOGPATH .. '/check.json'
152-
end
176+
local outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
177+
153178
-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
154179
util.saveFile(outpath, jsonb.beautify(results))
155180

0 commit comments

Comments
 (0)