Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/release/packages/generate-set-ucl.lua
39475 views
1
#!/usr/libexec/flua
2
3
--[[ usage:
4
generare-set-ucl.lua <template> [<variablename> <variablevalue>]
5
6
Generate the UCL for a set metapackage. The variables provided will be
7
substituted as UCL variables.
8
]]--
9
10
local ucl = require("ucl")
11
12
-- This parser is the output UCL we want to build.
13
local parser = ucl.parser()
14
15
if #arg < 1 then
16
io.stderr:write(arg[0] .. ": missing template filename\n")
17
os.exit(1)
18
end
19
20
local 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.
24
if #arg % 2 ~= 0 then
25
io.stderr:write(arg[0] .. ": expected an even number of arguments, "
26
.. "got " .. #arg .. "\n")
27
os.exit(1)
28
end
29
30
local pkgprefix = nil
31
local pkgversion = nil
32
local pkgdeps = ""
33
34
for 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)
47
end
48
49
-- Load the source template.
50
local res,err = parser:parse_file(template)
51
if not res then
52
io.stderr:write(arg[0] .. ": fail to parse(" .. template .. "): "
53
.. err .. "\n")
54
os.exit(1)
55
end
56
57
local obj = parser:get_object()
58
59
-- Dependency handling.
60
obj["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.
66
if 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
73
end
74
75
-- Add dependencies from SET_DEPENDS.
76
for dep in string.gmatch(pkgdeps, "[^%s]+") do
77
obj["deps"][dep] = {
78
["origin"] = "base"
79
}
80
end
81
82
-- Add a version key to all dependencies, otherwise pkg doesn't like it.
83
for dep, opts in pairs(obj["deps"]) do
84
if obj["deps"][dep]["version"] == nil then
85
obj["deps"][dep]["version"] = pkgversion
86
end
87
end
88
89
-- If there are no dependencies, remove the deps key, otherwise pkg raises an
90
-- error trying to read the manifest.
91
if next(obj["deps"]) == nil then
92
obj["deps"] = nil
93
end
94
95
-- Write the output.
96
io.stdout:write(ucl.to_format(obj, 'ucl', true))
97
98