Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow schema to be a multiplatform artifact #49

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import app.cash.treehouse.gradle.TreehouseSchemaGeneratorPlugin.Strategy.Compose
import app.cash.treehouse.gradle.TreehouseSchemaGeneratorPlugin.Strategy.Widget
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.attributes.Usage
import org.gradle.api.attributes.Usage.JAVA_RUNTIME
import org.gradle.api.attributes.Usage.USAGE_ATTRIBUTE
import org.gradle.api.tasks.JavaExec
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import java.io.File
Expand Down Expand Up @@ -38,7 +41,11 @@ abstract class TreehouseSchemaGeneratorPlugin(
TreehouseSchemaExtensionImpl::class.java,
)

val configuration = project.configurations.create("treehouseSchema")
val configuration = project.configurations.create("treehouseSchema") {
// Ensure we get JVM artifacts from any multiplatform dependencies for use with JavaExec.
val runtimeUsage = project.objects.named(Usage::class.java, JAVA_RUNTIME)
it.attributes.attribute(USAGE_ATTRIBUTE, runtimeUsage)
}
project.dependencies.add(
configuration.name,
"app.cash.treehouse:treehouse-schema-generator:$treehouseVersion",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package app.cash.treehouse.gradle
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget

class TreehouseSchemaPlugin : Plugin<Project> {
override fun apply(project: Project) {
Expand All @@ -12,18 +14,28 @@ class TreehouseSchemaPlugin : Plugin<Project> {
applied = true

val kotlin = project.extensions.getByType(KotlinJvmProjectExtension::class.java)
kotlin.target.apply {
compilations.getByName("main") { compilation ->
compilation.dependencies {
api("app.cash.treehouse:treehouse-schema-annotations:$treehouseVersion")
}
}
}
kotlin.target.applySchemaAnnotationDependency()
}
project.plugins.withId("org.jetbrains.kotlin.multiplatform") {
applied = true

val kotlin = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
kotlin.targets.all { it.applySchemaAnnotationDependency() }

// TODO check there is a JVM target in an afterEvaluate
}

project.afterEvaluate {
check(applied) {
"Treehouse schema plugin requires the Kotlin JVM plugin to be applied."
"Treehouse schema plugin requires the Kotlin JVM or multiplatform plugin to be applied."
}
}
}

private fun KotlinTarget.applySchemaAnnotationDependency() {
compilations.getByName("main") { compilation ->
compilation.dependencies {
api("app.cash.treehouse:treehouse-schema-annotations:$treehouseVersion")
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions treehouse-gradle/src/test/fixture/schema-jvm/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
buildscript {
dependencies {
classpath "app.cash.treehouse:treehouse-gradle:$treehouseVersion"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31'
}

repositories {
maven {
url "file://${rootDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
}
}

allprojects {
repositories {
maven {
url "file://${rootDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.mpp.stability.nowarn=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'app.cash.treehouse.schema'
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package example.counter

import app.cash.treehouse.schema.Children
import app.cash.treehouse.schema.Default
import app.cash.treehouse.schema.Property
import app.cash.treehouse.schema.Schema
import app.cash.treehouse.schema.Widget

@Schema([
CounterBox::class,
CounterText::class,
CounterButton::class,
])
interface Counter

@Widget(1)
data class CounterBox(
@Property(1) val orientation: Boolean, // TODO enum or whatever
@Children(1) val children: List<Any>,
)

@Widget(2)
data class CounterText(
@Property(1) val text: String?,
@Property(2) @Default("\"black\"") val color: String,
)

@Widget(3)
data class CounterButton(
@Property(1) val text: String?,
@Property(2) @Default("true") val enabled: Boolean,
@Property(3) val onClick: () -> Unit,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'app.cash.treehouse.schema.widget'

kotlin {
jvm()
}

treehouse {
schema = 'example.counter.Counter'
}

dependencies {
treehouseSchema project(':schema')
}
2 changes: 2 additions & 0 deletions treehouse-gradle/src/test/fixture/schema-jvm/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ':schema'
include ':schema:widget'
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
buildscript {
dependencies {
classpath "app.cash.treehouse:treehouse-gradle:$treehouseVersion"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31'
}

repositories {
maven {
url "file://${rootDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
}
}

allprojects {
repositories {
maven {
url "file://${rootDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
kotlin.mpp.stability.nowarn=true
kotlin.js.compiler=ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'app.cash.treehouse.schema'

kotlin {
js {
nodejs()
}
jvm()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package example.counter

import app.cash.treehouse.schema.Children
import app.cash.treehouse.schema.Default
import app.cash.treehouse.schema.Property
import app.cash.treehouse.schema.Schema
import app.cash.treehouse.schema.Widget

@Schema([
CounterBox::class,
CounterText::class,
CounterButton::class,
])
interface Counter

@Widget(1)
data class CounterBox(
@Property(1) val orientation: Boolean, // TODO enum or whatever
@Children(1) val children: List<Any>,
)

@Widget(2)
data class CounterText(
@Property(1) val text: String?,
@Property(2) @Default("\"black\"") val color: String,
)

@Widget(3)
data class CounterButton(
@Property(1) val text: String?,
@Property(2) @Default("true") val enabled: Boolean,
@Property(3) val onClick: () -> Unit,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'app.cash.treehouse.schema.widget'

kotlin {
js {
nodejs()
}
jvm()
}

treehouse {
schema = 'example.counter.Counter'
}

dependencies {
treehouseSchema project(':schema')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ':schema'
include ':schema:widget'
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class FixtureTest(
@Parameters(name = "{0}")
fun parameters() = listOf(
arrayOf("counter"),
arrayOf("schema-multiplatform"),
arrayOf("schema-jvm"),
)
}

Expand Down
3 changes: 3 additions & 0 deletions treehouse-schema-annotations/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ apply plugin: 'com.vanniktech.maven.publish'
apply plugin: 'org.jetbrains.dokka' // Must be applied here for publish plugin.

kotlin {
js() {
nodejs()
}
jvm()
ios()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import app.cash.treehouse.schema.generator.TreehouseGenerator.Type.Compose
import app.cash.treehouse.schema.generator.TreehouseGenerator.Type.Widget
import app.cash.treehouse.schema.parser.parseSchema
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.CliktError
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.convert
import com.github.ajalt.clikt.parameters.arguments.help
Expand Down Expand Up @@ -38,7 +39,11 @@ private class TreehouseGenerator : CliktCommand() {
.help("Fully-qualified class name for the @Schema-annotated interface")
.convert {
// Replace with https://youtrack.jetbrains.com/issue/KT-10440 once it ships.
Class.forName(it).kotlin
try {
Class.forName(it).kotlin
} catch (e: ClassNotFoundException) {
throw CliktError("Unable to load class: $it", e)
}
}

override fun run() {
Expand Down