JavaFX dependency builder

This commit is contained in:
Alexander Nozik 2020-03-16 15:22:16 +03:00
parent 9e8b21d327
commit bf3252dbd6
3 changed files with 80 additions and 2 deletions

View File

@ -7,7 +7,7 @@ plugins {
} }
group = "scientifik" group = "scientifik"
version = "0.4.0-dev" version = "0.4.0"
repositories { repositories {
gradlePluginPortal() gradlePluginPortal()

View File

@ -9,7 +9,8 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
enum class DependencyConfiguration { enum class DependencyConfiguration {
API, API,
IMPLEMENTATION IMPLEMENTATION,
COMPILE_ONLY
} }
enum class DependencySourceSet(val setName: String, val suffix: String) { enum class DependencySourceSet(val setName: String, val suffix: String) {
@ -32,6 +33,7 @@ internal fun Project.useDependency(
when (dependencyConfiguration) { when (dependencyConfiguration) {
DependencyConfiguration.API -> api(dep) DependencyConfiguration.API -> api(dep)
DependencyConfiguration.IMPLEMENTATION -> implementation(dep) DependencyConfiguration.IMPLEMENTATION -> implementation(dep)
DependencyConfiguration.COMPILE_ONLY-> compileOnly(dep)
} }
} }
} }
@ -47,6 +49,7 @@ internal fun Project.useDependency(
val configurationName = when (dependencyConfiguration) { val configurationName = when (dependencyConfiguration) {
DependencyConfiguration.API -> apiConfigurationName DependencyConfiguration.API -> apiConfigurationName
DependencyConfiguration.IMPLEMENTATION -> implementationConfigurationName DependencyConfiguration.IMPLEMENTATION -> implementationConfigurationName
DependencyConfiguration.COMPILE_ONLY-> compileOnlyConfigurationName
} }
add(configurationName, dep.second) add(configurationName, dep.second)
} }
@ -61,6 +64,7 @@ internal fun Project.useDependency(
val configurationName = when (dependencyConfiguration) { val configurationName = when (dependencyConfiguration) {
DependencyConfiguration.API -> apiConfigurationName DependencyConfiguration.API -> apiConfigurationName
DependencyConfiguration.IMPLEMENTATION -> implementationConfigurationName DependencyConfiguration.IMPLEMENTATION -> implementationConfigurationName
DependencyConfiguration.COMPILE_ONLY-> compileOnlyConfigurationName
} }
add(configurationName, dep.second) add(configurationName, dep.second)
} }

View File

@ -0,0 +1,74 @@
package scientifik
import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.Project
import org.gradle.kotlin.dsl.findByType
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler
enum class FXModule(val artifact: String, vararg val dependencies: FXModule) {
BASE("javafx-base"),
GRAPHICS("javafx-graphics", BASE),
CONTROLS("javafx-controls", GRAPHICS, BASE),
FXML("javafx-fxml", BASE),
MEDIA("javafx-media", GRAPHICS, BASE),
SWING("javafx-swing", GRAPHICS, BASE),
WEB("javafx-web", CONTROLS, GRAPHICS, BASE)
}
enum class FXPlatform(val id: String) {
WINDOWS("win"),
LINUX("linux"),
MAC("mac")
}
val defaultPlatform: FXPlatform = when {
Os.isFamily(Os.FAMILY_WINDOWS) -> FXPlatform.WINDOWS
Os.isFamily(Os.FAMILY_MAC) -> FXPlatform.MAC
Os.isFamily(Os.FAMILY_UNIX) -> FXPlatform.LINUX
else -> error("Platform not recognized")
}
fun KotlinDependencyHandler.addFXDependencies(
vararg modules: FXModule,
configuration: DependencyConfiguration,
version: String = "14",
platform: FXPlatform = defaultPlatform
) {
modules.flatMap { it.dependencies.toList() + it }.distinct().forEach {
val notation = "org.openjfx:${it.artifact}:$version:${platform.id}"
when (configuration) {
DependencyConfiguration.API -> api(notation)
DependencyConfiguration.IMPLEMENTATION -> implementation(notation)
DependencyConfiguration.COMPILE_ONLY -> compileOnly(notation)
}
}
}
fun Project.fx(
vararg modules: FXModule,
configuration: DependencyConfiguration = DependencyConfiguration.COMPILE_ONLY,
version: String = "14",
platform: FXPlatform = defaultPlatform
) {
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
extensions.findByType<KotlinMultiplatformExtension>()?.apply {
sourceSets.findByName("jvmMain")?.apply {
dependencies {
addFXDependencies(*modules, configuration = configuration, version = version, platform = platform)
}
}
}
}
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
extensions.findByType<KotlinJvmProjectExtension>()?.apply {
sourceSets.findByName("main")?.apply {
dependencies {
addFXDependencies(*modules, configuration = configuration, version = version, platform = platform)
}
}
}
}
}