Skip to content

Commit e51e5de

Browse files
committed
chore: cleanup temp-dirs
1 parent 797ecb2 commit e51e5de

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

app/src/main/java/com/diffplug/spotless/cli/SpotlessCLI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.diffplug.spotless.cli.core.FileResolver;
3636
import com.diffplug.spotless.cli.core.SpotlessActionContext;
3737
import com.diffplug.spotless.cli.core.SpotlessCommandLineStream;
38+
import com.diffplug.spotless.cli.core.SpotlessRunCleanup;
3839
import com.diffplug.spotless.cli.core.TargetFileTypeInferer;
3940
import com.diffplug.spotless.cli.core.TargetResolver;
4041
import com.diffplug.spotless.cli.execution.FormatterStepsSupplier;
@@ -98,6 +99,8 @@ public class SpotlessCLI implements SpotlessAction, SpotlessCommand, SpotlessAct
9899

99100
private @NotNull Output output;
100101

102+
private final SpotlessRunCleanup spotlessRunCleanup = SpotlessRunCleanup.INSTANCE; // just keep a reference here
103+
101104
@CommandLine.Option(
102105
names = {"--mode", "-m"},
103106
defaultValue = "APPLY",

app/src/main/java/com/diffplug/spotless/cli/core/ExecutionLayout.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ public Optional<Path> find(@Nullable Path searchPath) {
6969
if (found.toFile().canRead()) {
7070
return Optional.of(found);
7171
}
72-
if (searchPath.toFile().canRead()) {
73-
return Optional.of(searchPath);
74-
}
7572
return Optional.empty();
7673
}
7774

@@ -92,6 +89,7 @@ public Path buildDir() {
9289
}
9390
Path tempBuildDir = tempBuildDir();
9491
LOGGER.info("Using temporary build directory as buildDir: {}", tempBuildDir);
92+
SpotlessRunCleanup.INSTANCE.deleteDirOnCleanup(commandLineStream, tempBuildDir);
9593
return tempBuildDir;
9694
}
9795

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.cli.core;
17+
18+
import java.lang.ref.Cleaner;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.util.Comparator;
22+
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
public enum SpotlessRunCleanup {
27+
INSTANCE;
28+
29+
private static final Logger LOGGER = LoggerFactory.getLogger(SpotlessRunCleanup.class);
30+
31+
private static final Cleaner CLEANER = Cleaner.create();
32+
33+
public void deleteDirOnCleanup(Object reference, Path path) {
34+
LOGGER.debug("Registering cleanup for directory: {} -- reference: {}", path, reference);
35+
CLEANER.register(reference, new PathCleanup(path));
36+
}
37+
38+
private record PathCleanup(Path path) implements Runnable {
39+
40+
@Override
41+
public void run() {
42+
// delete the directory
43+
deleteDir();
44+
}
45+
46+
private void deleteDir() {
47+
if (!Files.exists(path)) {
48+
LOGGER.debug("Directory does not exist, nothing to delete: {}", path);
49+
return; // Directory does not exist, nothing to delete
50+
}
51+
LOGGER.info("Deleting directory: {}", path);
52+
try {
53+
Files.walk(path).sorted(Comparator.reverseOrder()).forEach(file -> {
54+
try {
55+
Files.delete(file);
56+
} catch (Exception e) {
57+
throw new RuntimeException("Failed to delete file: " + file, e);
58+
}
59+
});
60+
} catch (Exception e) {
61+
LOGGER.warn("Failed to delete directory: {}", path, e);
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)