Skip to content

Commit

Permalink
Fix to cache so that it doesn't hold on to a parsed properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevin committed Jul 19, 2024
1 parent bb4c980 commit a3830db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
43 changes: 25 additions & 18 deletions src/main/java/rife/bld/BldCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class BldCache {
private static final String PROPERTY_DEPENDENCIES_HASH = PROPERTY_DEPENDENCIES_PREFIX + PROPERTY_SUFFIX_HASH;

private final File hashFile_;
private final Properties hashProperties_ = new Properties();
private final VersionResolution resolution_;
private String extensionsHash_;
private String dependenciesHash_;
Expand All @@ -59,14 +58,6 @@ public BldCache(File bldLibDir, VersionResolution resolution) {

new File(bldLibDir, WRAPPER_PROPERTIES_HASH).delete();
new File(bldLibDir, BLD_BUILD_HASH).delete();

if (hashFile_.exists()) {
try {
hashProperties_.load(new FileInputStream(hashFile_));
} catch (IOException e) {
// no-op, we'll store a new properties file when we're writing the cache
}
}
}

public void fingerprintExtensions(Collection<String> repositories, Collection<String> extensions, boolean downloadSources, boolean downloadJavadoc) {
Expand Down Expand Up @@ -121,16 +112,29 @@ public boolean isExtensionHashValid() {
return validateExtensionsHash(extensionsHash_);
}

private Properties hashProperties() {
var properties = new Properties();
if (hashFile_.exists()) {
try {
properties.load(new FileInputStream(hashFile_));
} catch (IOException e) {
// no-op, we'll store a new properties file when we're writing the cache
}
}
return properties;
}

private boolean validateExtensionsHash(String hash) {
if (!hashFile_.exists() || hashProperties_.isEmpty()) {
var properties = hashProperties();
if (!hashFile_.exists() || properties.isEmpty()) {
return false;
}

if (!hash.equals(hashProperties_.getProperty(PROPERTY_EXTENSIONS_HASH))) {
if (!hash.equals(properties.getProperty(PROPERTY_EXTENSIONS_HASH))) {
return false;
}

var local_files = hashProperties_.getProperty(PROPERTY_EXTENSIONS_LOCAL);
var local_files = properties.getProperty(PROPERTY_EXTENSIONS_LOCAL);
if (local_files != null && !local_files.isEmpty()) {
var lines = StringUtils.split(local_files, "\n");
if (!lines.isEmpty()) {
Expand Down Expand Up @@ -164,21 +168,24 @@ public boolean isDependenciesHashValid() {
}

private boolean validateDependenciesHash(String hash) {
if (!hashFile_.exists() || hashProperties_.isEmpty()) {
var properties = hashProperties();
if (!hashFile_.exists() || properties.isEmpty()) {
return false;
}

return hash.equals(hashProperties_.getProperty(PROPERTY_DEPENDENCIES_HASH));
return hash.equals(properties.getProperty(PROPERTY_DEPENDENCIES_HASH));
}

public void writeCache() {
writeCache(null);
}

public void writeCache(List<File> extensionsLocalArtifacts) {
var properties = hashProperties();

try {
if (extensionsHash_ != null) {
hashProperties_.put(PROPERTY_EXTENSIONS_HASH, extensionsHash_);
properties.put(PROPERTY_EXTENSIONS_HASH, extensionsHash_);
}

if (extensionsLocalArtifacts != null) {
Expand All @@ -188,15 +195,15 @@ public void writeCache(List<File> extensionsLocalArtifacts) {
extensions_local.append("\n").append(file.lastModified()).append(':').append(file.getAbsolutePath());
}
}
hashProperties_.put(PROPERTY_EXTENSIONS_LOCAL, extensions_local.toString());
properties.put(PROPERTY_EXTENSIONS_LOCAL, extensions_local.toString());
}

if (dependenciesHash_ != null) {
hashProperties_.put(PROPERTY_DEPENDENCIES_HASH, dependenciesHash_);
properties.put(PROPERTY_DEPENDENCIES_HASH, dependenciesHash_);
}

hashFile_.getParentFile().mkdirs();
hashProperties_.store(new FileOutputStream(hashFile_), null);
properties.store(new FileOutputStream(hashFile_), null);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ void testUpdateHash()
bld.cache""", String.join("\n", files3));
FileUtils.writeString("updated", hash_file);

resolver = new WrapperExtensionResolver(tmp1, tmp2, new Properties(), new Properties(), List.of(MAVEN_CENTRAL), List.of("org.antlr:antlr4:4.11.1"), false, false);
resolver.updateExtensions();
var files4 = FileUtils.getFileList(tmp2);
assertEquals(10, files4.size());
Expand Down

0 comments on commit a3830db

Please sign in to comment.