Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions maintainers/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,10 @@
github = "aliaslion";
githubId = "122117018";
};
kuppo = {
name = "Peng Lei";
email = "swk-pl@163.com";
github = "kuppo";
githubId = "17398733";
};
}
176 changes: 176 additions & 0 deletions wrapperModules/s/stylua/check.nix
Comment thread
kuppo marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
pkgs,
self,
tlib,
...
}:
let
inherit (tlib)
fileContains
isDirectory
isFile
notIsFile
areEqual
test
;
default = self.wrappers.stylua.wrap {
inherit pkgs;
};
styluaFile = "styles/stylua.toml";
styluaToml = "/tmp/stylua.toml";
styluaTomlContent = ''
call_parentheses = "None"
column_width = 100
quote-style = "ForceSingle"
'';
in
test { wrapper = "stylua"; } {
"stylua default wrapper test" = [
(isDirectory default)
(notIsFile "${default}/${styluaFile}")
(notIsFile "${default}/bin/cp_stylua_toml")
# prepare the test lua file
''
echo "print 'ugly and bad but or and good'" > /tmp/test.lua
''
''
output=$("${default}/bin/stylua" -c /tmp/test.lua 2>&1)
echo "$output" | grep -q 'print("ugly and bad but or and good")'
''

# clean up
''
rm /tmp/test.lua
''
];

"customized style wrapper test" =
let
styluaWrapper = default.wrap {
customStyle = {
call_parentheses = "None";
column_width = 100;
quote_style = "ForceSingle";
};
};
in
[
(isDirectory styluaWrapper)
(isFile "${styluaWrapper}/${styluaFile}")
(fileContains "${styluaWrapper}/${styluaFile}" "${styluaTomlContent}")
(notIsFile "${styluaWrapper}/bin/cp_stylua_toml")

# prepare the test lua file
''
echo "print 'ugly and bad but or and good'" > /tmp/test.lua
''

''
[[ $(${styluaWrapper}/bin/stylua -c /tmp/test.lua 2>&1) == "" ]]
''

# clean up
''
rm /tmp/test.lua
''
];

"copy script enabled wrapper test" =
let
cpScriptWrapper = default.wrap {
customStyle = {
call_parentheses = "None";
column_width = 100;
quote_style = "ForceSingle";
};
generateCpScript = {
enable = true;
};
};
in
[

(isDirectory cpScriptWrapper)
(isFile "${cpScriptWrapper}/${styluaFile}")
(fileContains "${cpScriptWrapper}/${styluaFile}" "${styluaTomlContent}")
(isFile "${cpScriptWrapper}/bin/cp_stylua_toml")
(fileContains "${cpScriptWrapper}/bin/cp_stylua_toml" "bin/sh")

''
cd /tmp && ${cpScriptWrapper}/bin/cp_stylua_toml && cat stylua.toml && rm -f ${styluaToml}
''
];

"customized copy script name wrapper test" =
let
cpScriptNameWrapper = default.wrap {
customStyle = {
call_parentheses = "None";
column_width = 100;
quote_style = "ForceSingle";
};
generateCpScript = {
enable = true;
name = "./bin/test_script";
};
};
in
[

(isDirectory cpScriptNameWrapper)
(isFile "${cpScriptNameWrapper}/${styluaFile}")
(fileContains "${cpScriptNameWrapper}/${styluaFile}" "${styluaTomlContent}")
(isFile "${cpScriptNameWrapper}/bin/test_script")
(fileContains "${cpScriptNameWrapper}/bin/test_script" "bin/sh")

''
cd /tmp && ${cpScriptNameWrapper}/bin/test_script && \
[[ -e ${styluaToml} ]] && [[ -w ${styluaToml} ]] && \
grep -i "forcesingle" ${styluaToml} && rm -f ${styluaToml}
''

''
${cpScriptNameWrapper}/bin/test_script -h |
grep -i "add-doc" && [[ ! -e ${styluaToml} ]]
''

''
${cpScriptNameWrapper}/bin/test_script --help |
grep -i "add-doc" && [[ ! -e ${styluaToml} ]]
''

''
cd /tmp && ${cpScriptNameWrapper}/bin/test_script -i && \
[[ -e ${styluaToml} ]] && [[ -w ${styluaToml} ]] && \
grep -i 'enabled = true|false' ${styluaToml} && rm -f ${styluaToml}
''

''
cd /tmp && ${cpScriptNameWrapper}/bin/test_script --add-doc && \
[[ -e ${styluaToml} ]] && [[ -w ${styluaToml} ]] && \
grep -i 'enabled = true|false' ${styluaToml} && rm -f ${styluaToml}
''
];

"copy script only wrapper test" =
let
cpScriptOnlyWrapper = default.wrap {
generateCpScript.enable = true;
};
in
[
(isDirectory cpScriptOnlyWrapper)
(isFile "${cpScriptOnlyWrapper}/bin/cp_stylua_toml")
(notIsFile "${cpScriptOnlyWrapper}/${styluaFile}")

''
cd /tmp && ${cpScriptOnlyWrapper}/bin/cp_stylua_toml | grep "have not generated stylua.toml" \
&& [[ ! -e ${styluaToml} ]]
''

''
cd /tmp && ${cpScriptOnlyWrapper}/bin/cp_stylua_toml -i \
&& [[ -e ${styluaToml} ]] && grep 'enabled = true|false' ${styluaToml} && rm -f ${styluaToml}
''
];
}
121 changes: 121 additions & 0 deletions wrapperModules/s/stylua/module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
wlib,
lib,
config,
pkgs,
...
}:
{
imports = [ wlib.modules.default ];

options = {
customStyle = lib.mkOption {
type = wlib.types.structuredValueWith {
nullable = false;
typeName = "TOML";
};
default = { };
description = ''
nix configuration for the stylua.

Check [StyLua options](https://github.com/JohnnyMorganz/StyLua/blob/main/README.md#options).
'';
example = lib.literalExpression ''
settings = {
call_parentheses = "Always";
column_width = 100;
collapse_simple_statement = "Always";
indent_type = "Spaces";
indent_width = 2;
quote_style = "ForceDouble";
sort_requires.enabled = true;
};
'';
};
generateCpScript = lib.mkOption {
default = { };
description = ''
Options for copy script which help you quickly copy your
settings into `$CWD` for further customization.

The script's name can be customized.
With the `-i|--add-doc` option, it will add the configuraiton
documentation to the end of the copied file.
'';
type = lib.types.submodule {
options = {
enable = lib.mkEnableOption ''
generating a copy script.

If you don't add `customStyle`, the script will of cause copy nothing.
But you can use the `-i|--add-doc` option to generate one with only
documentation added.
'';
name = lib.mkOption {
type = lib.types.str;
default = "cp_stylua_toml";
description = ''
Customize the name of the copy script. If the name has `/` in it,
the wrapper will ignore and only take the base name.
'';
};
};
};
};
};
config = {
package = lib.mkDefault pkgs.stylua;
constructFiles.generatedConfig = lib.mkIf (config.customStyle != { }) {
content = builtins.toJSON config.customStyle;
relPath = "styles/stylua.toml";
builder = ''${pkgs.remarshal}/bin/json2toml "$1" "$2"'';
};
constructFiles."${baseNameOf config.generateCpScript.name}" =
lib.mkIf config.generateCpScript.enable
{
relPath = "bin/${baseNameOf config.generateCpScript.name}";
builder = "cp $1 $2 && chmod +x $2";
content = ''
#!${pkgs.bash}/bin/sh
help=$'cp_stylua_toml [-h|--help|-i|--add-doc]\nCopy stylua files.\nOptions:\n\t-h|--help\tPrint this help\n\t-i|--add-doc\tAdd the configuration doccumentation to the end of the stylua.toml'

target=$(pwd)/stylua.toml
source=${placeholder config.outputName}/styles/stylua.toml

doc=$(${placeholder config.outputName}/bin/stylua --help \
| ${pkgs.gawk}/bin/awk '/^FORMATTING OPTIONS:/{f=1;next}f{sub(/^[[:space:]]*/,"");if($0=="")next;sub(/^--/,"** ");gsub(/ *<[^>]*>/,"");gsub(/-/,"_");if($0=="Enable requires sorting")$0=$0" [enabled = true|false]";if(/^\*\*/)printf "\n";print "# "$0}')

if [ "$#" -ne 1 ]; then
[[ ! -e "$source" ]] \
&& echo "You have not generated stylua.toml. You can use -i|--add-doc option to add a stylua.toml with only documentation included." \
&& exit 0
cp -f "$source" "$target" && chmod u+w "$target"
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "$help"
exit 0
elif [ "$1" == "-i" ] || [ "$1" == "--add-doc" ]; then
[[ -e "$source" ]] && cp -f "$source" "$target"
touch "$target" && chmod u+w "$target" && echo "$doc" >> "$target"
fi
'';
};
flags."--config-path" = lib.mkIf (
config.customStyle != { }
) config.constructFiles.generatedConfig.path;
meta = {
maintainers = with wlib.maintainers; [
kuppo
];
description = ''
Wrapper Module for [Stylua](https://github.com/JohnnyMorganz/StyLua).

The wrapper is used to customize the `stylua.toml` file.
You can add you options into `config.customStyle` with pure nix expressions.

The wrapper also provides a script which copys the generated `stylua.toml`
into `$CWD`, in case you want to include the confitugation into the repo or
customize it somehow.
'';
};
};
}
Loading