diff --git a/jgrab-runner/src/main/java/com/athaydes/jgrab/Dependency.java b/jgrab-runner/src/main/java/com/athaydes/jgrab/Dependency.java index cd7048d..9838f6e 100644 --- a/jgrab-runner/src/main/java/com/athaydes/jgrab/Dependency.java +++ b/jgrab-runner/src/main/java/com/athaydes/jgrab/Dependency.java @@ -1,12 +1,15 @@ package com.athaydes.jgrab; -import java.util.Comparator; -import java.util.Set; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * A single dependency on an external module. */ @@ -39,6 +42,24 @@ public static Dependency of( String declaration ) { return new Dependency( parts[ 0 ], parts[ 1 ], parts.length == 2 ? "latest" : parts[ 2 ] ); } + public static String hashOf( Collection dependencies ) { + var list = new ArrayList<>( dependencies ); + list.sort( COMPARATOR ); + + MessageDigest digest; + try { + digest = MessageDigest.getInstance( "SHA-256" ); + } catch ( NoSuchAlgorithmException e ) { + throw new RuntimeException( e ); + } + for ( var dependency : list ) { + digest.update( dependency.group.getBytes( UTF_8 ) ); + digest.update( dependency.module.getBytes( UTF_8 ) ); + digest.update( dependency.version.getBytes( UTF_8 ) ); + } + return Base64.getUrlEncoder().encodeToString( digest.digest() ); + } + public static Set parseDependencies( Stream codeLines ) { return codeLines.flatMap( line -> { Matcher matcher = JGRAB_PATTERN.matcher( line ); diff --git a/jgrab-runner/src/main/java/com/athaydes/jgrab/daemon/PersistentCache.java b/jgrab-runner/src/main/java/com/athaydes/jgrab/daemon/PersistentCache.java index 958ebd1..adf27fd 100644 --- a/jgrab-runner/src/main/java/com/athaydes/jgrab/daemon/PersistentCache.java +++ b/jgrab-runner/src/main/java/com/athaydes/jgrab/daemon/PersistentCache.java @@ -1,6 +1,7 @@ package com.athaydes.jgrab.daemon; import com.athaydes.jgrab.Dependency; +import com.athaydes.jgrab.JGrabHome; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,11 +11,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardOpenOption; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -27,23 +24,12 @@ class PersistentCache { private static final Logger logger = LoggerFactory.getLogger( PersistentCache.class ); - private static final File JGRAB_HOME; - - static { - String userHome = System.getProperty( "user.home" ); - if ( userHome == null ) { - userHome = "."; - } - - JGRAB_HOME = new File( userHome, ".jgrab" ); - } - private final File cacheFile; private final AtomicBoolean isCacheLoaded = new AtomicBoolean( false ); private final Map, List> cache = new HashMap<>(); PersistentCache() { - this( new File( JGRAB_HOME, "deps-cache" ) ); + this( new File( JGrabHome.getDir(), "deps-cache" ) ); } PersistentCache( File cacheFile ) { @@ -77,7 +63,9 @@ void save() throws IOException { try ( BufferedWriter writer = Files.newBufferedWriter( tempCacheFile.toPath(), StandardOpenOption.WRITE ) ) { for (Map.Entry, List> entry : cache.entrySet()) { - String deps = entry.getKey().stream() + var dependencies = new ArrayList<>( entry.getKey() ); + dependencies.sort( Dependency.COMPARATOR ); + String deps = dependencies.stream() .map( Dependency::canonicalNotation ) .collect( Collectors.joining( "," ) ); String libs = entry.getValue().stream() @@ -97,7 +85,7 @@ void save() throws IOException { Files.move( tempCacheFile.toPath(), cacheFile.toPath() ); logger.info( "Dependencies cache saved at {}", cacheFile ); } else { - logger.warn( "Unable to save cache to {}. The file could not be re-created. Cache was save at {}", + logger.warn( "Unable to save cache to {}. The file could not be re-created. Cache was saved at {}", cacheFile, tempCacheFile ); } } diff --git a/jgrab-runner/src/main/java/com/athaydes/jgrab/jbuild/JBuildGrabber.java b/jgrab-runner/src/main/java/com/athaydes/jgrab/jbuild/JBuildGrabber.java index 01d995d..719019d 100644 --- a/jgrab-runner/src/main/java/com/athaydes/jgrab/jbuild/JBuildGrabber.java +++ b/jgrab-runner/src/main/java/com/athaydes/jgrab/jbuild/JBuildGrabber.java @@ -18,14 +18,11 @@ import java.io.File; import java.io.FilenameFilter; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; -import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.toList; import static jbuild.artifact.file.ArtifactFileWriter.WriteMode.FLAT_DIR; @@ -85,21 +82,7 @@ private File outputDirHashOf( Collection toGrab ) { //noinspection ResultOfMethodCallIgnored cacheDir.mkdirs(); - var list = new ArrayList<>( toGrab ); - list.sort( Dependency.COMPARATOR ); - - MessageDigest digest; - try { - digest = MessageDigest.getInstance( "SHA-256" ); - } catch ( NoSuchAlgorithmException e ) { - throw new RuntimeException( e ); - } - for ( var dependency : list ) { - digest.update( dependency.group.getBytes( UTF_8 ) ); - digest.update( dependency.module.getBytes( UTF_8 ) ); - digest.update( dependency.version.getBytes( UTF_8 ) ); - } - return new File( cacheDir, Base64.getUrlEncoder().encodeToString( digest.digest() ) ); + return new File( cacheDir, Dependency.hashOf( toGrab ) ); } private static Set artifacts( Collection toGrab ) {