Skip to content

Commit

Permalink
Add proper roundtrip behavior to the ForeignExe steps.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed May 29, 2024
1 parent 92664eb commit 5473eb1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
12 changes: 6 additions & 6 deletions lib/src/main/java/com/diffplug/spotless/cpp/ClangFormatStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public ClangFormatStep withPathToExe(String pathToExe) {
}

public FormatterStep create() {
return FormatterStep.createLazy(name(), this::createState, RoundtripState::state, State::toFunc);
return FormatterStep.createLazy(name(), this::createRoundtrip, RoundtripState::toEquality, EqualityState::toFunc);
}

private RoundtripState createState() throws IOException, InterruptedException {
private RoundtripState createRoundtrip() throws IOException, InterruptedException {
String howToInstall = "" +
"You can download clang-format from https://releases.llvm.org and " +
"then point Spotless to it with {@code pathToExe('/path/to/clang-format')} " +
Expand Down Expand Up @@ -98,13 +98,13 @@ static class RoundtripState implements Serializable {
this.exe = exe;
}

private State state() {
return new State(version, style, exe);
private EqualityState toEquality() {
return new EqualityState(version, style, exe);
}
}

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
static class State implements Serializable {
static class EqualityState implements Serializable {
private static final long serialVersionUID = -1825662356883926318L;
// used for up-to-date checks and caching
final String version;
Expand All @@ -113,7 +113,7 @@ static class State implements Serializable {
// used for executing
private transient @Nullable List<String> args;

State(String version, @Nullable String style, ForeignExe pathToExe) {
EqualityState(String version, @Nullable String style, ForeignExe pathToExe) {
this.version = version;
this.style = style;
this.exe = Objects.requireNonNull(pathToExe);
Expand Down
28 changes: 22 additions & 6 deletions lib/src/main/java/com/diffplug/spotless/go/GofmtFormatStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public GofmtFormatStep withGoExecutable(String pathToExe) {
}

public FormatterStep create() {
return FormatterStep.createLazy(name(), this::createState, GofmtFormatStep.State::toFunc);
return FormatterStep.createLazy(name(), this::createRountrip, RoundtripState::toEquality, EqualityState::toFunc);
}

private State createState() throws IOException, InterruptedException {
private RoundtripState createRountrip() throws IOException, InterruptedException {
String howToInstall = "gofmt is a part of standard go distribution. If spotless can't discover it automatically, " +
"you can point Spotless to the go binary with {@code pathToExe('/path/to/go')}";
final ForeignExe exe = ForeignExe.nameAndVersion("go", version)
Expand All @@ -76,18 +76,34 @@ private State createState() throws IOException, InterruptedException {
.fixWrongVersion(
"You can tell Spotless to use the version you already have with {@code gofmt('{versionFound}')}" +
"or you can install the currently specified Go version, {version}.\n" + howToInstall);
return new State(this, exe);
return new RoundtripState(version, exe);
}

static class RoundtripState implements Serializable {
private static final long serialVersionUID = 1L;

final String version;
final ForeignExe exe;

RoundtripState(String version, ForeignExe exe) {
this.version = version;
this.exe = exe;
}

private EqualityState toEquality() {
return new EqualityState(version, exe);
}
}

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
static class State implements Serializable {
static class EqualityState implements Serializable {
private static final long serialVersionUID = -1825662355363926318L;
// used for up-to-date checks and caching
final String version;
final transient ForeignExe exe;

public State(GofmtFormatStep step, ForeignExe goExecutable) {
this.version = step.version;
public EqualityState(String version, ForeignExe goExecutable) {
this.version = version;
this.exe = Objects.requireNonNull(goExecutable);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/src/main/java/com/diffplug/spotless/python/BlackStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public BlackStep withPathToExe(String pathToExe) {
}

public FormatterStep create() {
return FormatterStep.createLazy(name(), this::createState, RoundtripState::state, State::toFunc);
return FormatterStep.createLazy(name(), this::createRoundtrip, RoundtripState::toEquality, EqualityState::toFunc);
}

private RoundtripState createState() {
private RoundtripState createRoundtrip() {
String trackingIssue = "\n github issue to handle this better: https://github.com/diffplug/spotless/issues/674";
ForeignExe exeAbsPath = ForeignExe.nameAndVersion("black", version)
.pathToExe(pathToExe)
Expand All @@ -80,21 +80,21 @@ static class RoundtripState implements Serializable {
this.exe = exe;
}

private State state() {
return new State(version, exe);
private EqualityState toEquality() {
return new EqualityState(version, exe);
}
}

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
static class State implements Serializable {
static class EqualityState 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 String[] args;

State(String version, ForeignExe exeAbsPath) {
EqualityState(String version, ForeignExe exeAbsPath) {
this.version = version;
this.exe = Objects.requireNonNull(exeAbsPath);
}
Expand Down
28 changes: 22 additions & 6 deletions lib/src/main/java/com/diffplug/spotless/shell/ShfmtStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public ShfmtStep withPathToExe(String pathToExe) {
}

public FormatterStep create() {
return FormatterStep.createLazy(name(), this::createState, State::toFunc);
return FormatterStep.createLazy(name(), this::createRoundtrip, RoundtripState::toEquality, EqualityState::toFunc);
}

private State createState() throws IOException, InterruptedException {
private RoundtripState createRoundtrip() 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')} " +
Expand All @@ -79,11 +79,27 @@ private State createState() throws IOException, InterruptedException {
.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);
return new RoundtripState(version, exe);
}

static class RoundtripState implements Serializable {
private static final long serialVersionUID = 1L;

final String version;
final ForeignExe exe;

RoundtripState(String version, ForeignExe exe) {
this.version = version;
this.exe = exe;
}

private EqualityState toEquality() {
return new EqualityState(version, exe);
}
}

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
static class State implements Serializable {
static class EqualityState implements Serializable {
private static final long serialVersionUID = -1825662356883926318L;

// used for up-to-date checks and caching
Expand All @@ -93,8 +109,8 @@ static class State implements Serializable {
// used for executing
private transient @Nullable List<String> args;

State(ShfmtStep step, ForeignExe pathToExe) {
this.version = step.version;
EqualityState(String version, ForeignExe pathToExe) {
this.version = version;
this.exe = Objects.requireNonNull(pathToExe);
}

Expand Down

0 comments on commit 5473eb1

Please sign in to comment.