Lots of people want LuaRocks to function like npm
. While LuaRocks provides
some playful ideas for supporting per-project workflows,
I'm taking a stab with wrapper scripts.
sh
luarocks
Lua looks for modules in according to the environment variables LUA_PATH
and
LUA_CPATH
. All I'm doing is setting that with a couple shell scripts. I'm sure
adapting this for Windows is simple, but as I never use PowerShell or batch
files for anything, this project is restricted to Linux. In my case, I'm using
Windows Subsystem for Linux.
This is where the supplied wrappers will instruct Lua to search for modules.
LUA_PATH
<project_path>/lua/?.lua
<project_path>/lua/?/init.lua
<project_path>/lua_modules/share/lua/5.1/?.lua
<project_path>/lua_modules/share/lua/5.1/?/init.lua
$LUA_PATH
LUA_CPATH
<project_path>/lua_modules/lib/lua/5.1/?.so;
$LUA_CPATH
For more information on these environment variables, check out the Lua manual - specifically the sections explaining require
, package.path
, and package.cpath
.
The meat and potatoes of this project are the wrapper scripts bin/env
and
bin/luarocks
, which automatically set LUA_PATH
and LUA_CPATH
to use
the project-supplied modules before system modules.
This script automatically prepends project directories to the environment
variables LUA_PATH
and LUA_CPATH
before executing all arguments after as a
separate command. For example: bin/env lua51 -e "print(package.path)"
will
print the project's LUA_PATH
value.
You could also run busted
and other binaries that rely on LUA_PATH
and
LUA_CPATH
within the environment using bin/env busted
. Obviously, if you
have installed binaries through LuaRocks in the project, you will need to
reference its location in the lua_modules
folder.
This script calls bin/env
automatically and calls
/usr/bin/env luarocks --tree "<project_path>/lua_modules>"
.
For example, if you wanted to download all dependencies, use
bin/luarocks build --only-deps
.
This project comes with a simple nil-dev-1.rockspec
file. Obviously, the
filename and contents should be updated to reflect the project and its
dependencies. Unfortunately, LuaRocks does not support functionality similar
to npm install <package_name>
with the --save
or --save-dev
flags,
so the dependencies need to be
added by hand.