feat: generating plugins lua definition from nix
parent
954cfeca0e
commit
78c6045960
34
lib/deps.nix
34
lib/deps.nix
|
|
@ -6,7 +6,7 @@
|
||||||
types,
|
types,
|
||||||
...
|
...
|
||||||
}: let
|
}: let
|
||||||
inherit (builtins) isPath;
|
inherit (builtins) isPath foldl';
|
||||||
inherit (lib.attrsets) attrNames optionalAttrs;
|
inherit (lib.attrsets) attrNames optionalAttrs;
|
||||||
inherit (lib.lists) concatMap optional;
|
inherit (lib.lists) concatMap optional;
|
||||||
inherit (lib.strings) concatStringsSep fileContents;
|
inherit (lib.strings) concatStringsSep fileContents;
|
||||||
|
|
@ -98,6 +98,10 @@
|
||||||
cat <<'LUA' > $dir/config.lua
|
cat <<'LUA' > $dir/config.lua
|
||||||
${lua.wrapReturnFunction (getAllLua "config")}
|
${lua.wrapReturnFunction (getAllLua "config")}
|
||||||
LUA
|
LUA
|
||||||
|
|
||||||
|
cat <<'LUA' > $dir/deps.lua
|
||||||
|
return ${pluginsLuaDef plugins}
|
||||||
|
LUA
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -105,9 +109,37 @@
|
||||||
if isPath content
|
if isPath content
|
||||||
then fileContents content
|
then fileContents content
|
||||||
else content;
|
else content;
|
||||||
|
|
||||||
|
pluginLuaDef = memo: plugin: let
|
||||||
|
# plugin = builtins.removeAttrs plugin ["dependencies" "plugin"];
|
||||||
|
mkTypeFn = type: let
|
||||||
|
content = textOrContent plugin.${type};
|
||||||
|
in
|
||||||
|
optionalAttrs (! isNull plugin.${type}) {
|
||||||
|
${type} = with lua; lambda (raw content);
|
||||||
|
};
|
||||||
|
pluginName = plugin:
|
||||||
|
if plugin ? pname
|
||||||
|
then plugin.pname
|
||||||
|
else plugin.name;
|
||||||
|
hasDeps = plugin ? dependencies && plugin.dependencies != [];
|
||||||
|
name = pluginName plugin.plugin;
|
||||||
|
in
|
||||||
|
memo
|
||||||
|
// {
|
||||||
|
${name} =
|
||||||
|
{name = pluginName plugin.plugin;}
|
||||||
|
// (mkTypeFn "init")
|
||||||
|
// (mkTypeFn "config")
|
||||||
|
// (optionalAttrs hasDeps {
|
||||||
|
dependencies = map pluginName plugin.dependencies;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
pluginsLuaDef = plugins: lua.nix2lua (foldl' pluginLuaDef {} plugins);
|
||||||
in {
|
in {
|
||||||
inherit normalizePlugins;
|
inherit normalizePlugins;
|
||||||
inherit mkSlothFlakePlugin;
|
inherit mkSlothFlakePlugin;
|
||||||
inherit mkRuntimePlugin;
|
inherit mkRuntimePlugin;
|
||||||
inherit textOrContent;
|
inherit textOrContent;
|
||||||
|
inherit pluginsLuaDef;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
81
lib/lua.nix
81
lib/lua.nix
|
|
@ -1,6 +1,70 @@
|
||||||
{lib, ...}: let
|
{lib, ...}: let
|
||||||
inherit (lib.strings) removeSuffix;
|
inherit (lib.strings) removeSuffix;
|
||||||
|
inherit (builtins) match;
|
||||||
|
handleAst = data: let
|
||||||
|
ast = data.__ast;
|
||||||
|
in
|
||||||
|
if ast == "raw"
|
||||||
|
then data.data
|
||||||
|
else if ast == "return"
|
||||||
|
# TODO nix2lua can return null
|
||||||
|
then "return ${nix2lua data.data}"
|
||||||
|
else if ast == "fn"
|
||||||
|
then "function ${data.name}()\n${nix2lua data.data}\nend"
|
||||||
|
else abort ''Unknown ast type ${ast.__ast}'';
|
||||||
|
|
||||||
|
nix2lua = data: let
|
||||||
|
inherit (builtins) isInt isBool isNull isString isList isPath isAttrs isFunction typeOf concatStringsSep attrNames concatMap;
|
||||||
|
type = typeOf data;
|
||||||
|
condValue2list = val: value:
|
||||||
|
if isNull val
|
||||||
|
then []
|
||||||
|
else [value];
|
||||||
|
value2list = val: condValue2list val val;
|
||||||
|
in
|
||||||
|
if data ? __ast
|
||||||
|
then handleAst data
|
||||||
|
else if isInt data
|
||||||
|
then toString data
|
||||||
|
else if isBool data
|
||||||
|
then
|
||||||
|
if data
|
||||||
|
then "true"
|
||||||
|
else "false"
|
||||||
|
else if isString data || isPath data
|
||||||
|
then ''"${data}"''
|
||||||
|
else if isNull data
|
||||||
|
then "nil"
|
||||||
|
else if isFunction data
|
||||||
|
then (builtins.trace "Skipping function" null)
|
||||||
|
else if isList data
|
||||||
|
then let
|
||||||
|
nix2luaList = val: value2list (nix2lua val);
|
||||||
|
listContent = concatStringsSep ", " (concatMap nix2luaList data);
|
||||||
|
in "{ ${listContent} }"
|
||||||
|
else if isAttrs data
|
||||||
|
then let
|
||||||
|
mkKeyValue = key: let
|
||||||
|
value = data.${key};
|
||||||
|
luaKey =
|
||||||
|
if isNull (match "[a-zA-Z_][a-zA-Z_0-9]+" key)
|
||||||
|
then ''["${key}"]''
|
||||||
|
else key;
|
||||||
|
luaValue = nix2lua value;
|
||||||
|
in
|
||||||
|
# TODO Handle indentifier keys
|
||||||
|
condValue2list luaValue ''
|
||||||
|
${luaKey} = ${luaValue},
|
||||||
|
'';
|
||||||
|
attrsContent = concatMap mkKeyValue (attrNames data);
|
||||||
|
in ''
|
||||||
|
{
|
||||||
|
${concatStringsSep "" attrsContent}}
|
||||||
|
''
|
||||||
|
else abort ''Type "${type}"'';
|
||||||
in rec {
|
in rec {
|
||||||
|
inherit nix2lua;
|
||||||
|
|
||||||
wrapFunction = content: "function()\n${content}\nend";
|
wrapFunction = content: "function()\n${content}\nend";
|
||||||
wrapReturnFunction = content: "return ${wrapFunction content}";
|
wrapReturnFunction = content: "return ${wrapFunction content}";
|
||||||
wrapSelfInvokingFunction = {
|
wrapSelfInvokingFunction = {
|
||||||
|
|
@ -11,4 +75,21 @@ in rec {
|
||||||
(${wrapFunction (removeSuffix "\n" lua)})();
|
(${wrapFunction (removeSuffix "\n" lua)})();
|
||||||
-- end ${section}
|
-- end ${section}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
raw = data: {
|
||||||
|
inherit data;
|
||||||
|
__ast = "raw";
|
||||||
|
};
|
||||||
|
|
||||||
|
function = name: data: {
|
||||||
|
inherit name data;
|
||||||
|
__ast = "fn";
|
||||||
|
};
|
||||||
|
|
||||||
|
lambda = function "";
|
||||||
|
|
||||||
|
return = data: {
|
||||||
|
inherit data;
|
||||||
|
__ast = "return";
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue