From f2e146f6b3b8588f5a7a8dcf252900b6b40f77be Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Sun, 10 Mar 2024 14:13:50 -0400 Subject: [PATCH] [caching] Rewrite d1d5852 fixing #757 Original change in d1d5852 only addressed java and did so at detriment of all formatters. This caused certain platforms to continually update the cache file on every build. As seen on GitHub, Linux and Macos would change the *nix hashes for every file constantly including java. For Windows on Github, it was changing the entire file (not clear as to why entire file but that was observed over and over). Testing to show issue was done with 2.23.0 formatter release that introduced the problem. --- .../revelc/code/formatter/FormatterMojo.java | 33 +++++++++++++++++-- .../code/formatter/css/CssFormatter.java | 13 ++++++++ .../code/formatter/java/JavaFormatter.java | 26 +++++---------- .../javascript/JavascriptFormatter.java | 13 ++++++++ .../code/formatter/json/JsonFormatter.java | 13 ++++++++ .../formatter/jsoup/JsoupBasedFormatter.java | 13 ++++++++ .../code/formatter/xml/XMLFormatter.java | 13 ++++++++ 7 files changed, 105 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/revelc/code/formatter/FormatterMojo.java b/src/main/java/net/revelc/code/formatter/FormatterMojo.java index df3182bb..0855c98e 100644 --- a/src/main/java/net/revelc/code/formatter/FormatterMojo.java +++ b/src/main/java/net/revelc/code/formatter/FormatterMojo.java @@ -728,7 +728,22 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro final var log = this.getLog(); log.debug("Processing file: " + file); final var originalCode = this.readFileAsString(file); - final var originalHash = this.sha512hash(originalCode + this.javaFormatter.hashCode()); + + // Default to original hashing, adjust based on appropriate file type for inclusion of formatter options + var originalHash = this.sha512hash(originalCode); + if (file.getName().endsWith(".java")) { + originalHash = this.sha512hash(originalCode + this.javaFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".js")) { + originalHash = this.sha512hash(originalCode + this.jsFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".html")) { + originalHash = this.sha512hash(originalCode + this.htmlFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".xml")) { + originalHash = this.sha512hash(originalCode + this.xmlFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".json")) { + originalHash = this.sha512hash(originalCode + this.jsonFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".css")) { + originalHash = this.sha512hash(originalCode + this.cssFormatter.getOptions().hashCode()); + } final var canonicalPath = file.getCanonicalPath(); final var path = canonicalPath.substring(basedirPath.length()); @@ -816,7 +831,21 @@ protected void doFormatFile(final File file, final ResultCollector rc, final Pro if (Result.SKIPPED.equals(result)) { formattedHash = originalHash; } else { - formattedHash = this.sha512hash(formattedCode + this.javaFormatter.hashCode()); + // Default to formatted hashing, adjust based on appropriate file type for inclusion of formatter options + formattedHash = this.sha512hash(originalCode); + if (file.getName().endsWith(".java")) { + formattedHash = this.sha512hash(originalCode + this.javaFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".js")) { + formattedHash = this.sha512hash(originalCode + this.jsFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".html")) { + formattedHash = this.sha512hash(originalCode + this.htmlFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".xml")) { + formattedHash = this.sha512hash(originalCode + this.xmlFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".json")) { + formattedHash = this.sha512hash(originalCode + this.jsonFormatter.getOptions().hashCode()); + } else if (file.getName().endsWith(".css")) { + formattedHash = this.sha512hash(originalCode + this.cssFormatter.getOptions().hashCode()); + } } hashCache.setProperty(path, formattedHash); this.hashCacheWritten = true; diff --git a/src/main/java/net/revelc/code/formatter/css/CssFormatter.java b/src/main/java/net/revelc/code/formatter/css/CssFormatter.java index 4e86317c..34d1dea8 100644 --- a/src/main/java/net/revelc/code/formatter/css/CssFormatter.java +++ b/src/main/java/net/revelc/code/formatter/css/CssFormatter.java @@ -38,6 +38,9 @@ public class CssFormatter extends AbstractCacheableFormatter implements Formatte /** The formatter. */ private CSSFormat formatter; + /** The configuration options */ + private Map options; + @Override public void init(final Map options, final ConfigurationSource cfg) { super.initCfg(cfg); @@ -48,6 +51,7 @@ public void init(final Map options, final ConfigurationSource cf .parseBoolean(options.getOrDefault("useSourceStringValues", Boolean.FALSE.toString())); this.formatter = new CSSFormat().setPropertiesInSeparateLines(indent).setRgbAsHex(rgbAsHex) .setUseSourceStringValues(useSourceStringValues); + this.options = options; } @Override @@ -78,4 +82,13 @@ public boolean isInitialized() { return this.formatter != null; } + /** + * Gets the options. + * + * @return the options + */ + public Map getOptions() { + return options; + } + } diff --git a/src/main/java/net/revelc/code/formatter/java/JavaFormatter.java b/src/main/java/net/revelc/code/formatter/java/JavaFormatter.java index 82fd13ba..268911ff 100644 --- a/src/main/java/net/revelc/code/formatter/java/JavaFormatter.java +++ b/src/main/java/net/revelc/code/formatter/java/JavaFormatter.java @@ -17,7 +17,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.regex.Pattern; import org.eclipse.jdt.core.ToolFactory; @@ -48,22 +47,6 @@ public class JavaFormatter extends AbstractCacheableFormatter implements Formatt /** The configuration options */ private Map options; - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - JavaFormatter that = (JavaFormatter) o; - return Objects.equals(formatter, that.formatter) && Objects.equals(exclusionPattern, that.exclusionPattern) - && Objects.equals(options, that.options); - } - - @Override - public int hashCode() { - return Objects.hash(formatter, exclusionPattern, options); - } - @Override public void init(final Map options, final ConfigurationSource cfg) { super.initCfg(cfg); @@ -140,4 +123,13 @@ protected static IRegion[] getRegions(final String code, final Pattern exclusion return regions.toArray(new IRegion[0]); } + /** + * Gets the options. + * + * @return the options + */ + public Map getOptions() { + return options; + } + } diff --git a/src/main/java/net/revelc/code/formatter/javascript/JavascriptFormatter.java b/src/main/java/net/revelc/code/formatter/javascript/JavascriptFormatter.java index 0b422a60..61f39c02 100644 --- a/src/main/java/net/revelc/code/formatter/javascript/JavascriptFormatter.java +++ b/src/main/java/net/revelc/code/formatter/javascript/JavascriptFormatter.java @@ -35,11 +35,15 @@ public class JavascriptFormatter extends AbstractCacheableFormatter implements F /** The formatter. */ private CodeFormatter formatter; + /** The configuration options */ + private Map options; + @Override public void init(final Map options, final ConfigurationSource cfg) { super.initCfg(cfg); this.formatter = ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_EXISTING); + this.options = options; } @Override @@ -67,4 +71,13 @@ public boolean isInitialized() { return this.formatter != null; } + /** + * Gets the options. + * + * @return the options + */ + public Map getOptions() { + return options; + } + } diff --git a/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java b/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java index d64e6b10..cf9ae404 100644 --- a/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java +++ b/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java @@ -42,6 +42,9 @@ public class JsonFormatter extends AbstractCacheableFormatter implements Formatt private static final Pattern ANY_EOL = Pattern.compile("\\R"); + /** The configuration options */ + public Map options; + @Override public void init(final Map options, final ConfigurationSource cfg) { super.initCfg(cfg); @@ -76,6 +79,7 @@ public DefaultPrettyPrinter withSeparators(final Separators separators) { this.formatter.setDefaultPrettyPrinter(printer); this.formatter.enable(SerializationFeature.INDENT_OUTPUT); this.formatter.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, useAlphabeticalOrder); + this.options = options; } @Override @@ -98,4 +102,13 @@ public boolean isInitialized() { return this.formatter != null; } + /** + * Gets the options. + * + * @return the options + */ + public Map getOptions() { + return options; + } + } diff --git a/src/main/java/net/revelc/code/formatter/jsoup/JsoupBasedFormatter.java b/src/main/java/net/revelc/code/formatter/jsoup/JsoupBasedFormatter.java index fdb205e7..b7a80df1 100644 --- a/src/main/java/net/revelc/code/formatter/jsoup/JsoupBasedFormatter.java +++ b/src/main/java/net/revelc/code/formatter/jsoup/JsoupBasedFormatter.java @@ -47,6 +47,9 @@ public abstract class JsoupBasedFormatter extends AbstractCacheableFormatter imp /** The formatter. */ private OutputSettings formatter; + /** The configuration options */ + private Map options; + @Override public void init(final Map options, final ConfigurationSource cfg) { super.initCfg(cfg); @@ -59,6 +62,7 @@ public void init(final Map options, final ConfigurationSource cf this.formatter.outline(Boolean.parseBoolean(options.getOrDefault("outlineMode", Boolean.TRUE.toString()))); this.formatter.prettyPrint(Boolean.parseBoolean(options.getOrDefault("pretty", Boolean.TRUE.toString()))); this.formatter.syntax(Syntax.valueOf(options.getOrDefault("syntax", Syntax.html.name()))); + this.options = options; } @Override @@ -128,4 +132,13 @@ public boolean isInitialized() { return this.formatter != null; } + /** + * Gets the options. + * + * @return the options + */ + public Map getOptions() { + return options; + } + } diff --git a/src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java b/src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java index 272ec945..4b3ad430 100644 --- a/src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java +++ b/src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java @@ -31,6 +31,9 @@ public class XMLFormatter extends AbstractCacheableFormatter implements Formatte /** The formatter. */ private XmlDocumentFormatter formatter; + /** The configuration options */ + private Map options; + @Override public void init(final Map options, final ConfigurationSource cfg) { super.initCfg(cfg); @@ -45,6 +48,7 @@ public void init(final Map options, final ConfigurationSource cf prefs.setDeleteBlankLines(Boolean.parseBoolean(options.getOrDefault("deleteBlankLines", "false"))); this.formatter = new XmlDocumentFormatter(options.getOrDefault("lineending", System.lineSeparator()), prefs); + this.options = options; } @Override @@ -63,4 +67,13 @@ public boolean isInitialized() { return this.formatter != null; } + /** + * Gets the options. + * + * @return the options + */ + public Map getOptions() { + return options; + } + }