big changes, remove file from entry

This commit is contained in:
jacekpoz 2024-05-25 15:50:10 +02:00
parent 851e39d544
commit 9fc59b7048
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8

View file

@ -1,59 +1,99 @@
pkgs: {extraArgs, entries, templates}: let pkgs: src: {extraArgs, entries, templates}: let
inherit (pkgs) lib; inherit (pkgs) lib;
inherit (builtins) abort dirOf; inherit (builtins) abort dirOf toString;
inherit (lib.attrsets) hasAttr;
inherit (lib.lists) forEach findFirst; inherit (lib.lists) forEach findFirst;
inherit (lib.strings) concatMapStrings concatStrings escapeShellArg isString; inherit (lib.path) removePrefix;
inherit (lib.strings) concatMapStrings concatStrings escapeShellArg hasSuffix isString removeSuffix;
inherit (lib.trivial) functionArgs; inherit (lib.trivial) functionArgs;
args = {inherit pkgs;} args = {inherit pkgs;}
// (import ./stdlib.nix pkgs) // (import ./stdlib.nix pkgs)
// extraArgs; // extraArgs;
isBaseTemplate = template:
isString template.output;
findTemplateFile = entry: findTemplateFile = entry: let
findFirst (templateFile: let _template = findFirst (templateFile: let
templateFn = import templateFile; templateFn = import templateFile;
template = templateFn (functionArgs templateFn); template = templateFn (functionArgs templateFn);
in in
template.name == entry.template) template.name == entry.template)
null null
templates; templates;
in
if _template == null then
abort "unknown template `${entry.template}`"
else
_template;
applyTemplate = entry: templateFile: let applyTemplate = entry: templateFile: let
templateFn = import templateFile; templateFn = import templateFile;
template = templateFn (args // entry); template = templateFn (args // entry);
in in
if (isString template.output) if isBaseTemplate template then {
then {
inherit (entry) file; inherit (entry) file;
inherit (template) output; inherit (template) output;
} }
else let else let
newEntry = template.output; newEntry = template.output // {inherit (entry) file;};
foundTemplateFile = findTemplateFile newEntry; foundTemplateFile = findTemplateFile newEntry;
in in
if foundTemplateFile == null then
abort "template `${newEntry.template}` not found"
else
applyTemplate newEntry foundTemplateFile; applyTemplate newEntry foundTemplateFile;
processEntryFile = entryFile: let processEntryFile = entryFile: let
entryFn = import entryFile; entry = getEntry entryFile;
entry = entryFn args;
foundTemplateFile = findTemplateFile entry; foundTemplateFile = findTemplateFile entry;
in in
if foundTemplateFile == null then applyTemplate entry foundTemplateFile;
abort "template `${entry.template}` not found"
else
applyTemplate entry foundTemplateFile;
in /*sh*/'' replaceSuffix = from: to: string:
${concatMapStrings if !(hasSuffix from string) then
(result: /*sh*/'' abort "invalid suffix `${from}` for string `${string}`"
mkdir -p $out/${dirOf result.file} else
echo ${escapeShellArg result.output} > $out/${result.file} concatStrings [ (removeSuffix from string) to ];
'')
(forEach entries processEntryFile) getTemplateFormat = entry: templateFile: let
} templateFn = import templateFile;
'' # getEntry needs to go down to the base template for the format
# but any template through the way can ask for a file
# so we just give it a placeholder - an empty string here
# since the output of this thing doesn't matter
template = templateFn (args // entry // { file = ""; });
in
if isBaseTemplate template then
template.format
else let
newEntry = template.output;
foundTemplateFile = findTemplateFile newEntry;
in
getTemplateFormat newEntry foundTemplateFile;
getEntry = entryFile: let
sourceFile = toString (removePrefix src entryFile);
entry = (import entryFile) args;
foundTemplateFile = findTemplateFile entry;
entryFormat = getTemplateFormat entry foundTemplateFile;
in
if !(hasAttr "file" entry) then
entry // {
file = replaceSuffix ".nix" ".${entryFormat}" sourceFile;
}
else
entry;
in {
inherit getEntry;
buildScript = /*sh*/''
${concatMapStrings
(result: /*sh*/''
mkdir -p $out/${dirOf result.file}
echo ${escapeShellArg result.output} > $out/${result.file}
'')
(forEach entries processEntryFile)
}
'';
}