nte/engine.nix

65 lines
1.7 KiB
Nix
Raw Normal View History

2024-05-22 21:19:37 +02:00
pkgs: {extraArgs, entries, templates}: let
inherit (pkgs) lib;
inherit (builtins) abort;
inherit (lib.lists) elemAt forEach findFirst length remove;
inherit (lib.strings) concatMapStrings concatStrings escapeShellArg isString splitString;
inherit (lib.trivial) functionArgs;
args = {inherit pkgs;}
// (import ./stdlib.nix pkgs)
// extraArgs;
getDirectory = file: let
splitPath = splitString "/" file;
last = elemAt splitPath ((length splitPath) - 1);
in
concatStrings (remove last splitPath);
findTemplateFile = entry:
findFirst (templateFile: let
templateFn = import templateFile;
template = templateFn (functionArgs templateFn);
in
template.name == entry.template)
null
templates;
applyTemplate = entry: templateFile: let
templateFn = import templateFile;
template = templateFn (args // entry);
in
if (isString template.output)
then {
inherit (entry) file;
inherit (template) output;
}
else let
newEntry = template.output;
foundTemplateFile = findTemplateFile newEntry;
in
if foundTemplateFile == null then
2024-05-25 01:37:17 +02:00
abort "template `${newEntry.template}` not found"
2024-05-22 21:19:37 +02:00
else
applyTemplate newEntry foundTemplateFile;
processEntryFile = entryFile: let
entryFn = import entryFile;
entry = entryFn args;
foundTemplateFile = findTemplateFile entry;
in
if foundTemplateFile == null then
abort "template ${entry.template} not found"
else
applyTemplate entry foundTemplateFile;
in /*sh*/''
${concatMapStrings
(result: /*sh*/''
mkdir -p $out/${getDirectory result.file}
echo ${escapeShellArg result.output} > $out/${result.file}
'')
(forEach entries processEntryFile)
}
''