From 0e237aa1ad084e81ed698a910503f5dc20cee51f Mon Sep 17 00:00:00 2001 From: Julian Krings Date: Sat, 4 Oct 2025 13:41:48 +0200 Subject: [PATCH] fix generated build.gradle.kts on external dives on windows --- .../IrisSimpleExecutionEnvironment.kt | 70 ++++++++++++++++--- .../core/scripting/kotlin/runner/Utils.kt | 27 +++++++ 2 files changed, 86 insertions(+), 11 deletions(-) diff --git a/core/src/main/kotlin/com/volmit/iris/core/scripting/kotlin/environment/IrisSimpleExecutionEnvironment.kt b/core/src/main/kotlin/com/volmit/iris/core/scripting/kotlin/environment/IrisSimpleExecutionEnvironment.kt index 9987a0e01..21d65c5bc 100644 --- a/core/src/main/kotlin/com/volmit/iris/core/scripting/kotlin/environment/IrisSimpleExecutionEnvironment.kt +++ b/core/src/main/kotlin/com/volmit/iris/core/scripting/kotlin/environment/IrisSimpleExecutionEnvironment.kt @@ -4,6 +4,7 @@ import com.volmit.iris.Iris import com.volmit.iris.core.IrisSettings import com.volmit.iris.core.scripting.environment.SimpleEnvironment import com.volmit.iris.core.scripting.kotlin.base.* +import com.volmit.iris.core.scripting.kotlin.runner.FileComponents import com.volmit.iris.core.scripting.kotlin.runner.Script import com.volmit.iris.core.scripting.kotlin.runner.ScriptRunner import com.volmit.iris.core.scripting.kotlin.runner.classpath @@ -89,7 +90,7 @@ open class IrisSimpleExecutionEnvironment( } companion object { - private const val CLASSPATH = "val classpath = files(" + private const val CLASSPATH = "val classpath = mapOf(" private fun File.updateClasspath(classpath: List) { val test = if (exists()) readLines() else BASE_GRADLE @@ -97,24 +98,71 @@ open class IrisSimpleExecutionEnvironment( } private fun List.updateClasspath(classpath: List): String { - val classpath = classpath.joinToString(",", CLASSPATH, ")") { "\"${it.escapedPath}\"" } - val index = indexOfFirst { it.startsWith(CLASSPATH) } - if (index == -1) { - return "$classpath\n${joinToString("\n")}" + val components = linkedMapOf() + classpath.forEach { + val parts = it.canonicalPath.split(File.separatorChar) + if (parts.size <= 1) { + Iris.error("Invalid classpath entry: $it") + return@forEach + } + + var parent = components.computeIfAbsent(parts[0]) { FileComponents(parts[0], true) } + for (part in parts.subList(1, parts.size)) { + parent = parent.append(part) + } } + val mapped = components.values.associate { + var current = it + val root = buildString { + while (current.children.size == 1) { + append(current.segment) + append(File.separatorChar) + current = current.children.first() + } + append(current.segment) + append(File.separatorChar) + } + + val result = mutableSetOf() + val queue = ArrayDeque>>() + queue.add(null to current.children) + while (queue.isNotEmpty()) { + val pair = queue.removeFirst() + val path = pair.first?.let { p -> p + File.separatorChar } ?: "" + pair.second.forEach { child -> + val path = path + child.segment + if (child.children.isEmpty()) result.add(path) + else queue.add(path to child.children) + } + } + + root to result + } + + + val classpath = mapped.entries.joinToString(",", CLASSPATH, ")") { + "\"${it.key}\" to setOf(${it.value.joinToString(", ") { f -> "\"$f\"" }})" + } + + val mod = toMutableList() - mod[index] = classpath + val index = indexOfFirst { it.startsWith(CLASSPATH) } + if (index == -1) { + mod.clear() + mod.addAll(BASE_GRADLE) + } + + mod[if (index == -1) 0 else index] = classpath return mod.joinToString("\n") } private val File.escapedPath get() = absolutePath.replace("\\", "\\\\").replace("\"", "\\\"") - private const val ARTIFACT_ID = $$"local:${it.relativeTo(home).path.substringBeforeLast(\".jar\")}:1.0.0" + private const val ARTIFACT_ID = $$"local:${it.substringBeforeLast(\".jar\")}:1.0.0" private val BASE_GRADLE = """ - val classpath = files() - val home = file(System.getProperty("user.home")) + val classpath = mapOf() plugins { kotlin("jvm") version("2.2.0") @@ -123,7 +171,7 @@ open class IrisSimpleExecutionEnvironment( repositories { mavenCentral() flatDir { - dirs(home) + dirs(classpath.keys) } } @@ -135,7 +183,7 @@ open class IrisSimpleExecutionEnvironment( configurations.kotlinCompilerPluginClasspath { extendsFrom(script) } dependencies { - classpath.forEach { script("$ARTIFACT_ID") } + classpath.values.flatMap { it }.forEach { script("$ARTIFACT_ID") } }""".trimIndent().split("\n") } } \ No newline at end of file diff --git a/core/src/main/kotlin/com/volmit/iris/core/scripting/kotlin/runner/Utils.kt b/core/src/main/kotlin/com/volmit/iris/core/scripting/kotlin/runner/Utils.kt index 88dfcf2e9..c4f1f6e12 100644 --- a/core/src/main/kotlin/com/volmit/iris/core/scripting/kotlin/runner/Utils.kt +++ b/core/src/main/kotlin/com/volmit/iris/core/scripting/kotlin/runner/Utils.kt @@ -29,6 +29,33 @@ internal fun ResultValue.value(): Any? = else -> null } +internal class FileComponents( + val segment: String, + val root: Boolean = false, +) { + private val children0 = mutableMapOf() + val children get() = children0.values + + fun append(segment: String): FileComponents = + children0.computeIfAbsent(segment) { FileComponents(segment) } + + override fun hashCode(): Int { + var result = segment.hashCode() + result = 31 * result + children0.hashCode() + return result + } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is FileComponents) return false + + if (segment != other.segment) return false + if (children0 != other.children0) return false + + return true + } +} + private val workDir = File(".").normalize() internal fun createResolver(baseDir: File = workDir) = CompoundDependenciesResolver(baseDir)