Skip to content

Commit

Permalink
Convert site.conf to JSON during build
Browse files Browse the repository at this point in the history
This will allow us to use its content from other languages than Lua as
well.
  • Loading branch information
neocturne committed Jan 2, 2016
1 parent 97471a1 commit f23e024
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package/gluon-core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
22 changes: 14 additions & 8 deletions package/gluon-core/files/usr/lib/lua/gluon/site_config.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
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

module 'gluon.site_config'

setmetatable(_M,
{
__index = site_config,
__index = get_site_config(),
}
)

Expand Down
4 changes: 2 additions & 2 deletions package/gluon-site/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion scripts/check_site.sh
Original file line number Diff line number Diff line change
@@ -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()"
2 changes: 1 addition & 1 deletion scripts/site.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions scripts/site_config.lua
Original file line number Diff line number Diff line change
@@ -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')), {})()

0 comments on commit f23e024

Please sign in to comment.