Skip to content

Commit 971824d

Browse files
committed
Fix spotbugs issues
1 parent 77d302e commit 971824d

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

lib/src/main/java/com/diffplug/spotless/rome/RomeExecutableDownloader.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ public Path download(String version) throws IOException, InterruptedException {
117117
var url = getDownloadUrl(version, platform);
118118
var executablePath = getExecutablePath(version, platform);
119119
var checksumPath = getChecksumPath(executablePath);
120-
Files.createDirectories(executablePath.getParent());
120+
var executableDir = executablePath.getParent();
121+
if (executableDir != null) {
122+
Files.createDirectories(executableDir);
123+
}
121124
logger.info("Attempting to download Rome from '{}' to '{}'", url, executablePath);
122125
var request = HttpRequest.newBuilder(URI.create(url)).GET().build();
123126
var handler = BodyHandlers.ofFile(executablePath, WRITE_OPTIONS);
@@ -270,7 +273,11 @@ private String getArchitectureCodeName(Architecture architecture) throws IOExcep
270273
* @return The path with the checksum for the given file.
271274
*/
272275
private Path getChecksumPath(Path file) {
273-
return file.getParent().resolve(file.getFileName().toString() + ".md5");
276+
var parent = file.getParent();
277+
var base = parent != null ? parent : file;
278+
var fileName = file.getFileName();
279+
var checksumName = fileName != null ? fileName.toString() + ".md5" : "checksum.md5";
280+
return base.resolve(checksumName);
274281
}
275282

276283
/**

lib/src/main/java/com/diffplug/spotless/rome/RomeStep.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ private static void attemptToAddPosixPermission(Path file, PosixFilePermission p
134134
var newPermissions = new HashSet<>(Files.getPosixFilePermissions(file));
135135
newPermissions.add(permission);
136136
Files.setPosixFilePermissions(file, newPermissions);
137-
} catch (final Exception ignore) {}
137+
} catch (final Exception ignore) {
138+
logger.debug("Unable to add POSIX permission '{}' to file '{}'", permission, file);
139+
}
138140
}
139141

140142
/**
@@ -440,7 +442,7 @@ private String resolveFileName(File file) {
440442
return "jsx".equals(ext) || "js".equals(ext) || "mjs".equals(ext) || "cjs".equals(ext) ? name
441443
: "file.js";
442444
case "ts?":
443-
return "tsx".equals(ext) || "ts".equals(ext) || "tjs".equals(ext) || "tjs".equals(ext) ? name
445+
return "tsx".equals(ext) || "ts".equals(ext) || "mts".equals(ext) || "cts".equals(ext) ? name
444446
: "file.js";
445447
case "js":
446448
return "js".equals(ext) || "mjs".equals(ext) || "cjs".equals(ext) ? name : "file.js";

plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.security.NoSuchAlgorithmException;
2525
import java.util.Base64;
2626
import java.util.Objects;
27+
import java.util.Optional;
2728

2829
import org.codehaus.plexus.resource.ResourceManager;
2930
import org.codehaus.plexus.resource.loader.FileResourceCreationException;
@@ -121,10 +122,22 @@ private static byte[] hash(String value) {
121122
}
122123

123124
private static File findDataDir() {
124-
// E.g. ~/.m2/repository/com/diffplug/spotless/spotless-plugin-maven/1.2.3/spotless-plugin-maven-1.2.3.jar
125-
final var jarPath = Paths.get(AbstractRome.class.getProtectionDomain().getCodeSource().getLocation().getPath());
126-
final var base = jarPath.getParent().getParent().getParent();
127-
final var sub = base.resolve("spotless-data");
128-
return sub.toAbsolutePath().toFile();
125+
// JAR path is e.g.
126+
// ~/.m2/repository/com/diffplug/spotless/spotless-plugin-maven/1.2.3/spotless-plugin-maven-1.2.3.jar
127+
var codeSource = FileLocator.class.getProtectionDomain().getCodeSource();
128+
var location = codeSource != null ? codeSource.getLocation() : null;
129+
var path = location != null ? location.getPath() : null;
130+
var jarPath = path != null && !path.isBlank() ? Paths.get(path) : null;
131+
var parent1 = jarPath != null ? jarPath.getParent() : null;
132+
var parent2 = parent1 != null ? parent1.getParent() : null;
133+
var base = parent2 != null ? parent2.getParent() : null;
134+
var sub = base != null ? base.resolve("spotless-data") : null;
135+
if (sub != null) {
136+
return sub.toAbsolutePath().toFile();
137+
}
138+
else {
139+
var home = Paths.get(System.getenv("user.home"));
140+
return home.resolve(".rome").toAbsolutePath().toFile();
141+
}
129142
}
130143
}

0 commit comments

Comments
 (0)