dev.jbang.dependencies.DependencyCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbang-cli Show documentation
Show all versions of jbang-cli Show documentation
JBang Command Line Interface
package dev.jbang.dependencies;
import static dev.jbang.util.Util.warnMsg;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import dev.jbang.Settings;
import dev.jbang.util.Util;
public class DependencyCache {
private static Map> depCache = null;
private static Map> getCache() {
if (depCache == null) {
// Use cached classpath from previous run if present if
if (Files.isRegularFile(Settings.getCacheDependencyFile())) {
try (Reader out = Files.newBufferedReader(Settings.getCacheDependencyFile())) {
JsonDeserializer serializer = (json, typeOfT, context) -> {
JsonObject jsonObject = json.getAsJsonObject();
MavenCoordinate gav = MavenCoordinate.fromCanonicalString(jsonObject.get("gav").getAsString());
Path file = Paths.get(jsonObject.get("file").getAsString());
long ts = jsonObject.has("ts") ? jsonObject.get("ts").getAsLong() : 0;
return new ArtifactInfo(gav, file, ts);
};
Gson parser = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(ArtifactInfo.class, serializer)
.create();
Type empMapType = new TypeToken