move the bulk of the engine into stdlib.nix

This commit is contained in:
jacekpoz 2024-09-13 14:44:24 +02:00
parent 592db7009f
commit 8f4ecd557a
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
2 changed files with 86 additions and 75 deletions

View file

@ -1,86 +1,26 @@
pkgs: src: {extraArgs, entries, templates}: let
inherit (pkgs) lib;
inherit (builtins) abort dirOf toString;
inherit (lib.attrsets) hasAttr;
inherit (lib.lists) forEach findFirst;
inherit (lib.path) removePrefix;
inherit (lib.strings) concatMapStrings concatStrings escapeShellArg hasSuffix isString removeSuffix;
inherit (lib.trivial) functionArgs;
inherit (builtins) dirOf;
inherit (lib.lists) forEach;
inherit (lib.strings) concatMapStrings escapeShellArg;
args = {inherit pkgs getEntry applyTemplate;}
// (import ./stdlib.nix pkgs)
// extraArgs;
stdlib = import ./stdlib.nix pkgs;
isBaseTemplate = template:
isString template.output;
inherit (stdlib) applyTemplate findTemplateFn getEntry;
findTemplateFn = 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
(import template);
applyTemplate = templateFn: entry: let
template = templateFn (args // entry);
in
if isBaseTemplate template then {
inherit (entry) file;
inherit (template) output;
}
else let
newEntry = template.output // {inherit (entry) file;};
foundTemplateFn = findTemplateFn newEntry;
in
applyTemplate foundTemplateFn newEntry;
replaceSuffix = from: to: string:
if !(hasSuffix from string) then
abort "invalid suffix `${from}` for string `${string}`"
else
concatStrings [ (removeSuffix from string) to ];
getTemplateFormat = entry: templateFn: let
# 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;
foundTemplateFn = findTemplateFn newEntry;
in
getTemplateFormat newEntry foundTemplateFn;
getEntry = entryFile: let
sourceFile = toString (removePrefix src entryFile);
entry = (import entryFile) args;
foundTemplateFn = findTemplateFn entry;
entryFormat = getTemplateFormat entry foundTemplateFn;
in
if !(hasAttr "file" entry) then
entry // {
file = replaceSuffix ".nix" ".${entryFormat}" sourceFile;
}
else
entry;
args = {
inherit pkgs;
applyTemplate = applyTemplate args;
findTemplateFn = findTemplateFn templates;
getEntry = getEntry args src;
} // extraArgs;
processEntryFile = entryFile: let
foundTemplateFn = findTemplateFn entry;
entry = getEntry entryFile;
foundTemplateFn = findTemplateFn templates entry;
entry = getEntry args src entryFile;
in
applyTemplate foundTemplateFn entry;
applyTemplate args foundTemplateFn entry;
in /*sh*/''
${concatMapStrings

View file

@ -1,7 +1,78 @@
pkgs: let
inherit (pkgs) lib runCommand;
inherit (lib.strings) readFile;
inherit (builtins) abort toString;
inherit (lib.attrsets) hasAttr;
inherit (lib.lists) findFirst;
inherit (lib.path) removePrefix;
inherit (lib.strings) concatStrings hasSuffix isString readFile removeSuffix;
inherit (lib.trivial) functionArgs;
isBaseTemplate = template:
isString template.output;
findTemplateFn = templates: templateName: let
template = findFirst (templateFile: let
templateFn = import templateFile;
template' = templateFn (functionArgs templateFn);
in
template'.name == templateName)
null
templates;
in
if template == null then
abort "unknown template `${templateName}`"
else
(import template);
applyTemplate = args: templateFn: entry: let
template = templateFn (args // entry);
in
if isBaseTemplate template then {
inherit (entry) file;
inherit (template) output;
}
else let
newEntry = template.output // {inherit (entry) file;};
foundTemplateFn = findTemplateFn newEntry;
in
applyTemplate foundTemplateFn newEntry;
replaceSuffix = from: to: string:
if !(hasSuffix from string) then
abort "invalid suffix `${from}` for string `${string}`"
else
concatStrings [ (removeSuffix from string) to ];
getTemplateFormat = args: entry: templateFn: let
# 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;
foundTemplateFn = findTemplateFn newEntry;
in
getTemplateFormat newEntry foundTemplateFn;
getEntry = args: sourceDir: entryFile: let
sourceFile = toString (removePrefix sourceDir entryFile);
entry = (import entryFile) args;
foundTemplateFn = findTemplateFn entry;
entryFormat = getTemplateFormat entry foundTemplateFn;
in
if !(hasAttr "file" entry) then
entry // {
file = replaceSuffix ".nix" ".${entryFormat}" sourceFile;
}
else
entry;
in {
inherit applyTemplate findTemplateFn getEntry;
run = script: readFile (runCommand "run" {} ''
echo $(${script}) > $out
'');