1#!/usr/libexec/flua 2 3--[[ usage: 4generare-set-ucl.lua <template> [<variablename> <variablevalue>] 5 6Generate the UCL for a set metapackage. The variables provided will be 7substituted as UCL variables. 8]]-- 9 10local ucl = require("ucl") 11 12-- This parser is the output UCL we want to build. 13local parser = ucl.parser() 14 15if #arg < 1 then 16 io.stderr:write(arg[0] .. ": missing template filename\n") 17 os.exit(1) 18end 19 20local template = table.remove(arg, 1) 21 22-- Set any $VARIABLES from the command line in the parser. This causes ucl to 23-- automatically replace them when we load the source ucl. 24if #arg % 2 ~= 0 then 25 io.stderr:write(arg[0] .. ": expected an even number of arguments, " 26 .. "got " .. #arg .. "\n") 27 os.exit(1) 28end 29 30local pkgprefix = nil 31local pkgversion = nil 32local pkgdeps = "" 33 34for i = 2, #arg, 2 do 35 local varname = arg[i - 1] 36 local varvalue = arg[i] 37 38 if varname == "PKG_NAME_PREFIX" and #varvalue > 0 then 39 pkgprefix = varvalue 40 elseif varname == "VERSION" and #varvalue > 0 then 41 pkgversion = varvalue 42 elseif varname == "SET_DEPENDS" and #varvalue > 0 then 43 pkgdeps = varvalue 44 end 45 46 parser:register_variable(varname, varvalue) 47end 48 49-- Load the source template. 50local res,err = parser:parse_file(template) 51if not res then 52 io.stderr:write(arg[0] .. ": fail to parse(" .. template .. "): " 53 .. err .. "\n") 54 os.exit(1) 55end 56 57local obj = parser:get_object() 58 59-- Dependency handling. 60obj["deps"] = obj["deps"] or {} 61 62-- If PKG_NAME_PREFIX is provided, rewrite the names of dependency packages. 63-- We can't do this in UCL since variable substitution doesn't work in array 64-- keys. Note that this only applies to dependencies from the set UCL files, 65-- because SET_DEPENDS already have the correct prefix. 66if pkgprefix ~= nil then 67 newdeps = {} 68 for dep, opts in pairs(obj["deps"]) do 69 local newdep = pkgprefix .. "-" .. dep 70 newdeps[newdep] = opts 71 end 72 obj["deps"] = newdeps 73end 74 75-- Add dependencies from SET_DEPENDS. 76for dep in string.gmatch(pkgdeps, "[^%s]+") do 77 obj["deps"][dep] = { 78 ["origin"] = "base" 79 } 80end 81 82-- Add a version key to all dependencies, otherwise pkg doesn't like it. 83for dep, opts in pairs(obj["deps"]) do 84 if obj["deps"][dep]["version"] == nil then 85 obj["deps"][dep]["version"] = pkgversion 86 end 87end 88 89-- If there are no dependencies, remove the deps key, otherwise pkg raises an 90-- error trying to read the manifest. 91if next(obj["deps"]) == nil then 92 obj["deps"] = nil 93end 94 95-- Write the output. 96io.stdout:write(ucl.to_format(obj, 'ucl', true)) 97