Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the hardcoded .java check #57

Merged
merged 1 commit into from
Nov 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}