Skip to content

Commit

Permalink
feat: add JvmFiles and JvmFilesProvider classes for easier univer…
Browse files Browse the repository at this point in the history
…sal JVM targets manupulations

Signed-off-by: Artyom Shendrik <[email protected]>
  • Loading branch information
amal committed Dec 31, 2023
1 parent 6e3d137 commit c68f45d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
21 changes: 21 additions & 0 deletions fluxo-kmp-conf/src/main/kotlin/fluxo/conf/jvm/JvmFiles.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fluxo.conf.jvm

import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider

internal class JvmFiles(
val allRuntimeJars: FileCollection,
val mainJar: Provider<RegularFile>,
private val taskDependencies: Array<Any>,
) {
operator fun component1() = allRuntimeJars

operator fun component2() = mainJar

fun <T : Task> configureUsageBy(task: T, fn: T.(JvmFiles) -> Unit) {
task.dependsOn(taskDependencies)
task.fn(this)
}
}
57 changes: 57 additions & 0 deletions fluxo-kmp-conf/src/main/kotlin/fluxo/conf/jvm/JvmFilesProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package fluxo.conf.jvm

import MAIN_SOURCE_SET_NAME
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.SourceSet
import org.gradle.jvm.tasks.Jar
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget

internal sealed class JvmFilesProvider {
abstract fun jvmCompileFiles(project: Project): JvmFiles

abstract class GradleJvmFilesProvider : JvmFilesProvider() {
protected abstract val jarTaskName: String
protected abstract val compileFiles: FileCollection
protected abstract val runtimeFiles: FileCollection

override fun jvmCompileFiles(project: Project): JvmFiles {
val jarTask = project.tasks.named(jarTaskName, Jar::class.java)
val mainJar = jarTask.flatMap { it.archiveFile }
val runtimeJarFiles = project.objects.fileCollection().apply {
from(mainJar)
from(compileFiles.filter { it.path.endsWith(".jar") })
}
return JvmFiles(runtimeJarFiles, mainJar, arrayOf(jarTask))
}
}

class FromGradleSourceSet(private val sourceSet: SourceSet) :
GradleJvmFilesProvider() {
override val jarTaskName: String
get() = sourceSet.jarTaskName

override val compileFiles: FileCollection
get() = sourceSet.compileClasspath

override val runtimeFiles: FileCollection
get() = sourceSet.runtimeClasspath
}

class FromKotlinMppTarget(private val target: KotlinJvmTarget) :
GradleJvmFilesProvider() {

override val jarTaskName: String
get() = target.artifactsTaskName

override val compileFiles: FileCollection
get() = mainCompilation.compileDependencyFiles

override val runtimeFiles: FileCollection
get() = mainCompilation.runtimeDependencyFiles

private val mainCompilation: KotlinJvmCompilation
get() = target.compilations.getByName(MAIN_SOURCE_SET_NAME)
}
}

0 comments on commit c68f45d

Please sign in to comment.