Merge pull request #2019 from dibdot/adblock
[oweals/luci.git] / build / check-controllers.sh
1 #!/usr/bin/env bash
2
3 [ -d build ] || {
4         echo "Execute as ./build/check-controllers.sh" >&2
5         exit 1
6 }
7
8 find . -type f -name '*.lua' -path '*/controller/*' | while read controller; do
9         controller="${controller#./}"
10         base="${controller%%/controller/*}"
11
12         sed -rne 's#^.*\b(cbi|form)\([[:space:]]*("([^"]*)"|\047([^\047]*)\047)[[:space:]]*[,)].*$#\1 \3\4#gp' "$controller" | while read type map; do
13                 model="$base/model/cbi/$map.lua"
14                 package="${controller##*/controller/}"; package="${package%.lua}"; package="luci.controller.${package//\//.}"
15
16                 if ! grep -sqE '\bmodule[[:space:]]*\(?[[:space:]]*("|\047|\[=*\[)'"$package" "$controller"; then
17                         echo "'$controller' does not containt the expected\n\t'module(\"$package\", ...)' line.\n"
18                 fi
19
20                 grep -sqE '\b(Form|SimpleForm)[[:space:]]*\(' "$model" && ! grep -sqE '\bMap[[:space:]]*\(' "$model" && is_form=1 || is_form=0
21
22                 if [ ! -f "$model" ]; then
23                         echo -e "'$controller' references $type('$map')\n\tbut expected file '$model' does not exist.\n"
24                 elif [ $type = "cbi" -a $is_form = 1 ]; then
25                         echo -e "'$controller' references $type('$map')\n\tbut '$model' looks like a Form or SimpleForm.\n"
26                 elif [ $type = "form" -a $is_form = 0 ]; then
27                         echo -e "'$controller' references $type('$map')\n\tbut '$model' does not look like a Form or SimpleForm.\n"
28                 fi
29         done
30 done