lua-luarocks在Project中的应用
Install luarocks
mission: use luarocks
to manage third libs in lua project.
Guarantee the
luarocks
have been installed in your mac. if not , using the follow command.
1
$ brew install luarocks
Generally, the libs installed using luarocks
will locate at global position. But in real situation, we’d like install lib is each Project locally not in global. But in a real-world scenario, we prefer to install libs locally for each project rather than globally.
Exercise
I will install all packages inside a
lua_modules
folder in the root of a project, using the--tree
option of the LuaRocks CLI. As an example, installing the inspect package.
-
cd
the root of your Project. -
excute
luarocks install --tree=lua_modules inspect
tree=lua_modules
means the libs will be installed at that folder. -
create
setup.lua
andtest.lua
files.-
we need make configrations for
pageage
insetup.lua
file1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18-- setup.lua
local version = _VERSION:match("%d+%.%d+")
print('version',version) -- version 5.4
package.path = 'lua_modules/share/lua/'
.. version ..
'/?.lua;lua_modules/share/lua/'
.. version ..
'/?/init.lua;'
.. package.path
package.cpath = 'lua_modules/lib/lua/'
.. version ..
'/?.so;' .. package.cpath
print('package.path == ',package.path) -- package.path == lua_modules/share/lua/5.4/?.lua;lua_modules/share/lua/5.4/?/init.lua;/usr/local/share/lua/5.4/?.lua;/usr/local/share/lua/5.4/?/init.lua;/usr/local/lib/lua/5.4/?.lua;/usr/local/lib/lua/5.4/?/init.lua;./?.lua;./?/init.lua
print('package.cpath == ',package.cpath) -- package.cpath == lua_modules/lib/lua/5.4/?.so;/usr/local/lib/lua/5.4/?.so;/usr/local/lib/lua/5.4/loadall.so;./?.so -
test the lib in
test.lua
file1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16--[[
NOTE:
make sure setup.lua must be loaded. or `require "inspect"` cause error!
]]
require("setup")
local inspect = require "inspect"
local a = {1, 2}
print(inspect(a))
--[[
result:
{ 1, 2 }
]]
-
-
the demo dir as follows:
If you install libs locally, there is nothing when you excute
luarocks list
;
But if you install globally, there are the libs you installed before when excute that command.But if you install globally, the libraries you installed before will be available when executing that command.