Path: blob/main/release/packages/generate-set-ucl.lua
39475 views
#!/usr/libexec/flua12--[[ usage:3generare-set-ucl.lua <template> [<variablename> <variablevalue>]45Generate the UCL for a set metapackage. The variables provided will be6substituted as UCL variables.7]]--89local ucl = require("ucl")1011-- This parser is the output UCL we want to build.12local parser = ucl.parser()1314if #arg < 1 then15io.stderr:write(arg[0] .. ": missing template filename\n")16os.exit(1)17end1819local template = table.remove(arg, 1)2021-- Set any $VARIABLES from the command line in the parser. This causes ucl to22-- automatically replace them when we load the source ucl.23if #arg % 2 ~= 0 then24io.stderr:write(arg[0] .. ": expected an even number of arguments, "25.. "got " .. #arg .. "\n")26os.exit(1)27end2829local pkgprefix = nil30local pkgversion = nil31local pkgdeps = ""3233for i = 2, #arg, 2 do34local varname = arg[i - 1]35local varvalue = arg[i]3637if varname == "PKG_NAME_PREFIX" and #varvalue > 0 then38pkgprefix = varvalue39elseif varname == "VERSION" and #varvalue > 0 then40pkgversion = varvalue41elseif varname == "SET_DEPENDS" and #varvalue > 0 then42pkgdeps = varvalue43end4445parser:register_variable(varname, varvalue)46end4748-- Load the source template.49local res,err = parser:parse_file(template)50if not res then51io.stderr:write(arg[0] .. ": fail to parse(" .. template .. "): "52.. err .. "\n")53os.exit(1)54end5556local obj = parser:get_object()5758-- Dependency handling.59obj["deps"] = obj["deps"] or {}6061-- If PKG_NAME_PREFIX is provided, rewrite the names of dependency packages.62-- We can't do this in UCL since variable substitution doesn't work in array63-- keys. Note that this only applies to dependencies from the set UCL files,64-- because SET_DEPENDS already have the correct prefix.65if pkgprefix ~= nil then66newdeps = {}67for dep, opts in pairs(obj["deps"]) do68local newdep = pkgprefix .. "-" .. dep69newdeps[newdep] = opts70end71obj["deps"] = newdeps72end7374-- Add dependencies from SET_DEPENDS.75for dep in string.gmatch(pkgdeps, "[^%s]+") do76obj["deps"][dep] = {77["origin"] = "base"78}79end8081-- Add a version key to all dependencies, otherwise pkg doesn't like it.82for dep, opts in pairs(obj["deps"]) do83if obj["deps"][dep]["version"] == nil then84obj["deps"][dep]["version"] = pkgversion85end86end8788-- If there are no dependencies, remove the deps key, otherwise pkg raises an89-- error trying to read the manifest.90if next(obj["deps"]) == nil then91obj["deps"] = nil92end9394-- Write the output.95io.stdout:write(ucl.to_format(obj, 'ucl', true))969798