1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-10 18:12:34 +09:00

formats.lua: init

Add a lua format, based on `lib.generators.toLua`.
This commit is contained in:
Matt Sturgeon 2025-03-15 11:57:08 +00:00
parent 0be0e7529b
commit 2611d5bd70
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
3 changed files with 162 additions and 1 deletions

View file

@ -402,6 +402,31 @@ have a predefined type and string generator already declared under
: Outputs the given attribute set as an Elixir map, instead of the : Outputs the given attribute set as an Elixir map, instead of the
default Elixir keyword list default Elixir keyword list
`pkgs.formats.lua { asBindings ? false, multiline ? true, columnWidth ? 100, indentWidth ? 2, indentUsingTabs ? false }`
: A function taking an attribute set with values
`asBindings` (default `false`)
: Whether to treat attributes as variable bindings
`multiline` (default `true`)
: Whether to procude a multiline output. The output may still wrap across
multiple lines if it would otherwise exceed `columnWidth`.
`columnWidth` (default `100`)
: The column width to use to attempt to wrap lines.
`indentWidth` (default `2`)
: The width of a single indentation level.
`indentUsingTabs` (default `false`)
: Whether the indentation should use tabs instead of spaces.
`pkgs.formats.php { finalVariable }` []{#pkgs-formats-php} `pkgs.formats.php { finalVariable }` []{#pkgs-formats-php}
: A function taking an attribute set with values : A function taking an attribute set with values

View file

@ -47,7 +47,7 @@ rec {
inherit (lib) mkOptionType; inherit (lib) mkOptionType;
inherit (lib.types) nullOr oneOf coercedTo listOf nonEmptyListOf attrsOf either; inherit (lib.types) nullOr oneOf coercedTo listOf nonEmptyListOf attrsOf either;
inherit (lib.types) bool int float str path; inherit (lib.types) bool int float str path luaInline;
json = {}: { json = {}: {
@ -541,6 +541,66 @@ rec {
''; '';
}; };
lua =
{
asBindings ? false,
multiline ? true,
columnWidth ? 100,
indentWidth ? 2,
indentUsingTabs ? false,
}:
{
type =
let
valueType =
nullOr (oneOf [
bool
float
int
path
str
luaInline
(attrsOf valueType)
(listOf valueType)
])
// {
description = "lua value";
descriptionClass = "noun";
};
in
if asBindings then attrsOf valueType else valueType;
generate =
name: value:
pkgs.callPackage (
{ runCommand, stylua }:
runCommand name
{
nativeBuildInputs = [ stylua ];
inherit columnWidth;
inherit indentWidth;
indentType = if indentUsingTabs then "Tabs" else "Spaces";
value = lib.generators.toLua { inherit asBindings multiline; } value;
passAsFile = [ "value" ];
preferLocalBuild = true;
}
''
${lib.optionalString (!asBindings) ''
echo -n 'return ' >> $out
''}
cat $valuePath >> $out
stylua \
--no-editorconfig \
--line-endings Unix \
--column-width $columnWidth \
--indent-width $indentWidth \
--indent-type $indentType \
$out
''
) { };
# Alias for mkLuaInline
lib.mkRaw = lib.mkLuaInline;
};
# Outputs a succession of Python variable assignments # Outputs a succession of Python variable assignments
# Useful for many Django-based services # Useful for many Django-based services
pythonVars = {}: { pythonVars = {}: {

View file

@ -601,6 +601,82 @@ in runBuildTests {
''; '';
}; };
luaTable = shouldPass {
format = formats.lua { };
input = {
null = null;
false = false;
true = true;
int = 10;
float = 3.141;
str = "foo";
attrs.foo = null;
list = [
null
null
];
path = ./testfile;
inline = lib.mkLuaInline "hello('world')";
};
expected = ''
return {
["attrs"] = {
["foo"] = nil,
},
["false"] = false,
["float"] = 3.141,
["inline"] = (hello("world")),
["int"] = 10,
["list"] = {
nil,
nil,
},
["null"] = nil,
["path"] = "${./testfile}",
["str"] = "foo",
["true"] = true,
}
'';
};
luaBindings = shouldPass {
format = formats.lua {
asBindings = true;
};
input = {
null = null;
_false = false;
_true = true;
int = 10;
float = 3.141;
str = "foo";
attrs.foo = null;
list = [
null
null
];
path = ./testfile;
inline = lib.mkLuaInline "hello('world')";
};
expected = ''
_false = false
_true = true
attrs = {
["foo"] = nil,
}
float = 3.141
inline = (hello("world"))
int = 10
list = {
nil,
nil,
}
null = nil
path = "${./testfile}"
str = "foo"
'';
};
phpAtoms = shouldPass rec { phpAtoms = shouldPass rec {
format = formats.php { finalVariable = "config"; }; format = formats.php { finalVariable = "config"; };
input = { input = {