-
Notifications
You must be signed in to change notification settings - Fork 455
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
Add maven shell script formatting support via shfmt #1998
Merged
nedtwigg
merged 9 commits into
diffplug:main
from
tcrawford-figure:tylercrawford/issue-1567/support-sh-bash-maven
Jan 17, 2024
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d2fc6ca
Add maven shell script formattings support via shfmt
tcrawford-figure fc9b6ee
Update documentation to reflect maven shell formatting support
tcrawford-figure 88e95d3
Follow up with PR number
tcrawford-figure db75633
Fix support matrix
tcrawford-figure 96879e5
Put PR link in parentheses
tcrawford-figure e32e45d
Rename ShfmtIntegrationTest, disable ShellTest
tcrawford-figure e075ffa
Update Gradle changelog for shfmt support
tcrawford-figure 5351d78
Replace @Disabled with @ShfmtTest.
nedtwigg 4e5fa17
The special tests had a configuration bug at some point where their t…
nedtwigg 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 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 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 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 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 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
46 changes: 46 additions & 0 deletions
46
plugin-maven/src/main/java/com/diffplug/spotless/maven/shell/Shell.java
This file contains 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,46 @@ | ||
/* | ||
* Copyright 2024 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.maven.shell; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import org.apache.maven.project.MavenProject; | ||
|
||
import com.diffplug.spotless.maven.FormatterFactory; | ||
import com.diffplug.spotless.maven.generic.LicenseHeader; | ||
|
||
/** | ||
* A {@link FormatterFactory} implementation that corresponds to {@code <shell>...</shell>} configuration element. | ||
* <p> | ||
* It defines a formatter for shell source files that can execute both language agnostic (e.g. {@link LicenseHeader}) | ||
* and shell-specific (e.g. {@link Shfmt}) steps. | ||
*/ | ||
public class Shell extends FormatterFactory { | ||
@Override | ||
public Set<String> defaultIncludes(MavenProject project) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public String licenseHeaderDelimiter() { | ||
return null; | ||
} | ||
|
||
public void addShfmt(Shfmt shfmt) { | ||
addStepFactory(shfmt); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
plugin-maven/src/main/java/com/diffplug/spotless/maven/shell/Shfmt.java
This file contains 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,41 @@ | ||
/* | ||
* Copyright 2024 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.maven.shell; | ||
|
||
import org.apache.maven.plugins.annotations.Parameter; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.maven.FormatterStepConfig; | ||
import com.diffplug.spotless.maven.FormatterStepFactory; | ||
import com.diffplug.spotless.shell.ShfmtStep; | ||
|
||
public class Shfmt implements FormatterStepFactory { | ||
|
||
@Parameter | ||
private String version; | ||
|
||
@Parameter | ||
private String pathToExe; | ||
|
||
@Override | ||
public FormatterStep newFormatterStep(FormatterStepConfig config) { | ||
ShfmtStep shfmt = ShfmtStep.withVersion(version == null ? ShfmtStep.defaultVersion() : version); | ||
if (pathToExe != null) { | ||
shfmt = shfmt.withPathToExe(pathToExe); | ||
} | ||
return shfmt.create(); | ||
} | ||
} |
This file contains 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
36 changes: 36 additions & 0 deletions
36
plugin-maven/src/test/java/com/diffplug/spotless/maven/shell/ShellTest.java
This file contains 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,36 @@ | ||
/* | ||
* Copyright 2024 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.maven.shell; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.diffplug.spotless.maven.MavenIntegrationHarness; | ||
|
||
@Disabled // Disabled due to GHA not having shfmt | ||
public class ShellTest extends MavenIntegrationHarness { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ShellTest.class); | ||
|
||
@Test | ||
public void testFormatShell() throws Exception { | ||
writePomWithShellSteps("<shfmt/>"); | ||
setFile("shfmt.sh").toResource("shell/shfmt/shfmt.sh"); | ||
mavenRunner().withArguments("spotless:apply").runNoError(); | ||
assertFile("shfmt.sh").sameAsResource("shell/shfmt/shfmt.clean"); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please let me know if I missed some detail that enables these types of formatters (ones that need to shell out) to run in GitHub action workflows. I'd be happy to add whatever is needed to get this to run in GHA.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced
@Disabled
with@ShfmtTest
. If you rungradlew test
these tests will not run. They only run when you run./gradlew testShfmt
to run them.If you want them to run on CI, you should add them with the npm test
spotless/.github/workflows/ci.yml
Lines 79 to 81 in 797a665
./gradlew testNpm testShfmt
The only tricky part is getting shfmt onto the CI runner. Not that hard though. Feel free to open a PR for it and mess around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect! Thank you for the clarification.