Skip to content

Commit 5473eb1

Browse files
committed
Add proper roundtrip behavior to the ForeignExe steps.
1 parent 92664eb commit 5473eb1

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

lib/src/main/java/com/diffplug/spotless/cpp/ClangFormatStep.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public ClangFormatStep withPathToExe(String pathToExe) {
6464
}
6565

6666
public FormatterStep create() {
67-
return FormatterStep.createLazy(name(), this::createState, RoundtripState::state, State::toFunc);
67+
return FormatterStep.createLazy(name(), this::createRoundtrip, RoundtripState::toEquality, EqualityState::toFunc);
6868
}
6969

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

101-
private State state() {
102-
return new State(version, style, exe);
101+
private EqualityState toEquality() {
102+
return new EqualityState(version, style, exe);
103103
}
104104
}
105105

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

116-
State(String version, @Nullable String style, ForeignExe pathToExe) {
116+
EqualityState(String version, @Nullable String style, ForeignExe pathToExe) {
117117
this.version = version;
118118
this.style = style;
119119
this.exe = Objects.requireNonNull(pathToExe);

lib/src/main/java/com/diffplug/spotless/go/GofmtFormatStep.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public GofmtFormatStep withGoExecutable(String pathToExe) {
6363
}
6464

6565
public FormatterStep create() {
66-
return FormatterStep.createLazy(name(), this::createState, GofmtFormatStep.State::toFunc);
66+
return FormatterStep.createLazy(name(), this::createRountrip, RoundtripState::toEquality, EqualityState::toFunc);
6767
}
6868

69-
private State createState() throws IOException, InterruptedException {
69+
private RoundtripState createRountrip() throws IOException, InterruptedException {
7070
String howToInstall = "gofmt is a part of standard go distribution. If spotless can't discover it automatically, " +
7171
"you can point Spotless to the go binary with {@code pathToExe('/path/to/go')}";
7272
final ForeignExe exe = ForeignExe.nameAndVersion("go", version)
@@ -76,18 +76,34 @@ private State createState() throws IOException, InterruptedException {
7676
.fixWrongVersion(
7777
"You can tell Spotless to use the version you already have with {@code gofmt('{versionFound}')}" +
7878
"or you can install the currently specified Go version, {version}.\n" + howToInstall);
79-
return new State(this, exe);
79+
return new RoundtripState(version, exe);
80+
}
81+
82+
static class RoundtripState implements Serializable {
83+
private static final long serialVersionUID = 1L;
84+
85+
final String version;
86+
final ForeignExe exe;
87+
88+
RoundtripState(String version, ForeignExe exe) {
89+
this.version = version;
90+
this.exe = exe;
91+
}
92+
93+
private EqualityState toEquality() {
94+
return new EqualityState(version, exe);
95+
}
8096
}
8197

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

89-
public State(GofmtFormatStep step, ForeignExe goExecutable) {
90-
this.version = step.version;
105+
public EqualityState(String version, ForeignExe goExecutable) {
106+
this.version = version;
91107
this.exe = Objects.requireNonNull(goExecutable);
92108
}
93109

lib/src/main/java/com/diffplug/spotless/python/BlackStep.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public BlackStep withPathToExe(String pathToExe) {
5656
}
5757

5858
public FormatterStep create() {
59-
return FormatterStep.createLazy(name(), this::createState, RoundtripState::state, State::toFunc);
59+
return FormatterStep.createLazy(name(), this::createRoundtrip, RoundtripState::toEquality, EqualityState::toFunc);
6060
}
6161

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

83-
private State state() {
84-
return new State(version, exe);
83+
private EqualityState toEquality() {
84+
return new EqualityState(version, exe);
8585
}
8686
}
8787

8888
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
89-
static class State implements Serializable {
89+
static class EqualityState implements Serializable {
9090
private static final long serialVersionUID = -1825662356883926318L;
9191
// used for up-to-date checks and caching
9292
final String version;
9393
final transient ForeignExe exe;
9494
// used for executing
9595
private transient @Nullable String[] args;
9696

97-
State(String version, ForeignExe exeAbsPath) {
97+
EqualityState(String version, ForeignExe exeAbsPath) {
9898
this.version = version;
9999
this.exe = Objects.requireNonNull(exeAbsPath);
100100
}

lib/src/main/java/com/diffplug/spotless/shell/ShfmtStep.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public ShfmtStep withPathToExe(String pathToExe) {
6060
}
6161

6262
public FormatterStep create() {
63-
return FormatterStep.createLazy(name(), this::createState, State::toFunc);
63+
return FormatterStep.createLazy(name(), this::createRoundtrip, RoundtripState::toEquality, EqualityState::toFunc);
6464
}
6565

66-
private State createState() throws IOException, InterruptedException {
66+
private RoundtripState createRoundtrip() throws IOException, InterruptedException {
6767
String howToInstall = "" +
6868
"You can download shfmt from https://github.com/mvdan/sh and " +
6969
"then point Spotless to it with {@code pathToExe('/path/to/shfmt')} " +
@@ -79,11 +79,27 @@ private State createState() throws IOException, InterruptedException {
7979
.fixWrongVersion(
8080
"You can tell Spotless to use the version you already have with {@code shfmt('{versionFound}')}" +
8181
"or you can download the currently specified version, {version}.\n" + howToInstall);
82-
return new State(this, exe);
82+
return new RoundtripState(version, exe);
83+
}
84+
85+
static class RoundtripState implements Serializable {
86+
private static final long serialVersionUID = 1L;
87+
88+
final String version;
89+
final ForeignExe exe;
90+
91+
RoundtripState(String version, ForeignExe exe) {
92+
this.version = version;
93+
this.exe = exe;
94+
}
95+
96+
private EqualityState toEquality() {
97+
return new EqualityState(version, exe);
98+
}
8399
}
84100

85101
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
86-
static class State implements Serializable {
102+
static class EqualityState implements Serializable {
87103
private static final long serialVersionUID = -1825662356883926318L;
88104

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

96-
State(ShfmtStep step, ForeignExe pathToExe) {
97-
this.version = step.version;
112+
EqualityState(String version, ForeignExe pathToExe) {
113+
this.version = version;
98114
this.exe = Objects.requireNonNull(pathToExe);
99115
}
100116

0 commit comments

Comments
 (0)