mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-02 02:34:24 +00:00
Reload Zed schemas after they are regenerated
Zed's JSON server caches schema files until its configuration changes and never reports schema file edits. Schema files are now written through one writer that hands back the text, and a hash of those texts goes into the Zed settings as irisSchemaHash, so regenerated schemas reach the editor. v3: f31d41a9f
This commit is contained in:
1 parent
ffbd0db03a
commit
fc3ea69d74
9 files changed
+355
-121
No files matched your search
+16
-20
@@ -22,10 +22,10 @@ import art.arcane.iris.core.loader.IrisData;
|
|||||||
import art.arcane.iris.core.loader.ResourceLoader;
|
import art.arcane.iris.core.loader.ResourceLoader;
|
||||||
import art.arcane.iris.core.project.ModuleResourceSchemas;
|
import art.arcane.iris.core.project.ModuleResourceSchemas;
|
||||||
import art.arcane.iris.core.project.SchemaBuilder;
|
import art.arcane.iris.core.project.SchemaBuilder;
|
||||||
|
import art.arcane.iris.core.project.SchemaFiles;
|
||||||
import art.arcane.iris.core.project.ZedProjectSettings;
|
import art.arcane.iris.core.project.ZedProjectSettings;
|
||||||
import art.arcane.iris.engine.object.annotations.Snippet;
|
import art.arcane.iris.engine.object.annotations.Snippet;
|
||||||
import art.arcane.iris.spi.IrisLogging;
|
import art.arcane.iris.spi.IrisLogging;
|
||||||
import art.arcane.iris.util.common.scheduling.J;
|
|
||||||
import art.arcane.volmlib.util.collection.KList;
|
import art.arcane.volmlib.util.collection.KList;
|
||||||
import art.arcane.volmlib.util.format.Form;
|
import art.arcane.volmlib.util.format.Form;
|
||||||
import art.arcane.volmlib.util.io.IO;
|
import art.arcane.volmlib.util.io.IO;
|
||||||
@@ -34,6 +34,9 @@ import art.arcane.volmlib.util.json.JSONObject;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public final class ModdedWorkspaceGenerator {
|
public final class ModdedWorkspaceGenerator {
|
||||||
private ModdedWorkspaceGenerator() {
|
private ModdedWorkspaceGenerator() {
|
||||||
@@ -44,18 +47,20 @@ public final class ModdedWorkspaceGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static File writeWorkspace(IrisData data, File folder, boolean immediateSchemas) throws IOException {
|
public static File writeWorkspace(IrisData data, File folder, boolean immediateSchemas) throws IOException {
|
||||||
JSONObject workspaceConfig = buildWorkspace(data, immediateSchemas);
|
List<CompletableFuture<String>> schemaWrites = new ArrayList<>();
|
||||||
|
JSONObject workspaceConfig = buildWorkspace(data, immediateSchemas, schemaWrites);
|
||||||
File workspace = new File(folder, folder.getName() + ".code-workspace");
|
File workspace = new File(folder, folder.getName() + ".code-workspace");
|
||||||
IO.writeAll(workspace, workspaceConfig.toString(4));
|
IO.writeAll(workspace, workspaceConfig.toString(4));
|
||||||
new ZedProjectSettings(folder).writeIrisSettings(workspaceConfig.getJSONObject("settings").getJSONArray("json.schemas"));
|
JSONArray schemas = workspaceConfig.getJSONObject("settings").getJSONArray("json.schemas");
|
||||||
|
new ZedProjectSettings(folder).writeIrisSettings(schemas, SchemaFiles.awaitTexts(schemaWrites));
|
||||||
return workspace;
|
return workspace;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JSONObject buildWorkspace(IrisData data) {
|
public static JSONObject buildWorkspace(IrisData data) {
|
||||||
return buildWorkspace(data, false);
|
return buildWorkspace(data, false, new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSONObject buildWorkspace(IrisData data, boolean immediateSchemas) {
|
private static JSONObject buildWorkspace(IrisData data, boolean immediateSchemas, List<CompletableFuture<String>> schemaWrites) {
|
||||||
JSONObject ws = new JSONObject();
|
JSONObject ws = new JSONObject();
|
||||||
JSONArray folders = new JSONArray();
|
JSONArray folders = new JSONArray();
|
||||||
JSONObject folder = new JSONObject();
|
JSONObject folder = new JSONObject();
|
||||||
@@ -85,20 +90,21 @@ public final class ModdedWorkspaceGenerator {
|
|||||||
json.put("editor.suggest.insertMode", "replace");
|
json.put("editor.suggest.insertMode", "replace");
|
||||||
settings.put("[json]", json);
|
settings.put("[json]", json);
|
||||||
settings.put("json.maxItemsComputed", 30000);
|
settings.put("json.maxItemsComputed", 30000);
|
||||||
settings.put("json.schemas", buildSchemas(data, immediateSchemas));
|
settings.put("json.schemas", buildSchemas(data, immediateSchemas, schemaWrites));
|
||||||
ws.put("settings", settings);
|
ws.put("settings", settings);
|
||||||
return ws;
|
return ws;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSONArray buildSchemas(IrisData data, boolean immediateSchemas) {
|
private static JSONArray buildSchemas(IrisData data, boolean immediateSchemas, List<CompletableFuture<String>> schemaWrites) {
|
||||||
JSONArray schemas = new JSONArray();
|
JSONArray schemas = new JSONArray();
|
||||||
|
|
||||||
for (ResourceLoader<?> loader : data.getLoaders().v()) {
|
for (ResourceLoader<?> loader : data.getLoaders().v()) {
|
||||||
if (loader.supportsSchemas()) {
|
if (loader.supportsSchemas()) {
|
||||||
schemas.put(immediateSchemas ? loader.buildSchemaImmediately() : loader.buildSchema());
|
schemas.put(loader.schemaMapping());
|
||||||
|
schemaWrites.add(loader.writeSchemaFile(immediateSchemas));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ModuleResourceSchemas.entries(data, immediateSchemas).forEach(schemas::put);
|
ModuleResourceSchemas.entries(data, immediateSchemas, schemaWrites).forEach(schemas::put);
|
||||||
|
|
||||||
for (Class<?> snippetClass : data.resolveSnippets()) {
|
for (Class<?> snippetClass : data.resolveSnippets()) {
|
||||||
try {
|
try {
|
||||||
@@ -114,17 +120,7 @@ public final class ModdedWorkspaceGenerator {
|
|||||||
entry.put("url", "./.iris/schema/snippet/" + snipType + "-schema.json");
|
entry.put("url", "./.iris/schema/snippet/" + snipType + "-schema.json");
|
||||||
schemas.put(entry);
|
schemas.put(entry);
|
||||||
File schemaFile = new File(data.getDataFolder(), ".iris/schema/snippet/" + snipType + "-schema.json");
|
File schemaFile = new File(data.getDataFolder(), ".iris/schema/snippet/" + snipType + "-schema.json");
|
||||||
if (immediateSchemas) {
|
schemaWrites.add(SchemaFiles.write(schemaFile, () -> new SchemaBuilder(snippetClass, data).construct().toString(4), immediateSchemas));
|
||||||
IO.writeAll(schemaFile, new SchemaBuilder(snippetClass, data).construct().toString(4));
|
|
||||||
} else {
|
|
||||||
J.attemptAsync(() -> {
|
|
||||||
try {
|
|
||||||
IO.writeAll(schemaFile, new SchemaBuilder(snippetClass, data).construct().toString(4));
|
|
||||||
} catch (Throwable e) {
|
|
||||||
IrisLogging.reportError(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
if (immediateSchemas) {
|
if (immediateSchemas) {
|
||||||
throw new IllegalStateException("Could not write snippet schema for " + snippetClass.getName(), e);
|
throw new IllegalStateException("Could not write snippet schema for " + snippetClass.getName(), e);
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import art.arcane.iris.spi.IrisServices;
|
|||||||
import art.arcane.iris.spi.IrisPlatforms;
|
import art.arcane.iris.spi.IrisPlatforms;
|
||||||
import art.arcane.iris.core.IrisSettings;
|
import art.arcane.iris.core.IrisSettings;
|
||||||
import art.arcane.iris.core.project.SchemaBuilder;
|
import art.arcane.iris.core.project.SchemaBuilder;
|
||||||
|
import art.arcane.iris.core.project.SchemaFiles;
|
||||||
import art.arcane.iris.engine.framework.PreservationRegistry;
|
import art.arcane.iris.engine.framework.PreservationRegistry;
|
||||||
import art.arcane.volmlib.util.cache.AtomicCache;
|
import art.arcane.volmlib.util.cache.AtomicCache;
|
||||||
import art.arcane.iris.engine.framework.Engine;
|
import art.arcane.iris.engine.framework.Engine;
|
||||||
@@ -72,6 +73,7 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@@ -79,6 +81,7 @@ import java.util.concurrent.RejectedExecutionException;
|
|||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
@@ -96,7 +99,7 @@ public class ResourceLoader<T extends IrisRegistrant> implements MeteredCache {
|
|||||||
private static final int PREFETCH_KEY_LENGTH_LIMIT = 1_024;
|
private static final int PREFETCH_KEY_LENGTH_LIMIT = 1_024;
|
||||||
private static final long PREFETCH_FILE_SIZE_LIMIT = 4L * 1_024L * 1_024L;
|
private static final long PREFETCH_FILE_SIZE_LIMIT = 4L * 1_024L * 1_024L;
|
||||||
private static volatile ExecutorService schemaBuildExecutor;
|
private static volatile ExecutorService schemaBuildExecutor;
|
||||||
private static final Set<String> schemaBuildQueue = ConcurrentHashMap.newKeySet();
|
private static final Map<String, CompletableFuture<String>> schemaBuildQueue = new ConcurrentHashMap<>();
|
||||||
protected final AtomicCache<KList<File>> folderCache;
|
protected final AtomicCache<KList<File>> folderCache;
|
||||||
protected volatile KSet<String> firstAccess;
|
protected volatile KSet<String> firstAccess;
|
||||||
protected File root;
|
protected File root;
|
||||||
@@ -150,7 +153,7 @@ public class ResourceLoader<T extends IrisRegistrant> implements MeteredCache {
|
|||||||
private static synchronized ExecutorService schemaBuildExecutor() {
|
private static synchronized ExecutorService schemaBuildExecutor() {
|
||||||
ExecutorService current = schemaBuildExecutor;
|
ExecutorService current = schemaBuildExecutor;
|
||||||
if (current == null || current.isShutdown()) {
|
if (current == null || current.isShutdown()) {
|
||||||
schemaBuildQueue.clear();
|
abandonQueuedSchemaBuilds();
|
||||||
current = Executors.newSingleThreadExecutor(runnable -> {
|
current = Executors.newSingleThreadExecutor(runnable -> {
|
||||||
Thread thread = new Thread(runnable, "Iris-Schema-Builder");
|
Thread thread = new Thread(runnable, "Iris-Schema-Builder");
|
||||||
thread.setDaemon(true);
|
thread.setDaemon(true);
|
||||||
@@ -166,16 +169,17 @@ public class ResourceLoader<T extends IrisRegistrant> implements MeteredCache {
|
|||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void abandonQueuedSchemaBuilds() {
|
||||||
|
schemaBuildQueue.values().forEach(build -> build.complete(SchemaFiles.FAILED_SCHEMA_TEXT));
|
||||||
|
schemaBuildQueue.clear();
|
||||||
|
}
|
||||||
|
|
||||||
public JSONObject buildSchema() {
|
public JSONObject buildSchema() {
|
||||||
return buildSchema(false);
|
writeSchemaFile(false);
|
||||||
|
return schemaMapping();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject buildSchemaImmediately() {
|
public JSONObject schemaMapping() {
|
||||||
return buildSchema(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private JSONObject buildSchema(boolean immediate) {
|
|
||||||
IrisLogging.debug("Building Schema " + objectClass.getSimpleName() + " " + root.getPath());
|
|
||||||
JSONObject o = new JSONObject();
|
JSONObject o = new JSONObject();
|
||||||
KList<String> fm = new KList<>();
|
KList<String> fm = new KList<>();
|
||||||
|
|
||||||
@@ -185,39 +189,40 @@ public class ResourceLoader<T extends IrisRegistrant> implements MeteredCache {
|
|||||||
|
|
||||||
o.put("fileMatch", new JSONArray(fm.toArray()));
|
o.put("fileMatch", new JSONArray(fm.toArray()));
|
||||||
o.put("url", "./.iris/schema/" + getFolderName() + "-schema.json");
|
o.put("url", "./.iris/schema/" + getFolderName() + "-schema.json");
|
||||||
File a = new File(getManager().getDataFolder(), ".iris/schema/" + getFolderName() + "-schema.json");
|
|
||||||
if (immediate) {
|
|
||||||
try {
|
|
||||||
IO.writeAll(a, new SchemaBuilder(objectClass, manager).construct().toString(4));
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalStateException("Could not write schema " + a.getAbsolutePath(), e);
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
String schemaPath = a.getAbsolutePath();
|
|
||||||
if (schemaBuildQueue.add(schemaPath)) {
|
|
||||||
try {
|
|
||||||
schemaBuildExecutor().execute(() -> {
|
|
||||||
try {
|
|
||||||
IO.writeAll(a, new SchemaBuilder(objectClass, manager).construct().toString(4));
|
|
||||||
} catch (Throwable e) {
|
|
||||||
IrisLogging.reportError(e);
|
|
||||||
} finally {
|
|
||||||
schemaBuildQueue.remove(schemaPath);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (RejectedExecutionException rejected) {
|
|
||||||
// Never let a dead pool leak the queue entry or escape into workspace
|
|
||||||
// generation (which would delete the user's .code-workspace on the way out).
|
|
||||||
schemaBuildQueue.remove(schemaPath);
|
|
||||||
IrisLogging.warn("Schema build skipped (executor unavailable): " + schemaPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CompletableFuture<String> writeSchemaFile(boolean immediate) {
|
||||||
|
IrisLogging.debug("Building Schema " + objectClass.getSimpleName() + " " + root.getPath());
|
||||||
|
File schemaFile = new File(getManager().getDataFolder(), ".iris/schema/" + getFolderName() + "-schema.json");
|
||||||
|
Supplier<String> schemaText = () -> new SchemaBuilder(objectClass, manager).construct().toString(4);
|
||||||
|
if (immediate) {
|
||||||
|
return SchemaFiles.write(schemaFile, schemaText, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
String schemaPath = schemaFile.getAbsolutePath();
|
||||||
|
CompletableFuture<String> build = new CompletableFuture<>();
|
||||||
|
CompletableFuture<String> queued = schemaBuildQueue.putIfAbsent(schemaPath, build);
|
||||||
|
if (queued != null) {
|
||||||
|
return queued;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
schemaBuildExecutor().execute(() -> {
|
||||||
|
String text = SchemaFiles.writeReporting(schemaFile, schemaText);
|
||||||
|
schemaBuildQueue.remove(schemaPath, build);
|
||||||
|
build.complete(text);
|
||||||
|
});
|
||||||
|
} catch (RejectedExecutionException rejected) {
|
||||||
|
// Never let a dead pool leak the queue entry or escape into workspace
|
||||||
|
// generation (which would delete the user's .code-workspace on the way out).
|
||||||
|
schemaBuildQueue.remove(schemaPath, build);
|
||||||
|
build.complete(SchemaFiles.FAILED_SCHEMA_TEXT);
|
||||||
|
IrisLogging.warn("Schema build skipped (executor unavailable): " + schemaPath);
|
||||||
|
}
|
||||||
|
return build;
|
||||||
|
}
|
||||||
|
|
||||||
public File findFile(String name) {
|
public File findFile(String name) {
|
||||||
if (name == null || name.trim().isEmpty()) {
|
if (name == null || name.trim().isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -38,12 +38,12 @@ import org.dom4j.Element;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@SuppressWarnings("ALL")
|
@SuppressWarnings("ALL")
|
||||||
public class IrisCodeWorkspace {
|
public class IrisCodeWorkspace {
|
||||||
@@ -179,12 +179,12 @@ public class IrisCodeWorkspace {
|
|||||||
settings.put("json.maxItemsComputed", 30000);
|
settings.put("json.maxItemsComputed", 30000);
|
||||||
JSONArray schemas = new JSONArray();
|
JSONArray schemas = new JSONArray();
|
||||||
List<JSONObject> schemaEntries = new ArrayList<>();
|
List<JSONObject> schemaEntries = new ArrayList<>();
|
||||||
|
List<CompletableFuture<String>> schemaWrites = new ArrayList<>();
|
||||||
IrisData dm = IrisData.get(project.getPath());
|
IrisData dm = IrisData.get(project.getPath());
|
||||||
for (ResourceLoader<?> resourceLoader : dm.getLoaders().v()) {
|
for (ResourceLoader<?> resourceLoader : dm.getLoaders().v()) {
|
||||||
if (resourceLoader.supportsSchemas()) {
|
if (resourceLoader.supportsSchemas()) {
|
||||||
schemaEntries.add(immediateSchemas
|
schemaEntries.add(resourceLoader.schemaMapping());
|
||||||
? resourceLoader.buildSchemaImmediately()
|
schemaWrites.add(resourceLoader.writeSchemaFile(immediateSchemas));
|
||||||
: resourceLoader.buildSchema());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,18 +193,7 @@ public class IrisCodeWorkspace {
|
|||||||
String snipType = snippetClass.getDeclaredAnnotation(Snippet.class).value();
|
String snipType = snippetClass.getDeclaredAnnotation(Snippet.class).value();
|
||||||
schemaEntries.add(snippetSchemaEntry(snipType));
|
schemaEntries.add(snippetSchemaEntry(snipType));
|
||||||
File schemaFile = new File(dm.getDataFolder(), SNIPPET_SCHEMA_FOLDER + snipType + SCHEMA_SUFFIX);
|
File schemaFile = new File(dm.getDataFolder(), SNIPPET_SCHEMA_FOLDER + snipType + SCHEMA_SUFFIX);
|
||||||
if (immediateSchemas) {
|
schemaWrites.add(SchemaFiles.write(schemaFile, () -> new SchemaBuilder(snippetClass, dm).constructSnippet().toString(4), immediateSchemas));
|
||||||
IO.writeAll(schemaFile, new SchemaBuilder(snippetClass, dm).constructSnippet().toString(4));
|
|
||||||
} else {
|
|
||||||
IrisData snippetData = dm;
|
|
||||||
J.attemptAsync(() -> {
|
|
||||||
try {
|
|
||||||
IO.writeAll(schemaFile, new SchemaBuilder(snippetClass, snippetData).constructSnippet().toString(4));
|
|
||||||
} catch (Throwable e) {
|
|
||||||
IrisLogging.reportError(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
if (immediateSchemas) {
|
if (immediateSchemas) {
|
||||||
throw new IllegalStateException("Could not write snippet schema for " + snippetClass.getName(), e);
|
throw new IllegalStateException("Could not write snippet schema for " + snippetClass.getName(), e);
|
||||||
@@ -217,9 +206,9 @@ public class IrisCodeWorkspace {
|
|||||||
if (IrisValues.irisClassOf(type.name()).isPresent()) {
|
if (IrisValues.irisClassOf(type.name()).isPresent()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
schemaEntries.add(moduleSnippetSchema(dm, type, immediateSchemas));
|
schemaEntries.add(moduleSnippetSchema(dm, type, immediateSchemas, schemaWrites));
|
||||||
}
|
}
|
||||||
schemaEntries.addAll(ModuleResourceSchemas.entries(dm, immediateSchemas));
|
schemaEntries.addAll(ModuleResourceSchemas.entries(dm, immediateSchemas, schemaWrites));
|
||||||
|
|
||||||
schemaEntries.sort(Comparator.comparing(entry -> entry.getString("url")));
|
schemaEntries.sort(Comparator.comparing(entry -> entry.getString("url")));
|
||||||
for (JSONObject entry : schemaEntries) {
|
for (JSONObject entry : schemaEntries) {
|
||||||
@@ -236,7 +225,7 @@ public class IrisCodeWorkspace {
|
|||||||
IrisLogging.warn("Could not update IntelliJ schema mappings for " + project.getPath().getAbsolutePath() + "; VSCode workspace generation will continue.");
|
IrisLogging.warn("Could not update IntelliJ schema mappings for " + project.getPath().getAbsolutePath() + "; VSCode workspace generation will continue.");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
new ZedProjectSettings(project.getPath()).writeIrisSettings(schemas);
|
new ZedProjectSettings(project.getPath()).writeIrisSettings(schemas, SchemaFiles.awaitTexts(schemaWrites));
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
IrisLogging.reportError(e);
|
IrisLogging.reportError(e);
|
||||||
IrisLogging.warn("Could not update Zed schema mappings for " + project.getPath().getAbsolutePath() + "; VSCode workspace generation will continue.");
|
IrisLogging.warn("Could not update Zed schema mappings for " + project.getPath().getAbsolutePath() + "; VSCode workspace generation will continue.");
|
||||||
@@ -249,16 +238,11 @@ public class IrisCodeWorkspace {
|
|||||||
* A module's snippet type gets its own schema for the files under {@code snippet/<name>/}, as Iris's snippets do. IrisApi's
|
* A module's snippet type gets its own schema for the files under {@code snippet/<name>/}, as Iris's snippets do. IrisApi's
|
||||||
* value types share their snippet folders with the Iris classes that read the same JSON, whose schemas Iris writes.
|
* value types share their snippet folders with the Iris classes that read the same JSON, whose schemas Iris writes.
|
||||||
*/
|
*/
|
||||||
private static JSONObject moduleSnippetSchema(IrisData data, ObjectType type, boolean immediate) {
|
private static JSONObject moduleSnippetSchema(IrisData data, ObjectType type, boolean immediate, List<CompletableFuture<String>> schemaWrites) {
|
||||||
String name = type.snippet().orElseThrow();
|
String name = type.snippet().orElseThrow();
|
||||||
JSONObject entry = snippetSchemaEntry(name);
|
|
||||||
File schemaFile = new File(data.getDataFolder(), SNIPPET_SCHEMA_FOLDER + name + SCHEMA_SUFFIX);
|
File schemaFile = new File(data.getDataFolder(), SNIPPET_SCHEMA_FOLDER + name + SCHEMA_SUFFIX);
|
||||||
if (immediate) {
|
schemaWrites.add(SchemaFiles.write(schemaFile, () -> SchemaBuilder.moduleSnippet(type, data).toString(4), immediate));
|
||||||
writeSchema(schemaFile, SchemaBuilder.moduleSnippet(type, data));
|
return snippetSchemaEntry(name);
|
||||||
} else {
|
|
||||||
J.attemptAsync(() -> writeSchema(schemaFile, SchemaBuilder.moduleSnippet(type, data)));
|
|
||||||
}
|
|
||||||
return entry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSONObject snippetSchemaEntry(String snippetFolder) {
|
private static JSONObject snippetSchemaEntry(String snippetFolder) {
|
||||||
@@ -272,14 +256,6 @@ public class IrisCodeWorkspace {
|
|||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeSchema(File schemaFile, JSONObject schema) {
|
|
||||||
try {
|
|
||||||
IO.writeAll(schemaFile, schema.toString(4));
|
|
||||||
} catch (IOException failure) {
|
|
||||||
throw new UncheckedIOException("Could not write schema " + schemaFile.getAbsolutePath(), failure);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateIntelliJSchemaMappings(JSONArray schemas) {
|
private void updateIntelliJSchemaMappings(JSONArray schemas) {
|
||||||
File schemasFile = new File(project.getPath(), ".idea" + File.separator + "jsonSchemas.xml");
|
File schemasFile = new File(project.getPath(), ".idea" + File.separator + "jsonSchemas.xml");
|
||||||
Document doc = IO.read(schemasFile);
|
Document doc = IO.read(schemasFile);
|
||||||
|
|||||||
@@ -20,19 +20,16 @@ package art.arcane.iris.core.project;
|
|||||||
|
|
||||||
import art.arcane.iris.core.loader.IrisData;
|
import art.arcane.iris.core.loader.IrisData;
|
||||||
import art.arcane.iris.core.modules.PackModulesException;
|
import art.arcane.iris.core.modules.PackModulesException;
|
||||||
import art.arcane.iris.util.common.scheduling.J;
|
|
||||||
import art.arcane.volmlib.util.collection.KList;
|
import art.arcane.volmlib.util.collection.KList;
|
||||||
import art.arcane.volmlib.util.format.Form;
|
import art.arcane.volmlib.util.format.Form;
|
||||||
import art.arcane.volmlib.util.io.IO;
|
|
||||||
import art.arcane.volmlib.util.json.JSONArray;
|
import art.arcane.volmlib.util.json.JSONArray;
|
||||||
import art.arcane.volmlib.util.json.JSONObject;
|
import art.arcane.volmlib.util.json.JSONObject;
|
||||||
import com.volmit.host.schema.ResourceKindSchema;
|
import com.volmit.host.schema.ResourceKindSchema;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.UncheckedIOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The studio schemas of a pack's module resource kinds, written the way Iris's loaders write theirs: one schema file
|
* The studio schemas of a pack's module resource kinds, written the way Iris's loaders write theirs: one schema file
|
||||||
@@ -52,6 +49,13 @@ public final class ModuleResourceSchemas {
|
|||||||
* {@code immediate}, or in the background. A pack whose modules fail to load gets none; its load reported why.
|
* {@code immediate}, or in the background. A pack whose modules fail to load gets none; its load reported why.
|
||||||
*/
|
*/
|
||||||
public static List<JSONObject> entries(IrisData data, boolean immediate) {
|
public static List<JSONObject> entries(IrisData data, boolean immediate) {
|
||||||
|
return entries(data, immediate, new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* As {@link #entries(IrisData, boolean)}, adding the write of each schema file to {@code schemaWrites}.
|
||||||
|
*/
|
||||||
|
public static List<JSONObject> entries(IrisData data, boolean immediate, List<CompletableFuture<String>> schemaWrites) {
|
||||||
List<ResourceKindSchema> kinds;
|
List<ResourceKindSchema> kinds;
|
||||||
try {
|
try {
|
||||||
kinds = data.getModules().pack().schema().resources();
|
kinds = data.getModules().pack().schema().resources();
|
||||||
@@ -60,18 +64,14 @@ public final class ModuleResourceSchemas {
|
|||||||
}
|
}
|
||||||
List<JSONObject> entries = new ArrayList<>();
|
List<JSONObject> entries = new ArrayList<>();
|
||||||
for (ResourceKindSchema kind : kinds) {
|
for (ResourceKindSchema kind : kinds) {
|
||||||
entries.add(entry(data, kind, immediate));
|
entries.add(entry(data, kind, immediate, schemaWrites));
|
||||||
}
|
}
|
||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSONObject entry(IrisData data, ResourceKindSchema kind, boolean immediate) {
|
private static JSONObject entry(IrisData data, ResourceKindSchema kind, boolean immediate, List<CompletableFuture<String>> schemaWrites) {
|
||||||
File schemaFile = new File(data.getDataFolder(), SCHEMA_FOLDER + kind.folder() + SCHEMA_SUFFIX);
|
File schemaFile = new File(data.getDataFolder(), SCHEMA_FOLDER + kind.folder() + SCHEMA_SUFFIX);
|
||||||
if (immediate) {
|
schemaWrites.add(SchemaFiles.write(schemaFile, () -> SchemaBuilder.moduleResource(kind, data).toString(4), immediate));
|
||||||
write(schemaFile, SchemaBuilder.moduleResource(kind, data));
|
|
||||||
} else {
|
|
||||||
J.attemptAsync(() -> write(schemaFile, SchemaBuilder.moduleResource(kind, data)));
|
|
||||||
}
|
|
||||||
KList<String> fileMatches = new KList<>();
|
KList<String> fileMatches = new KList<>();
|
||||||
for (int depth = 1; depth < FOLDER_MATCH_DEPTH; depth++) {
|
for (int depth = 1; depth < FOLDER_MATCH_DEPTH; depth++) {
|
||||||
fileMatches.add("/" + kind.folder() + Form.repeat("/*", depth) + ".json");
|
fileMatches.add("/" + kind.folder() + Form.repeat("/*", depth) + ".json");
|
||||||
@@ -81,12 +81,4 @@ public final class ModuleResourceSchemas {
|
|||||||
entry.put("url", "./" + SCHEMA_FOLDER + kind.folder() + SCHEMA_SUFFIX);
|
entry.put("url", "./" + SCHEMA_FOLDER + kind.folder() + SCHEMA_SUFFIX);
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void write(File schemaFile, JSONObject schema) {
|
|
||||||
try {
|
|
||||||
IO.writeAll(schemaFile, schema.toString(4));
|
|
||||||
} catch (IOException failure) {
|
|
||||||
throw new UncheckedIOException("Could not write schema " + schemaFile.getAbsolutePath(), failure);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* Iris is a World Generator for Minecraft Servers
|
||||||
|
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package art.arcane.iris.core.project;
|
||||||
|
|
||||||
|
import art.arcane.iris.spi.IrisLogging;
|
||||||
|
import art.arcane.iris.util.common.parallel.MultiBurst;
|
||||||
|
import art.arcane.volmlib.util.io.IO;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UncheckedIOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes schema files and hands back the text written, so the editor settings can carry a hash of what is on disk.
|
||||||
|
*/
|
||||||
|
public final class SchemaFiles {
|
||||||
|
public static final String FAILED_SCHEMA_TEXT = "";
|
||||||
|
|
||||||
|
private SchemaFiles() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds and writes {@code schemaFile} now, or in the background. A background write that fails is reported and
|
||||||
|
* completes with an empty text; a write now throws.
|
||||||
|
*/
|
||||||
|
public static CompletableFuture<String> write(File schemaFile, Supplier<String> schemaText, boolean immediate) {
|
||||||
|
if (immediate) {
|
||||||
|
return CompletableFuture.completedFuture(writeNow(schemaFile, schemaText));
|
||||||
|
}
|
||||||
|
return writeAsync(schemaFile, schemaText);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<String> writeAsync(File schemaFile, Supplier<String> schemaText) {
|
||||||
|
return MultiBurst.burst.completeValueAsync(() -> writeReporting(schemaFile, schemaText));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String writeNow(File schemaFile, Supplier<String> schemaText) {
|
||||||
|
String text = schemaText.get();
|
||||||
|
try {
|
||||||
|
IO.writeAll(schemaFile, text);
|
||||||
|
} catch (IOException failure) {
|
||||||
|
throw new UncheckedIOException("Could not write schema " + schemaFile.getAbsolutePath(), failure);
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String writeReporting(File schemaFile, Supplier<String> schemaText) {
|
||||||
|
try {
|
||||||
|
return writeNow(schemaFile, schemaText);
|
||||||
|
} catch (Throwable failure) {
|
||||||
|
IrisLogging.reportError(failure);
|
||||||
|
return FAILED_SCHEMA_TEXT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> awaitTexts(List<CompletableFuture<String>> schemaWrites) {
|
||||||
|
return schemaWrites.stream().map(SchemaFiles::awaitText).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String awaitText(CompletableFuture<String> schemaWrite) {
|
||||||
|
try {
|
||||||
|
return schemaWrite.join();
|
||||||
|
} catch (RuntimeException failure) {
|
||||||
|
IrisLogging.reportError(failure);
|
||||||
|
return FAILED_SCHEMA_TEXT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,16 +17,24 @@ import java.io.IOException;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.HexFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keeps the JSON schema mappings in a pack's Zed project settings in sync with the VSCode workspace.
|
* Keeps the JSON schema mappings in a pack's Zed project settings in sync with the VSCode workspace.
|
||||||
* The file belongs to the user, so Iris only replaces {@code lsp.json-language-server.settings.json.schemas}
|
* The file belongs to the user, so Iris only replaces {@code lsp.json-language-server.settings.json.schemas}
|
||||||
* and makes sure {@code file_types.JSONC} contains {@code json}.
|
* and {@code lsp.json-language-server.settings.irisSchemaHash}, and makes sure {@code file_types.JSONC} contains {@code json}.
|
||||||
*/
|
*/
|
||||||
public final class ZedProjectSettings {
|
public final class ZedProjectSettings {
|
||||||
|
private static final List<String> JSON_SERVER_SETTINGS_PATH = List.of("lsp", "json-language-server", "settings");
|
||||||
private static final List<String> SCHEMAS_PARENT_PATH = List.of("lsp", "json-language-server", "settings", "json");
|
private static final List<String> SCHEMAS_PARENT_PATH = List.of("lsp", "json-language-server", "settings", "json");
|
||||||
private static final String SCHEMAS_KEY = "schemas";
|
private static final String SCHEMAS_KEY = "schemas";
|
||||||
|
// Zed's JSON server caches schema files until its configuration changes, and Zed never reports schema file edits.
|
||||||
|
// Carrying a hash of the schema contents in the configuration makes regenerated schemas reach the editor.
|
||||||
|
private static final String SCHEMA_HASH_KEY = "irisSchemaHash";
|
||||||
|
private static final int SCHEMA_HASH_HEX_LENGTH = 16;
|
||||||
private static final List<String> FILE_TYPES_PATH = List.of("file_types");
|
private static final List<String> FILE_TYPES_PATH = List.of("file_types");
|
||||||
// Pack files may contain comments; Zed's JSONC language keeps schema support but stops flagging them.
|
// Pack files may contain comments; Zed's JSONC language keeps schema support but stops flagging them.
|
||||||
private static final String JSONC_LANGUAGE = "JSONC";
|
private static final String JSONC_LANGUAGE = "JSONC";
|
||||||
@@ -48,7 +56,7 @@ public final class ZedProjectSettings {
|
|||||||
return settingsFile;
|
return settingsFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeIrisSettings(JSONArray schemas) throws IOException {
|
public void writeIrisSettings(JSONArray schemas, List<String> schemaTexts) throws IOException {
|
||||||
String existing = Files.isRegularFile(settingsFile) ? Files.readString(settingsFile, StandardCharsets.UTF_8) : null;
|
String existing = Files.isRegularFile(settingsFile) ? Files.readString(settingsFile, StandardCharsets.UTF_8) : null;
|
||||||
JsonObject settings = parseOrNull(existing);
|
JsonObject settings = parseOrNull(existing);
|
||||||
if (settings == null) {
|
if (settings == null) {
|
||||||
@@ -56,7 +64,7 @@ public final class ZedProjectSettings {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mergeSchemas(settings, schemas) || !mergeJsoncFileType(settings)) {
|
if (!mergeSchemas(settings, schemas) || !mergeSchemaHash(settings, schemaTexts) || !mergeJsoncFileType(settings)) {
|
||||||
IrisLogging.warn("Could not merge Iris settings into " + settingsFile + ": " + String.join(".", SCHEMAS_PARENT_PATH)
|
IrisLogging.warn("Could not merge Iris settings into " + settingsFile + ": " + String.join(".", SCHEMAS_PARENT_PATH)
|
||||||
+ " must be an object and " + String.join(".", FILE_TYPES_PATH) + "." + JSONC_LANGUAGE + " a list; leaving it untouched.");
|
+ " must be an object and " + String.join(".", FILE_TYPES_PATH) + "." + JSONC_LANGUAGE + " a list; leaving it untouched.");
|
||||||
return;
|
return;
|
||||||
@@ -86,6 +94,32 @@ public final class ZedProjectSettings {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean mergeSchemaHash(JsonObject settings, List<String> schemaTexts) {
|
||||||
|
JsonObject jsonServerSettings = findOrCreateObject(settings, JSON_SERVER_SETTINGS_PATH);
|
||||||
|
if (jsonServerSettings == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
jsonServerSettings.addProperty(SCHEMA_HASH_KEY, hashSchemaTexts(schemaTexts));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String hashSchemaTexts(List<String> schemaTexts) {
|
||||||
|
MessageDigest digest = sha256();
|
||||||
|
for (String schemaText : schemaTexts) {
|
||||||
|
digest.update(schemaText.getBytes(StandardCharsets.UTF_8));
|
||||||
|
digest.update((byte) 0);
|
||||||
|
}
|
||||||
|
return HexFormat.of().formatHex(digest.digest()).substring(0, SCHEMA_HASH_HEX_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MessageDigest sha256() {
|
||||||
|
try {
|
||||||
|
return MessageDigest.getInstance("SHA-256");
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new IllegalStateException("Every Java runtime must provide SHA-256", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean mergeJsoncFileType(JsonObject settings) {
|
private static boolean mergeJsoncFileType(JsonObject settings) {
|
||||||
JsonObject fileTypes = findOrCreateObject(settings, FILE_TYPES_PATH);
|
JsonObject fileTypes = findOrCreateObject(settings, FILE_TYPES_PATH);
|
||||||
if (fileTypes == null) {
|
if (fileTypes == null) {
|
||||||
|
|||||||
@@ -143,16 +143,23 @@ public class IrisCodeWorkspaceTest {
|
|||||||
JSONArray schemas = assertWorkspaceSchemasMaterialized(pack, canonicalWorkspace);
|
JSONArray schemas = assertWorkspaceSchemasMaterialized(pack, canonicalWorkspace);
|
||||||
assertTrue("Studio open must give Zed the same schemas",
|
assertTrue("Studio open must give Zed the same schemas",
|
||||||
Files.readString(pack.toPath().resolve(".zed/settings.json"), StandardCharsets.UTF_8).contains(schemas.getJSONObject(0).getString("url")));
|
Files.readString(pack.toPath().resolve(".zed/settings.json"), StandardCharsets.UTF_8).contains(schemas.getJSONObject(0).getString("url")));
|
||||||
|
String firstSchemaHash = zedSchemaHash(pack);
|
||||||
String firstSchemaPath = schemas.getJSONObject(0).getString("url").substring(2);
|
String firstSchemaPath = schemas.getJSONObject(0).getString("url").substring(2);
|
||||||
Files.writeString(canonicalWorkspace.toPath(), "{\"settings\":{\"json.schemas\":[]}}", StandardCharsets.UTF_8);
|
Files.writeString(canonicalWorkspace.toPath(), "{\"settings\":{\"json.schemas\":[]}}", StandardCharsets.UTF_8);
|
||||||
Files.delete(new File(pack, firstSchemaPath).toPath());
|
Files.delete(new File(pack, firstSchemaPath).toPath());
|
||||||
|
|
||||||
workspace.prepareAndOpenEditor();
|
workspace.prepareAndOpenEditor();
|
||||||
assertWorkspaceSchemasMaterialized(pack, canonicalWorkspace);
|
assertWorkspaceSchemasMaterialized(pack, canonicalWorkspace);
|
||||||
|
assertEquals("Regenerating identical schemas must keep Zed's schema hash", firstSchemaHash, zedSchemaHash(pack));
|
||||||
assertEquals("Studio open must target only the canonical workspace", "unrelated",
|
assertEquals("Studio open must target only the canonical workspace", "unrelated",
|
||||||
Files.readString(unrelatedWorkspace.toPath(), StandardCharsets.UTF_8));
|
Files.readString(unrelatedWorkspace.toPath(), StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String zedSchemaHash(File pack) throws Exception {
|
||||||
|
JSONObject settings = new JSONObject(Files.readString(pack.toPath().resolve(".zed/settings.json"), StandardCharsets.UTF_8));
|
||||||
|
return settings.getJSONObject("lsp").getJSONObject("json-language-server").getJSONObject("settings").getString("irisSchemaHash");
|
||||||
|
}
|
||||||
|
|
||||||
private static JSONArray assertWorkspaceSchemasMaterialized(File pack, File canonicalWorkspace) throws Exception {
|
private static JSONArray assertWorkspaceSchemasMaterialized(File pack, File canonicalWorkspace) throws Exception {
|
||||||
JSONObject configuration = new JSONObject(Files.readString(canonicalWorkspace.toPath(), StandardCharsets.UTF_8));
|
JSONObject configuration = new JSONObject(Files.readString(canonicalWorkspace.toPath(), StandardCharsets.UTF_8));
|
||||||
JSONArray schemas = configuration.getJSONObject("settings").getJSONArray("json.schemas");
|
JSONArray schemas = configuration.getJSONObject("settings").getJSONArray("json.schemas");
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package art.arcane.iris.core.project;
|
||||||
|
|
||||||
|
import art.arcane.iris.core.IrisSettings;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
|
public class SchemaFilesTest {
|
||||||
|
private static final String SCHEMA_TEXT = "{\"type\": \"object\"}";
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
|
private SchemaTestPlatform platform;
|
||||||
|
private IrisSettings previousSettings;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void bindPlatform() throws Exception {
|
||||||
|
platform = SchemaTestPlatform.bind(temporaryFolder.newFolder("platform"));
|
||||||
|
previousSettings = IrisSettings.settings;
|
||||||
|
IrisSettings.settings = new IrisSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void restorePlatform() {
|
||||||
|
IrisSettings.settings = previousSettings;
|
||||||
|
platform.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void immediateWriteReturnsTheTextItWrote() throws Exception {
|
||||||
|
File schemaFile = new File(temporaryFolder.getRoot(), ".iris/schema/biome-schema.json");
|
||||||
|
|
||||||
|
String text = SchemaFiles.write(schemaFile, () -> SCHEMA_TEXT, true).join();
|
||||||
|
|
||||||
|
assertEquals(SCHEMA_TEXT, text);
|
||||||
|
assertEquals(SCHEMA_TEXT, Files.readString(schemaFile.toPath(), StandardCharsets.UTF_8).stripTrailing());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void immediateWriteThrowsWhenTheSchemaCannotBeBuilt() {
|
||||||
|
File schemaFile = new File(temporaryFolder.getRoot(), "broken-schema.json");
|
||||||
|
|
||||||
|
assertThrows(IllegalStateException.class, () -> SchemaFiles.write(schemaFile, () -> {
|
||||||
|
throw new IllegalStateException("broken");
|
||||||
|
}, true));
|
||||||
|
assertFalse(schemaFile.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void backgroundWriteReturnsTheTextItWrote() throws Exception {
|
||||||
|
File schemaFile = new File(temporaryFolder.getRoot(), "region-schema.json");
|
||||||
|
|
||||||
|
String text = SchemaFiles.write(schemaFile, () -> SCHEMA_TEXT, false).join();
|
||||||
|
|
||||||
|
assertEquals(SCHEMA_TEXT, text);
|
||||||
|
assertEquals(SCHEMA_TEXT, Files.readString(schemaFile.toPath(), StandardCharsets.UTF_8).stripTrailing());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void failedBackgroundWriteHashesAsEmptyText() {
|
||||||
|
File schemaFile = new File(temporaryFolder.getRoot(), "broken-schema.json");
|
||||||
|
|
||||||
|
String text = SchemaFiles.write(schemaFile, () -> {
|
||||||
|
throw new IllegalStateException("broken");
|
||||||
|
}, false).join();
|
||||||
|
|
||||||
|
assertEquals(SchemaFiles.FAILED_SCHEMA_TEXT, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void awaitTextsKeepsOrderAndMapsFailuresToEmptyText() {
|
||||||
|
CompletableFuture<String> failed = new CompletableFuture<>();
|
||||||
|
failed.completeExceptionally(new IllegalStateException("broken"));
|
||||||
|
|
||||||
|
List<String> texts = SchemaFiles.awaitTexts(List.of(CompletableFuture.completedFuture("a"), failed, CompletableFuture.completedFuture("c")));
|
||||||
|
|
||||||
|
assertEquals(List.of("a", SchemaFiles.FAILED_SCHEMA_TEXT, "c"), texts);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,11 +14,16 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.attribute.FileTime;
|
import java.nio.file.attribute.FileTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class ZedProjectSettingsTest {
|
public class ZedProjectSettingsTest {
|
||||||
|
private static final String BIOME_SCHEMA_TEXT = "{\"enum\": [\"land/plains\"]}";
|
||||||
|
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
@@ -26,7 +31,7 @@ public class ZedProjectSettingsTest {
|
|||||||
public void createsSettingsFileWithSchemasWhenMissing() throws Exception {
|
public void createsSettingsFileWithSchemasWhenMissing() throws Exception {
|
||||||
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||||
|
|
||||||
zed.writeIrisSettings(biomeSchemas());
|
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||||
|
|
||||||
assertTrue("Zed settings must be created", Files.isRegularFile(zed.settingsFile()));
|
assertTrue("Zed settings must be created", Files.isRegularFile(zed.settingsFile()));
|
||||||
JsonObject settings = readSettings(zed.settingsFile());
|
JsonObject settings = readSettings(zed.settingsFile());
|
||||||
@@ -52,7 +57,7 @@ public class ZedProjectSettingsTest {
|
|||||||
}
|
}
|
||||||
""", StandardCharsets.UTF_8);
|
""", StandardCharsets.UTF_8);
|
||||||
|
|
||||||
zed.writeIrisSettings(biomeSchemas());
|
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||||
|
|
||||||
JsonObject settings = readSettings(zed.settingsFile());
|
JsonObject settings = readSettings(zed.settingsFile());
|
||||||
assertEquals(JsonParser.parseString("[\"*.jsonc5\", \"json\"]"), jsoncGlobsIn(settings));
|
assertEquals(JsonParser.parseString("[\"*.jsonc5\", \"json\"]"), jsoncGlobsIn(settings));
|
||||||
@@ -78,7 +83,7 @@ public class ZedProjectSettingsTest {
|
|||||||
""";
|
""";
|
||||||
Files.writeString(zed.settingsFile(), jsonc, StandardCharsets.UTF_8);
|
Files.writeString(zed.settingsFile(), jsonc, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
zed.writeIrisSettings(biomeSchemas());
|
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||||
|
|
||||||
assertEquals(jsonc, Files.readString(zed.settingsFile(), StandardCharsets.UTF_8));
|
assertEquals(jsonc, Files.readString(zed.settingsFile(), StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
@@ -86,18 +91,58 @@ public class ZedProjectSettingsTest {
|
|||||||
@Test
|
@Test
|
||||||
public void unchangedSchemasDoNotRewriteTheFile() throws Exception {
|
public void unchangedSchemasDoNotRewriteTheFile() throws Exception {
|
||||||
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||||
zed.writeIrisSettings(biomeSchemas());
|
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||||
Path file = zed.settingsFile();
|
Path file = zed.settingsFile();
|
||||||
String first = Files.readString(file, StandardCharsets.UTF_8);
|
String first = Files.readString(file, StandardCharsets.UTF_8);
|
||||||
FileTime stamp = FileTime.fromMillis(Files.getLastModifiedTime(file).toMillis() - 60_000L);
|
FileTime stamp = FileTime.fromMillis(Files.getLastModifiedTime(file).toMillis() - 60_000L);
|
||||||
Files.setLastModifiedTime(file, stamp);
|
Files.setLastModifiedTime(file, stamp);
|
||||||
|
|
||||||
zed.writeIrisSettings(biomeSchemas());
|
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||||
|
|
||||||
assertEquals(first, Files.readString(file, StandardCharsets.UTF_8));
|
assertEquals(first, Files.readString(file, StandardCharsets.UTF_8));
|
||||||
assertEquals("Unchanged Zed settings must not be rewritten", stamp.toMillis(), Files.getLastModifiedTime(file).toMillis());
|
assertEquals("Unchanged Zed settings must not be rewritten", stamp.toMillis(), Files.getLastModifiedTime(file).toMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void changedSchemaContentChangesTheSchemaHash() throws Exception {
|
||||||
|
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||||
|
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||||
|
String firstHash = schemaHashIn(readSettings(zed.settingsFile()));
|
||||||
|
|
||||||
|
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT.replace("land/plains", "land/meadow")));
|
||||||
|
|
||||||
|
String secondHash = schemaHashIn(readSettings(zed.settingsFile()));
|
||||||
|
assertNotEquals("A regenerated schema must change Zed's JSON server configuration", firstHash, secondHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void schemaHashIsAStableSixteenDigitHexOfTheTexts() throws Exception {
|
||||||
|
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||||
|
|
||||||
|
zed.writeIrisSettings(biomeSchemas(), List.of("a", "b"));
|
||||||
|
String hash = schemaHashIn(readSettings(zed.settingsFile()));
|
||||||
|
zed.writeIrisSettings(biomeSchemas(), List.of("a", "b"));
|
||||||
|
|
||||||
|
assertTrue(hash.matches("[0-9a-f]{16}"));
|
||||||
|
assertEquals(hash, schemaHashIn(readSettings(zed.settingsFile())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void textBoundariesChangeTheSchemaHash() throws Exception {
|
||||||
|
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||||
|
zed.writeIrisSettings(biomeSchemas(), List.of("ab", "c"));
|
||||||
|
String firstHash = schemaHashIn(readSettings(zed.settingsFile()));
|
||||||
|
|
||||||
|
zed.writeIrisSettings(biomeSchemas(), List.of("a", "bc"));
|
||||||
|
|
||||||
|
assertNotEquals(firstHash, schemaHashIn(readSettings(zed.settingsFile())));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String schemaHashIn(JsonObject settings) {
|
||||||
|
return settings.getAsJsonObject("lsp").getAsJsonObject("json-language-server")
|
||||||
|
.getAsJsonObject("settings").get("irisSchemaHash").getAsString();
|
||||||
|
}
|
||||||
|
|
||||||
private static JSONArray biomeSchemas() {
|
private static JSONArray biomeSchemas() {
|
||||||
JSONObject entry = new JSONObject();
|
JSONObject entry = new JSONObject();
|
||||||
entry.put("fileMatch", new JSONArray(new Object[]{"/biomes/*.json", "/biomes/*/*.json"}));
|
entry.put("fileMatch", new JSONArray(new Object[]{"/biomes/*.json", "/biomes/*/*.json"}));
|
||||||
|
|||||||
Reference in new issue
Block a user