Skip to content

Commit

Permalink
Move ProcessRunner and such out of lib and into plugin-gradle.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jul 6, 2024
1 parent 7d5ed9b commit 1ac58b7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.changelog.gradle;
package com.diffplug.spotless.changelog;

import static java.util.Objects.requireNonNull;

Expand All @@ -34,8 +34,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import pl.tlinkowski.annotation.basic.NullOr;

/**
* Shelling out to a process is harder than it ought to be in Java.
Expand Down Expand Up @@ -82,7 +81,7 @@ private static boolean machineIsWin() {
}

/** Executes the given shell command (using {@code cmd} on windows and {@code sh} on unix). */
public Result shellWinUnix(@Nullable File cwd, @Nullable Map<String, String> environment, String cmdWin, String cmdUnix) throws IOException, InterruptedException {
public Result shellWinUnix(@NullOr File cwd, @NullOr Map<String, String> environment, String cmdWin, String cmdUnix) throws IOException, InterruptedException {
List<String> args;
if (machineIsWin()) {
args = Arrays.asList("cmd", "/c", cmdWin);
Expand All @@ -98,7 +97,7 @@ public Result exec(String... args) throws IOException, InterruptedException {
}

/** Creates a process with the given arguments, the given byte array is written to stdin immediately. */
public Result exec(@Nullable byte[] stdin, String... args) throws IOException, InterruptedException {
public Result exec(@NullOr byte[] stdin, String... args) throws IOException, InterruptedException {
return exec(stdin, Arrays.asList(args));
}

Expand All @@ -108,12 +107,12 @@ public Result exec(List<String> args) throws IOException, InterruptedException {
}

/** Creates a process with the given arguments, the given byte array is written to stdin immediately. */
public Result exec(@Nullable byte[] stdin, List<String> args) throws IOException, InterruptedException {
public Result exec(@NullOr byte[] stdin, List<String> args) throws IOException, InterruptedException {
return exec(null, null, stdin, args);
}

/** Creates a process with the given arguments, the given byte array is written to stdin immediately. */
public Result exec(@Nullable File cwd, @Nullable Map<String, String> environment, @Nullable byte[] stdin, List<String> args) throws IOException, InterruptedException {
public Result exec(@NullOr File cwd, @NullOr Map<String, String> environment, @NullOr byte[] stdin, List<String> args) throws IOException, InterruptedException {
LongRunningProcess process = start(cwd, environment, stdin, args);
try {
// wait for the process to finish
Expand All @@ -130,7 +129,7 @@ public Result exec(@Nullable File cwd, @Nullable Map<String, String> environment
* <br>
* Delegates to {@link #start(File, Map, byte[], boolean, List)} with {@code false} for {@code redirectErrorStream}.
*/
public LongRunningProcess start(@Nullable File cwd, @Nullable Map<String, String> environment, @Nullable byte[] stdin, List<String> args) throws IOException {
public LongRunningProcess start(@NullOr File cwd, @NullOr Map<String, String> environment, @NullOr byte[] stdin, List<String> args) throws IOException {
return start(cwd, environment, stdin, false, args);
}

Expand All @@ -142,7 +141,7 @@ public LongRunningProcess start(@Nullable File cwd, @Nullable Map<String, String
* To dispose this {@code ProcessRunner} instance, either call {@link #close()} or {@link LongRunningProcess#close()}. After
* {@link #close()} or {@link LongRunningProcess#close()} has been called, this {@code ProcessRunner} instance must not be used anymore.
*/
public LongRunningProcess start(@Nullable File cwd, @Nullable Map<String, String> environment, @Nullable byte[] stdin, boolean redirectErrorStream, List<String> args) throws IOException {
public LongRunningProcess start(@NullOr File cwd, @NullOr Map<String, String> environment, @NullOr byte[] stdin, boolean redirectErrorStream, List<String> args) throws IOException {
checkState();
ProcessBuilder builder = new ProcessBuilder(args);
if (cwd != null) {
Expand Down Expand Up @@ -203,7 +202,7 @@ public static class Result {
private final int exitCode;
private final byte[] stdOut, stdErr;

public Result(@Nonnull List<String> args, int exitCode, @Nonnull byte[] stdOut, @Nullable byte[] stdErr) {
public Result(List<String> args, int exitCode, byte[] stdOut, byte[] stdErr) {
this.args = args;
this.exitCode = exitCode;
this.stdOut = stdOut;
Expand Down Expand Up @@ -295,7 +294,7 @@ public class LongRunningProcess extends Process implements AutoCloseable {
private final Future<byte[]> outputFut;
private final Future<byte[]> errorFut;

public LongRunningProcess(@Nonnull Process delegate, @Nonnull List<String> args, @Nonnull Future<byte[]> outputFut, @Nullable Future<byte[]> errorFut) {
public LongRunningProcess(Process delegate, List<String> args, Future<byte[]> outputFut, @NullOr Future<byte[]> errorFut) {
this.delegate = requireNonNull(delegate);
this.args = args;
this.outputFut = outputFut;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.changelog.gradle;
package com.diffplug.spotless.changelog;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down

0 comments on commit 1ac58b7

Please sign in to comment.