Skip to content

Commit 7de7e58

Browse files
committed
scripts: image_customization_lib: add include() function
Add a simple way to include image customization Lua snippets. Can be used to split long files, to include the same options multiple times in different conditional branches, or even to pass values back to the including file using return. Relative paths are interpreted relative to the site root (where image-customization.lua is located). Relative includes would become confusing when subdirectories are involved (as they are still interpreted relative to the site root, and proper tracking of current directory for each include seems fairly complex for a very niche use case), so we simply reject this case for now; this way, we can implement this however we want in the future if deemed necessary, without a breaking change.
1 parent 9268e9c commit 7de7e58

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

docs/user/site.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,39 @@ disable_factory()
788788
disable_factory()
789789
end
790790
791+
include(path)
792+
Includes another image customization file. Relative paths are interpreted relative to the site
793+
repository root.
794+
795+
.. code-block:: lua
796+
:caption: target-settings.lua
797+
798+
features {'wireless-encryption-wpa3'}
799+
800+
.. code-block:: lua
801+
:caption: image-customization.lua
802+
803+
if target('x86', '64') then
804+
include 'target-settings.lua'
805+
end
806+
807+
Values returned from the included file become the return value of ``include``:
808+
809+
.. code-block:: lua
810+
:caption: matches-device.lua
811+
812+
return device {
813+
'openmesh-a40',
814+
'openmesh-a60',
815+
}
816+
817+
.. code-block:: lua
818+
:caption: image-customization.lua
819+
820+
if include('matches-device.lua') then
821+
packages {'iperf3'}
822+
end
823+
791824
Technically, the image customization file is evaluated once for each device, allowing
792825
to make use of regular Lua *if* statements for device-specific configuration as
793826
can be seen in the examples.

scripts/image_customization_lib.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ local function evaluate_device(env, dev)
9999
return dev.options.class == class
100100
end
101101

102+
function funcs.include(path)
103+
assert(
104+
type(path) == 'string',
105+
'incorrect use of include(): path expected as first argument')
106+
107+
if string.sub(path, 1, 1) ~= '/' then
108+
assert(
109+
string.find(path, '/') == nil,
110+
'incorrect use of include(): including files from subdirectories is unsupported')
111+
path = env.GLUON_SITEDIR .. '/' .. path
112+
end
113+
local f = assert(loadfile(path))
114+
setfenv(f, funcs)
115+
return f()
116+
end
117+
102118
-- Evaluate the feature definition files
103119
setfenv(M.customization_file, funcs)
104120
M.customization_file()

0 commit comments

Comments
 (0)