Skip to content

Commit

Permalink
refactor(cli/check): outline some functions and add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Jan 24, 2025
1 parent 9616662 commit dec6bcd
Showing 1 changed file with 74 additions and 51 deletions.
125 changes: 74 additions & 51 deletions script/cli/check_worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,65 @@ local function clear_line()
io.write('\x0D', (' '):rep(80), '\x0D')
end

--- @param i integer
--- @param max integer
--- @param results table<string, table[]>
local function report_progress(i, max, results)
local filesWithErrors = 0
local errors = 0
for _, diags in pairs(results) do
filesWithErrors = filesWithErrors + 1
errors = errors + #diags
end

clear_line()
io.write(
('>'):rep(math.ceil(i / max * 20)),
('='):rep(20 - math.ceil(i / max * 20)),
' ',
('0'):rep(#tostring(max) - #tostring(i)),
tostring(i),
'/',
tostring(max)
)
if errors > 0 then
io.write(' [', lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors), ']')
end
io.flush()
end

--- @param uri string
--- @param checkLevel integer
local function apply_check_level(uri, checkLevel)
local config_disables = util.arrayToHash(config.get(uri, 'Lua.diagnostics.disable'))
local config_severities = config.get(uri, 'Lua.diagnostics.severity')
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
serverity = config_severities[name] or serverity
if serverity:sub(-1) == '!' then
serverity = serverity:sub(1, -2)
end
if define.DiagnosticSeverity[serverity] > checkLevel then
config_disables[name] = true
end
end
config.set(uri, 'Lua.diagnostics.disable', util.getTableKeys(config_disables, true))
end

local function downgrade_checks_to_opened(uri)
local diagStatus = config.get(uri, 'Lua.diagnostics.neededFileStatus')
for d, status in pairs(diagStatus) do
if status == 'Any' or status == 'Any!' then
diagStatus[d] = 'Opened!'
end
end
for d, status in pairs(protoDiag.getDefaultStatus()) do
if status == 'Any' or status == 'Any!' then
diagStatus[d] = 'Opened!'
end
end
config.set(uri, 'Lua.diagnostics.neededFileStatus', diagStatus)
end

function export.runCLI()
lang(LOCALE)

Expand All @@ -121,18 +180,16 @@ function export.runCLI()
end
rootUri = rootUri:gsub("/$", "")

if CHECKLEVEL then
if not define.DiagnosticSeverity[CHECKLEVEL] then
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
return
end
if CHECKLEVEL and not define.DiagnosticSeverity[CHECKLEVEL] then
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
return
end
local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSeverity.Warning

util.enableCloseFunction()

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

local function errorhandler(err)
print(err)
Expand Down Expand Up @@ -164,31 +221,12 @@ function export.runCLI()

ws.awaitReady(rootUri)

local disables = util.arrayToHash(config.get(rootUri, 'Lua.diagnostics.disable'))
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
serverity = config.get(rootUri, 'Lua.diagnostics.severity')[name] or serverity
if serverity:sub(-1) == '!' then
serverity = serverity:sub(1, -2)
end
if define.DiagnosticSeverity[serverity] > checkLevel then
disables[name] = true
end
end
config.set(rootUri, 'Lua.diagnostics.disable', util.getTableKeys(disables, true))
-- Disable any diagnostics that are above the check level
apply_check_level(rootUri, checkLevel)

-- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
local diagStatus = config.get(rootUri, 'Lua.diagnostics.neededFileStatus')
for d, status in pairs(diagStatus) do
if status == 'Any' or status == 'Any!' then
diagStatus[d] = 'Opened!'
end
end
for d, status in pairs(protoDiag.getDefaultStatus()) do
if status == 'Any' or status == 'Any!' then
diagStatus[d] = 'Opened!'
end
end
config.set(rootUri, 'Lua.diagnostics.neededFileStatus', diagStatus)
-- Downgrade file opened status to Opened for everything to avoid
-- reporting during compilation on files that do not belong to this thread
downgrade_checks_to_opened(rootUri)

local uris = files.getChildFiles(rootUri)
local max = #uris
Expand All @@ -197,28 +235,12 @@ function export.runCLI()
if (i % numThreads + 1) == threadId then
files.open(uri)
diag.doDiagnostic(uri, true)
-- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
if (os.clock() - lastClock > 0.2 or i == #uris) and not QUIET then
-- Print regularly but always print the last entry to ensure
-- that logs written to files don't look incomplete.
if not QUIET and (os.clock() - lastClock > 0.2 or i == max) then
lastClock = os.clock()
client:update()
local output = '\x0D'
.. ('>'):rep(math.ceil(i / max * 20))
.. ('='):rep(20 - math.ceil(i / max * 20))
.. ' '
.. ('0'):rep(#tostring(max) - #tostring(i))
.. tostring(i) .. '/' .. tostring(max)
io.write(output)
local filesWithErrors = 0
local errors = 0
for _, diags in pairs(results) do
filesWithErrors = filesWithErrors + 1
errors = errors + #diags
end
if errors > 0 then
local errorDetails = ' [' .. lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors) .. ']'
io.write(errorDetails)
end
io.flush()
report_progress(i, max, results)
end
end
end
Expand All @@ -239,7 +261,8 @@ function export.runCLI()

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

Expand Down

0 comments on commit dec6bcd

Please sign in to comment.