Skip to content

Commit 7de8a01

Browse files
galjonsfigurmarcelstoer
authored andcommittedJun 28, 2019
Add luacheck config and configuration for Travis CI (#2790)
* Move luacheck conf and fix Travis for all possible filenames * Add lua script to help with luacheck config * Add xargs approach for current luac.cross file checking * Enable luacheck but do not break build
1 parent 93b1a2d commit 7de8a01

6 files changed

+1164
-3
lines changed
 

‎.travis.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ script:
3030
- if [ "$OS" = "windows" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/ci-build-windows-ms.sh; fi
3131
- if [ "$OS" = "linux" -a "$TRAVIS_PULL_REQUEST" != "false" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/pr-build.sh; fi
3232
- cd "$TRAVIS_BUILD_DIR"
33-
- LUA_FILES=`find lua_modules lua_examples -iname "*.lua"`
34-
- echo checking $LUA_FILES
35-
- $LUACC -p $LUA_FILES
33+
- echo "checking:"
34+
- find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 echo
35+
- find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 $LUACC -p
36+
- if [ "$OS" = "linux" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/run-luacheck.sh || true ; fi
37+

‎CONTRIBUTING.md

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Use the platform and tools you feel most comfortable with. There are no constrai
2626
- [Full-fledged Linux environment](http://www.esp8266.com/wiki/doku.php?id=toolchain#how_to_setup_a_vm_to_host_your_toolchain), either physical or virtual.
2727
- [Docker image](https://hub.docker.com/r/marcelstoer/nodemcu-build/) which allows running the build inside the container as if you were running a build script on your local machine.
2828

29+
## Writing Lua Code
30+
A great resource about writing Lua for NodeMCU can be found in [Lua Developer FAQ](https://nodemcu.readthedocs.io/en/latest/lua-developer-faq/) - make sure to read it! When you're writing your Lua code and it's not working as it should you can test it with `luacheck` tool that can help you find various types of bugs. To install it you have to install [luarocks](https://luarocks.org/) and use command `sudo luarocks install luacheck` to install the tool. Now you're ready to go! By using this command (assuming you're in `nodemcu-firmware` directory):
31+
32+
`luacheck --config tools/luacheck_config.lua <your file to check>`
33+
34+
you can look for bugs and problems within the code!
35+
2936
## Writing Documentation
3037
The NodeMCU documentation is maintained within the same repository as the code. The primary reason is to keep the two in sync more easily. It's thus trivial for the NodeMCU team to verify that a PR includes the necessary documentation. Furthermore, the documentation is merged automatically with the code if it moves from branch X to Y.
3138

‎tools/luacheck_config.lua

+883
Large diffs are not rendered by default.

‎tools/luacheck_config_helper.lua

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/lua
2+
3+
---
4+
-- Script to extract names and the functions themselves from NodeMCU modules and
5+
-- to help creating luacheck configuration.
6+
-- Usage: <in modules catalog> ../../tools/luacheck_config_helper.lua *.c (or filename for single module)
7+
8+
local M = {}
9+
10+
-- Recursive object dumper, for debugging.
11+
-- (c) 2010 David Manura, MIT License.
12+
-- From: https://github.com/davidm/lua-inspect/blob/master/lib/luainspect/dump.lua
13+
local ignore_keys_ = {lineinfo = true}
14+
local norecurse_keys_ = {parent = true, ast = true}
15+
local function dumpstring_key_(k, isseen, newindent)
16+
local ks = type(k) == 'string' and k:match'^[%a_][%w_]*$' and k or
17+
'[' .. M.dumpstring(k, isseen, newindent) .. ']'
18+
return ks
19+
end
20+
21+
local function sort_keys_(a, b)
22+
if type(a) == 'number' and type(b) == 'number' then
23+
return a < b
24+
elseif type(a) == 'number' then
25+
return false
26+
elseif type(b) == 'number' then
27+
return true
28+
elseif type(a) == 'string' and type(b) == 'string' then
29+
return a < b
30+
else
31+
return tostring(a) < tostring(b) -- arbitrary
32+
end
33+
end
34+
35+
function M.dumpstring(o, isseen, indent, key)
36+
isseen = isseen or {}
37+
indent = indent or ''
38+
39+
if type(o) == 'table' then
40+
if isseen[o] or norecurse_keys_[key] then
41+
return (type(o.tag) == 'string' and '`' .. o.tag .. ':' or '') .. tostring(o)
42+
else isseen[o] = true end -- avoid recursion
43+
44+
local used = {}
45+
46+
local tag = o.tag
47+
local s = '{'
48+
if type(o.tag) == 'string' then
49+
s = '`' .. tag .. s; used['tag'] = true
50+
end
51+
local newindent = indent .. ' '
52+
53+
local ks = {}; for k in pairs(o) do ks[#ks+1] = k end
54+
table.sort(ks, sort_keys_)
55+
56+
local forcenummultiline
57+
for k in pairs(o) do
58+
if type(k) == 'number' and type(o[k]) == 'table' then forcenummultiline = true end
59+
end
60+
61+
-- inline elements
62+
for _,k in ipairs(ks) do
63+
if ignore_keys_[k] then used[k] = true
64+
elseif (type(k) ~= 'number' or not forcenummultiline) and
65+
type(k) ~= 'table' and (type(o[k]) ~= 'table' or norecurse_keys_[k])
66+
then
67+
s = s .. dumpstring_key_(k, isseen, newindent) .. ' = ' .. M.dumpstring(o[k], isseen, newindent, k) .. ', '
68+
used[k] = true
69+
end
70+
end
71+
72+
-- elements on separate lines
73+
local done
74+
for _,k in ipairs(ks) do
75+
if not used[k] then
76+
if not done then s = s .. '\n'; done = true end
77+
s = s .. newindent .. dumpstring_key_(k, isseen) .. ' = ' .. M.dumpstring(o[k], isseen, newindent, k) .. ',\n'
78+
end
79+
end
80+
s = s:gsub(',(%s*)$', '%1')
81+
s = s .. (done and indent or '') .. '}'
82+
return s
83+
elseif type(o) == 'string' then
84+
return string.format('%q', o)
85+
else
86+
return tostring(o)
87+
end
88+
end
89+
-- End of dump.lua code
90+
91+
local function printTables(fileName)
92+
93+
local findBegin, field
94+
if type(fileName) ~= "string" then error("Wrong argument") end
95+
local file = io.open(fileName, "r")
96+
if not file then error("Can't open file") end
97+
local result = {}
98+
result.fields = {}
99+
100+
for line in file:lines() do
101+
findBegin, _, field = string.find(line, "LROT_BEGIN%((%g+)%)")
102+
if findBegin then
103+
io.write(field.." = ")
104+
end
105+
findBegin, _, field = string.find(line, "LROT_PUBLIC_BEGIN%((%g+)%)")
106+
if findBegin then
107+
print(field.." = ")
108+
end
109+
110+
findBegin, _, field = string.find(line, "LROT_FUNCENTRY%(%s?(%g+),")
111+
if not findBegin then
112+
findBegin, _, field = string.find(line, "LROT_NUMENTRY%(%s?(%g+),")
113+
end
114+
if not findBegin then
115+
findBegin, _, field = string.find(line, "LROT_TABENTRY%(%s?(%g+),")
116+
end
117+
118+
if findBegin then
119+
if not string.find(field, "__") then
120+
result.fields[field] = {}
121+
end
122+
end
123+
124+
findBegin = string.find(line, "LROT_END")
125+
if findBegin then
126+
print(string.gsub(M.dumpstring(result), "{}", "empty")..',')
127+
result = {}
128+
result.fields = {}
129+
end
130+
end
131+
end
132+
133+
local function main()
134+
for i = 1, #arg do
135+
printTables(arg[i])
136+
end
137+
end
138+
139+
main()

‎tools/travis/localua.sh

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/bin/bash
2+
3+
# Downloads and installs a self-contained Lua and LuaRocks.
4+
# Supports Linux, macOS and MSYS2.
5+
# Copyright (c) 2015-2019 Pierre Chapuis, MIT Licensed.
6+
# Latest stable version available at: https://loadk.com/localua.sh
7+
# Maintained at: https://github.com/oploadk/localua
8+
9+
DEFAULT_LUA_V="5.3.5"
10+
DEFAULT_LR_V="3.0.4"
11+
12+
usage () {
13+
>&2 echo -e "USAGE: $0 output-dir [lua-version] [luarocks-version]\n"
14+
>&2 echo -n "The first optional argument is the Lua version, "
15+
>&2 echo -n "the second one is the LuaRocks version. "
16+
>&2 echo -e "Defaults are Lua $DEFAULT_LUA_V and LuaRocks $DEFAULT_LR_V.\n"
17+
>&2 echo -n "You can set a custom build directory with environment "
18+
>&2 echo -e "variable LOCALUA_BUILD_DIRECTORY (not useful in general).\n"
19+
>&2 echo -e "You can set a custom makefile target with LOCALUA_TARGET.\n"
20+
>&2 echo -e "You can disable LUA_COMPAT by setting LOCALUA_NO_COMPAT.\n"
21+
>&2 echo -e "You can skip luarocks by setting LOCALUA_NO_LUAROCKS."
22+
exit 1
23+
}
24+
25+
# Set output directory, Lua version and LuaRocks version
26+
27+
ODIR="$1"
28+
[ -z "$ODIR" ] && usage
29+
30+
LUA_V="$2"
31+
[ -z "$LUA_V" ] && LUA_V="$DEFAULT_LUA_V"
32+
33+
LUA_SHORTV="$(echo $LUA_V | cut -c 1-3)"
34+
35+
LR_V="$3"
36+
[ -z "$LR_V" ] && LR_V="$DEFAULT_LR_V"
37+
38+
# Set build directory
39+
40+
BDIR="$LOCALUA_BUILD_DIRECTORY"
41+
[ -z "$BDIR" ] && BDIR="$(mktemp -d /tmp/localua-XXXXXX)"
42+
43+
# Create output directory and get absolute path
44+
45+
mkdir -p "$ODIR"
46+
>/dev/null pushd "$ODIR"
47+
ODIR="$(pwd)"
48+
>/dev/null popd
49+
50+
# Download, unpack and build Lua and LuaRocks
51+
52+
if [ -z "$LOCALUA_TARGET" ]; then
53+
case "$(uname)" in
54+
Linux)
55+
LOCALUA_TARGET="linux";;
56+
Darwin)
57+
LOCALUA_TARGET="macosx";;
58+
MSYS*)
59+
LOCALUA_TARGET="msys";;
60+
*)
61+
LOCALUA_TARGET="posix";;
62+
esac
63+
fi
64+
65+
pushd "$BDIR"
66+
wget "http://www.lua.org/ftp/lua-${LUA_V}.tar.gz"
67+
tar xf "lua-${LUA_V}.tar.gz"
68+
pushd "lua-${LUA_V}"
69+
sed 's#"/usr/local/"#"'"$ODIR"'/"#' "src/luaconf.h" > "$BDIR/t"
70+
mv "$BDIR/t" "src/luaconf.h"
71+
if [ ! -z "$LOCALUA_NO_COMPAT" ]; then
72+
sed 's#-DLUA_COMPAT_5_2##' "src/Makefile" > "$BDIR/t"
73+
sed 's#-DLUA_COMPAT_ALL##' "$BDIR/t" > "src/Makefile"
74+
fi
75+
76+
if [ "$LOCALUA_TARGET" = "msys" ]; then
77+
>> "src/Makefile" echo
78+
>> "src/Makefile" echo 'msys:' >> "src/Makefile"
79+
>> "src/Makefile" echo -ne "\t"
80+
>> "src/Makefile" echo '$(MAKE) "LUA_A=lua53.dll" "LUA_T=lua.exe" \'
81+
>> "src/Makefile" echo -ne "\t"
82+
>> "src/Makefile" echo '"AR=$(CC) -shared -Wl,--out-implib,liblua.dll.a -o" "RANLIB=strip --strip-unneeded" \'
83+
>> "src/Makefile" echo -ne "\t"
84+
>> "src/Makefile" echo '"SYSCFLAGS=-DLUA_BUILD_AS_DLL -DLUA_USE_POSIX -DLUA_USE_DLOPEN" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe'
85+
>> "src/Makefile" echo -ne "\t"
86+
>> "src/Makefile" echo '$(MAKE) "LUAC_T=luac.exe" luac.exe'
87+
88+
make -C src "$LOCALUA_TARGET" || exit 1
89+
make \
90+
TO_BIN="lua.exe luac.exe lua53.dll" \
91+
TO_LIB="liblua.a liblua.dll.a" \
92+
INSTALL_TOP="$ODIR" install || exit 1
93+
else
94+
make "$LOCALUA_TARGET" || exit 1
95+
make INSTALL_TOP="$ODIR" install || exit 1
96+
fi
97+
98+
popd
99+
if [ -z "$LOCALUA_NO_LUAROCKS" ]; then
100+
wget "http://luarocks.org/releases/luarocks-${LR_V}.tar.gz"
101+
tar xf "luarocks-${LR_V}.tar.gz"
102+
pushd "luarocks-${LR_V}"
103+
./configure --with-lua="$ODIR" --prefix="$ODIR" \
104+
--lua-version="$LUA_SHORTV" \
105+
--sysconfdir="$ODIR/luarocks" --force-config
106+
make bootstrap
107+
popd
108+
fi
109+
popd
110+
111+
# Cleanup
112+
113+
rm -rf "$BDIR"

‎tools/travis/run-luacheck.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "Installing Lua 5.3, LuaRocks and Luacheck"
6+
(
7+
cd "$TRAVIS_BUILD_DIR" || exit
8+
bash tools/travis/localua.sh cache/localua || exit
9+
cache/localua/bin/luarocks install luacheck || exit
10+
)
11+
12+
(
13+
echo "Static analysys of:"
14+
find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 echo
15+
)
16+
17+
(find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 cache/localua/bin/luacheck --config tools/luacheck_config.lua) || exit

0 commit comments

Comments
 (0)
Please sign in to comment.