Skip to content

Commit e644715

Browse files
author
ntwigg
committed
Cleaner attempt.
1 parent da4b316 commit e644715

File tree

2 files changed

+6
-42
lines changed

2 files changed

+6
-42
lines changed

src/main/java/com/diffplug/webtools/flywayjooq/SetupCleanupDockerFlyway.java

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.gradle.api.GradleException;
2020
import org.postgresql.ds.PGSimpleDataSource;
2121
import webtools.Env;
22+
import com.diffplug.webtools.node.SetupCleanup;
2223

2324
import java.io.*;
2425
import java.nio.charset.StandardCharsets;
@@ -58,7 +59,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
5859
return FileVisitResult.CONTINUE;
5960
}
6061
});
61-
startImpl(keyFile(projectDir), this);
62+
new Impl().start(keyFile(projectDir), this);
6263
} catch (Exception e) {
6364
var rootCause = Throwables.getRootCause(e);
6465
if (rootCause != null && rootCause.getMessage() != null) {
@@ -70,27 +71,6 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
7071
}
7172
}
7273

73-
private void startImpl(File keyFile, SetupCleanupDockerFlyway key) throws Exception {
74-
synchronized (key.getClass()) {
75-
byte[] required = toBytes(key);
76-
if (keyFile.exists()) {
77-
byte[] actual = java.nio.file.Files.readAllBytes(keyFile.toPath());
78-
if (java.util.Arrays.equals(actual, required)) {
79-
// short-circuit if our state is already setup
80-
return;
81-
} else {
82-
java.nio.file.Files.delete(keyFile.toPath());
83-
@SuppressWarnings("unchecked")
84-
SetupCleanupDockerFlyway lastKey = (SetupCleanupDockerFlyway) fromBytes(required);
85-
new Impl().doStop(lastKey);
86-
}
87-
}
88-
// write out the key
89-
new Impl().doStart(key);
90-
java.nio.file.Files.createDirectories(keyFile.toPath().getParent());
91-
java.nio.file.Files.write(keyFile.toPath(), required);
92-
}
93-
}
9474

9575
void forceStop(File projectDir) throws Exception {
9676
try {
@@ -164,24 +144,6 @@ public static void keepTrying(Throwing.Runnable toAttempt) {
164144
}
165145
}
166146

167-
private static byte[] toBytes(Object key) {
168-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
169-
try (ObjectOutputStream objectOutput = new ObjectOutputStream(bytes)) {
170-
objectOutput.writeObject(key);
171-
} catch (IOException e) {
172-
throw new RuntimeException(e);
173-
}
174-
return bytes.toByteArray();
175-
}
176-
177-
private static Object fromBytes(byte[] raw) throws ClassNotFoundException {
178-
ByteArrayInputStream bytes = new ByteArrayInputStream(raw);
179-
try (ObjectInputStream objectOutput = new ObjectInputStream(bytes)) {
180-
return objectOutput.readObject();
181-
} catch (IOException e) {
182-
throw new RuntimeException(e);
183-
}
184-
}
185147

186148
DockerComposeRule rule() {
187149
return DockerComposeRule.builder()
@@ -195,7 +157,8 @@ DockerComposeRule rule() {
195157
.build();
196158
}
197159

198-
private static class Impl {
160+
private static class Impl extends SetupCleanup<SetupCleanupDockerFlyway> {
161+
@Override
199162
protected void doStart(SetupCleanupDockerFlyway key) throws IOException, InterruptedException {
200163
DockerComposeRule rule;
201164
String ip;
@@ -255,6 +218,7 @@ protected void doStart(SetupCleanupDockerFlyway key) throws IOException, Interru
255218
Files.write(schema, key.flywaySchemaDump, StandardCharsets.UTF_8);
256219
}
257220

221+
@Override
258222
protected void doStop(SetupCleanupDockerFlyway key) throws IOException, InterruptedException {
259223
if (!Env.isGitHubAction()) {
260224
DockerCompose compose = key.rule().dockerCompose();

src/main/java/com/diffplug/webtools/node/SetupCleanup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.nio.file.Files;
2525
import java.util.Arrays;
2626

27-
abstract class SetupCleanup<K> {
27+
public abstract class SetupCleanup<K> {
2828
public void start(File keyFile, K key) throws Exception {
2929
synchronized (key.getClass()) {
3030
byte[] required = toBytes(key);

0 commit comments

Comments
 (0)