Skip to content

Commit

Permalink
gradle add subfolder supprot
Browse files Browse the repository at this point in the history
  • Loading branch information
itboy87 committed Jan 30, 2025
1 parent 62c596b commit 2b6c9ca
Showing 1 changed file with 71 additions and 14 deletions.
85 changes: 71 additions & 14 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import java.nio.file.Paths
import kotlin.io.path.absolute
import kotlin.io.path.pathString
import kotlin.io.path.relativeTo
import kotlin.jvm.optionals.getOrNull

plugins {
Expand Down Expand Up @@ -73,6 +77,7 @@ subprojects {
compilerOptions.suppressWarnings.set(true)
// @TODO: We should actually, convert warnings to errors and start removing warnings
compilerOptions.freeCompilerArgs.add("-nowarn")
compilerOptions.freeCompilerArgs.add("-Xklib-duplicated-unique-name-strategy=allow-all-with-warning")
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink::class) {
Expand All @@ -99,12 +104,19 @@ class MicroAmper(val project: Project) {
val kotlinBasePlatforms by lazy { kotlinPlatforms.groupBy { getKotlinBasePlatform(it) } }

fun getKotlinBasePlatform(platform: String): String =
platform.removeSuffix("X64").removeSuffix("X86").removeSuffix("Arm64").removeSuffix("Arm32").removeSuffix("Simulator").removeSuffix("Device")
platform.removeSuffix("X64").removeSuffix("X86").removeSuffix("Arm64").removeSuffix("Arm32")
.removeSuffix("Simulator").removeSuffix("Device")
.also {
check(it.all { !it.isDigit() })
}

data class Dep(val path: String, val exported: Boolean, val test: Boolean, val platform: String, val compileOnly: Boolean) {
data class Dep(
val path: String,
val exported: Boolean,
val test: Boolean,
val platform: String,
val compileOnly: Boolean
) {
val rplatform = platform.takeIf { it.isNotEmpty() } ?: "common"
val configuration =
"$rplatform${if (test) "Test" else "Main"}${if (exported) "Api" else if (compileOnly) "CompileOnly" else "Implementation"}"
Expand All @@ -122,7 +134,8 @@ class MicroAmper(val project: Project) {
//println("product=$tline")
when {
tline.startsWith("platforms:") -> {
val platforms = tline.substringAfter('[').substringBeforeLast(']').split(',').map { it.trim() }
val platforms =
tline.substringAfter('[').substringBeforeLast(']').split(',').map { it.trim() }
kotlinPlatforms.addAll(platforms)
}
}
Expand All @@ -144,10 +157,23 @@ class MicroAmper(val project: Project) {
val test = mode.startsWith("test")
val compileOnly = line.contains(Regex(":\\s*compile-only"))
val exported = line.contains(Regex(":\\s*exported"))
val path = tline.removePrefix("-").removeSuffix(": exported").removeSuffix(":exported").removeSuffix(": compile-only")
val path = tline.removePrefix("-").removeSuffix(": exported").removeSuffix(":exported")
.removeSuffix(": compile-only")
.removeSuffix(":compile-only").trim()
if (platform.isBlank() || kotlinBasePlatforms.contains(platform) || kotlinAliases.contains(platform) || platform == "apple") {
deps += Dep(path = path, exported = exported, test = test, platform = platform, compileOnly = compileOnly)
if (platform.isBlank() || kotlinBasePlatforms.contains(platform) || kotlinAliases.contains(
platform
) || platform == "apple"
) {
deps += Dep(
path = if (path.contains("/")) replaceLeadingDotsWithFolders(
path,
file.parentFile.absolutePath
) else path,
exported = exported,
test = test,
platform = platform,
compileOnly = compileOnly
)
} else {
println("platform not included: $platform in $project")
}
Expand All @@ -167,6 +193,23 @@ class MicroAmper(val project: Project) {
}
}

fun replaceLeadingDotsWithFolders(relativePath: String, basePath: String): String {
// Normalize and convert both paths to absolute
val base = Paths.get(basePath).normalize()
val resolvedRelative = base.resolve(relativePath).normalize()
// println("base: $basePath")
// println("resolvedRelative: $resolvedRelative")
// println("resolvedRelative2: ${resolvedRelative.relativeTo(Paths.get(relativePath))}")
val totalCounts = relativePath
.split('/', '\\')
.count { it == ".." }
return if (totalCounts > 0) {
resolvedRelative.pathString.substringAfter(rootDir.absolutePath)
} else {
relativePath
}
}

data class SourceSetPair(val main: KotlinSourceSet, val test: KotlinSourceSet) {
fun dependsOn(other: SourceSetPair) {
main.dependsOn(other.main)
Expand Down Expand Up @@ -212,6 +255,7 @@ class MicroAmper(val project: Project) {
fun applyTo() = with(project) {
project.kotlin.sourceSets {
ssDependsOn("native", "common")
ssDependsOn("native", "nonJvm")
ssDependsOn("posix", "native")
ssDependsOn("apple", "posix")
ssDependsOn("appleNonWatchos", "apple")
Expand All @@ -237,6 +281,13 @@ class MicroAmper(val project: Project) {
if (isNative) ssDependsOn(basePlatform, "native")
if (platform != basePlatform) ssDependsOn(platform, basePlatform)
}

all {
languageSettings {
languageVersion = "2.0"
apiVersion = "2.0"
}
}
}

for (platform in kotlinPlatforms) {
Expand All @@ -251,14 +302,14 @@ class MicroAmper(val project: Project) {
browser {
testTask {
useMocha {
timeout = "15s"
timeout = "9s"
}
}
}
nodejs {
testTask {
useMocha {
timeout = "15s"
timeout = "9s"
}
}
}
Expand All @@ -269,14 +320,14 @@ class MicroAmper(val project: Project) {
browser {
testTask {
useMocha {
timeout = "15s"
timeout = "9s"
}
}
}
nodejs {
testTask {
useMocha {
timeout = "15s"
timeout = "9s"
}
}
}
Expand Down Expand Up @@ -317,8 +368,8 @@ class MicroAmper(val project: Project) {
compilerOptions {
// apiVersion: Allow to use declarations only from the specified version of bundled libraries
// languageVersion: Provide source compatibility with specified language version
//this.apiVersion.set(KotlinVersion.KOTLIN_2_0)
//this.languageVersion.set(KotlinVersion.KOTLIN_2_0)
this.apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0)
this.languageVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0)
}
}
}
Expand All @@ -339,12 +390,18 @@ class MicroAmper(val project: Project) {
for (dep in deps) {
add(
dep.configuration, when {
dep.path.contains('/') -> project(":${File(dep.path).name}")
dep.path.contains('/') -> {
val realPath = dep.path.replace("/", ":")
project(":$realPath")
}

dep.path.startsWith("\$") -> {
when (dep.path) {
"\$kotlin-test" -> "org.jetbrains.kotlin:kotlin-test"
else -> {
val result = libFinder.findLibrary(dep.path.replace("\$libs.", "").replace(".", "-")).getOrNull()?.get()
val result =
libFinder.findLibrary(dep.path.replace("\$libs.", "").replace(".", "-"))
.getOrNull()?.get()
result?.toString() ?: TODO("Unknown ${dep.path}, $result")
}
}
Expand Down

0 comments on commit 2b6c9ca

Please sign in to comment.