From bf3252dbd6939a76bec1122f6a1bbbc1d1fb26cb Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 16 Mar 2020 15:22:16 +0300 Subject: [PATCH] JavaFX dependency builder --- build.gradle.kts | 2 +- src/main/kotlin/scientifik/extensions.kt | 6 +- src/main/kotlin/scientifik/fx.kt | 74 ++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/scientifik/fx.kt diff --git a/build.gradle.kts b/build.gradle.kts index 2807ed1..f15c20d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "scientifik" -version = "0.4.0-dev" +version = "0.4.0" repositories { gradlePluginPortal() diff --git a/src/main/kotlin/scientifik/extensions.kt b/src/main/kotlin/scientifik/extensions.kt index 9681060..f32e2dc 100644 --- a/src/main/kotlin/scientifik/extensions.kt +++ b/src/main/kotlin/scientifik/extensions.kt @@ -9,7 +9,8 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension enum class DependencyConfiguration { API, - IMPLEMENTATION + IMPLEMENTATION, + COMPILE_ONLY } enum class DependencySourceSet(val setName: String, val suffix: String) { @@ -32,6 +33,7 @@ internal fun Project.useDependency( when (dependencyConfiguration) { DependencyConfiguration.API -> api(dep) DependencyConfiguration.IMPLEMENTATION -> implementation(dep) + DependencyConfiguration.COMPILE_ONLY-> compileOnly(dep) } } } @@ -47,6 +49,7 @@ internal fun Project.useDependency( val configurationName = when (dependencyConfiguration) { DependencyConfiguration.API -> apiConfigurationName DependencyConfiguration.IMPLEMENTATION -> implementationConfigurationName + DependencyConfiguration.COMPILE_ONLY-> compileOnlyConfigurationName } add(configurationName, dep.second) } @@ -61,6 +64,7 @@ internal fun Project.useDependency( val configurationName = when (dependencyConfiguration) { DependencyConfiguration.API -> apiConfigurationName DependencyConfiguration.IMPLEMENTATION -> implementationConfigurationName + DependencyConfiguration.COMPILE_ONLY-> compileOnlyConfigurationName } add(configurationName, dep.second) } diff --git a/src/main/kotlin/scientifik/fx.kt b/src/main/kotlin/scientifik/fx.kt new file mode 100644 index 0000000..f284d23 --- /dev/null +++ b/src/main/kotlin/scientifik/fx.kt @@ -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()?.apply { + sourceSets.findByName("jvmMain")?.apply { + dependencies { + addFXDependencies(*modules, configuration = configuration, version = version, platform = platform) + } + } + } + } + + pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { + extensions.findByType()?.apply { + sourceSets.findByName("main")?.apply { + dependencies { + addFXDependencies(*modules, configuration = configuration, version = version, platform = platform) + } + } + } + } +} \ No newline at end of file