Skip to content

Commit fefec5a

Browse files
committed
Reduce code formatter collection occurrences
1 parent 23a84c7 commit fefec5a

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

core/src/main/java/com/cosium/code/format/AbstractFormatMojo.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cosium.code.format;
22

3+
import com.cosium.code.format.formatter.CodeFormatters;
34
import java.io.IOException;
45
import java.nio.file.FileSystems;
56
import java.nio.file.FileVisitResult;
@@ -26,12 +27,13 @@ protected final void doExecute() throws MojoExecutionException, MojoFailureExcep
2627
getLog().debug("Using pattern '" + pattern + "'");
2728
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + globPattern);
2829

30+
CodeFormatters codeFormatters = collectCodeFormatters();
2931
for (Path sourceDir : sourceDirs()) {
30-
walk(sourceDir, pathMatcher);
32+
walk(codeFormatters, sourceDir, pathMatcher);
3133
}
3234
}
3335

34-
private void walk(Path directoryToWalk, PathMatcher pathMatcher)
36+
private void walk(CodeFormatters codeFormatters, Path directoryToWalk, PathMatcher pathMatcher)
3537
throws MojoExecutionException, MojoFailureException {
3638
Path targetDir = targetDir();
3739
try {
@@ -48,7 +50,7 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
4850
return FileVisitResult.CONTINUE;
4951
}
5052
try {
51-
process(path);
53+
process(codeFormatters, path);
5254
} catch (MojoExecutionException | MojoFailureException e) {
5355
throw new MavenGitCodeFormatException(e);
5456
}
@@ -75,5 +77,5 @@ public FileVisitResult visitFileFailed(Path file, IOException exc) {
7577
}
7678
}
7779

78-
protected abstract void process(Path path) throws MojoExecutionException, MojoFailureException;
80+
protected abstract void process(CodeFormatters codeFormatters, Path path) throws MojoExecutionException, MojoFailureException;
7981
}

core/src/main/java/com/cosium/code/format/AbstractMavenGitCodeFormatMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected final String artifactId() {
7777
return currentProject.getArtifactId();
7878
}
7979

80-
protected final CodeFormatters codeFormatters() {
80+
protected final CodeFormatters collectCodeFormatters() {
8181
return new CodeFormatters(createCodeFormatters());
8282
}
8383

core/src/main/java/com/cosium/code/format/FormatCodeMojo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cosium.code.format;
22

3+
import com.cosium.code.format.formatter.CodeFormatters;
34
import com.cosium.code.format_spi.CodeFormatter;
45
import com.cosium.code.format_spi.FileExtension;
56
import com.cosium.code.format_spi.LineRanges;
@@ -19,8 +20,8 @@
1920
public class FormatCodeMojo extends AbstractFormatMojo {
2021

2122
@Override
22-
protected void process(Path path) {
23-
codeFormatters()
23+
protected void process(CodeFormatters codeFormatters, Path path) {
24+
codeFormatters
2425
.forFileExtension(FileExtension.parse(path))
2526
.forEach(formatter -> format(path, formatter));
2627
}

core/src/main/java/com/cosium/code/format/OnPreCommitMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected void doExecute() throws MojoExecutionException {
2525
}
2626

2727
private void onPreCommit() throws IOException, GitAPIException {
28-
GitStagedFiles.read(getLog(), gitRepository(), this::isFormattable).format(codeFormatters());
28+
GitStagedFiles.read(getLog(), gitRepository(), this::isFormattable).format(collectCodeFormatters());
2929
}
3030

3131
private boolean isFormattable(Path path) {

core/src/main/java/com/cosium/code/format/ValidateCodeFormat.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cosium.code.format;
22

3+
import com.cosium.code.format.formatter.CodeFormatters;
34
import com.cosium.code.format_spi.CodeFormatter;
45
import com.cosium.code.format_spi.FileExtension;
56
import java.io.IOException;
@@ -16,15 +17,15 @@
1617
@Mojo(name = "validate-code-format", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
1718
public class ValidateCodeFormat extends AbstractFormatMojo {
1819
@Override
19-
protected void process(Path path) throws MojoFailureException {
20-
if (validate(path)) {
20+
protected void process(CodeFormatters codeFormatters, Path path) throws MojoFailureException {
21+
if (validate(codeFormatters, path)) {
2122
return;
2223
}
2324
throw new MojoFailureException(path + " is not correctly formatted !");
2425
}
2526

26-
private boolean validate(Path path) {
27-
return codeFormatters().forFileExtension(FileExtension.parse(path)).stream()
27+
private boolean validate(CodeFormatters codeFormatters, Path path) {
28+
return codeFormatters.forFileExtension(FileExtension.parse(path)).stream()
2829
.map(formatter -> doValidate(path, formatter))
2930
.filter(valid -> !valid)
3031
.findFirst()

0 commit comments

Comments
 (0)