-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun
executable file
·305 lines (265 loc) · 8.05 KB
/
run
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env lua
--| It is an automation system for development and code publishing
--|
--| * clean Remove compile and test stage artifacts
--| * dockbuild Build Docker instance for tests
--| * docklist List available Docker confs
--| * dockrun Run command on Docker test instance
--| * docktest Run Lua deva files through the Docker test instance
--| * howl Update documentation using howl
--| * help Retrieve the list of documented items
--| * install Install the rockspec for all Lua versions
--| * remove Uninstall the rockspec for all Lua versions
--| * sparse Run semantic parser for C
--| * test Compile, and run Lua deva files
local command = {}
local sh = require 'etc.run.sh'
local util = require 'etc.run.util'
local config = require 'etc.run.config'
local luaVersions = {"5.1","5.2","5.3","5.4"}
local mandir = "./man"
local testdir = "./test"
local luabin = {
["5.1"] = sh.whereis("lua%s","5.1","51"),
["5.2"] = sh.whereis("lua%s","5.2","52"),
["5.3"] = sh.whereis("lua%s","5.3","53"),
["5.4"] = sh.whereis("lua%s","5.4","54"),
}
function docker_names()
local files = sh.rexec("ls etc/docker/*.dockerfile")
local names = {}
for i,dfile in ipairs(files) do
names[(dfile:gsub("^.*/",""):gsub(".dockerfile$",""))] = 1
end
return names
end
function docker_image(name)
name = name or "luawax_debian"
if sh.OS ~= "Linux" then
print("This command requires a Linux machine host")
os.exit(1)
end
if not docker_names()[name] then
command.docklist()
os.exit(1)
end
local name = sh.rexec("docker images | grep %q | awk '{print $1}'",name)[1]
if not name then
print(("Try build first with:\n\n\t./run dockbuild %s\n"):format(arg[2]))
os.exit(1)
end
end
--
-- Public actions
-- Below functions are used as actions called directly from Ex:
-- ./run docklist
function command.clean()
arg[1] = 'clean'
require 'etc.run.make'
os.exit(0)
end
function command.help()
cmd = ([[
{ cat $(find %s -name '*.lua') | grep '^\s*\--\$' | cut -d\$ -f2- |cut -d' ' -f2-;
cat $(find %s -name '*.md') | grep '######'|cut -d' ' -f2- | tr -d '`';
} 2> /dev/null | fzf
]]):format(testdir,mandir)
os.execute(cmd)
end
function command.howl()
print 'Generating wiki...'
os.execute 'howl --from ./test --from ./doc --fmt wiki ../wax.wiki'
print 'Generating vim help...'
os.execute 'howl --from ./test --from ./doc --fmt vim ~/.config/nvim/doc/wax'
print 'Updating vim help tags...'
os.execute 'nvim --cmd ":helptags ~/.config/nvim/doc\n" --cmd ":q"'
print 'Done.'
end
do
local test_build, test_lua
function command.test(mod)
local mods = config.modules
-- Single module test
if mod then
if not mods[mod] then
util.die('Unavailable module: %s', mod)
end
for _, luaver in ipairs(luaVersions) do
test_build(luaver, mod)
test_lua(luaver, mod)
end
-- All modules test
else
for _, luaver in ipairs(luaVersions) do
test_build(luaver)
test_lua(luaver)
end
end
end
function test_build(luaver, modules)
sh.printhead("Compiling for Lua "..luaver)
if (module) then
sh.exec("SINGLE_PACKAGE=%q LUA_VERSION=%q WAXTFLAG=1 luarocks --tree ./tree --lua-version %s make %s",module, luaver, luaver, config.rockspec)
else
sh.exec("LUA_VERSION=%q WAXTFLAG=1 luarocks --tree ./tree --lua-version %s make %s",luaver,luaver,config.rockspec)
end
sh.printfoot()
end
function test_lua(luaver,module)
local lbin = luabin[luaver]
local lpath = ("./tree/share/lua/%s/?.lua;./tree/share/lua/%s/?/init.lua"):format(luaver,luaver)
local cpath = ("./tree/lib/lua/%s/?.so" ):format(luaver)
local cmd = ('find %q -name "*.lua" 2>/dev/null'):format(testdir)
if module then
cmd = ("%s| grep '^%s/%s$'"):format(cmd, testdir, (module:gsub('%.', '/')..'.lua'))
end
local p = io.popen(cmd:format(testdir, module),"r")
if p == nil then return end
sh.printhead("Testing with Lua "..luaver)
testnum = 0
local file = p:read()
while file do
testnum = testnum + 1
sh.printbody((
file:gsub('./test/','')
:gsub('%.lua$', '')
:gsub('/','.')
))
sh.exec(
[[ TZ=UTC+0 %s -e 'package.path=%q package.cpath=%q' %q ]],
lbin, lpath, cpath, file
)
file = p:read()
end
sh.printbody ''
sh.printfoot (('%d tests'):format(testnum))
end
end
function command.sparse()
print [[
Sparse, a semantic parser and static analyzer for C.
For info see: https://sparse.docs.kernel.org
]]
local conf = {
std = 'gnu89',
file = 'src/*.c'
}
if arg[2] == 'help' then
print [[
help print this help
--file=X sparse only X (default *)
--std=X use C standard X (default gnu89)
]]
end
for i=2, 4, 1 do
if arg[i] then
local c,v = arg[i]:match('%-%-(%w+)=(.+)')
if c and v then conf[c]=v end
end
end
local cmd = table.concat {
(' for i in %s; do '):format( conf.file ),
[[ echo sparsing "$i" ; ]],
[[ sparse -Wsparse-error ]],
('-std=%s'):format( conf.std ),
[[ -Wno-declaration-after-statement ]],
[[ -Wsparse-all ]],
[[ -I/usr/include/lua%s ]],
[[ -I./src ]],
[[ -I./src/ext ]],
[[ -I./src/lib ]],
[[ "$i" 2>&1 | ]],
[[ grep -v "unknown attribute\|note: in included file" | ]],
[[ tee /dev/stderr; ]],
[[ done ]]
}
print("\nRunning sparse")
for _,luaver in ipairs(luaVersions) do
sh.printhead (("Sparse for Lua %s"):format(luaver))
sh.exec(cmd:format(luaver))
sh.printfoot ("")
end
print("\nSparsed OK! :)\n")
end
function command.docklist()
print("\nAvailable docker confs:\n")
for name,_ in pairs(docker_names()) do print(name) end
end
function command.dockbuild()
local img = docker_image(arg[2])
if img then
sh.rexec([[docker rmi "%s:latest"]],img)
sh.exec([[docker build -t "%s:latest" -f "etc/docker/%s.dockerfile" .]], imgname, imgname)
else
command.docklist()
end
end
docker_run_cmd = [[docker run -ti --rm --mount=type=bind,source=%q,target=/devel %q %s]]
function command.dockrun()
local img = docker_image(arg[2])
local runcmd = docker_run_cmd:format(sh.PWD, img, "bash")
os.execute(runcmd)
end
function command.docktest()
local strgetimg = "docker images | grep %q | awk '{print $1}'"
local strcmd = [[bash -c "cd /devel && TERM=%q ./run test || exit 1; ./run clean"]]
local strnotimg = "Try build first with:\n\n\t./run dockbuild %s\n"
local imgname = arg[2] or "luawax_debian"
if imgname and docker_names()[imgname] then
imgname = sh.rexec(strgetimg, imgname)[1]
if not imgname then
print(strnotimg:format(arg[2]))
os.exit(1)
end
local cmd = strcmd:format(sh.TERM) -- run inside docker
sh.exec(docker_run_cmd:format(sh.PWD, imgname, cmd))
end
end
function command.install()
local cmd = 'luarocks --lua-version %q make %q'
for k,_ in pairs(luabin) do
os.execute(cmd:format(k, config.rockspec))
end
end
function command.remove(rockspec)
local cmd = 'luarocks --lua-version %q remove --force %q'
for k,_ in pairs(luabin) do
os.execute(cmd:format(k, rockspec or config.rockspec))
end
end
function command.ref()
local p = io.popen[[grep -n '^[-/]\{2\}\$' -R | fzf --delimiter='[^\$]\s*\$' --with-nth=2]]
local option = p:read()
p:close()
if option then
local file, target = option:match('^([^:]+):(%d+)')
local f = io.open(file)
local l, line = 0
target = tonumber(target)
while true do
line = f:read()
if not line then return end
l = l+1
if l >= target then
local content = line:match('^%-%-[%$|](.+)')
if content or line:match('^%s*$') then
print(content)
else
return
end
end
end
end
end
if command[arg[1]] then
command[arg[1]]( (table.unpack or unpack)(arg,2) )
else
local f = io.open(arg[0])
repeat
line = f:read()
if line and line:find('^%-%-[|{}]%s?') == 1 then
print(line:sub(5))
end
until not line
f:close()
end