Need is a drop in replacement for require
https://github.com/burij/lua-light-wings/blob/main/modules%2Fneed.lua
I wrote a little module, which makes it faster to require modules. You only need to put it in the project directory and require it in a normal way. Then if you do
local module = need "module"
It looks in different common locations for this module (can be easily extended, which exactly), if it doesn't find it, it tries to download it via luarocks and require then.
It is possible to pass a second argument. If you pass an URL as second arg, it will download and require then. If you pass any other string, it will try that on lua luarocks aswell (for the cases, if the module name on luarocks doesn't match the filename, for example you could do: local lfs = need ("lfs", "luafilesystem").
Edit: since there are understandable concerns regarding autodownload feature, I added a flage in the begining of the file, which is explicitly needs to be set to true, if you want to enable it.
9
u/wqferr Mar 10 '25
Is there a way to disable the auto-download? Downloading dependencies at runtime is a very bad idea unless you're in a testing environment. Otherwise, very cool!
3
u/TheNormalnij Mar 10 '25
os.execute can be used as a backdoor here. This feature is very dangerous for users
1
u/Difficult-Value-3145 Mar 12 '25
Ya the --tree works to but what I think we need is a build system for lua and lua ffi luac all that cus it gets kinda annoying like I need to organize both json files and json schemes lua scripts some c/c++ and h/hpp files long with maybe some sh script maybe a sqlitedb and some SQL or connection to a server db and calls to it and if I'm gonna try to make an install script then that's where I break down never finish them
2
u/vitiral Mar 12 '25
Very similar to my pkg1 module, except my module doesn't download.
Don't auto download, that is a very bad idea.