Skip to content

Commit

Permalink
scripts: image_customization_lib: add include() function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
neocturne committed Jan 5, 2024
1 parent 19ea520 commit fa09986
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/user/site.rst
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@ disable_factory()
Disables factory image generation. Sysupgrade images are still generated and stored in the image
output directory.

include(path)
Includes another image customization file. Relative paths are interpreted relative to the site
repository root. Values returned from the included file become the return value of ``include``.

Technically, the image customzation file is evaluated once for each device, allowing
to make use of regular Lua *if* statements for device-specific configuration as
can be seen in the example.
Expand Down
12 changes: 12 additions & 0 deletions scripts/image_customization_lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ local function evaluate_device(env, dev)
return dev.options.class == class
end

function funcs.include(path)
if string.sub(path, 1, 1) ~= '/' then
assert(
string.find(path, '/') == nil,
'incorrect use of include(): including files from subdirectories is unsupported')
path = env.GLUON_SITEDIR .. '/' .. path
end
local f = assert(loadfile(path))
setfenv(f, funcs)
return f()
end

-- Evaluate the feature definition files
setfenv(M.customization_file, funcs)
M.customization_file()
Expand Down

0 comments on commit fa09986

Please sign in to comment.