Skip to content

Commit

Permalink
Replace brackets in runAfterPush (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Jul 6, 2024
2 parents e70b716 + bbffb0c commit db92aa4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- The `runAfterPush` command should replace brackets as documented. ([#52](https://github.com/diffplug/spotless-changelog/pull/52))

## [3.1.0] - 2024-07-06
### Added
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ subprojects {
}
tasks.register('configCacheTest', Test) {
systemProperty 'com.diffplug.config-cache-test', 'true'
testClassesDirs = testing.suites.test.sources.output.classesDirs
classpath = testing.suites.test.sources.runtimeClasspath
}
tasks.named('check') {
dependsOn 'configCacheTest'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2023 DiffPlug
* Copyright (C) 2019-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -116,6 +116,21 @@ public void tagBranchPush() throws GitAPIException {
push(cfg.branch, RemoteRefUpdate.Status.OK);
}

public void runAfterPush() {
if (cfg.runAfterPush == null) {
return;
}
try (var runner = new ProcessRunner()) {
var result = runner.shell(formatTagMessage(cfg.runAfterPush));
System.out.write(result.stdOut());
System.out.flush();
System.err.write(result.stdErr());
System.err.flush();
} catch (IOException | InterruptedException e) {
throw new RuntimeException("runAfterPush failed: " + formatTagMessage(cfg.runAfterPush), e);
}
}

private String formatCommitMessage(final String commitMessage) {
return commitMessage.replace(GitCfg.COMMIT_MESSAGE_VERSION, model.versions().next());
}
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 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, @NullOr 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
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,7 @@ public void push() throws IOException, GitAPIException {
GitActions git = data.gitCfg.withChangelog(data.changelogFile, data.model());
git.addAndCommit();
git.tagBranchPush();
if (data.gitCfg.runAfterPush != null) {
try (var runner = new ProcessRunner()) {
var result = runner.shell(data.gitCfg.runAfterPush);
System.out.write(result.stdOut());
System.out.flush();
System.err.write(result.stdErr());
System.err.flush();
} catch (IOException | InterruptedException e) {
throw new GradleException("runAfterPush failed: " + data.gitCfg.runAfterPush, e);
}
}
git.runAfterPush();
}
}
}

0 comments on commit db92aa4

Please sign in to comment.