Skip to content

Commit 665a4e6

Browse files
committed
refactoring
1 parent 23dac6e commit 665a4e6

File tree

3 files changed

+82
-67
lines changed

3 files changed

+82
-67
lines changed

nattlua/cli/init.lua

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,22 @@ function cli.version()
331331
io.write("LuaJIT " .. jit.version .. "\n")
332332
end
333333

334+
local function sorted_pairs(tbl)
335+
local keys = {}
336+
337+
for k in pairs(tbl) do
338+
table.insert(keys, k)
339+
end
340+
341+
table.sort(keys)
342+
local i = 0
343+
return function()
344+
i = i + 1
345+
346+
if keys[i] then return keys[i], tbl[keys[i]] end
347+
end
348+
end
349+
334350
function cli.help(command)
335351
local commands = cli.get_config().commands
336352

@@ -359,7 +375,7 @@ function cli.help(command)
359375
io.write("\n" .. colors.bold("Usage:") .. "\n nattlua <command> [options]\n\n")
360376
io.write(colors.bold("Commands:") .. "\n")
361377

362-
for name, cmd in pairs(commands) do
378+
for name, cmd in sorted_pairs(commands) do
363379
io.write(
364380
" " .. colors.yellow(name) .. "\n " .. (
365381
cmd.description or

nlconfig.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ config.commands = {}
1414

1515
do -- custom commands specific for nattlua
1616
config.commands["build-vscode"] = {
17+
description = "Build and install the NattLua VSCode extension",
1718
cb = function()
1819
os.execute(
1920
"cd vscode_extension && yarn && yarn build && code --install-extension nattlua-0.0.1.vsix"
2021
)
2122
end,
2223
}
2324
config.commands["install"] = {
25+
description = "Install NattLua binary to ~/.local/bin (Linux only)",
2426
cb = function()
2527
-- only linux for now
2628
os.execute("mkdir -p ~/.local/bin")
@@ -29,11 +31,72 @@ do -- custom commands specific for nattlua
2931
end,
3032
}
3133
config.commands["test"] = {
34+
description = "Run NattLua test suite with optional test filter",
3235
cb = function(args)
3336
assert(loadfile("test/run.lua"))(args[1])
37+
os.exit() -- no need to wait for gc to complete
38+
end,
39+
}
40+
config.commands["remove-coverage"] = {
41+
description = "Remove all .coverage files from the project directory",
42+
cb = function(args)
43+
local util = require("examples.util")
44+
local paths = util.GetFilesRecursively("./", "lua.coverage")
45+
46+
for _, path in ipairs(paths) do
47+
os.remove(path)
48+
end
49+
end,
50+
}
51+
config.commands["coverage"] = {
52+
description = "Generate code coverage reports by running tests with instrumentation",
53+
cb = function(args)
54+
local covered = {}
55+
local fs = require("nattlua.other.fs")
56+
local preprocess = require("test.helpers.preprocess")
57+
local coverage = require("test.helpers.coverage")
58+
preprocess.Init(
59+
(
60+
function()
61+
local tbl = {}
62+
63+
for k, v in pairs(package.loaded) do
64+
if k:find("nattlua") then table.insert(tbl, k) end
65+
end
66+
67+
return tbl
68+
end
69+
)()
70+
)
71+
72+
function preprocess.Preprocess(code, name, path, from)
73+
if from == "package" then
74+
if path and path:find("^nattlua/") then
75+
covered[name] = path
76+
return coverage.Preprocess(code, name)
77+
end
78+
end
79+
80+
return code
81+
end
82+
83+
assert(loadfile("test/run.lua"))()
84+
85+
for name, path in pairs(covered) do
86+
local content = coverage.Collect(name)
87+
88+
if content then
89+
local f = assert(io.open(path .. ".coverage", "w"))
90+
f:write(content)
91+
f:close()
92+
else
93+
print("unable to find coverage information for " .. name)
94+
end
95+
end
3496
end,
3597
}
3698
config.commands["build-markdown"] = {
99+
description = "Generate markdown documentation for AI assistants (modes: all, core, minimal, tests)",
37100
cb = function(args)
38101
local mode = args[1]
39102
-- this is just for something like a single file you can paste into gemini 1.5 or chatgpt. gemini's ai studio interface kind of doesn't work with many files, so this is easier.
@@ -130,6 +193,7 @@ end
130193

131194
do -- these override existing commands and should probably be made more generic
132195
config.commands["build"] = {
196+
description = "Build NattLua into a single distributable Lua file with optional modes (fast)",
133197
cb = function(args)
134198
local mode = args[1]
135199
local Compiler = require("nattlua.compiler")

test/run.lua

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,4 @@
11
local path = ...
2-
local preprocess = require("test.helpers.preprocess")
3-
local coverage = require("test.helpers.coverage")
4-
local is_coverage = path == "coverage"
5-
6-
if is_coverage then path = nil end
7-
8-
local covered = {}
9-
10-
if is_coverage then
11-
preprocess.Init(
12-
(
13-
function()
14-
local tbl = {}
15-
16-
for k, v in pairs(package.loaded) do
17-
if k:find("nattlua") then table.insert(tbl, k) end
18-
end
19-
20-
return tbl
21-
end
22-
)()
23-
)
24-
25-
function preprocess.Preprocess(code, name, path, from)
26-
if from == "package" then
27-
if path and path:find("^nattlua/") then
28-
covered[name] = path
29-
return coverage.Preprocess(code, name)
30-
end
31-
end
32-
33-
return code
34-
end
35-
end
36-
372
local assert = _G.assert
383
local loadfile = _G.loadfile
394
local get_time = require("test.helpers.get_time")
@@ -46,17 +11,6 @@ local pcall = _G.pcall
4611
local table = _G.table
4712
require("test.environment")
4813

49-
if path == "remove_coverage" then
50-
local util = require("examples.util")
51-
local paths = util.GetFilesRecursively("./", "lua.coverage")
52-
53-
for _, path in ipairs(paths) do
54-
os.remove(path)
55-
end
56-
57-
return
58-
end
59-
6014
local function find_tests(path)
6115
if path and path:sub(-5) == ".nlua" then return {path} end
6216

@@ -175,22 +129,6 @@ else
175129
end
176130
end
177131

178-
if is_coverage then
179-
local fs = require("nattlua.other.fs")
180-
181-
for name, path in pairs(covered) do
182-
local content = coverage.Collect(name)
183-
184-
if content then
185-
local f = assert(io.open(path .. ".coverage", "w"))
186-
f:write(content)
187-
f:close()
188-
else
189-
print("unable to find coverage information for " .. name)
190-
end
191-
end
192-
end
193-
194132
if not _G.ON_EDITOR_SAVE then profiler.Stop() end
195133

196134
if total > 0 then
@@ -200,9 +138,7 @@ if total > 0 then
200138
format_time(time_taken_before_tests),
201139
" seconds\n"
202140
)
203-
end
204-
205-
--[=[
141+
end--[=[
206142
if ALL_NODES then
207143
208144
for _, nodes in pairs(ALL_NODES) do
@@ -262,4 +198,3 @@ if ALL_NODES then
262198
print(lua)
263199
end
264200
]=]
265-
os.exit() -- no need to wait for gc to complete

0 commit comments

Comments
 (0)