-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shell script support via shfmt (#1994)
- Loading branch information
Showing
13 changed files
with
372 additions
and
6 deletions.
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
110 changes: 110 additions & 0 deletions
110
lib/src/main/java/com/diffplug/spotless/shell/ShfmtStep.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,110 @@ | ||
/* | ||
* 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.shell; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.diffplug.spotless.ForeignExe; | ||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.ProcessRunner; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
public class ShfmtStep { | ||
public static String name() { | ||
return "shfmt"; | ||
} | ||
|
||
public static String defaultVersion() { | ||
return "3.7.0"; | ||
} | ||
|
||
private final String version; | ||
private final @Nullable String pathToExe; | ||
|
||
private ShfmtStep(String version, @Nullable String pathToExe) { | ||
this.version = version; | ||
this.pathToExe = pathToExe; | ||
} | ||
|
||
public static ShfmtStep withVersion(String version) { | ||
return new ShfmtStep(version, null); | ||
} | ||
|
||
public ShfmtStep withPathToExe(String pathToExe) { | ||
return new ShfmtStep(version, pathToExe); | ||
} | ||
|
||
public FormatterStep create() { | ||
return FormatterStep.createLazy(name(), this::createState, State::toFunc); | ||
} | ||
|
||
private State createState() throws IOException, InterruptedException { | ||
String howToInstall = "" + | ||
"You can download shfmt from https://github.com/mvdan/sh and " + | ||
"then point Spotless to it with {@code pathToExe('/path/to/shfmt')} " + | ||
"or you can use your platform's package manager:" + | ||
"\n win: choco install shfmt" + | ||
"\n mac: brew install shfmt" + | ||
"\n linux: apt install shfmt" + | ||
"\n github issue to handle this better: https://github.com/diffplug/spotless/issues/673"; | ||
final ForeignExe exe = ForeignExe.nameAndVersion("shfmt", version) | ||
.pathToExe(pathToExe) | ||
.versionRegex(Pattern.compile("(\\S*)")) | ||
.fixCantFind(howToInstall) | ||
.fixWrongVersion( | ||
"You can tell Spotless to use the version you already have with {@code shfmt('{versionFound}')}" + | ||
"or you can download the currently specified version, {version}.\n" + howToInstall); | ||
return new State(this, exe); | ||
} | ||
|
||
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED") | ||
static class State implements Serializable { | ||
private static final long serialVersionUID = -1825662356883926318L; | ||
// used for up-to-date checks and caching | ||
final String version; | ||
final transient ForeignExe exe; | ||
// used for executing | ||
private transient @Nullable List<String> args; | ||
|
||
State(ShfmtStep step, ForeignExe pathToExe) { | ||
this.version = step.version; | ||
this.exe = Objects.requireNonNull(pathToExe); | ||
} | ||
|
||
String format(ProcessRunner runner, String input, File file) throws IOException, InterruptedException { | ||
if (args == null) { | ||
args = List.of(exe.confirmVersionAndGetAbsolutePath(), "-i", "2", "-ci"); | ||
} | ||
|
||
return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8); | ||
} | ||
|
||
FormatterFunc.Closeable toFunc() { | ||
ProcessRunner runner = new ProcessRunner(); | ||
return FormatterFunc.Closeable.of(runner, this::format); | ||
} | ||
} | ||
} |
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
73 changes: 73 additions & 0 deletions
73
plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ShellExtension.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,73 @@ | ||
/* | ||
* 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.gradle.spotless; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.inject.Inject; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.shell.ShfmtStep; | ||
|
||
public class ShellExtension extends FormatExtension { | ||
private static final String SHELL_FILE_EXTENSION = "*.sh"; | ||
|
||
static final String NAME = "shell"; | ||
|
||
@Inject | ||
public ShellExtension(SpotlessExtension spotless) { | ||
super(spotless); | ||
} | ||
|
||
/** If the user hasn't specified files, assume all shell files should be checked. */ | ||
@Override | ||
protected void setupTask(SpotlessTask task) { | ||
if (target == null) { | ||
target = parseTarget(SHELL_FILE_EXTENSION); | ||
} | ||
super.setupTask(task); | ||
} | ||
|
||
/** Adds the specified version of <a href="https://github.com/mvdan/sh">shfmt</a>. */ | ||
public ShfmtExtension shfmt(String version) { | ||
Objects.requireNonNull(version); | ||
return new ShfmtExtension(version); | ||
} | ||
|
||
/** Adds the specified version of <a href="https://github.com/mvdan/sh">shfmt</a>. */ | ||
public ShfmtExtension shfmt() { | ||
return shfmt(ShfmtStep.defaultVersion()); | ||
} | ||
|
||
public class ShfmtExtension { | ||
ShfmtStep step; | ||
|
||
ShfmtExtension(String version) { | ||
this.step = ShfmtStep.withVersion(version); | ||
addStep(createStep()); | ||
} | ||
|
||
public ShfmtExtension pathToExe(String pathToExe) { | ||
step = step.withPathToExe(pathToExe); | ||
replaceStep(createStep()); | ||
return this; | ||
} | ||
|
||
private FormatterStep createStep() { | ||
return step.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
41 changes: 41 additions & 0 deletions
41
plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ShfmtIntegrationTest.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.gradle.spotless; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.diffplug.spotless.tag.ShfmtTest; | ||
|
||
@ShfmtTest | ||
public class ShfmtIntegrationTest extends GradleIntegrationHarness { | ||
@Test | ||
void shfmt() throws IOException { | ||
setFile("build.gradle").toLines( | ||
"plugins {", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"spotless {", | ||
" shell {", | ||
" shfmt()", | ||
" }", | ||
"}"); | ||
setFile("shfmt.sh").toResource("shell/shfmt/shfmt.sh"); | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
assertFile("shfmt.sh").sameAsResource("shell/shfmt/shfmt.clean"); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
testlib/src/main/java/com/diffplug/spotless/tag/ShfmtTest.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,30 @@ | ||
/* | ||
* 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.tag; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import org.junit.jupiter.api.Tag; | ||
|
||
@Target({TYPE, METHOD}) | ||
@Retention(RUNTIME) | ||
@Tag("Shfmt") | ||
public @interface ShfmtTest {} |
Oops, something went wrong.