Skip to content

Commit d1d5852

Browse files
authored
Cache config (#734)
* Include config in the hash * Allow to skip the cache * Change failure log level to error and emit per warning/error
1 parent 4b63f1e commit d1d5852

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

src/main/java/net/revelc/code/formatter/FormatterMojo.java

+31-12
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import org.eclipse.text.edits.MalformedTreeException;
6161
import org.xml.sax.SAXException;
6262

63-
import com.google.common.base.Strings;
6463
import com.google.common.hash.Hashing;
6564

6665
import net.revelc.code.formatter.css.CssFormatter;
@@ -262,6 +261,14 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
262261
@Parameter(defaultValue = "formatter-maven-plugin/ph-css/css.properties", property = "configcssfile", required = true)
263262
private String configCssFile;
264263

264+
/**
265+
* Whether the formatting cache is skipped.
266+
*
267+
* @since 2.23.0
268+
*/
269+
@Parameter(defaultValue = "false", property = "formatter.cache.skip")
270+
private boolean skipFormattingCache;
271+
265272
/**
266273
* Whether the java formatting is skipped.
267274
*/
@@ -462,9 +469,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
462469
this.formatFile(file, rc, hashCache, basedirPath);
463470
} else {
464471
rc.readOnlyCount++;
472+
this.getLog().warn("File " + file + " is read only");
465473
}
466474
} else {
467475
rc.failCount++;
476+
this.getLog().error("File " + file + " does not exist");
468477
}
469478
}
470479

@@ -478,9 +487,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
478487
final var elapsed = TimeUtil.printDuration(duration);
479488

480489
final String results = String.format(
481-
"Processed %d files in %s (Formatted: %d, Unchanged: %d, Failed: %d, Readonly: %d)", numberOfFiles,
482-
elapsed, rc.successCount, rc.skippedCount, rc.failCount, rc.readOnlyCount);
483-
this.getLog().info(results);
490+
"Processed %d files in %s (Formatted: %d, Skipped: %d, Unchanged: %d, Failed: %d, Readonly: %d)",
491+
numberOfFiles, elapsed, rc.successCount, rc.skippedCount, rc.unchangedCount, rc.failCount,
492+
rc.readOnlyCount);
493+
494+
if (rc.failCount > 0)
495+
this.getLog().error(results);
496+
else if (rc.readOnlyCount > 0)
497+
this.getLog().warn(results);
498+
else
499+
this.getLog().info(results);
484500
}
485501
}
486502

@@ -678,7 +694,7 @@ private void formatFile(final File file, final ResultCollector rc, final Propert
678694
this.doFormatFile(file, rc, hashCache, basedirPath, false);
679695
} catch (IOException | MalformedTreeException | BadLocationException e) {
680696
rc.failCount++;
681-
this.getLog().warn(e);
697+
this.getLog().error(e);
682698
}
683699
}
684700

@@ -711,14 +727,14 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro
711727
final var log = this.getLog();
712728
log.debug("Processing file: " + file);
713729
final var originalCode = this.readFileAsString(file);
714-
final var originalHash = this.sha512hash(originalCode);
730+
final var originalHash = this.sha512hash(originalCode + this.javaFormatter.hashCode());
715731

716732
final var canonicalPath = file.getCanonicalPath();
717733
final var path = canonicalPath.substring(basedirPath.length());
718734
final var cachedHash = hashCache.getProperty(path);
719-
if (cachedHash != null && cachedHash.equals(originalHash)) {
735+
if (!skipFormattingCache && cachedHash != null && cachedHash.equals(originalHash)) {
720736
rc.skippedCount++;
721-
log.debug("File is already formatted.");
737+
log.debug("Cache hit: file is already formatted.");
722738
return;
723739
}
724740

@@ -786,7 +802,7 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro
786802

787803
// Process the result type
788804
if (Result.SKIPPED.equals(result)) {
789-
rc.skippedCount++;
805+
rc.unchangedCount++;
790806
} else if (Result.SUCCESS.equals(result)) {
791807
rc.successCount++;
792808
} else if (Result.FAIL.equals(result)) {
@@ -799,14 +815,14 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro
799815
if (Result.SKIPPED.equals(result)) {
800816
formattedHash = originalHash;
801817
} else {
802-
formattedHash = this.sha512hash(Strings.nullToEmpty(formattedCode));
818+
formattedHash = this.sha512hash(formattedCode + this.javaFormatter.hashCode());
803819
}
804820
hashCache.setProperty(path, formattedHash);
805821
this.hashCacheWritten = true;
806822

807823
// If we had determined to skip write, do so now after cache was written
808824
if (Result.SKIPPED.equals(result)) {
809-
log.debug("File is already formatted. Writing to cache only.");
825+
log.debug("File is already formatted. Writing to cache only.");
810826
return;
811827
}
812828

@@ -1071,9 +1087,12 @@ static class ResultCollector {
10711087
/** The fail count. */
10721088
int failCount;
10731089

1074-
/** The skipped count. */
1090+
/** The skipped count is incremented for cached files that haven't changed since being cached. */
10751091
int skippedCount;
10761092

1093+
/** The unchanged count is incremented on cache misses when a file remains unchanged after formatting. */
1094+
int unchangedCount;
1095+
10771096
/** The read only count. */
10781097
int readOnlyCount;
10791098
}

src/main/java/net/revelc/code/formatter/java/JavaFormatter.java

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.ArrayList;
1717
import java.util.List;
1818
import java.util.Map;
19+
import java.util.Objects;
1920
import java.util.regex.Pattern;
2021

2122
import org.eclipse.jdt.core.ToolFactory;
@@ -37,17 +38,37 @@
3738
*/
3839
public class JavaFormatter extends AbstractCacheableFormatter implements Formatter {
3940

41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o)
44+
return true;
45+
if (o == null || getClass() != o.getClass())
46+
return false;
47+
JavaFormatter that = (JavaFormatter) o;
48+
return Objects.equals(formatter, that.formatter) && Objects.equals(exclusionPattern, that.exclusionPattern)
49+
&& Objects.equals(options, that.options);
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return Objects.hash(formatter, exclusionPattern, options);
55+
}
56+
4057
/** The formatter. */
4158
private CodeFormatter formatter;
4259

4360
/** The exclusion pattern. */
4461
private Pattern exclusionPattern;
4562

63+
/** The configuration options */
64+
private Map<String, String> options;
65+
4666
@Override
4767
public void init(final Map<String, String> options, final ConfigurationSource cfg) {
4868
super.initCfg(cfg);
4969

5070
this.formatter = ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_EXISTING);
71+
this.options = options;
5172
}
5273

5374
@Override

0 commit comments

Comments
 (0)