2020-05-31 11:04:10 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
local function to_keys(t)
|
|
|
|
local ret = {}
|
|
|
|
for _, v in ipairs(t) do
|
|
|
|
ret[v] = true
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function collect_keys(t)
|
|
|
|
local ret = {}
|
|
|
|
for v in pairs(t) do
|
|
|
|
table.insert(ret, v)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2020-08-28 20:20:13 +00:00
|
|
|
function M.get_packages(files, features)
|
2020-08-28 20:04:06 +00:00
|
|
|
local enabled_features = to_keys(features)
|
|
|
|
local handled_features = {}
|
|
|
|
local packages = {}
|
2020-05-31 11:04:10 +00:00
|
|
|
|
|
|
|
local funcs = {}
|
|
|
|
|
2020-08-28 20:04:06 +00:00
|
|
|
local function add_pkgs(pkgs)
|
|
|
|
for _, pkg in ipairs(pkgs or {}) do
|
|
|
|
packages[pkg] = true
|
2020-05-31 11:04:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-28 20:04:06 +00:00
|
|
|
function funcs._(feature)
|
|
|
|
return enabled_features[feature] ~= nil
|
|
|
|
end
|
2020-05-31 11:04:10 +00:00
|
|
|
|
2020-08-28 20:04:06 +00:00
|
|
|
function funcs.feature(feature, pkgs)
|
|
|
|
assert(
|
|
|
|
type(feature) == 'string',
|
|
|
|
'Incorrect use of feature(): pass a feature name without _ as first argument')
|
|
|
|
|
|
|
|
if enabled_features[feature] then
|
|
|
|
handled_features[feature] = true
|
|
|
|
add_pkgs(pkgs)
|
2020-05-31 11:04:10 +00:00
|
|
|
end
|
2020-08-28 20:04:06 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function funcs.when(cond, pkgs)
|
|
|
|
assert(
|
|
|
|
type(cond) == 'boolean',
|
|
|
|
'Incorrect use of when(): pass a locical expression of _-prefixed strings as first argument')
|
|
|
|
|
|
|
|
if cond then
|
|
|
|
add_pkgs(pkgs)
|
2020-05-31 11:04:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-28 20:20:13 +00:00
|
|
|
-- Evaluate the feature definition files
|
|
|
|
for _, file in ipairs(files) do
|
|
|
|
local f, err = loadfile(file)
|
|
|
|
if not f then
|
|
|
|
error('Failed to parse feature definition: ' .. err)
|
|
|
|
end
|
|
|
|
setfenv(f, funcs)
|
|
|
|
f()
|
2020-08-28 18:34:54 +00:00
|
|
|
end
|
2020-05-31 11:04:10 +00:00
|
|
|
|
|
|
|
-- Handle default packages
|
|
|
|
for _, feature in ipairs(features) do
|
2020-08-28 20:04:06 +00:00
|
|
|
if not handled_features[feature] then
|
2020-05-31 11:04:10 +00:00
|
|
|
packages['gluon-' .. feature] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return collect_keys(packages)
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|