Alexey Tsvetkov
4 years ago
committed by
Alexey Tsvetkov
8 changed files with 153 additions and 14 deletions
@ -0,0 +1,14 @@ |
|||||||
|
plugins { |
||||||
|
kotlin("jvm") |
||||||
|
id("maven-publish") |
||||||
|
} |
||||||
|
|
||||||
|
mavenPublicationConfig { |
||||||
|
displayName = "Compose Desktop Preview Runtime" |
||||||
|
description = "Runtime helpers for Compose Desktop Preview" |
||||||
|
artifactId = "compose-preview-runtime-desktop" |
||||||
|
} |
||||||
|
|
||||||
|
dependencies { |
||||||
|
compileOnly("org.jetbrains.kotlin:kotlin-stdlib") |
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.desktop.preview.runtime |
||||||
|
|
||||||
|
import kotlin.reflect.KProperty1 |
||||||
|
|
||||||
|
class ComposePreviewRunner { |
||||||
|
companion object { |
||||||
|
private const val PREVIEW_ANNOTATION_FQ_NAME = "androidx.compose.ui.tooling.desktop.preview.Preview" |
||||||
|
|
||||||
|
@JvmStatic |
||||||
|
fun main(args: Array<String>) { |
||||||
|
val classLoader = ComposePreviewRunner::class.java.classLoader |
||||||
|
|
||||||
|
val previewFqName = args[0] |
||||||
|
val previewClassFqName = previewFqName.substringBeforeLast(".") |
||||||
|
val previewMethodName = previewFqName.substringAfterLast(".") |
||||||
|
val previewClass = classLoader.loadClass(previewClassFqName) |
||||||
|
val previewMethod = previewClass.methods.find { it.name == previewMethodName } |
||||||
|
?: error("Could not find method '$previewMethodName' in class '${previewClass.canonicalName}'") |
||||||
|
|
||||||
|
val content = previewMethod.invoke(previewClass) |
||||||
|
val previewAnnotation = previewMethod.annotations.find { it.annotationClass.qualifiedName == PREVIEW_ANNOTATION_FQ_NAME } |
||||||
|
?: error("Could not find '$PREVIEW_ANNOTATION_FQ_NAME' annotation on '$previewClassFqName#$previewMethodName'") |
||||||
|
val environmentKClassProperty = previewAnnotation.annotationClass.members.find { it is KProperty1<*, *> && it.name == "environment" } |
||||||
|
as KProperty1<Any, Class<*>> |
||||||
|
val environmentClass = environmentKClassProperty.get(previewAnnotation) |
||||||
|
val previewEnvironment = environmentClass |
||||||
|
.getDeclaredConstructor() |
||||||
|
.newInstance() |
||||||
|
val showMethod = previewEnvironment.javaClass |
||||||
|
.methods.find { it.name == "show" } |
||||||
|
?: error("Could not find 'show' in class '${environmentClass.canonicalName}'") |
||||||
|
showMethod.invoke(previewEnvironment, content) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
package org.jetbrains.compose.desktop.preview.internal |
||||||
|
|
||||||
|
import org.gradle.api.Project |
||||||
|
import org.jetbrains.compose.composeVersion |
||||||
|
import org.jetbrains.compose.desktop.application.dsl.Application |
||||||
|
import org.jetbrains.compose.desktop.application.internal.javaHomeOrDefault |
||||||
|
import org.jetbrains.compose.desktop.application.internal.provider |
||||||
|
import org.jetbrains.compose.desktop.preview.tasks.AbstractRunComposePreviewTask |
||||||
|
|
||||||
|
internal const val PREVIEW_RUNTIME_CLASSPATH_CONFIGURATION = "composeDesktopPreviewRuntimeClasspath" |
||||||
|
private val COMPOSE_PREVIEW_RUNTIME_DEPENDENCY = "org.jetbrains.compose:compose-preview-runtime-desktop:$composeVersion" |
||||||
|
|
||||||
|
fun Project.initializePreview() { |
||||||
|
configurations.create(PREVIEW_RUNTIME_CLASSPATH_CONFIGURATION).defaultDependencies { deps -> |
||||||
|
deps.add(dependencies.create(COMPOSE_PREVIEW_RUNTIME_DEPENDENCY)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
internal fun AbstractRunComposePreviewTask.configureRunPreviewTask(app: Application) { |
||||||
|
app._configurationSource?.let { configSource -> |
||||||
|
dependsOn(configSource.jarTaskName) |
||||||
|
classpath = configSource.runtimeClasspath(project) |
||||||
|
javaHome.set(provider { app.javaHomeOrDefault() }) |
||||||
|
jvmArgs.set(provider { app.jvmArgs }) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
package org.jetbrains.compose.desktop.preview.tasks |
||||||
|
|
||||||
|
import org.gradle.api.file.FileCollection |
||||||
|
import org.gradle.api.provider.ListProperty |
||||||
|
import org.gradle.api.provider.Property |
||||||
|
import org.gradle.api.tasks.* |
||||||
|
import org.jetbrains.compose.desktop.application.internal.javaExecutable |
||||||
|
import org.jetbrains.compose.desktop.application.internal.notNullProperty |
||||||
|
import org.jetbrains.compose.desktop.preview.internal.PREVIEW_RUNTIME_CLASSPATH_CONFIGURATION |
||||||
|
import org.jetbrains.compose.desktop.tasks.AbstractComposeDesktopTask |
||||||
|
|
||||||
|
abstract class AbstractRunComposePreviewTask : AbstractComposeDesktopTask() { |
||||||
|
@get:InputFiles |
||||||
|
internal lateinit var classpath: FileCollection |
||||||
|
|
||||||
|
@get:InputFiles |
||||||
|
internal val previewRuntimeClasspath: FileCollection |
||||||
|
get() = project.configurations.getByName(PREVIEW_RUNTIME_CLASSPATH_CONFIGURATION) |
||||||
|
|
||||||
|
@get:Internal |
||||||
|
internal val javaHome: Property<String> = objects.notNullProperty<String>().apply { |
||||||
|
set(providers.systemProperty("java.home")) |
||||||
|
} |
||||||
|
|
||||||
|
@get:Input |
||||||
|
@get:Optional |
||||||
|
internal val jvmArgs: ListProperty<String> = objects.listProperty(String::class.java) |
||||||
|
|
||||||
|
@TaskAction |
||||||
|
fun run() { |
||||||
|
val target = project.findProperty("compose.desktop.preview.target") as String |
||||||
|
execOperations.javaexec { javaExec -> |
||||||
|
javaExec.executable = javaExecutable(javaHome.get()) |
||||||
|
javaExec.main = "org.jetbrains.compose.desktop.preview.runtime.ComposePreviewRunner" |
||||||
|
javaExec.classpath = previewRuntimeClasspath + classpath |
||||||
|
javaExec.args = listOf(target) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue