Load Lua-files by relative path

只谈情不闲聊 提交于 2019-11-29 21:22:11

There is a way of deducing the "local path" of a file (more concretely, the string that was used to load the file).

If you are requiring a file inside lib.foo.bar, you might be doing something like this:

require 'lib.foo.bar'

Then you can get the path to the file as the first element (and only) ... variable, when you are outside all functions. In other words:

-- lib/foo/bar.lua
local pathOfThisFile = ... -- pathOfThisFile is now 'lib.foo.bar'

Now, to get the "folder" you need to remove the filename. Simplest way is using match:

local folderOfThisFile = (...):match("(.-)[^%.]+$") -- returns 'lib.foo.'

And there you have it. Now you can prepend that string to other file names and use that to require:

require(folderOfThisFile .. 'baz')     -- require('lib.foo.baz')
require(folderOfThisFile .. 'bazinga') -- require('lib.foo.bazinga')

If you move bar.lua around, folderOfThisFile will get automatically updated.

You can do

package.path = './mylib/?.lua;' .. package.path

Or

local oldreq = require
local require = function(s) return oldreq('mylib.' .. s) end

Then

-- do all the requires
require('mylib-utils')
require('mylib-helpers')
require('mylib-other-stuff')

-- and optionally restore the old require, if you did it the second way
require = oldreq

Under the Conky's Lua environment I've managed to include my common.lua (in the same directory) as require(".common"). Note the leading dot . character.

I'm using the following snippet. It should work both for files loaded with require, and for files called via the command line. Then use requireRel instead of require for those you wish to be loaded with a relative path.

local requireRel
if arg and arg[0] then
    package.path = arg[0]:match("(.-)[^\\/]+$") .. "?.lua;" .. package.path
    requireRel = require
elseif ... then
    local d = (...):match("(.-)[^%.]+$")
    function requireRel(module) return require(d .. module) end
end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!