Skip to content

Commit

Permalink
chore: fix for buildConfig configuration cache and disable k2
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshg committed Oct 9, 2023
1 parent 2633025 commit 3673db3
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 50 deletions.
12 changes: 11 additions & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ application {
applicationDefaultJvmArgs += jvmArguments(forAppRun = true)
}

buildConfig { catalogVersions = project.versionCatalogMapOf() }
buildConfig {
projectName = rootProject.name
projectVersion = project.version.toString()
projectDesc = rootProject.description
gitCommit = semver.commits.get().first()
catalogVersions = project.versionCatalogMapOf()
}

dependencies {
commonMainApi(libs.arrow.suspendapp)
Expand All @@ -39,6 +45,10 @@ kotlin.sourceSets.jsMain {
artifacts { add(commonJsResources.name, tasks.jsProcessResources) }
}

// tasks.buildConfig {
// outputs.upToDateWhen { false }
// }

// configurations {
// // Collects dependencies, constraints to be used by Consumable and Resolvable configurations.
// val webResources by dependencyScope("webResources")
Expand Down
19 changes: 16 additions & 3 deletions common/src/commonMain/kotlin/dev/suresh/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ package dev.suresh

import BuildConfig
import kotlin.jvm.JvmName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotlinx.serialization.json.Json

expect val platform: TargetPlatform
expect val platform: Platform

interface TargetPlatform {
interface Platform {

val name: String

Expand Down Expand Up @@ -58,7 +60,7 @@ interface TargetPlatform {
mapOf(
"java" to sysProp("java.runtime.version", "n/a"),
"kotlin" to KotlinVersion.CURRENT.toString(),
"platform" to "Kotlin ${this@TargetPlatform.name}",
"platform" to "Kotlin ${this@Platform.name}",
),
"git" to
mapOf(
Expand Down Expand Up @@ -96,3 +98,14 @@ val utcDateTimeNow
/** Gets the current date and time in the system's default time zone. */
val localDateTimeNow
get() = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())

/**
* Runs the given suspend block on a virtual thread, so that we can call blocking I/O APIs from
* coroutines
*/
suspend inline fun <T> runOnVirtualThread(crossinline block: suspend CoroutineScope.() -> T): T =
withContext(platform.vtDispatcher) { block() }

/** A coroutine scope that uses [Platform.vtDispatcher] as its dispatcher. */
val virtualThreadScope
get() = CoroutineScope(platform.vtDispatcher)
4 changes: 2 additions & 2 deletions common/src/desktopMain/kotlin/dev/suresh/Platform.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.suresh

actual val platform: TargetPlatform = DesktopPlatform
actual val platform: Platform = DesktopPlatform

object DesktopPlatform : TargetPlatform {
object DesktopPlatform : Platform {
override val name: String = "Desktop JVM"
}
4 changes: 2 additions & 2 deletions common/src/jsMain/kotlin/dev/suresh/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package dev.suresh

import kotlinx.browser.window

actual val platform: TargetPlatform = JsPlatform
actual val platform: Platform = JsPlatform

object JsPlatform : TargetPlatform {
object JsPlatform : Platform {
override val name: String = "JS"

override val osInfo: Map<String, String?>
Expand Down
4 changes: 2 additions & 2 deletions common/src/jvmMain/kotlin/dev/suresh/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import java.util.concurrent.Executors
import kotlinx.coroutines.asCoroutineDispatcher
import org.slf4j.LoggerFactory

actual val platform: TargetPlatform = JvmPlatform
actual val platform: Platform = JvmPlatform

object JvmPlatform : TargetPlatform {
object JvmPlatform : Platform {

private val log = LoggerFactory.getLogger(this::class.java)

Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ org.gradle.kotlin.dsl.allWarningsAsErrors=true

## Kotlin
kotlin.code.style=official
kotlin.experimental.tryK2=true
kotlin.daemon.jvmargs=--show-version --enable-preview -Xmx6g
kotlin.jvm.target.validation.mode=warning
kotlin.build.report.output=build_scan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ kotlin {
// }
// }
//
// targets["metadata"].compilations["main"].defaultSourceSet {
// dependsOn(jvmCommon)
// }

// val target = targets.first { it.platformType == KotlinPlatformType.common }
// val compilation = target.compilations["main"]
// // OR val compilation = targets["metadata"].compilations["main"]
// compilation.defaultSourceSet.kotlin.srcDir(buildConfig)
// // val newSourceSet = sourceSets.create("gen")
// // compilation.defaultSourceSet.dependsOn(newSourceSet)

jvmMain {
// dependsOn(jvmCommon)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
package tasks

import com.javiersc.semver.project.gradle.plugin.Commit
import gg.jte.ContentType
import gg.jte.TemplateEngine
import gg.jte.output.StringOutput
import gg.jte.generated.precompiled.StaticTemplates
import javax.inject.Inject
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.*
import org.gradle.language.base.plugins.LifecycleBasePlugin

@CacheableTask
abstract class BuildConfig @Inject constructor(private val extn: BuildConfigExtension) :
abstract class BuildConfig @Inject constructor(@Nested val extn: BuildConfigExtension) :
DefaultTask() {

@get:Input val version = extn.projectVersion

@get:Internal internal val templateName = "BuildConfig.kte"

@get:[OutputDirectory Optional]
val generatedOutputDir: DirectoryProperty = extn.outputDir

init {
description = "Generate build config class"
group = LifecycleBasePlugin.BUILD_TASK_NAME
}

@TaskAction
fun execute() {
val dir = generatedOutputDir.asFile.get()
val dir = extn.outputDir.asFile.get()
dir.deleteRecursively()
dir.mkdirs()

Expand All @@ -40,7 +32,7 @@ abstract class BuildConfig @Inject constructor(private val extn: BuildConfigExte
val pkg = fqName.substringBeforeLast(".", "")

val file = dir.resolve("$className.kt")
logger.quiet("Generated build config file: ${file.path}")
logger.quiet("Generated build config file: ${file.name}")

// Get git commit info
val gitCommit = run {
Expand All @@ -59,38 +51,39 @@ abstract class BuildConfig @Inject constructor(private val extn: BuildConfigExte
mapOf(
"name" to extn.projectName.get(),
"description" to extn.projectDesc.get(),
"version" to version.get(),
"version" to extn.projectVersion.get(),
)

// the<VersionCatalogsExtension>().named("libs").
val params =
mapOf(
"className" to className,
"pkg" to pkg,
"projectProps" to rootProjectProps,
"gitCommit" to gitCommit,
"catalogVersions" to extn.catalogVersions.get(),
"dependencies" to extn.dependencies.get(),
)
// val content = StringOutput()
// val tmplEngine = TemplateEngine.createPrecompiled(ContentType.Plain).apply {
// setTrimControlStructures(true) }
// tmplEngine.render(templateName, params, content)

val content = StringOutput()
val tmplEngine =
TemplateEngine.createPrecompiled(ContentType.Plain).apply { setTrimControlStructures(true) }
val content =
StaticTemplates()
.BuildConfig(
className = className,
pkg = pkg,
projectProps = rootProjectProps,
gitCommit = gitCommit,
catalogVersions = extn.catalogVersions.get(),
dependencies = extn.dependencies.get())
.render()

tmplEngine.render(templateName, params, content)
file.writeText(content.toString())
// outputs.dirs(generatedOutputDir)
file.writeText(content)
// outputs.dirs(extn.outputDir)
}
}

open class BuildConfigExtension @Inject constructor(layout: ProjectLayout, objects: ObjectFactory) {
@get:Input val classFqName = objects.property<String>().convention("BuildConfig")
@get:Input val projectVersion = objects.property<String>()
@get:Input val projectName = objects.property<String>()
@get:Input val projectDesc = objects.property<String>()
@get:Input val gitCommit = objects.property<Commit>()
@get:Input val catalogVersions = objects.mapProperty<String, String>().convention(emptyMap())
@get:Input val dependencies = objects.listProperty<String>().convention(emptyList())
val projectVersion = objects.property<String>()
@Internal val gitCommit = objects.property<Commit>()
@get:[OutputDirectory Optional]
val outputDir =
objects.directoryProperty().convention(layout.buildDirectory.dir("generated/buildconfig"))
}
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ kotlin-ksp = "1.9.20-Beta2-1.0.13"
kotlin-jvmtarget = "21"
kotlin-dsl-jvmtarget = "17"
kotlin-api-version = "1.9"
kotlin-lang-version = "2.0"
kotlin-lang-version = "1.9"
gradle = "8.4"
node-version = "20.6.1"
java-vendor = "Oracle"
Expand Down Expand Up @@ -49,7 +49,7 @@ dokka = "1.9.0"
intellij-coverage = "1.0.733"
intellij-markdown = "0.5.2"
jgit = "6.5.0.202303070854-r"
jte = "3.1.1"
jte = "3.1.2"
jimfs = "1.3.0"
junit = "5.10.0"
koin = "3.4.1"
Expand Down Expand Up @@ -81,7 +81,7 @@ uri-kmp = "0.0.15"
cash-turbine = "1.0.0"
kmp-store5 = "5.0.0-beta01"
kmp-settings = "1.0.0"
parsus = "0.5.5"
parsus = "0.6.0"
java-keyring = "1.0.3"
java-keychain = "1.1.0"
webjars-xterm = "5.1.0"
Expand Down Expand Up @@ -114,15 +114,15 @@ npm-vega-lite = "5.13.0"
npm-jsjoda-tz = "2.18.0"

# Plugin versions
benmanes = "0.48.0"
benmanes = "0.49.0"
foojay-resolver = "0.7.0"
gradle-enterprise = "3.15.1"
nexus-publish = "2.0.0-rc-1"
shadow = "8.1.1"
spotless = "6.22.0"
semver-plugin = "0.5.0-rc.5"
taskinfo = "2.1.0"
dependency-analysis = "1.24.0"
dependency-analysis = "1.25.0"
best-practices-plugin = "0.10"
graalvm-nativeimage = "0.9.27"
github-depgraph = "0.1.0"
Expand Down

0 comments on commit 3673db3

Please sign in to comment.