-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·321 lines (279 loc) · 8.01 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env lua
--| It is an automation system for development and code publishing
--|
--| * clean Remove compile and test stage artifacts
--| * 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 tests
local command = {}
local luaVersions = {"5.1","5.2","5.3","5.4"}
local docdir = "./test"
local testdir = "./test"
local sh do sh = {}
unpack = unpack or table.unpack
local DEBUG = os.getenv('DEBUG')
--$ sh.screensize() : number, number
--| Get the screen rows,columns in chars defaulting to 25, 80
function sh.screensize()
local p,s = io.popen('stty size')
if p then
s = p:read() p:close()
return s:match('(%d+) (%d+)')
end
return 25,80
end
--$ sh.rexec(command: string, ...: string) : table, number
--| Execute command and returns its contents on a table and its numeric exit
function sh.rexec(cmd, ...)
local cs = {...}
if #cs > 0 then
cmd = cmd:format(unpack(cs))
end
local proc = io.popen(cmd..';echo $?')
local lp, lc
local r = {}
if proc then
while true do
lp = lc
lc = proc:read()
rc = 1
if lc then
if lp then r[rc] = lp end
rc=rc+1
else
if lp then
return r, lp
else
return r, 1
end
end
end
else
return r, 1
end
end
--$ sh.exec(command: string, ...: string)
--| Execute command and if it has errors abort Lua script
function sh.exec(cmd, ...)
local cs = {...}
if #cs > 0 then
cmd = cmd:format(unpack(cs))
end
if DEBUG then print("EXEC "..cmd) end
local _,cols = sh.screensize()
local proc = io.popen(cmd..';echo $?')
local prev, curr = nil, nil
if proc then
while true do
prev = curr
curr = proc:read()
if curr then
if prev then
for i = 0, prev:len(), cols do
io.stdout:write('│ '..prev:sub(i, i-6+cols)..'\n')
end
end
else
if prev ~= "0" then
print("\n\n//// Exit Status", prev,"\n\n")
prev = tonumber(prev or 1)
os.exit(prev)
end
return
end
end
else
os.exit(1)
end
end
--$ sh.whereis(pattern: string, ... : string)
--| Find the command in system using the single field `pattern` filled with
--| subsequent string parameters.
--|
--| Ex:
--|
--| ```lua
--| sh.whereis('lua%s','51','5.1') -- matches `lua51` and `lua5.1`
--| sh.whereis('%ssed','','g') -- matches `sed` and `gsed`
--| ```
function sh.whereis(cmd, ...)
local targets = {}
local cs = {...}
if #cs > 0 then
for _,v in ipairs(cs) do
table.insert(targets,cmd:format(v))
end
else
targets[1] = cmd
end
for _,v in ipairs(sh.PATH) do
for _,w in ipairs(targets) do
local f = v .. '/' .. w
local fh = io.open(f,'r')
if fh ~= nil then
io.close(fh)
return f
end
end
end
end
function sh.getpath()
local path = {}
local syspath = os.getenv("PATH")
local P=1;
for p in syspath:gmatch("([^:]+)") do
path[P] = p
P = P+1
end
return path
end
sh.PATH = sh.getpath()
sh.OS = sh.rexec("uname -s")[1];
sh.SED = sh.whereis("%ssed","","g")
sh.PWD = sh.rexec("realpath .")[1];
sh.TERM = os.getenv("TERM") or ""
end
local L do L = {}
if luaver == '5.1' then
L.setfenv = setfenv
function L.load(chunk, envt)
local fn, err = loadstring(chunk, nil)
if not fn then return fn, err end
if envt then setfenv(fn, envt) end
return fn
end
function L.loadfile(filename, envt)
local fn, err = loadfile(filename)
if err then return fn, err end
if envt then return setfenv(fn, envt) end
return fn
end
else
function L.setfenv(fn, envt)
debug.upvaluejoin(fn, 1, function() return envt end, 1)
return fn
end
function L.load(chunk, envt)
return load(chunk, nil, 't', envt)
end
function L.loadfile(f, e)
return loadfile(f, 't', e)
end
end
end
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"),
}
local
function list_modules(pre)
local proc = io.popen('find ./capi ./lua -name "*.lua"','r')
if not proc then return end
local list = {}
local line
while true do
line = proc:read()
if not line then break end
list[line:gsub('^%./',''):gsub('%.lua$',''):gsub('/','.')]=1
end
for k,_ in pairs(list) do print(k) end
end
--
-- Public actions
-- Below functions are used as actions called directly from Ex:
-- ./run docklist
function command.clean()
print("Cleaning project")
sh.exec("rm -rf ./tree ./wax ./out ./lua ./luarocks ./lua_modules ./.luarocks")
sh.exec("rm -rf ./lua ./luarocks ./lua_modules ./.luarocks")
sh.exec("find . -name '*.o' -delete")
end
function command.help()
cmd = ([[
cat $(find %s -name '*.lua') \
| grep '\--\$'|cut -d' ' -f2- 2> /dev/null | fzf
]]):format(docdir)
os.execute(cmd)
end
function compile(config, cfile)
local cmd_obj = '@CC @CFLAGS -I @LUA_INCDIR -c @cfile -o @cpath.@OBJ_EXTENSION'
local cmd_shobj = '@CC @LIBFLAG -o @cpath.@LIB_EXTENSION @cpath.@OBJ_EXTENSION'
os.execute(('mkdir -p %q'):format(config.cpath))
os.execute((cmd_obj):gsub('@([%w_]+)', function(p) return config[p] end))
os.execute((cmd_shobj):gsub('@([%w_]+)', function(p) return config[p] end))
end
function command.test()
local module = arg[2]
if not module then return list_modules() end
for i,luaver in ipairs(luaVersions) do
local p = io.popen(([[luarocks --lua-version %q config]]):format(luaver))
if p then
local config = {}
L.load(p:read('a*'),config)()
if config then
config.variables.lua_version = config.lua_version
config.variables.lua_interpreter = config.lua_interpreter
config = config.variables
config.CFLAGS = '-Wall -Wextra -O2 -fPIC -fdiagnostics-color=always'
end
config.mpath = module:gsub('%.','/')
config.cfile = ('./%s.c'):format(config.mpath)
config.cpath = ('./.out/%s/%s'):format(config.lua_version,config.mpath)
local exists = io.open(config.cfile)
if exists then
exists:close()
compile(config)
end
local cpath = ('./.out/%s/?.%s;;'):format(
config.lua_version,
config.LIB_EXTENSION
)
local st, st1, st2 = os.execute(('LUA_PATH="" LUA_CPATH=%q %s %q'):format(
cpath,
config.lua_interpreter,
config.cfile:gsub('%.c$','.lua')
))
if (st2 or st1 or st2) == 0 then
print(('Lua %s OK'):format(config.lua_version))
end
end
end
end
function command.sparse()
local cmd = table.concat {
[[ for i in $(find . -name "*c") ; do ]],
[[echo sparsing "$i" ; ]],
[[sparse -Wsparse-error ]],
[[ -Wno-declaration-after-statement ]],
[[ -Wsparse-all ]],
[[ -I/usr/include/lua%s ]],
[[ "$i" 2>&1 | ]],
[[ grep -v "unknown attribute\|note: in included file" | ]],
[[ tee /dev/stderr; ]],
[[ done ]]
}
print("\nRunning sparse")
for _,luaver in ipairs(luaVersions) do
print (("\n╒═══╡ Lua %s"):format(luaver))
sh.exec(cmd:format(luaver))
print ("╰╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶")
end
print("\nSparsed OK! :)\n")
end
docker_run_cmd = [[docker run -ti --rm --mount=type=bind,source=%q,target=/devel %q %s]]
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