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,54 +1,93 @@
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
abort "template `${entry.template}` not found"
else
applyTemplate entry foundTemplateFile; applyTemplate entry foundTemplateFile;
in /*sh*/'' replaceSuffix = from: to: string:
if !(hasSuffix from string) then
abort "invalid suffix `${from}` for string `${string}`"
else
concatStrings [ (removeSuffix from string) to ];
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 ${concatMapStrings
(result: /*sh*/'' (result: /*sh*/''
mkdir -p $out/${dirOf result.file} mkdir -p $out/${dirOf result.file}
@ -56,4 +95,5 @@ in /*sh*/''
'') '')
(forEach entries processEntryFile) (forEach entries processEntryFile)
} }
'' '';
}