diff --git a/include/package.mk b/include/package.mk new file mode 100644 index 00000000..8e2af5b1 --- /dev/null +++ b/include/package.mk @@ -0,0 +1,8 @@ +include $(INCLUDE_DIR)/package.mk + +# Annoyingly, make's shell function replaces all newlines with spaces, so we have to do some escaping work. Yuck. +define GluonCheckSite +[ -z "$$GLUONDIR" ] || sed -e 's/-@/\n/g' -e 's/+@/@/g' <<'END__GLUON__CHECK__SITE' | "$$GLUONDIR"/scripts/check_site.sh +$(shell cat $(1) | sed -ne '1h; 1!H; $$ {g; s/@/+@/g; s/\n/-@/g; p}') +END__GLUON__CHECK__SITE +endef diff --git a/scripts/check_site.sh b/scripts/check_site.sh new file mode 100755 index 00000000..2f2db553 --- /dev/null +++ b/scripts/check_site.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +export GLUON_SITE_CONFIG="$GLUONDIR/site/site.conf" + +SITE_CONFIG_LUA=packages/gluon/gluon/gluon-core/files/usr/lib/lua/gluon/site_config.lua +CHECK_SITE_LIB=scripts/check_site_lib.lua + +"$GLUONDIR"/openwrt/staging_dir/host/bin/lua -e "site = dofile(os.getenv('GLUONDIR') .. '/${SITE_CONFIG_LUA}'); dofile(os.getenv('GLUONDIR') .. '/${CHECK_SITE_LIB}'); dofile()" diff --git a/scripts/check_site_lib.lua b/scripts/check_site_lib.lua new file mode 100644 index 00000000..a2b5500b --- /dev/null +++ b/scripts/check_site_lib.lua @@ -0,0 +1,85 @@ +local function loadvar(varname) + local ok, val = pcall(assert(loadstring('return site.' .. varname))) + if ok then + return val + else + return nil + end +end + +local function assert_type(var, t, msg) + assert(type(var) == t, msg) +end + + +function need_string(varname, required) + local var = loadvar(varname) + + if required == false and var == nil then + return nil + end + + assert_type(var, 'string', "site.conf error: expected `" .. varname .. "' to be a string") + return var +end + +function need_string_match(varname, pat, required) + local var = need_string(varname, required) + + if not var then + return nil + end + + assert(var:match(pat), "site.conf error: expected `" .. varname .. "' to match pattern `" .. pat .. "'") + + return var +end + +function need_number(varname, required) + local var = loadvar(varname) + + if required == false and var == nil then + return nil + end + + assert_type(var, 'number', "site.conf error: expected `" .. varname .. "' to be a number") + + return var +end + +function need_array(varname, subcheck, required) + local var = loadvar(varname) + + if required == false and var == nil then + return nil + end + + assert_type(var, 'table', "site.conf error: expected `" .. varname .. "' to be an array") + + for _, e in ipairs(var) do + subcheck(e) + end + + return var +end + +function need_table(varname, subcheck, required) + local var = loadvar(varname) + + if required == false and var == nil then + return nil + end + + assert_type(var, 'table', "site.conf error: expected `" .. varname .. "' to be a table") + + for k, v in pairs(var) do + subcheck(k, v) + end + + return var +end + +function need_string_array(varname, required) + return assert(pcall(need_array, varname, function(e) assert_type(e, 'string') end, required), + "site.conf error: expected `" .. varname .. "' to be a string array") +end