add _forge_util.lua module to simplify macro lookups

We'll rely on this until we can drop support for RPM 4.16 and RHEL 9.
The function have similar behavior to the macros table in the way that
they handle undefined macros.
This commit is contained in:
Maxwell G 2024-03-02 17:20:22 +00:00
commit 48679d415e
Signed by: gotmax23
GPG key ID: F79E4E25E8C661F8
2 changed files with 46 additions and 0 deletions

View file

@ -61,6 +61,7 @@ export MACRO_LUA_DIR="%{buildroot}%{_rpmluadir}"
%doc README.md NEWS.md
%{_rpmmacrodir}/macros.forge
%{_rpmluadir}/fedora/srpm/forge.lua
%{_rpmluadir}/fedora/srpm/_forge_util.lua
%changelog

View file

@ -0,0 +1,45 @@
-- Utilities for evaluating macros without having to construct rpm.expand() statements.
-- Uses the macros table on RPM >= 4.17.
--- Return true if a macro is defined and false otherwise.
local function is_defined(name)
if macros then
return macros[name] ~= nil
end
return rpm.expand("%{?" .. name .. ":1}") == "1"
end
--- Expand a macro. Return nil if the macro is undefined.
local function get_macro(name)
if macros then
return macros[name]
end
if not is_defined(name) then
return nil
end
return rpm.expand("%{" .. name .. "}")
end
-- Get a flag. Pass true to has_arg if the flag accepts an argument.
-- For flags with arguments, return the flag argument or nil.
-- For flags without arguments, return a truthy value for defined or nil if the
-- flag was not passed.
local function get_flag(name, has_arg)
if opt then
return opt[name]
end
if not is_defined("-" .. name) then
return nil
end
local ender = "}"
if has_arg then
ender = "*" .. ender
end
return rpm.expand("%{-" .. name .. ender)
end
return {
is_defined = is_defined,
get_macro = get_macro,
get_flag = get_flag,
}