diff options
-rw-r--r-- | src/luarocks/fs/lua.lua | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index c169f89c..495327ea 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
@@ -801,20 +801,16 @@ function fs_lua.check_command_permissions(flags) | |||
801 | end | 801 | end |
802 | 802 | ||
803 | --- Check whether a file is a Lua script | 803 | --- Check whether a file is a Lua script |
804 | -- Either a '.lua' extension, or a shebang with lua interpreter | 804 | -- When the file can be succesfully compiled by the configured |
805 | -- @param name (string) filename to check | 805 | -- Lua interpreter, it's considered to be a valid Lua file. |
806 | -- @return true if the filename has a '.lua' extension or the file contains a | 806 | -- @param name filename of file to check |
807 | -- 'lua' interpreter shebang | 807 | -- @return boolean true, if it is a Lua script, false otherwise |
808 | function fs_lua.is_lua(name) | 808 | function fs_lua.is_lua(name) |
809 | local is_lua = name:lower():match("%.lua$") -- .lua extension, so it's a Lua file | 809 | name = name:gsub([[%\]],"/") -- normalize on fw slash to prevent escaping issues |
810 | if not is_lua then -- check for shebang | 810 | local lua = fs.Q(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)) -- get lua interpreter configured |
811 | local file = io.open(name) | 811 | -- execute on configured interpreter, might not be the same as the interpreter LR is run on |
812 | if file then | 812 | local result = fs.execute_string(lua..[[ -e "if loadfile(']]..name..[[') then os.exit() else os.exit(1) end"]]) |
813 | is_lua = file:read():match("#!.*lua.*") -- no extension, but a shebang, so it's a Lua file | 813 | return (result == true) |
814 | file:close() | ||
815 | end | ||
816 | end | ||
817 | return not (is_lua == nil) | ||
818 | end | 814 | end |
819 | 815 | ||
820 | return fs_lua | 816 | return fs_lua |