tl;dr: I want to create lua packages with a custom directory name pattern, having problem with search paths.
The problem
I've got an application that I'm wanted to allow the user to write plugins for, following a similar model to Lightroom:
- A default set of plugins are stored in
<app data>/plugins/<name>.myplugin
<name>.myplugin
is a directory bundle which may contain a set of scripts, binaries or other resources- A plugin may export many different functions to the app, via different scripts
- The names of the exported functions are listed in an
info.lua
file that is read by the app
The problem I'm grappling with is how best to wrap the plugins as packages (modules + submodules) or regular scripts. I envisage that a plugin might include 3rd party modules:
Foo.myplugin/
info.lua - returns a table with plugin name, version info, list of exported functions, etc
Foo.lua - defines the main functions exported by this plugin, which calls other scripts:
UsefulFunctions.lua - used by Foo.lua
3rdparty/3rdparty.lua - 3rd party module
If I set the package search path, package.path
to include
<appdata>/?.myplugin/?.lua
then I can load the package with Foo=require 'Foo'
. However, I can't work out how to get submodules loaded. If Foo.lua
calls UsefulFunctions=require 'UsefulFunctions'
then this load fails because lua's search path tries to look for UsefulFunctions.myplugin/UsefulFunctions.lua
. I can't load it with require 'Foo.UsefulFunctions'
either, for similar reasons.
Some options:
- One workaround would be to explicitly add each plugin's path to the package path, but this would cause problems if two plugins each contained a submodule of the same name.
- Another option is to write the plugins to use regular lua scripts rather than providing modules, but this still means that the search paths have to be set up inside each plugin.
- A fallback option could be to lose the
.myplugin
suffix which would simplify the package search path. - Patch Lua to explicitly support this type of search path
Is there any way of providing the functionality I need?
I'm currently on Lua 5.1. I know 5.2 has more control over package search paths, but I don't think I have the option of updating to it at the moment. I'm also using luabind, though I don't think it is relevant to this.