mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-12-19 15:09:18 +00:00
also include parent class for schema generation
This commit is contained in:
@@ -38,6 +38,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InaccessibleObjectException;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -117,49 +118,13 @@ public class SchemaBuilder {
|
|||||||
JSONArray required = new JSONArray();
|
JSONArray required = new JSONArray();
|
||||||
JSONArray extended = new JSONArray();
|
JSONArray extended = new JSONArray();
|
||||||
|
|
||||||
if (c.isAssignableFrom(IrisRegistrant.class) || IrisRegistrant.class.isAssignableFrom(c)) {
|
var parent = c.getSuperclass();
|
||||||
for (Field k : IrisRegistrant.class.getDeclaredFields()) {
|
while (parent != null && IrisRegistrant.class.isAssignableFrom(parent)) {
|
||||||
k.setAccessible(true);
|
buildProperties(properties, required, extended, parent);
|
||||||
|
parent = parent.getSuperclass();
|
||||||
if (Modifier.isStatic(k.getModifiers()) || Modifier.isFinal(k.getModifiers()) || Modifier.isTransient(k.getModifiers())) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject property = buildProperty(k, c);
|
buildProperties(properties, required, extended, c);
|
||||||
|
|
||||||
if (Boolean.TRUE == property.remove("!required")) {
|
|
||||||
required.put(k.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Boolean.TRUE == property.remove("!top")) {
|
|
||||||
extended.put(property);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
properties.put(k.getName(), property);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Field k : c.getDeclaredFields()) {
|
|
||||||
k.setAccessible(true);
|
|
||||||
|
|
||||||
if (Modifier.isStatic(k.getModifiers()) || Modifier.isFinal(k.getModifiers()) || Modifier.isTransient(k.getModifiers())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONObject property = buildProperty(k, c);
|
|
||||||
|
|
||||||
if (Boolean.TRUE == property.remove("!required")) {
|
|
||||||
required.put(k.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Boolean.TRUE == property.remove("!top")) {
|
|
||||||
extended.put(property);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
properties.put(k.getName(), property);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (required.length() > 0) {
|
if (required.length() > 0) {
|
||||||
o.put("required", required);
|
o.put("required", required);
|
||||||
@@ -174,6 +139,33 @@ public class SchemaBuilder {
|
|||||||
return buildSnippet(o, c);
|
return buildSnippet(o, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void buildProperties(JSONObject properties, JSONArray required, JSONArray extended, Class<?> c) {
|
||||||
|
for (Field k : c.getDeclaredFields()) {
|
||||||
|
if (Modifier.isStatic(k.getModifiers()) || Modifier.isFinal(k.getModifiers()) || Modifier.isTransient(k.getModifiers())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
k.setAccessible(true);
|
||||||
|
} catch (InaccessibleObjectException e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject property = buildProperty(k, c);
|
||||||
|
|
||||||
|
if (Boolean.TRUE == property.remove("!top")) {
|
||||||
|
extended.put(property);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Boolean.TRUE == property.remove("!required")) {
|
||||||
|
required.put(k.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
properties.put(k.getName(), property);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private JSONObject buildProperty(Field k, Class<?> cl) {
|
private JSONObject buildProperty(Field k, Class<?> cl) {
|
||||||
JSONObject prop = new JSONObject();
|
JSONObject prop = new JSONObject();
|
||||||
String type = getType(k.getType());
|
String type = getType(k.getType());
|
||||||
@@ -616,7 +608,7 @@ public class SchemaBuilder {
|
|||||||
if (present) d.add(" ");
|
if (present) d.add(" ");
|
||||||
if (value instanceof List) {
|
if (value instanceof List) {
|
||||||
d.add(SYMBOL_LIMIT__N + " Default Value is an empty list");
|
d.add(SYMBOL_LIMIT__N + " Default Value is an empty list");
|
||||||
} else if (!cl.isPrimitive() && !(value instanceof Number) && !(value instanceof String) && !(cl.isEnum()) && !KeyedType.isKeyed(cl)) {
|
} else if (!k.getType().isPrimitive() && !(value instanceof Number) && !(value instanceof String) && !(value instanceof Enum<?>) && !KeyedType.isKeyed(k.getType())) {
|
||||||
d.add(SYMBOL_LIMIT__N + " Default Value is a default object (create this object to see default properties)");
|
d.add(SYMBOL_LIMIT__N + " Default Value is a default object (create this object to see default properties)");
|
||||||
} else {
|
} else {
|
||||||
d.add(SYMBOL_LIMIT__N + " Default Value is " + value);
|
d.add(SYMBOL_LIMIT__N + " Default Value is " + value);
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ class ScriptRunner(
|
|||||||
dependencyResolver(resolver)
|
dependencyResolver(resolver)
|
||||||
packDirectory(baseDir)
|
packDirectory(baseDir)
|
||||||
sharedClassloader(sharedClassLoader)
|
sharedClassloader(sharedClassLoader)
|
||||||
|
server(true)
|
||||||
|
|
||||||
if (SimpleScript::class.java.isAssignableFrom(type.java))
|
if (SimpleScript::class.java.isAssignableFrom(type.java))
|
||||||
return@createCompilationConfigurationFromTemplate
|
return@createCompilationConfigurationFromTemplate
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import kotlin.script.experimental.dependencies.addRepository
|
|||||||
import kotlin.script.experimental.dependencies.impl.SimpleExternalDependenciesResolverOptionsParser
|
import kotlin.script.experimental.dependencies.impl.SimpleExternalDependenciesResolverOptionsParser
|
||||||
import kotlin.script.experimental.jvm.JvmDependency
|
import kotlin.script.experimental.jvm.JvmDependency
|
||||||
import kotlin.script.experimental.jvm.JvmDependencyFromClassLoader
|
import kotlin.script.experimental.jvm.JvmDependencyFromClassLoader
|
||||||
|
import kotlin.script.experimental.jvm.updateClasspath
|
||||||
import kotlin.script.experimental.jvm.util.classpathFromClassloader
|
import kotlin.script.experimental.jvm.util.classpathFromClassloader
|
||||||
import kotlin.script.experimental.util.PropertiesCollection
|
import kotlin.script.experimental.util.PropertiesCollection
|
||||||
import kotlin.script.experimental.util.filterByAnnotationType
|
import kotlin.script.experimental.util.filterByAnnotationType
|
||||||
@@ -67,8 +68,9 @@ private fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefineme
|
|||||||
?: return context.compilationConfiguration.asSuccess()
|
?: return context.compilationConfiguration.asSuccess()
|
||||||
|
|
||||||
val reports = mutableListOf<ScriptDiagnostic>()
|
val reports = mutableListOf<ScriptDiagnostic>()
|
||||||
val loader = context.compilationConfiguration[ScriptCompilationConfiguration.sharedClassloader] ?: loader
|
val loader = context.compilationConfiguration[ScriptCompilationConfiguration.sharedClassloader]
|
||||||
val resolver = context.compilationConfiguration[ScriptCompilationConfiguration.dependencyResolver] ?: resolver
|
val resolver = context.compilationConfiguration[ScriptCompilationConfiguration.dependencyResolver] ?: resolver
|
||||||
|
val server = context.compilationConfiguration[ScriptCompilationConfiguration.server] ?: false
|
||||||
context.compilationConfiguration[ScriptCompilationConfiguration.packDirectory]
|
context.compilationConfiguration[ScriptCompilationConfiguration.packDirectory]
|
||||||
?.addPack(resolver)
|
?.addPack(resolver)
|
||||||
?: context.script.locationId
|
?: context.script.locationId
|
||||||
@@ -93,13 +95,18 @@ private fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefineme
|
|||||||
}
|
}
|
||||||
|
|
||||||
return runBlocking {
|
return runBlocking {
|
||||||
resolver.resolveDependencies(annotations)
|
resolver.resolveDependencies(annotations, server)
|
||||||
}.onSuccess { classpath ->
|
}.onSuccess { classpath ->
|
||||||
context.compilationConfiguration.with {
|
context.compilationConfiguration.with {
|
||||||
|
if (!server) {
|
||||||
|
updateClasspath(classpath.map { it.first })
|
||||||
|
return@with
|
||||||
|
}
|
||||||
|
|
||||||
val newClasspath = classpath.filterNewClasspath(this[ScriptCompilationConfiguration.dependencies])
|
val newClasspath = classpath.filterNewClasspath(this[ScriptCompilationConfiguration.dependencies])
|
||||||
?: return@with
|
?: return@with
|
||||||
val shared = classpath.mapNotNull { p -> p.first.takeIf { p.second } }
|
val shared = classpath.mapNotNull { p -> p.first.takeIf { p.second } }
|
||||||
if (shared.isNotEmpty()) loader.addFiles(shared)
|
if (shared.isNotEmpty()) loader!!.addFiles(shared)
|
||||||
|
|
||||||
val regular = newClasspath
|
val regular = newClasspath
|
||||||
.map { p -> p.first }
|
.map { p -> p.first }
|
||||||
@@ -120,7 +127,8 @@ private fun Collection<Pair<File, Boolean>>.filterNewClasspath(known: Collection
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun ExternalDependenciesResolver.resolveDependencies(
|
private suspend fun ExternalDependenciesResolver.resolveDependencies(
|
||||||
annotations: Iterable<ScriptSourceAnnotation<*>>
|
annotations: Iterable<ScriptSourceAnnotation<*>>,
|
||||||
|
server: Boolean
|
||||||
): ResultWithDiagnostics<List<Pair<File, Boolean>>> {
|
): ResultWithDiagnostics<List<Pair<File, Boolean>>> {
|
||||||
val reports = mutableListOf<ScriptDiagnostic>()
|
val reports = mutableListOf<ScriptDiagnostic>()
|
||||||
annotations.forEach { (annotation, locationWithId) ->
|
annotations.forEach { (annotation, locationWithId) ->
|
||||||
@@ -152,6 +160,10 @@ private suspend fun ExternalDependenciesResolver.resolveDependencies(
|
|||||||
*annotation.options,
|
*annotation.options,
|
||||||
locationWithId = locationWithId
|
locationWithId = locationWithId
|
||||||
).onSuccess { options ->
|
).onSuccess { options ->
|
||||||
|
if (!server && true == options.server) {
|
||||||
|
return@onSuccess listOf<Pair<File, Boolean>>().asSuccess()
|
||||||
|
}
|
||||||
|
|
||||||
annotation.artifactsCoordinates.asIterable().flatMapSuccess { artifactCoordinates ->
|
annotation.artifactsCoordinates.asIterable().flatMapSuccess { artifactCoordinates ->
|
||||||
resolve(artifactCoordinates, options, locationWithId)
|
resolve(artifactCoordinates, options, locationWithId)
|
||||||
}.map { files -> files.map { it to (options.shared ?: false) } }
|
}.map { files -> files.map { it to (options.shared ?: false) } }
|
||||||
@@ -160,6 +172,7 @@ private suspend fun ExternalDependenciesResolver.resolveDependencies(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val ExternalDependenciesResolver.Options.shared get() = flag("shared")
|
private val ExternalDependenciesResolver.Options.shared get() = flag("shared")
|
||||||
|
private val ExternalDependenciesResolver.Options.server get() = flag("server")
|
||||||
internal val ClassLoader.classpath get() = classpathFromClassloader(this) ?: emptyList()
|
internal val ClassLoader.classpath get() = classpathFromClassloader(this) ?: emptyList()
|
||||||
|
|
||||||
internal fun <R> ResultWithDiagnostics<R>.valueOrThrow(message: CharSequence): R = valueOr {
|
internal fun <R> ResultWithDiagnostics<R>.valueOrThrow(message: CharSequence): R = valueOr {
|
||||||
@@ -168,7 +181,8 @@ internal fun <R> ResultWithDiagnostics<R>.valueOrThrow(message: CharSequence): R
|
|||||||
|
|
||||||
internal val ScriptCompilationConfigurationKeys.dependencyResolver by PropertiesCollection.key(resolver, true)
|
internal val ScriptCompilationConfigurationKeys.dependencyResolver by PropertiesCollection.key(resolver, true)
|
||||||
internal val ScriptCompilationConfigurationKeys.packDirectory by PropertiesCollection.key<File>(null, true)
|
internal val ScriptCompilationConfigurationKeys.packDirectory by PropertiesCollection.key<File>(null, true)
|
||||||
internal val ScriptCompilationConfigurationKeys.sharedClassloader by PropertiesCollection.key(loader, true)
|
internal val ScriptCompilationConfigurationKeys.sharedClassloader by PropertiesCollection.key<SharedClassLoader>(null, true)
|
||||||
|
internal val ScriptCompilationConfigurationKeys.server by PropertiesCollection.key(false, isTransient = true)
|
||||||
|
|
||||||
private fun File.addPack(resolver: CompoundDependenciesResolver) = resolver.addPack(this)
|
private fun File.addPack(resolver: CompoundDependenciesResolver) = resolver.addPack(this)
|
||||||
private fun <R> ResultWithDiagnostics<R>.appendReports(reports : Collection<ScriptDiagnostic>) =
|
private fun <R> ResultWithDiagnostics<R>.appendReports(reports : Collection<ScriptDiagnostic>) =
|
||||||
@@ -190,7 +204,9 @@ internal fun ScriptCompilationConfiguration.Builder.configure() {
|
|||||||
refineConfiguration {
|
refineConfiguration {
|
||||||
beforeParsing { context -> try {
|
beforeParsing { context -> try {
|
||||||
context.compilationConfiguration.with {
|
context.compilationConfiguration.with {
|
||||||
ScriptCompilationConfiguration.dependencies.append((this[ScriptCompilationConfiguration.sharedClassloader] ?: loader).dependency)
|
if (context.compilationConfiguration[ScriptCompilationConfiguration.server] ?: false) {
|
||||||
|
ScriptCompilationConfiguration.dependencies.append(this[ScriptCompilationConfiguration.sharedClassloader]!!.dependency)
|
||||||
|
}
|
||||||
}.asSuccess()
|
}.asSuccess()
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
ResultWithDiagnostics.Failure(e.asDiagnostics())
|
ResultWithDiagnostics.Failure(e.asDiagnostics())
|
||||||
|
|||||||
Reference in New Issue
Block a user