Repository navigation
Expand file tree
/
Copy pathrun
More file actions
97 lines (86 loc) · 2.9 KB
/
Copy pathrun
File metadata and controls
97 lines (86 loc) · 2.9 KB
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
-- Run scripts in workspace.
--
-- Usage:
-- - **show scripts**: <lua> run
-- - **run a script**: <lua> run <script_name> [<args>]
local cwd = debug.getinfo(1, 'S').source:match('^@?(.*)[/\\]') or '.'
local sep = package.config:sub(1, 1)
local win = sep == "\\"
local function ujoin(...)
local n = select('#', ...)
local u = {...}
for i = 1, n do if not u[i] then
u[i] = ''
end end
return table.concat(u, sep, 1, n)
end
local function list_scripts(dir)
local scripts = {}
local command = (win and 'dir /b "%s"' or 'ls "%s"'):format(dir)
local phandle = io.popen(command, 'r')
if not phandle then return scripts end
for name in (phandle:read('*a') or ''):gmatch('[^\r\n]+') do
if name:match('(.+)%.lua$') then
table.insert(scripts, name:sub(1, -5))
end
end
phandle:close()
table.sort(scripts)
return scripts
end
local function shell_quote(value)
value = tostring(value)
if win then return '"' .. value:gsub('"', '""') .. '"' end
return "'" .. value:gsub("'", "'\\''") .. "'"
end
local function find_lua_command()
local LUA = os.getenv('LUA')
if LUA and LUA ~= '' then return LUA end
-- Lua stores the interpreter and its options at negative arg indexes.
-- The exact index differs between Lua versions and invocation forms.
for i = -1, -10, -1 do
local value = arg[i]
if type(value) == 'string' then
local name = value:gsub('^.*[/\\]', ''):lower()
if name:match('^lua') or name:match('^luajit') then
return value
end
end
end
if pcall(require, 'jit') then return 'luajit' end
local major, minor = (_VERSION or ''):match('Lua%s+(%d+)%.(%d+)')
if major and minor then return 'lua' .. major .. minor end
return 'lua'
end
if #arg == 0 then
local scripts = list_scripts(ujoin(cwd, "scripts"))
if #scripts == 0 then os.exit(1) end
local cwd_short = cwd:match((("&([^&]+)$"):gsub("&", sep))) or 'workspace'
print("Scripts available in "..cwd_short.." via `lua run`:")
for _, v in ipairs(scripts) do print("- "..v) end
os.exit(0)
end
local scriptname = arg[1] or ''
local scriptpath = ujoin(cwd, "scripts", scriptname..".lua")
local scriptFile = io.open(scriptpath, 'r')
if not scriptFile then
io.write(io.stderr, "lua error Missing script: \""..scriptname.."\"\n")
io.write(io.stderr, "lua error\n")
io.write(io.stderr, "lua error To see a list of scripts, run:\n")
io.write(io.stderr, "lua error lua run\n")
os.exit(1)
end
scriptFile:close()
local command = (win and 'call ' or '')
.. shell_quote(find_lua_command()) .. ' ' .. shell_quote(scriptpath)
for i = 2, #arg do command = command .. ' ' .. shell_quote(arg[i]) end
local a, b, c = os.execute(command)
local code
if type(a) == 'number' then
code = a
elseif a then
code = c or 0
else
code = c or b or 1
end
os.exit(code)