Skip to content

Instantly share code, notes, and snippets.

@ochaton
Last active January 12, 2023 09:41
Show Gist options
  • Save ochaton/b99dc6ab6f956222aeb50e9b79a32b24 to your computer and use it in GitHub Desktop.
Save ochaton/b99dc6ab6f956222aeb50e9b79a32b24 to your computer and use it in GitHub Desktop.
--[[
This is handy module to set package.path and package.cpath
]]
local fio = require 'fio'
local soext = (jit.os == "OSX" and "dylib" or "so")
local ROCKS_LIB_PATH = '.rocks/lib/tarantool'
local ROCKS_LUA_PATH = '.rocks/share/tarantool'
local LIB_TEMPLATES = { '?.'..soext, ROCKS_LIB_PATH .. '/?.'..soext }
local LUA_TEMPLATES = { '?.lua', '?/init.lua', ROCKS_LUA_PATH .. '/?.lua', ROCKS_LUA_PATH .. '/?/init.lua' }
local function uniq(list)
local kv = {}
local r = {}
for _, item in ipairs(list) do
if not kv[item] then
table.insert(r, item)
kv[item]=true
end
end
return r
end
return function(path)
if not path then
local file = fio.abspath(debug.getinfo(2, "S").source:sub(2))
local link = fio.readlink(file)
if not link then
path = fio.dirname(file)
elseif link:sub(1, 1) == '/' then
-- link to absolute path
path = fio.dirname(fio.abspath(link))
else
-- link to relative path
path = fio.dirname(fio.abspath(fio.pathjoin(fio.dirname(file), link)))
end
end
local root = fio.abspath(path)
for _, cfg in ipairs { { 'path', LUA_TEMPLATES }, { 'cpath', LIB_TEMPLATES } } do
local paths = {}
for _, template in ipairs(cfg[2]) do
table.insert(paths, fio.pathjoin(root, template))
end
for _, p in ipairs(package[cfg[1]]:gsub(';$', ''):split(';')) do
table.insert(paths, p)
end
package[cfg[1]] = table.concat(uniq(paths), ';')
end
return root
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment