Skip to content

Commit

Permalink
Add content hash to written classpath config
Browse files Browse the repository at this point in the history
  • Loading branch information
szarnekow committed Jan 10, 2024
1 parent abe00bb commit 6a33588
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void testWriteClassPathConfiguration() throws IOException {
try(FileReader reader = new FileReader(configFile, StandardCharsets.UTF_8)) {
onlyProd.load(reader);
}
assertEquals(5, onlyProd.size());
assertEquals(7, onlyProd.size());

testBuilder.setClassPathEntries(ImmutableList.of("test-data/standalone.with.reference/target/classes/",
"test-data/missing.jar"));
Expand All @@ -205,7 +205,7 @@ public void testWriteClassPathConfiguration() throws IOException {
}

assertTrue(alsoTest.entrySet().containsAll(onlyProd.entrySet()));
assertEquals(10, alsoTest.size());
assertEquals(14, alsoTest.size());

testBuilder.setClassPathEntries(ImmutableList.of("test-data/standalone.with.reference/target/classes/"));
testBuilder.setClasspathConfigurationLocation(configFile.getAbsolutePath(), "prod", "prod-out");
Expand All @@ -216,7 +216,7 @@ public void testWriteClassPathConfiguration() throws IOException {
try(FileReader reader = new FileReader(configFile, StandardCharsets.UTF_8)) {
alsoTest.load(reader);
}
assertEquals(9, alsoTest.size());
assertEquals(12, alsoTest.size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -103,6 +105,7 @@
import com.google.common.base.Joiner;
import com.google.common.base.Stopwatch;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -355,11 +358,11 @@ private void writeClassPathConfiguration(Iterable<String> modelRoots, boolean cl
String key = String.valueOf(existing.getKey());
return key.startsWith(prefix);
});
intoProperties(modelRoots, prefix + "model.", properties);
intoProperties(sourceDirs, prefix + "src.", properties);
intoProperties(modelRoots, prefix + "model.", properties, true);
intoProperties(sourceDirs, prefix + "src.", properties, false);
if (classpath) {
intoProperties(List.of(classOutputDirectory), prefix + "bin.", properties);
intoProperties(classPathEntries, prefix + "cp.", properties);
intoProperties(List.of(classOutputDirectory), prefix + "bin.", properties, false);
intoProperties(classPathEntries, prefix + "cp.", properties, true);
}
try (Writer writer = new TailWriter(new FileWriter(file, StandardCharsets.UTF_8), 1)) {
new Properties() {
Expand All @@ -378,10 +381,15 @@ public Set<Map.Entry<Object, Object>> entrySet() {
}
}

private void intoProperties(Iterable<String> values, String prefix, Properties target) {
private void intoProperties(Iterable<String> values, String prefix, Properties target, boolean hash) {
int i = 0;
for(String value: values) {
target.put(prefix + i, new File(value).getAbsolutePath());
String key = prefix + i;
target.put(key, new File(value).getAbsolutePath());
if (hash) {
IPath path = new Path(value);
target.put(key + ".hash", classpathInfos.hashClassesOrJar(path).asString());
}
i++;
}
target.put(prefix + "count", String.valueOf(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface ClasspathEntryHash {

void accept(ClasspathEntryHashVisitor visitor);

String asString();

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ public void accept(ClasspathEntryHashVisitor visitor) {
public byte[] asBytes() {
return hashCode.asBytes();
}

public String asString() {
return hashCode.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
package org.eclipse.xtext.builder.standalone.incremental;

import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Stream;

import org.eclipse.core.runtime.IPath;

import com.google.common.hash.HashCode;
import com.google.common.hash.Hasher;

public class FineGrainedEntryHash implements ClasspathEntryHash {
private final Map<IPath, HashCode> classHashes;
Expand All @@ -33,5 +37,14 @@ public void accept(ClasspathEntryHashVisitor visitor) {
public Map<IPath, HashCode> classHashes() {
return Collections.unmodifiableMap(classHashes);
}

@Override
public String asString() {
Stream<Entry<IPath, HashCode>> sorted = classHashes.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey, Comparator.comparing(IPath::toString)));
Stream<byte[]> bytes = sorted.map(Map.Entry::getValue).map(HashCode::asBytes);
Hasher hasher = BinaryFileHashing.hashFunction().newHasher();
bytes.forEachOrdered(hasher::putBytes);
return hasher.hash().toString();
}

}

0 comments on commit 6a33588

Please sign in to comment.