Skip to content

Commit

Permalink
Merge pull request #57 from HubSpot/remove-hardcoded-java-check
Browse files Browse the repository at this point in the history
Remove the hardcoded .java check
  • Loading branch information
jhaber authored Nov 4, 2021
2 parents 7bb58bf + c518ac2 commit a1711a5
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/main/java/com/hubspot/maven/plugins/prettier/CheckMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hubspot.maven.plugins.prettier.diff.DiffGenerator;
import com.hubspot.maven.plugins.prettier.diff.GenerateDiffArgs;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -37,15 +38,11 @@ protected String getPrettierCommand() {

@Override
protected void handlePrettierLogLine(String line) {
if (line.endsWith(".java")) {
line = trimLogLevel(line);
line = trimLogLevel(line);

Path baseDir = project
.getBasedir()
.toPath()
.toAbsolutePath();

incorrectlyFormattedFiles.add(baseDir.resolve(line));
Path maybeFile = resolveFile(line);
if (Files.isRegularFile(maybeFile)) {
incorrectlyFormattedFiles.add(maybeFile);
String message = "Incorrectly formatted file: " + line;
if (fail) {
getLog().error(message);
Expand Down Expand Up @@ -98,12 +95,27 @@ private DiffGenerator instantiateDiffGenerator() throws MojoExecutionException {
}
}

private Path resolveFile(String relativePath) {
Path baseDir = project
.getBasedir()
.toPath()
.toAbsolutePath();

return baseDir.resolve(relativePath);
}

private static String trimLogLevel(String line) {
if (line.contains("]")) {
// converts something like '[warn] src/main/java/Test.java' -> 'src/main/java/Test.java'
return line.substring(line.indexOf(']') + 2);
} else {
int closeBracketIndex = line.indexOf(']');
if (closeBracketIndex < 0) {
return line;
}

int startFileIndex = closeBracketIndex + "] ".length();
if (startFileIndex >= line.length()) {
return line;
}

// converts something like '[warn] src/main/java/Test.java' -> 'src/main/java/Test.java'
return line.substring(startFileIndex);
}
}

0 comments on commit a1711a5

Please sign in to comment.