From f23e02478718cd2f1b17719fdf83c4b85113149c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer <mschiffer@universe-factory.net> Date: Wed, 30 Dec 2015 03:35:17 +0100 Subject: [PATCH] Convert site.conf to JSON during build This will allow us to use its content from other languages than Lua as well. --- Makefile | 2 +- package/gluon-core/Makefile | 2 +- .../files/usr/lib/lua/gluon/site_config.lua | 22 ++++++++++++------- package/gluon-site/Makefile | 4 ++-- scripts/check_site.sh | 2 +- scripts/site.sh | 2 +- scripts/site_config.lua | 9 ++++++++ 7 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 scripts/site_config.lua diff --git a/Makefile b/Makefile index bb61eb92d..a1b69459c 100644 --- a/Makefile +++ b/Makefile @@ -338,7 +338,7 @@ prepare-image: FORCE +$(SUBMAKE) -C $(TOPDIR)/target/linux/$(BOARD)/image image_prepare KDIR="$(BOARD_KDIR)" prepare: FORCE - @$(STAGING_DIR_HOST)/bin/lua $(GLUONDIR)/package/gluon-core/files/usr/lib/lua/gluon/site_config.lua \ + @$(STAGING_DIR_HOST)/bin/lua $(GLUONDIR)/scripts/site_config.lua \ || (echo 'Your site configuration did not pass validation.'; false) mkdir -p $(GLUON_IMAGEDIR) $(BOARD_BUILDDIR) diff --git a/package/gluon-core/Makefile b/package/gluon-core/Makefile index ddf67dcd8..ecd6fbe31 100644 --- a/package/gluon-core/Makefile +++ b/package/gluon-core/Makefile @@ -12,7 +12,7 @@ define Package/gluon-core SECTION:=gluon CATEGORY:=Gluon TITLE:=Base files of Gluon - DEPENDS:=+gluon-site +lua-platform-info +luci-base +odhcp6c +firewall + DEPENDS:=+gluon-site +lua-platform-info +luci-base +luci-lib-jsonc +odhcp6c +firewall endef diff --git a/package/gluon-core/files/usr/lib/lua/gluon/site_config.lua b/package/gluon-core/files/usr/lib/lua/gluon/site_config.lua index cf1514838..6f01b066f 100644 --- a/package/gluon-core/files/usr/lib/lua/gluon/site_config.lua +++ b/package/gluon-core/files/usr/lib/lua/gluon/site_config.lua @@ -1,12 +1,18 @@ -local config = os.getenv('GLUON_SITE_CONFIG') or '/lib/gluon/site.conf' +local function get_site_config() + local config = '/lib/gluon/site.json' -local function loader() - coroutine.yield('return ') - coroutine.yield(io.open(config):read('*a')) -end + local json = require 'luci.jsonc' + local ltn12 = require 'luci.ltn12' + + local file = assert(io.open(config)) + + local decoder = json.new() + ltn12.pump.all(ltn12.source.file(io.open(config)), decoder:sink()) --- setfenv doesn't work with Lua 5.2 anymore, but we're using 5.1 -local site_config = setfenv(assert(load(coroutine.wrap(loader), 'site.conf')), {})() + file:close() + + return assert(decoder:get()) +end local setmetatable = setmetatable @@ -14,7 +20,7 @@ module 'gluon.site_config' setmetatable(_M, { - __index = site_config, + __index = get_site_config(), } ) diff --git a/package/gluon-site/Makefile b/package/gluon-site/Makefile index abd057c90..a7084ad0b 100644 --- a/package/gluon-site/Makefile +++ b/package/gluon-site/Makefile @@ -5,7 +5,7 @@ PKG_VERSION:=$(if $(GLUON_SITE_CODE),$(GLUON_SITE_CODE),1) PKG_RELEASE:=$(GLUON_RELEASE) PKG_FILE_DEPENDS := $(GLUON_SITEDIR)/site.conf $(GLUON_SITEDIR)/i18n/ -PKG_BUILD_DEPENDS := luci-base/host +PKG_BUILD_DEPENDS := luci-base/host lua-cjson/host PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) @@ -33,7 +33,7 @@ endef define Package/gluon-site/install $(INSTALL_DIR) $(1)/lib/gluon - $(CP) $(GLUON_SITEDIR)/site.conf $(1)/lib/gluon/site.conf + lua -e 'print(require("cjson").encode(assert(dofile("$(GLUONDIR)/scripts/site_config.lua"))))' > $(1)/lib/gluon/site.json echo "$(GLUON_RELEASE)" > $(1)/lib/gluon/release $(call GluonInstallI18N,gluon-site,$(1)) diff --git a/scripts/check_site.sh b/scripts/check_site.sh index e0aa43e57..0ca303160 100755 --- a/scripts/check_site.sh +++ b/scripts/check_site.sh @@ -1,6 +1,6 @@ #!/bin/sh -SITE_CONFIG_LUA=package/gluon-core/files/usr/lib/lua/gluon/site_config.lua +SITE_CONFIG_LUA=scripts/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/site.sh b/scripts/site.sh index 82016cbe5..5038438a8 100755 --- a/scripts/site.sh +++ b/scripts/site.sh @@ -1,5 +1,5 @@ #!/bin/sh -SITE_CONFIG_LUA=package/gluon-core/files/usr/lib/lua/gluon/site_config.lua +SITE_CONFIG_LUA=scripts/site_config.lua "$GLUONDIR"/openwrt/staging_dir/host/bin/lua -e "print(assert(dofile(os.getenv('GLUONDIR') .. '/${SITE_CONFIG_LUA}').$1))" 2>/dev/null diff --git a/scripts/site_config.lua b/scripts/site_config.lua new file mode 100644 index 000000000..a50f115bf --- /dev/null +++ b/scripts/site_config.lua @@ -0,0 +1,9 @@ +local config = os.getenv('GLUON_SITE_CONFIG') + +local function loader() + coroutine.yield('return ') + coroutine.yield(io.open(config):read('*a')) +end + +-- setfenv doesn't work with Lua 5.2 anymore, but we're using 5.1 +return setfenv(assert(load(coroutine.wrap(loader), 'site.conf')), {})() -- GitLab