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 (builtins) abort dirOf;
inherit (builtins) abort dirOf toString;
inherit (lib.attrsets) hasAttr;
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;
args = {inherit pkgs;}
// (import ./stdlib.nix pkgs)
// extraArgs;
isBaseTemplate = template:
isString template.output;
findTemplateFile = entry:
findFirst (templateFile: let
findTemplateFile = entry: let
_template = findFirst (templateFile: let
templateFn = import templateFile;
template = templateFn (functionArgs templateFn);
in
template.name == entry.template)
null
templates;
in
if _template == null then
abort "unknown template `${entry.template}`"
else
_template;
applyTemplate = entry: templateFile: let
templateFn = import templateFile;
template = templateFn (args // entry);
in
if (isString template.output)
then {
if isBaseTemplate template then {
inherit (entry) file;
inherit (template) output;
}
else let
newEntry = template.output;
newEntry = template.output // {inherit (entry) file;};
foundTemplateFile = findTemplateFile newEntry;
in
if foundTemplateFile == null then
abort "template `${newEntry.template}` not found"
else
applyTemplate newEntry foundTemplateFile;
processEntryFile = entryFile: let
entryFn = import entryFile;
entry = entryFn args;
entry = getEntry entryFile;
foundTemplateFile = findTemplateFile entry;
in
if foundTemplateFile == null then
abort "template `${entry.template}` not found"
else
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
(result: /*sh*/''
mkdir -p $out/${dirOf result.file}
@ -56,4 +95,5 @@ in /*sh*/''
'')
(forEach entries processEntryFile)
}
''
'';
}