64 lines
2 KiB
Java
64 lines
2 KiB
Java
package eu.m724.blog.data;
|
|
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import org.eclipse.jgit.api.Git;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.util.AbstractMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
public record Site(
|
|
String name,
|
|
String baseUrl,
|
|
|
|
Map<String, Object> custom
|
|
) {
|
|
public static Site fromConfig(Git git) throws IOException {
|
|
var content = Files.readString(git.getRepository().getDirectory().toPath().getParent().resolve("config.json"));
|
|
var json = JsonParser.parseString(content).getAsJsonObject().asMap();
|
|
|
|
String name = null;
|
|
String baseUrl = null;
|
|
var custom = new HashMap<String, Object>();
|
|
|
|
for (var entry : json.entrySet()) {
|
|
switch (entry.getKey()) {
|
|
case "name":
|
|
name = entry.getValue().getAsString();
|
|
break;
|
|
case "baseUrl":
|
|
baseUrl = entry.getValue().getAsString();
|
|
break;
|
|
default:
|
|
custom.put(entry.getKey(), eToO(entry.getValue()));
|
|
}
|
|
}
|
|
|
|
return new Site(
|
|
name, baseUrl, custom
|
|
);
|
|
}
|
|
|
|
private static Map<String, Object> deep(JsonObject jsonObject) {
|
|
return jsonObject.entrySet().stream()
|
|
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), eToO(e.getValue())))
|
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
|
}
|
|
|
|
private static Object eToO(JsonElement element) {
|
|
if (element.isJsonArray()) {
|
|
return element.getAsJsonArray().asList().stream().map(Site::eToO).toList();
|
|
} else if (element.isJsonObject()) {
|
|
return deep(element.getAsJsonObject());
|
|
} else if (element.isJsonPrimitive()) {
|
|
// TODO
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|