-
Notifications
You must be signed in to change notification settings - Fork 53
feat(wrapperModulers.stylua): init #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kuppo
wants to merge
13
commits into
BirdeeHub:main
Choose a base branch
from
kuppo:stylua
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+303
−0
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9ed5585
feat(wrapperModulers.stylua): init
e638bd7
feat(wrapperModules.stylua): remove the defaultStyle option
04455cb
feat(wrapperModules.stylua): update the cp script
08de140
docs(wrapperModules.stylua): update doc
7737f00
style(wrapperModules.stylua): reformat the code a bit
4fb88ac
fix(wrapperModules.stylua): add condition to generate file
8a699a4
test(wrapperModules.stylua): add test for stylua wrapper
1fe53c8
style(wapperModules.stylua): format code
a1454ea
test(wrapperModules.stylua): add test for the wrapper program itself
5de6df5
feat(wrapperModules.stylua): optimize the cp script doc generation
3bcb95f
fix(wrapperModules.stylua): update cp script to handle missing stylua…
91b0e85
test(wrapperModules.stylua): split test into differnt categories
d7fc9aa
doc(wrapperModules.stylua): minor change to doc strings
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} | ||
| '' | ||
| ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| ''; | ||
| }; | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.