Skip to content

Commit 14f8ee5

Browse files
committed
refactor(cli/check): outline some functions and add some comments
1 parent 5808ec4 commit 14f8ee5

File tree

2 files changed

+112
-89
lines changed

2 files changed

+112
-89
lines changed

script/cli/check.lua

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,44 @@ 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, quiet)
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+
if quiet then
39+
args[#args + 1] = '--quiet'
40+
end
41+
args[#args + 1] = '--quiet_worker'
42+
args[#args + 1] = '--check_out_path'
43+
args[#args + 1] = logFileForThread(threadId)
44+
end
45+
return args
46+
end
47+
1048
function export.runCLI()
1149
local numThreads = tonumber(NUM_THREADS or 1)
1250

@@ -21,51 +59,13 @@ function export.runCLI()
2159
exe = exe..'.exe'
2260
end
2361

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-
if QUIET then
53-
args[#args + 1] = '--quiet'
54-
end
55-
args[#args + 1] = '--quiet_worker'
56-
args[#args + 1] = '--check_out_path'
57-
args[#args + 1] = logFileForThread(threadId)
58-
end
59-
return args
60-
end
61-
6262
if not QUIET and numThreads > 1 then
6363
print(lang.script('CLI_CHECK_MULTIPLE_WORKERS', numThreads))
6464
end
6565

6666
local procs = {}
6767
for i = 1, numThreads do
68-
local process, err = subprocess.spawn({buildArgs(i)})
68+
local process, err = subprocess.spawn({buildArgs(exe, numThreads, i, QUIET)})
6969
if err then
7070
print(err)
7171
end

script/cli/check_worker.lua

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,65 @@ local function quiet()
120120
return QUIET or QUIET_WORKER
121121
end
122122

123+
--- @param i integer
124+
--- @param max integer
125+
--- @param results table<string, table[]>
126+
local function report_progress(i, max, results)
127+
local filesWithErrors = 0
128+
local errors = 0
129+
for _, diags in pairs(results) do
130+
filesWithErrors = filesWithErrors + 1
131+
errors = errors + #diags
132+
end
133+
134+
clear_line()
135+
io.write(
136+
('>'):rep(math.ceil(i / max * 20)),
137+
('='):rep(20 - math.ceil(i / max * 20)),
138+
' ',
139+
('0'):rep(#tostring(max) - #tostring(i)),
140+
tostring(i),
141+
'/',
142+
tostring(max)
143+
)
144+
if errors > 0 then
145+
io.write(' [', lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors), ']')
146+
end
147+
io.flush()
148+
end
149+
150+
--- @param uri string
151+
--- @param checkLevel integer
152+
local function apply_check_level(uri, checkLevel)
153+
local config_disables = util.arrayToHash(config.get(uri, 'Lua.diagnostics.disable'))
154+
local config_severities = config.get(uri, 'Lua.diagnostics.severity')
155+
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
156+
serverity = config_severities[name] or serverity
157+
if serverity:sub(-1) == '!' then
158+
serverity = serverity:sub(1, -2)
159+
end
160+
if define.DiagnosticSeverity[serverity] > checkLevel then
161+
config_disables[name] = true
162+
end
163+
end
164+
config.set(uri, 'Lua.diagnostics.disable', util.getTableKeys(config_disables, true))
165+
end
166+
167+
local function downgrade_checks_to_opened(uri)
168+
local diagStatus = config.get(uri, 'Lua.diagnostics.neededFileStatus')
169+
for d, status in pairs(diagStatus) do
170+
if status == 'Any' or status == 'Any!' then
171+
diagStatus[d] = 'Opened!'
172+
end
173+
end
174+
for d, status in pairs(protoDiag.getDefaultStatus()) do
175+
if status == 'Any' or status == 'Any!' then
176+
diagStatus[d] = 'Opened!'
177+
end
178+
end
179+
config.set(uri, 'Lua.diagnostics.neededFileStatus', diagStatus)
180+
end
181+
123182
function export.runCLI()
124183
lang(LOCALE)
125184

@@ -139,18 +198,16 @@ function export.runCLI()
139198
end
140199
rootUri = rootUri:gsub("/$", "")
141200

142-
if CHECKLEVEL then
143-
if not define.DiagnosticSeverity[CHECKLEVEL] then
144-
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
145-
return
146-
end
201+
if CHECKLEVEL and not define.DiagnosticSeverity[CHECKLEVEL] then
202+
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
203+
return
147204
end
148205
local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSeverity.Warning
149206

150207
util.enableCloseFunction()
151208

152209
local lastClock = os.clock()
153-
local results = {}
210+
local results = {} --- @type table<string, table[]>
154211

155212
local function errorhandler(err)
156213
print(err)
@@ -182,31 +239,12 @@ function export.runCLI()
182239

183240
ws.awaitReady(rootUri)
184241

185-
local disables = util.arrayToHash(config.get(rootUri, 'Lua.diagnostics.disable'))
186-
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
187-
serverity = config.get(rootUri, 'Lua.diagnostics.severity')[name] or serverity
188-
if serverity:sub(-1) == '!' then
189-
serverity = serverity:sub(1, -2)
190-
end
191-
if define.DiagnosticSeverity[serverity] > checkLevel then
192-
disables[name] = true
193-
end
194-
end
195-
config.set(rootUri, 'Lua.diagnostics.disable', util.getTableKeys(disables, true))
242+
-- Disable any diagnostics that are above the check level
243+
apply_check_level(rootUri, checkLevel)
196244

197-
-- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
198-
local diagStatus = config.get(rootUri, 'Lua.diagnostics.neededFileStatus')
199-
for d, status in pairs(diagStatus) do
200-
if status == 'Any' or status == 'Any!' then
201-
diagStatus[d] = 'Opened!'
202-
end
203-
end
204-
for d, status in pairs(protoDiag.getDefaultStatus()) do
205-
if status == 'Any' or status == 'Any!' then
206-
diagStatus[d] = 'Opened!'
207-
end
208-
end
209-
config.set(rootUri, 'Lua.diagnostics.neededFileStatus', diagStatus)
245+
-- Downgrade file opened status to Opened for everything to avoid
246+
-- reporting during compilation on files that do not belong to this thread
247+
downgrade_checks_to_opened(rootUri)
210248

211249
local uris = files.getChildFiles(rootUri)
212250
local max = #uris
@@ -215,28 +253,12 @@ function export.runCLI()
215253
if (i % numThreads + 1) == threadId then
216254
files.open(uri)
217255
diag.doDiagnostic(uri, true)
218-
-- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
256+
-- Print regularly but always print the last entry to ensure
257+
-- that logs written to files don't look incomplete.
219258
if not quiet() and (os.clock() - lastClock > 0.2 or i == #uris) then
220259
lastClock = os.clock()
221260
client:update()
222-
local output = '\x0D'
223-
.. ('>'):rep(math.ceil(i / max * 20))
224-
.. ('='):rep(20 - math.ceil(i / max * 20))
225-
.. ' '
226-
.. ('0'):rep(#tostring(max) - #tostring(i))
227-
.. tostring(i) .. '/' .. tostring(max)
228-
io.write(output)
229-
local filesWithErrors = 0
230-
local errors = 0
231-
for _, diags in pairs(results) do
232-
filesWithErrors = filesWithErrors + 1
233-
errors = errors + #diags
234-
end
235-
if errors > 0 then
236-
local errorDetails = ' [' .. lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors) .. ']'
237-
io.write(errorDetails)
238-
end
239-
io.flush()
261+
report_progress(i, max, results)
240262
end
241263
end
242264
end
@@ -257,7 +279,8 @@ function export.runCLI()
257279

258280
if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
259281
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
260-
-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
282+
-- Always write result, even if it's empty to make sure no one
283+
-- accidentally looks at an old output after a successful run.
261284
util.saveFile(outpath, jsonb.beautify(results))
262285
end
263286

0 commit comments

Comments
 (0)