Path: blob/main/release/packages/generate-set-ucl.lua
103097 views
#!/usr/libexec/flua1--2-- Copyright (c) 2024-2025 Baptiste Daroussin <[email protected]>3-- Copyright (c) 2025 Lexi Winter <[email protected]>4--5-- SPDX-License-Identifier: BSD-2-Clause6--78--[[ usage:9generate-set-ucl.lua <template> [<variablename> <variablevalue>]1011Generate the UCL for a set metapackage. The variables provided will be12substituted as UCL variables.13]]--1415local ucl = require("ucl")1617-- This parser is the output UCL we want to build.18local parser = ucl.parser()1920if #arg < 1 then21io.stderr:write(arg[0] .. ": missing template filename\n")22os.exit(1)23end2425local template = table.remove(arg, 1)2627-- Set any $VARIABLES from the command line in the parser. This causes ucl to28-- automatically replace them when we load the source ucl.29if #arg % 2 ~= 0 then30io.stderr:write(arg[0] .. ": expected an even number of arguments, "31.. "got " .. #arg .. "\n")32os.exit(1)33end3435local pkgprefix = nil36local pkgversion = nil37local pkgdeps = ""3839for i = 2, #arg, 2 do40local varname = arg[i - 1]41local varvalue = arg[i]4243if varname == "PKG_NAME_PREFIX" and #varvalue > 0 then44pkgprefix = varvalue45elseif varname == "VERSION" and #varvalue > 0 then46pkgversion = varvalue47elseif varname == "SET_DEPENDS" and #varvalue > 0 then48pkgdeps = varvalue49end5051parser:register_variable(varname, varvalue)52end5354-- Load the source template.55local res,err = parser:parse_file(template)56if not res then57io.stderr:write(arg[0] .. ": fail to parse(" .. template .. "): "58.. err .. "\n")59os.exit(1)60end6162local obj = parser:get_object()6364-- Dependency handling.65obj["deps"] = obj["deps"] or {}6667-- If PKG_NAME_PREFIX is provided, rewrite the names of dependency packages.68-- We can't do this in UCL since variable substitution doesn't work in array69-- keys. Note that this only applies to dependencies from the set UCL files,70-- because SET_DEPENDS already have the correct prefix.71if pkgprefix ~= nil then72newdeps = {}73for dep, opts in pairs(obj["deps"]) do74local newdep = pkgprefix .. "-" .. dep75newdeps[newdep] = opts76end77obj["deps"] = newdeps78end7980-- Add dependencies from SET_DEPENDS.81for dep in string.gmatch(pkgdeps, "[^%s]+") do82obj["deps"][dep] = {83["origin"] = "base/"..dep84}85end8687-- Add a version and origin key to all dependencies, otherwise pkg88-- doesn't like it.89for dep, opts in pairs(obj["deps"]) do90obj["deps"][dep]["origin"] = obj["deps"][dep]["origin"] or "base/"..dep91obj["deps"][dep]["version"] = obj["deps"][dep]["version"] or pkgversion92end9394-- If there are no dependencies, remove the deps key, otherwise pkg raises an95-- error trying to read the manifest.96if next(obj["deps"]) == nil then97obj["deps"] = nil98end99100-- Write the output.101io.stdout:write(ucl.to_format(obj, 'ucl', true))102103104