Skip to content

Commit

Permalink
[caching] Rewrite d1d5852 fixing revelc#757
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hazendaz committed Mar 10, 2024
1 parent ae6c279 commit f2e146f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 19 deletions.
33 changes: 31 additions & 2 deletions src/main/java/net/revelc/code/formatter/FormatterMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/revelc/code/formatter/css/CssFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class CssFormatter extends AbstractCacheableFormatter implements Formatte
/** The formatter. */
private CSSFormat formatter;

/** The configuration options */
private Map<String, String> options;

@Override
public void init(final Map<String, String> options, final ConfigurationSource cfg) {
super.initCfg(cfg);
Expand All @@ -48,6 +51,7 @@ public void init(final Map<String, String> 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
Expand Down Expand Up @@ -78,4 +82,13 @@ public boolean isInitialized() {
return this.formatter != null;
}

/**
* Gets the options.
*
* @return the options
*/
public Map<String, String> getOptions() {
return options;
}

}
26 changes: 9 additions & 17 deletions src/main/java/net/revelc/code/formatter/java/JavaFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,22 +47,6 @@ public class JavaFormatter extends AbstractCacheableFormatter implements Formatt
/** The configuration options */
private Map<String, String> 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<String, String> options, final ConfigurationSource cfg) {
super.initCfg(cfg);
Expand Down Expand Up @@ -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<String, String> getOptions() {
return options;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ public class JavascriptFormatter extends AbstractCacheableFormatter implements F
/** The formatter. */
private CodeFormatter formatter;

/** The configuration options */
private Map<String, String> options;

@Override
public void init(final Map<String, String> options, final ConfigurationSource cfg) {
super.initCfg(cfg);

this.formatter = ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_EXISTING);
this.options = options;
}

@Override
Expand Down Expand Up @@ -67,4 +71,13 @@ public boolean isInitialized() {
return this.formatter != null;
}

/**
* Gets the options.
*
* @return the options
*/
public Map<String, String> getOptions() {
return options;
}

}
13 changes: 13 additions & 0 deletions src/main/java/net/revelc/code/formatter/json/JsonFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> options;

@Override
public void init(final Map<String, String> options, final ConfigurationSource cfg) {
super.initCfg(cfg);
Expand Down Expand Up @@ -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
Expand All @@ -98,4 +102,13 @@ public boolean isInitialized() {
return this.formatter != null;
}

/**
* Gets the options.
*
* @return the options
*/
public Map<String, String> getOptions() {
return options;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public abstract class JsoupBasedFormatter extends AbstractCacheableFormatter imp
/** The formatter. */
private OutputSettings formatter;

/** The configuration options */
private Map<String, String> options;

@Override
public void init(final Map<String, String> options, final ConfigurationSource cfg) {
super.initCfg(cfg);
Expand All @@ -59,6 +62,7 @@ public void init(final Map<String, String> 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
Expand Down Expand Up @@ -128,4 +132,13 @@ public boolean isInitialized() {
return this.formatter != null;
}

/**
* Gets the options.
*
* @return the options
*/
public Map<String, String> getOptions() {
return options;
}

}
13 changes: 13 additions & 0 deletions src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class XMLFormatter extends AbstractCacheableFormatter implements Formatte
/** The formatter. */
private XmlDocumentFormatter formatter;

/** The configuration options */
private Map<String, String> options;

@Override
public void init(final Map<String, String> options, final ConfigurationSource cfg) {
super.initCfg(cfg);
Expand All @@ -45,6 +48,7 @@ public void init(final Map<String, String> 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
Expand All @@ -63,4 +67,13 @@ public boolean isInitialized() {
return this.formatter != null;
}

/**
* Gets the options.
*
* @return the options
*/
public Map<String, String> getOptions() {
return options;
}

}

0 comments on commit f2e146f

Please sign in to comment.