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