core/build: simplify; move parent functions to lib

This commit is contained in:
raf 2024-02-20 01:12:17 +03:00
commit 9b2e2ef833
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
3 changed files with 41 additions and 38 deletions

View file

@ -8,13 +8,16 @@
# - the addition of the function `entryBefore` indicating a "wanted # - the addition of the function `entryBefore` indicating a "wanted
# by" relationship. # by" relationship.
{lib}: let {lib}: let
inherit (lib) all filterAttrs nvim mapAttrs toposort; inherit (builtins) isAttrs map toJSON isString elem;
inherit (lib.attrsets) attrNames attrValues filterAttrs mapAttrs;
inherit (lib.lists) all toposort;
inherit (lib) nvim;
in { in {
empty = {}; empty = {};
isEntry = e: e ? data && e ? after && e ? before; isEntry = e: e ? data && e ? after && e ? before;
isDag = dag: isDag = dag:
builtins.isAttrs dag && all nvim.dag.isEntry (builtins.attrValues dag); isAttrs dag && all nvim.dag.isEntry (attrValues dag);
/* /*
Takes an attribute set containing entries built by entryAnywhere, Takes an attribute set containing entries built by entryAnywhere,
@ -76,17 +79,17 @@ in {
*/ */
topoSort = dag: let topoSort = dag: let
dagBefore = dag: name: dagBefore = dag: name:
builtins.attrNames attrNames
(filterAttrs (_n: v: builtins.elem name v.before) dag); (filterAttrs (_n: v: elem name v.before) dag);
normalizedDag = normalizedDag =
mapAttrs (n: v: { mapAttrs (n: v: {
inherit (v) data;
name = n; name = n;
data = v.data;
after = v.after ++ dagBefore dag n; after = v.after ++ dagBefore dag n;
}) })
dag; dag;
before = a: b: builtins.elem a.name b.after; before = a: b: elem a.name b.after;
sorted = toposort before (builtins.attrValues normalizedDag); sorted = toposort before (attrValues normalizedDag);
in in
if sorted ? result if sorted ? result
then { then {
@ -104,4 +107,23 @@ in {
entryAfter = nvim.dag.entryBetween []; entryAfter = nvim.dag.entryBetween [];
entryBefore = before: nvim.dag.entryBetween before []; entryBefore = before: nvim.dag.entryBetween before [];
resolveDag = {
name,
dag,
mapResult,
}: let
# When the value is a string, default it to dag.entryAnywhere
finalDag = lib.mapAttrs (_: value:
if isString value
then nvim.dag.entryAnywhere value
else value)
dag;
sortedDag = nvim.dag.topoSort finalDag;
result =
if sortedDag ? result
then mapResult sortedDag.result
else abort ("Dependency cycle in ${name}: " + toJSON sortedDag);
in
result;
} }

View file

@ -1,4 +1,3 @@
# From home-manager: https://github.com/nix-community/home-manager/blob/master/modules/lib/booleans.nix
{lib}: { {lib}: {
# Converts a boolean to a yes/no string. This is used in lots of # Converts a boolean to a yes/no string. This is used in lots of
# configuration formats. # configuration formats.

View file

@ -4,12 +4,13 @@
lib, lib,
... ...
}: let }: let
inherit (builtins) attrValues attrNames map mapAttrs toJSON isString concatStringsSep filter; inherit (builtins) attrValues attrNames map mapAttrs concatStringsSep filter;
inherit (lib) mkOption types mapAttrsFlatten filterAttrs optionalString getAttrs literalExpression; inherit (lib) mkOption types mapAttrsFlatten filterAttrs optionalString getAttrs literalExpression;
inherit (lib) nvim; inherit (lib) nvim;
inherit (nvim.lua) toLuaObject; inherit (nvim.lua) toLuaObject;
inherit (nvim.vim) valToVim; inherit (nvim.vim) valToVim;
inherit (nvim.bool) mkBool; inherit (nvim.bool) mkBool;
inherit (nvim.dag) resolveDag;
cfg = config.vim; cfg = config.vim;
@ -179,11 +180,13 @@ in {
}; };
config = let config = let
filterNonNull = mappings: filterAttrs (_name: value: value != null) mappings;
globalsScript = globalsScript =
mapAttrsFlatten (name: value: "let g:${name}=${valToVim value}") mapAttrsFlatten (name: value: "let g:${name}=${valToVim value}")
(filterNonNull cfg.globals); (filterNonNull cfg.globals);
# TODO: everything below this line needs to be moved to lib
filterNonNull = mappings: filterAttrs (_name: value: value != null) mappings;
toLuaBindings = mode: maps: toLuaBindings = mode: maps:
map (value: '' map (value: ''
vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config}) vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config})
@ -202,24 +205,12 @@ in {
omap = toLuaBindings "o" config.vim.maps.operator; omap = toLuaBindings "o" config.vim.maps.operator;
icmap = toLuaBindings "ic" config.vim.maps.insertCommand; icmap = toLuaBindings "ic" config.vim.maps.insertCommand;
resolveDag = { mkSection = r: ''
name, -- SECTION: ${r.name}
dag, ${r.data}
mapResult, '';
}: let
# When the value is a string, default it to dag.entryAnywhere mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r)));
finalDag = lib.mapAttrs (_: value:
if isString value
then nvim.dag.entryAnywhere value
else value)
dag;
sortedDag = nvim.dag.topoSort finalDag;
result =
if sortedDag ? result
then mapResult sortedDag.result
else abort ("Dependency cycle in ${name}: " + toJSON sortedDag);
in
result;
in { in {
vim = { vim = {
startPlugins = map (x: x.package) (attrValues cfg.extraPlugins); startPlugins = map (x: x.package) (attrValues cfg.extraPlugins);
@ -227,11 +218,6 @@ in {
globalsScript = nvim.dag.entryAnywhere (concatStringsSep "\n" globalsScript); globalsScript = nvim.dag.entryAnywhere (concatStringsSep "\n" globalsScript);
luaScript = let luaScript = let
mkSection = r: ''
-- SECTION: ${r.name}
${r.data}
'';
mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r)));
luaConfig = resolveDag { luaConfig = resolveDag {
name = "lua config script"; name = "lua config script";
dag = cfg.luaConfigRC; dag = cfg.luaConfigRC;
@ -241,11 +227,6 @@ in {
nvim.dag.entryAfter ["globalsScript"] luaConfig; nvim.dag.entryAfter ["globalsScript"] luaConfig;
extraPluginConfigs = let extraPluginConfigs = let
mkSection = r: ''
-- SECTION: ${r.name}
${r.data}
'';
mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r)));
extraPluginsDag = mapAttrs (_: { extraPluginsDag = mapAttrs (_: {
after, after,
setup, setup,
@ -253,6 +234,7 @@ in {
}: }:
nvim.dag.entryAfter after setup) nvim.dag.entryAfter after setup)
cfg.extraPlugins; cfg.extraPlugins;
pluginConfig = resolveDag { pluginConfig = resolveDag {
name = "extra plugins config"; name = "extra plugins config";
dag = extraPluginsDag; dag = extraPluginsDag;