9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2025-12-19 15:09:18 +00:00

fix generated build.gradle.kts on external dives on windows

This commit is contained in:
Julian Krings
2025-10-04 13:41:48 +02:00
parent b46c413f6b
commit 0e237aa1ad
2 changed files with 86 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ import com.volmit.iris.Iris
import com.volmit.iris.core.IrisSettings import com.volmit.iris.core.IrisSettings
import com.volmit.iris.core.scripting.environment.SimpleEnvironment import com.volmit.iris.core.scripting.environment.SimpleEnvironment
import com.volmit.iris.core.scripting.kotlin.base.* 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.Script
import com.volmit.iris.core.scripting.kotlin.runner.ScriptRunner import com.volmit.iris.core.scripting.kotlin.runner.ScriptRunner
import com.volmit.iris.core.scripting.kotlin.runner.classpath import com.volmit.iris.core.scripting.kotlin.runner.classpath
@@ -89,7 +90,7 @@ open class IrisSimpleExecutionEnvironment(
} }
companion object { companion object {
private const val CLASSPATH = "val classpath = files(" private const val CLASSPATH = "val classpath = mapOf("
private fun File.updateClasspath(classpath: List<File>) { private fun File.updateClasspath(classpath: List<File>) {
val test = if (exists()) readLines() else BASE_GRADLE val test = if (exists()) readLines() else BASE_GRADLE
@@ -97,24 +98,71 @@ open class IrisSimpleExecutionEnvironment(
} }
private fun List<String>.updateClasspath(classpath: List<File>): String { private fun List<String>.updateClasspath(classpath: List<File>): String {
val classpath = classpath.joinToString(",", CLASSPATH, ")") { "\"${it.escapedPath}\"" } val components = linkedMapOf<String, FileComponents>()
val index = indexOfFirst { it.startsWith(CLASSPATH) } classpath.forEach {
if (index == -1) { val parts = it.canonicalPath.split(File.separatorChar)
return "$classpath\n${joinToString("\n")}" 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<String>()
val queue = ArrayDeque<Pair<String?, Collection<FileComponents>>>()
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() 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") return mod.joinToString("\n")
} }
private val File.escapedPath private val File.escapedPath
get() = absolutePath.replace("\\", "\\\\").replace("\"", "\\\"") 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 = """ private val BASE_GRADLE = """
val classpath = files() val classpath = mapOf()
val home = file(System.getProperty("user.home"))
plugins { plugins {
kotlin("jvm") version("2.2.0") kotlin("jvm") version("2.2.0")
@@ -123,7 +171,7 @@ open class IrisSimpleExecutionEnvironment(
repositories { repositories {
mavenCentral() mavenCentral()
flatDir { flatDir {
dirs(home) dirs(classpath.keys)
} }
} }
@@ -135,7 +183,7 @@ open class IrisSimpleExecutionEnvironment(
configurations.kotlinCompilerPluginClasspath { extendsFrom(script) } configurations.kotlinCompilerPluginClasspath { extendsFrom(script) }
dependencies { dependencies {
classpath.forEach { script("$ARTIFACT_ID") } classpath.values.flatMap { it }.forEach { script("$ARTIFACT_ID") }
}""".trimIndent().split("\n") }""".trimIndent().split("\n")
} }
} }

View File

@@ -29,6 +29,33 @@ internal fun ResultValue.value(): Any? =
else -> null else -> null
} }
internal class FileComponents(
val segment: String,
val root: Boolean = false,
) {
private val children0 = mutableMapOf<String, FileComponents>()
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() private val workDir = File(".").normalize()
internal fun createResolver(baseDir: File = workDir) = CompoundDependenciesResolver(baseDir) internal fun createResolver(baseDir: File = workDir) = CompoundDependenciesResolver(baseDir)