CLAF is a set of wrappers and libraries for the standard API which makes the Garry's Mod addons development easier.
See wiki for the documentation.
CLAF provides functional programming, aliases and shortcuts for the standard API to make the addon programming easier.
Quickly modify tables with functional programming features:
-- Filtering numbers in traditional way...
local numbers = { 1, 2, 3, 4, 5 }
for k, number in pairs(numbers) do
if number % 2 != 0 then
numbers[k] = nil
end
end
-- ...and in functional way
local numbers = { 1, 2, 3, 4, 5 }
numbers = Filter(numbers, function(x) return IsOdd(x) end)
local inp = {
{ { x = 1 }, { x = 2 } },
{ { x = 3 }, { x = 4 } },
}
local result = Pipe(inp)
:Flatten()
:Map('x')
:Sum()
assert(result == 10)
Run code that can cause errors in Try()
:
Try(function()
-- some error-potential code
end,
-- error handler
function(errorMessage)
print('something went wrong: '..errorMessage)
end)
LiquidType = Enum { 'WATER', 'LAVA', 'OIL' }
bottle = { liquid = LiquidType.WATER }
Settings = Flags { 'SHOW_WELCOME', 'SHOW_HELP', 'SHOW_HINTS' }
-- Combining flags
userSettings = bit.bor(Settings.SHOW_WELCOME, Settings.SHOW_HELP)
-- Reading flags
welcome = bit.band(userSettings, Settings.SHOW_WELCOME) -- true
help = bit.band(userSettings, Settings.SHOW_HELP) -- true
hints = bit.band(userSettings, Settings.SHOW_HINTS) -- false
Easily insert variables' values into strings using the string interpolation:
local name = 'John'
str = fmt'My name is {name}'
-- 'My name is John'
- Subscribe to CLAF addon in Steam Workshop.
- Add the following line to the beginning of the source files where CLAF is used:
include 'claf.lua'
Add dependency of CLAF Steam Workshop addon on your addon. Alternatively, you can copy the library files into your addon, but don't forget to include a license notice (see LICENSE).
-
Install WSL or MinGW, because running Lua natively on Windows is sort of a nightmare.
-
Install Lua.
For WSL (assuming you have Ubuntu chosen as your WSL distribution):
sudo apt install lua5.2
For MinGW:
pacman -S mingw-w64-x86_64-lua
-
Clone the repository.
-
Install LuaRocks.
For WSL (assuming you have Ubuntu chosen as your WSL distribution):
sudo apt install luarocks
For MinGW:
pacman -S mingw-w64-lua-luarocks
-
Run
lua maint.lua prepare-dev
in the root directory of the project. This will install development dependencies such as tools for formatting, testing and linting the code.
maint.lua
is a script that helps with the development of the library. It can be used to run tests, format the code, etc.
It can be invoked by running lua maint.lua <task>
in the root directory of the project, where <task>
is one of the following:
prepare-dev
- installs development dependencies.prepare-ci
- used in CI; installs development dependencies globally with sudo. This makes the tools available system-wide.test
- runs tests.generate-coverage-data
- runsbusted -c
which geneates coverage data inluacov.stats.out
file. Use thecover
task to generate a humar-readable HTML report.cover
- runs tests and generates a coverage report in HTML format.check-coverage-percentage
- checks if the coverage percentage is above the threshold. The threshold is defined intools/check-coverage-percentage.sh
.lint
- scans for potential errors in the code.format
- reformats the code.format-check
- checks if the code is formatted according to a standard.ci
- runs all the checks that are run on the CI server. If the command fails, it means that the code is not ready to be committed.