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:
@@ -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<File>) {
|
||||
val test = if (exists()) readLines() else BASE_GRADLE
|
||||
@@ -97,24 +98,71 @@ open class IrisSimpleExecutionEnvironment(
|
||||
}
|
||||
|
||||
private fun List<String>.updateClasspath(classpath: List<File>): 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<String, FileComponents>()
|
||||
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<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()
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -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<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()
|
||||
internal fun createResolver(baseDir: File = workDir) = CompoundDependenciesResolver(baseDir)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user