Skip to content

Commit fa09986

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 19ea520 commit fa09986

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

docs/user/site.rst

+4
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,10 @@ disable_factory()
728728
Disables factory image generation. Sysupgrade images are still generated and stored in the image
729729
output directory.
730730

731+
include(path)
732+
Includes another image customization file. Relative paths are interpreted relative to the site
733+
repository root. Values returned from the included file become the return value of ``include``.
734+
731735
Technically, the image customzation file is evaluated once for each device, allowing
732736
to make use of regular Lua *if* statements for device-specific configuration as
733737
can be seen in the example.

scripts/image_customization_lib.lua

+12
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ local function evaluate_device(env, dev)
7979
return dev.options.class == class
8080
end
8181

82+
function funcs.include(path)
83+
if string.sub(path, 1, 1) ~= '/' then
84+
assert(
85+
string.find(path, '/') == nil,
86+
'incorrect use of include(): including files from subdirectories is unsupported')
87+
path = env.GLUON_SITEDIR .. '/' .. path
88+
end
89+
local f = assert(loadfile(path))
90+
setfenv(f, funcs)
91+
return f()
92+
end
93+
8294
-- Evaluate the feature definition files
8395
setfenv(M.customization_file, funcs)
8496
M.customization_file()

0 commit comments

Comments
 (0)