Skip to content

Commit

Permalink
feature: Minimal Gradle project to run a Jetbrains Compose for Desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
bric3 committed Sep 15, 2022
0 parents commit 8090846
Show file tree
Hide file tree
Showing 18 changed files with 533 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
9 changes: 9 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
`kotlin-dsl`
}

dependencies {
implementation(libs.gradleplugin.kotlin.jvm)
implementation(libs.gradleplugin.jetbrains.compose)
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}
10 changes: 10 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dependencyResolutionManagement {
repositories {
gradlePluginPortal()
}
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}
28 changes: 28 additions & 0 deletions buildSrc/src/main/kotlin/ProjectExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import org.gradle.kotlin.dsl.the

/**
* Workaround for accessing the version catalog from the buildSrc project.
*
* Allows to use libs directly in the kotlin conventions scripts.
*
* Still needed to set in `buildSrc/settings.gradle.kts`
* ```kotlin
* dependencyResolutionManagement {
* versionCatalogs {
* create("libs") {
* from(files("../gradle/libs.versions.toml"))
* }
* }
* }
* ```
*
* And te following dependency in `buildSrc/build.gradle.kts`
* ```kotlin
* dependencies {
* implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
* }
* ```
*
* @see https://github.com/gradle/gradle/issues/15383
*/
val org.gradle.api.Project.libs get() = the<org.gradle.accessors.dm.LibrariesForLibs>()
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const val JVM_LANGUAGE_LEVEL = 18
39 changes: 39 additions & 0 deletions buildSrc/src/main/kotlin/java-common-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
plugins {
java
}

repositories {
mavenCentral()
}

dependencies {
constraints {
// Dependency versions as constraints
}
}

testing {
suites {
@Suppress("UNUSED_VARIABLE")
val test by getting(JvmTestSuite::class) {
useJUnitJupiter(libs.versions.jupiter.get())
}
}
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(JVM_LANGUAGE_LEVEL))
}
}

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
options.release.set(JVM_LANGUAGE_LEVEL)
options.compilerArgs = listOf("--enable-preview")
}

tasks.withType<JavaExec> {
jvmArgs = listOf("--enable-preview")
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
}
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/java-library-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugins {
id("java-common-conventions")
`java-library`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id("java-common-conventions")
kotlin("jvm")
id("org.jetbrains.compose")
}

repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
maven("https://androidx.dev/storage/compose-compiler/repository/")
}

dependencies {
implementation(compose.desktop.currentOs)
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>() {
kotlinOptions.jvmTarget = "$JVM_LANGUAGE_LEVEL"
kotlinOptions.freeCompilerArgs += listOf(
"-Xjvm-default=all",
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true", // compat with kotlin 1.7.20-RC
)
}

// Substitute the compiler to make Compose work with Kotlin 1.7.20-RC
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute(module("org.jetbrains.compose.compiler:compiler")).apply {
using(module(libs.compose.compiler.get().toString()))
}
}
}
13 changes: 13 additions & 0 deletions gctk-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id("jetpack-compose-desktop-application-conventions")
}

dependencies {
implementation(project(":gctk-lib"))
}

compose.desktop {
application {
mainClass = "io.github.bric3.gctk.app.MainKt"
}
}
27 changes: 27 additions & 0 deletions gctk-app/src/main/kotlin/io/github/bric3/gctk/app/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.bric3.gctk.app

import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import kotlin.system.exitProcess


fun main() = application {
Window(
title = "GCTK",
onCloseRequest = {
exitProcess(0)
}
) {
val counter = remember { mutableStateOf(0) }
MaterialTheme {
Button(onClick = { counter.value++ }) {
Text("I've been clicked ${counter.value} times")
}
}
}
}
3 changes: 3 additions & 0 deletions gctk-lib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id("java-library-conventions")
}
14 changes: 14 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[versions]
jupiter = "5.9.0"
kotlin = "1.7.20-RC"
jetbrains-compose = "1.2.0-alpha01-dev774"


[libraries]
# Compose Compiler Version for Kotlin 1.7.20-RC acquired from https://androidx.dev/storage/compose-compiler/repository/
compose-compiler = { module = "androidx.compose.compiler:compiler", version = "1.4.0-dev-k1.7.20-RC-a143c065804" }

gradleplugin-kotlin-jvm = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
gradleplugin-jetbrains-compose = { module = "org.jetbrains.compose:compose-gradle-plugin", version.ref = "jetbrains-compose" }

[bundles]
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 8090846

Please sign in to comment.