-
Notifications
You must be signed in to change notification settings - Fork 483
Respect .editorconfig
settings for formatting shell via shfmt
#2031
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
Changes from all commits
4898462
d95436a
4ae13c9
1b3335d
733f7da
3c2701d
8bf8d8f
4c4b7bc
2c38247
28b079c
62f27ca
42b43dc
fc82b24
06325f8
6e52d6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
import com.diffplug.spotless.shell.ShfmtStep; | ||
|
||
public class ShellExtension extends FormatExtension { | ||
private static final String SHELL_FILE_EXTENSION = "*.sh"; | ||
private static final String SHELL_FILE_EXTENSION = "**/*.sh"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's very likely that project's aren't storing their shell scripts as siblings to the build file so this updates the search to be more inclusive by default. |
||
|
||
static final String NAME = "shell"; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,19 +23,52 @@ | |
|
||
@ShfmtTest | ||
public class ShellExtensionTest extends GradleIntegrationHarness { | ||
|
||
@Test | ||
void shfmt() throws IOException { | ||
setFile("build.gradle").toLines( | ||
void shfmtWithEditorconfig() throws IOException { | ||
String fileDir = "shell/shfmt/with-config/"; | ||
|
||
setFile("build.gradle.kts").toLines( | ||
"plugins {", | ||
" id 'com.diffplug.spotless'", | ||
" id(\"com.diffplug.spotless\")", | ||
"}", | ||
"spotless {", | ||
" shell {", | ||
" shfmt()", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we can add an option to override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I can tell, when configuring ktlint for formatting, you actually provide the .editorconfig path to ktlint and that is an input supported by ktlint. As far as I can tell, there is no way to provide an With that said, I did request that an input flag be added for situations like this: mvdan/sh#1055 |
||
" }", | ||
"}"); | ||
setFile("shfmt.sh").toResource("shell/shfmt/shfmt.sh"); | ||
|
||
setFile(".editorconfig").toResource(fileDir + ".editorconfig"); | ||
|
||
setFile("shfmt.sh").toResource(fileDir + "shfmt.sh"); | ||
setFile("scripts/other.sh").toResource(fileDir + "other.sh"); | ||
|
||
gradleRunner().withArguments("spotlessApply").build(); | ||
assertFile("shfmt.sh").sameAsResource("shell/shfmt/shfmt.clean"); | ||
|
||
assertFile("shfmt.sh").sameAsResource(fileDir + "shfmt.clean"); | ||
assertFile("scripts/other.sh").sameAsResource(fileDir + "other.clean"); | ||
} | ||
|
||
@Test | ||
void shfmtWithoutEditorconfig() throws IOException { | ||
String fileDir = "shell/shfmt/without-config/"; | ||
|
||
setFile("build.gradle.kts").toLines( | ||
"plugins {", | ||
" id(\"com.diffplug.spotless\")", | ||
"}", | ||
"spotless {", | ||
" shell {", | ||
" shfmt()", | ||
" }", | ||
"}"); | ||
|
||
setFile("shfmt.sh").toResource(fileDir + "shfmt.sh"); | ||
setFile("scripts/other.sh").toResource(fileDir + "other.sh"); | ||
|
||
gradleRunner().withArguments("spotlessApply").build(); | ||
|
||
assertFile("shfmt.sh").sameAsResource(fileDir + "shfmt.clean"); | ||
assertFile("scripts/other.sh").sameAsResource(fileDir + "other.clean"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1021,11 +1021,40 @@ Uses Jackson and YAMLFactory to pretty print objects: | |
</jackson> | ||
``` | ||
|
||
## Shell | ||
|
||
- `com.diffplug.spotless.maven.FormatterFactory.addStepFactory(FormatterStepFactory)` [code](./src/main/java/com/diffplug/spotless/maven/shell/Shell.java) | ||
|
||
```xml | ||
<configuration> | ||
<shell> | ||
<includes> <!-- Not required. Defaults to **/*.sh --> | ||
<include>scripts/**/*.sh</include> | ||
</includes> | ||
|
||
<shfmt /> <!-- has its own section below --> | ||
</shell> | ||
</configuration> | ||
``` | ||
|
||
### shfmt | ||
|
||
[homepage](https://github.com/mvdan/sh). [changelog](https://github.com/mvdan/sh/blob/master/CHANGELOG.md). | ||
|
||
When formatting shell scripts via `shfmt`, configure `shfmt` settings via `.editorconfig`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we can note a bit more here, that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is an incorrect assumption, surprisingly. If I were to have the following project structure, all shell files will respect the
|
||
|
||
```xml | ||
<shfmt> | ||
<version>3.7.0</version> <!-- optional: Custom version of 'mvdan/sh' --> | ||
<pathToExe>/opt/homebrew/bin/shfmt</pathToExe> <!-- optional: if shfmt is not on your path, you must specify its location manually --> | ||
</shfmt> | ||
``` | ||
|
||
## Gherkin | ||
|
||
- `com.diffplug.spotless.maven.FormatterFactory.addStepFactory(FormatterStepFactory)` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/gherkin/Gherkin.java) | ||
|
||
```gradle | ||
```xml | ||
<configuration> | ||
<gherkin> | ||
<includes> <!-- You have to set the target manually --> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
|
||
[*.sh] | ||
indent_style = space | ||
indent_size = 2 | ||
space_redirects = true | ||
switch_case_indent = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
|
||
fruit="apple" | ||
|
||
case $fruit in | ||
"apple") | ||
echo "This is a red fruit." | ||
;; | ||
"banana") | ||
echo "This is a yellow fruit." | ||
;; | ||
"orange") | ||
echo "This is an orange fruit." | ||
;; | ||
*) | ||
echo "Unknown fruit." | ||
;; | ||
esac | ||
|
||
echo "This is some text." > output.txt |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
|
||
fruit="apple" | ||
|
||
case $fruit in | ||
"apple") | ||
echo "This is a red fruit." | ||
;; | ||
"banana") | ||
echo "This is a yellow fruit." | ||
;; | ||
"orange") | ||
echo "This is an orange fruit." | ||
;; | ||
*) | ||
echo "Unknown fruit." | ||
;; | ||
esac | ||
|
||
echo "This is some text." > output.txt |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
|
||
fruit="apple" | ||
|
||
case $fruit in | ||
"apple") | ||
echo "This is a red fruit." | ||
;; | ||
"banana") | ||
echo "This is a yellow fruit." | ||
;; | ||
"orange") | ||
echo "This is an orange fruit." | ||
;; | ||
*) | ||
echo "Unknown fruit." | ||
;; | ||
esac | ||
|
||
echo "This is some text." >output.txt |
Uh oh!
There was an error while loading. Please reload this page.