1919import org .gradle .api .GradleException ;
2020import org .postgresql .ds .PGSimpleDataSource ;
2121import webtools .Env ;
22+ import com .diffplug .webtools .node .SetupCleanup ;
2223
2324import java .io .*;
2425import 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 ();
0 commit comments