====== Setup a Lua Project ======
This section supposed you've already followed the previous note: [[lua-toolchain|Setup The Lua Toolchain]]
Create a new folder **new_project** that will hold our new project.
$ mkdir new_project
$ cd new_project
Create the src directory
$ mkdir src
Initialize the project with LuaRocks
$ luarocks init --lua-versions "5.3" --no-wrapper-scripts
The project folder has been populated with some files:
$ ls -av
.gitignore
.luarocks
lua_modules
new_project-dev-1.rockspec
src
.gitignore is already setup to ignore **./lua_modules** **./.luarocks** which are not required into a repository.
===== Local and Project Related Modules =====
LuaRocks installs local or project related modules.
* Local Module: are available for all the project. They are installed into **~/.local/lib**
* => Not covered here: we've already installed local modules by the other note: [[lua-toolchain|Setup The Lua Toolchain]]
* Project Related Modules: are available just for the project. They are installed into the project directory at **./lua_modules**
== Setup Project Related Modules ==
The directory for the project related modules is already available at **./lua_modules**, but we still need to tell Lua where is located.
Create a file call **src/setup.lua**. This file tells to Lua that the project related modules are installed in **./lua_modules**
-- src/setup.lua
local version = _VERSION:match("%d+%.%d+")
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
== The main file ==
We are almost done. Create the main file **src/main.lua**
-- src/main.lua
print('Hello!!!')
Run the main script and include the setup file. Note that the **setup** argument doesn't have the **.lua** extention:
$ lua -l src/setup src/main.lua
== Lua Format ==
We already have local installed the luaformat module.
Custom formatting preferences goes into the file .**lua-format** located in the project directory. For example, this are some customization:
# .lua-format
keep_simple_control_block_one_line: false
keep_simple_function_one_line: false
column_table_limit: 1
See all the available settings [[https://github.com/Koihik/LuaFormatter]]
Let's format the src/main.lua
$ lua-format --in-place src/main.lua
== Lua Check ==
Run the following to check all file in src
$ luacheck src
Checking src/main.lua OK
You can try to add a unused variable the the **src/main.lua** as the follow:
-- src/main.lua
print('hello!!!')
local int = 1
Run again luacheck:
$ luacheck src
Checking src/main.lua 1 warning
src/main.lua:3:7: unused variable int
You can configure LuaCheck to be more permissive. LuaCheck settings go to the file **.luacheckrc** in your project directory.
-- .luacheckrc
allow_defined = true
See all the available settings [[https://luacheck.readthedocs.io/en/stable/config.html]]
==== Install Project Related Modules ====
Install project related modules use LuaRock. For example, let's install the **ansicolors** module
luarocks install --tree=lua_modules ansicolors
Edit the **src/main.lua** file as the following:
-- src/main.lua
local colors = require 'ansicolors'
print(colors('%{green}hello'))
Run the main script as following, which include the **setup** file
lua -l src/setup src/main.lua
==== Keep Your Modules in Sync: LuaRocks Spec file ====
When you have a project related module that you want to **keep** in the repository, you have to configure the LuaRocks Spec file, new_project-dev-1.rockspec. In our example, let's add **ansicolors** module into the dependencies table:
package = "new_project"
version = "dev-1"
source = {
url = "*** please add URL for source tarball, zip or repository here ***"
}
description = {
homepage = "*** please enter a project homepage ***",
license = "*** please specify a license ***"
}
dependencies = {
"lua ~> 5.3", -- /!\ it is a list: add a coma here
"ansicolors" -- /!\ the ansicolors module
}
build = {
type = "builtin",
modules = {}
}
Having the LuaRock spec file configured with dependencies, the project related modules will be installed with the following:
$ luarocks install --deps-only new_project-dev-1.rockspec