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

Fix copying resources of for our custom compilation (don't merge unti… #8

Merged
merged 4 commits into from
Jul 12, 2024
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ To build and run storytale example:
./gradlew :examples:desktopStoriesRun
# to run wasm stories
./gradlew :examples:wasmJsBrowserStoriesDevelopmentRun
# to run iOS stories
./gradlew :examples:iosSimulatorArm64StoriesRun
```
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ plugins {
alias(libs.plugins.jetbrainsCompose) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.storytale) apply false
// alias(libs.plugins.storytale) apply false
}
2 changes: 1 addition & 1 deletion examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsCompose)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.storytale)
// alias(libs.plugins.storytale)
}

kotlin {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/commonStories/kotlin/PrimaryButton.story.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ val `PrimaryButton default state` by story {

val `PrimaryButton disabled` by story {
PrimaryButton(onClick = {}, enabled = false)
}
}
3 changes: 0 additions & 3 deletions examples/src/iosMain/kotlin/MainViewController.kt

This file was deleted.

7 changes: 3 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ androidx-espresso-core = "3.5.1"
androidx-material = "1.12.0"
androidx-test-junit = "1.1.5"
compose = "1.6.11"
compose-plugin = "1.6.10"
junit = "4.13.2"
kotlin = "2.0.20-dev-7027"
kotlin = "2.1.0-dev-1571"
kotlin-poet = "1.3.0"

[libraries]
Expand All @@ -32,15 +31,15 @@ androidx-activity-compose = { module = "androidx.activity:activity-compose", ver
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
compose-gradle-plugin = { module = "org.jetbrains.compose:compose-gradle-plugin", version.ref = "compose-plugin" }
compose-gradle-plugin = { module = "org.jetbrains.compose:compose-gradle-plugin", version.ref = "compose" }
kotlin-compiler-embeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlin" }
kotlin-klib-util = { module = "org.jetbrains.kotlin:kotlin-util-klib", version.ref = "kotlin" }
kotlin-poet = { module = "com.squareup:kotlinpoet", version.ref = "kotlin-poet" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
storytale = { id = "org.jetbrains.compose.storytale", version.ref = "storytale" }
5 changes: 5 additions & 0 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
"@jridgewell/resolve-uri" "^3.1.0"
"@jridgewell/sourcemap-codec" "^1.4.14"

"@js-joda/core@3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@js-joda/core/-/core-3.2.0.tgz#3e61e21b7b2b8a6be746df1335cf91d70db2a273"
integrity sha512-PMqgJ0sw5B7FKb2d5bWYIoxjri+QlW/Pys7+Rw82jSH0QN3rB05jZ/VrrsUdh1w4+i2kw9JOejXGq/KhDOX7Kg==

"@jsonjoy.com/base64@^1.1.1":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@jsonjoy.com/base64/-/base64-1.1.2.tgz#cf8ea9dcb849b81c95f14fc0aaa151c6b54d2578"
Expand Down
62 changes: 41 additions & 21 deletions modules/compiler-plugin/src/kotlin/StorytaleLoweringExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.expressions.IrBlock
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.util.isTopLevel
Expand All @@ -24,40 +25,45 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name

private class MentionAllStoriesGettersInsideMainFunctionLowering(
private val context: IrPluginContext
) : DeclarationTransformer {
private class MentionAllStoriesGettersInsideMainFunctionLowering(private val context: IrPluginContext) : DeclarationTransformer {
private val allFirstStoriesGetter = mutableListOf<IrStatement>()
private val alreadyMentionedFiles = mutableSetOf<IrFile>()

private val STORYTALE_FQN = FqName("org.jetbrains.compose.storytale")
private val STORY_FACTORY_ID = ClassId(STORYTALE_FQN, Name.identifier("StoryDelegate"))

private val KOTLIN_NATIVE_FQN = FqName("kotlin.native")
private val HIDE_FROM_OBJC_ID = ClassId(KOTLIN_NATIVE_FQN, Name.identifier("HiddenFromObjC"))

private val storyFactory = context.referenceClass(STORY_FACTORY_ID)
private val anyType = context.irBuiltIns.anyType
private val unitType = context.irBuiltIns.unitType
private val hiddenFromObjCAnnotation = context.referenceConstructors(HIDE_FROM_OBJC_ID).single()

override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
val factoryType = storyFactory?.defaultType ?: return null

if (declaration is IrProperty && declaration.isStory(factoryType) &&
declaration.parent !in alreadyMentionedFiles
) {
val getter = declaration.getter!!

alreadyMentionedFiles.add(declaration.parent as IrFile)
allFirstStoriesGetter.add(
IrCallImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
getter.returnType,
getter.symbol,
0,
0,
if (declaration is IrProperty && declaration.isStory(factoryType)) {
declaration.addHiddenFromObjCAnnotation()

if (declaration.parent !in alreadyMentionedFiles) {
val getter = declaration.getter!!

alreadyMentionedFiles.add(declaration.parent as IrFile)
allFirstStoriesGetter.add(
IrCallImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
getter.returnType,
getter.symbol,
0,
0,
)
)
)
}
}

if (declaration is IrSimpleFunction && declaration.isGeneratedMain()) {
if (declaration is IrSimpleFunction && declaration.isGeneratedMainViewController()) {
(declaration.body as? IrBlockBody)?.statements?.add(
0,
IrObservableBlockImpl(unitType, allFirstStoriesGetter)
Expand All @@ -72,9 +78,23 @@ private class MentionAllStoriesGettersInsideMainFunctionLowering(
it.origin == IrDeclarationOrigin.PROPERTY_DELEGATE && it.type == factoryType
} ?: false

private fun IrSimpleFunction.isGeneratedMain() =
private fun IrSimpleFunction.isGeneratedMainViewController() =
isTopLevel && visibility.isPublicAPI &&
kotlinFqName.asString() == "org.jetbrains.compose.storytale.generated.main"
kotlinFqName.asString() == "org.jetbrains.compose.storytale.generated.MainViewController"


private fun IrDeclaration.addHiddenFromObjCAnnotation() {
val annotation = IrConstructorCallImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
anyType,
hiddenFromObjCAnnotation,
0,
0,
0
)
context.metadataDeclarationRegistrar.addMetadataVisibleAnnotationsToElement(this, annotation)
}

private class IrObservableBlockImpl(
override var type: IrType,
Expand Down
6 changes: 3 additions & 3 deletions modules/gallery/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ kotlin {
js()
wasmJs()
jvm()
// iosX64()
// iosArm64()
// iosSimulatorArm64()
iosX64()
iosArm64()
iosSimulatorArm64()

sourceSets {
commonMain.dependencies {
Expand Down
15 changes: 9 additions & 6 deletions modules/gradle-plugin/src/main/kotlin/JsMultiplatformTasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package org.jetbrains.compose.plugin.storytale
import org.gradle.api.Project
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.tasks.AbstractCopyTask
import org.gradle.configurationcache.extensions.capitalized
import org.gradle.kotlin.dsl.task
import org.gradle.kotlin.dsl.withType
import org.jetbrains.compose.web.tasks.UnpackSkikoWasmRuntimeTask
import org.jetbrains.kotlin.gradle.dsl.JsModuleKind
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.targets.js.ir.JsIrBinary
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrCompilation
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import org.jetbrains.kotlin.gradle.plugin.mpp.resources.resolve.AggregateResourcesTask

fun Project.processJsCompilation(extension: StorytaleExtension, target: KotlinJsIrTarget) {
project.logger.info("Configuring storytale for Kotlin/JS")
Expand All @@ -33,6 +34,7 @@ fun Project.createWasmAndJsStorytaleCompilation(
target.compilations.create(StorytaleGradlePlugin.STORYTALE_SOURCESET_SUFFIX) as KotlinJsIrCompilation

storytaleCompilation.associateWith(mainCompilation)
extension.resourcesPublicationExtension.setupResourceResolvingForTarget(target, storytaleCompilation)

(mainCompilation.target as KotlinJsTargetDsl).apply {
// force to create executable: required for IR, do nothing on Legacy
Expand All @@ -50,16 +52,20 @@ fun Project.createWasmAndJsStorytaleCompilation(

extension.project.afterEvaluate {
val unpackSkikoTask = extension.project.tasks.withType<UnpackSkikoWasmRuntimeTask>().single()
val aggregateResourcesTask = extension.project.tasks
.getByName("${storytaleCompilation.name.lowercase()}${target.name.capitalized()}AggregateResources") as AggregateResourcesTask

sourceSet.resources.srcDirs(
"$storytaleBuildDir/resources",
unpackSkikoTask.outputDir,
mainCompilation.defaultSourceSet.resources
aggregateResourcesTask.outputDirectory,
mainCompilation.defaultSourceSet.resources,
)

extension.project.tasks.named(processResourcesTaskName).configure {
dependsOn(unpackSkikoTask)
dependsOn(generateTaskName)
dependsOn(aggregateResourcesTask)

(this as? AbstractCopyTask)?.duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
Expand All @@ -81,10 +87,7 @@ fun Project.createWasmAndJsStorytaleCompilation(
}

private fun Kotlin2JsCompile.configureOptions() {
compilerOptions.apply {
sourceMap.set(true)
moduleKind.set(JsModuleKind.MODULE_ES)
}
compilerOptions.sourceMap.set(true)
}

private fun Project.createJsStorytaleGenerateSourceTask(extension: StorytaleExtension, target: KotlinJsIrTarget) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ open class JsSourceGeneratorTask : DefaultTask() {
addImport("org.jetbrains.skiko.wasm", "onWasmReady")
addImport("org.jetbrains.compose.storytale.gallery", "Gallery")

function("main") {
function("MainViewController") {
addAnnotation(optInExperimentalComposeUi)
addStatement("""
|onWasmReady {
Expand All @@ -53,6 +53,10 @@ open class JsSourceGeneratorTask : DefaultTask() {
|}""".trimMargin()
)
}

function("main") {
addStatement("MainViewController()")
}
}.build()

file.writeTo(outputSourcesDir)
Expand Down
17 changes: 11 additions & 6 deletions modules/gradle-plugin/src/main/kotlin/JvmSourceGeneratorTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ open class JvmSourceGeneratorTask : DefaultTask() {
addImport("androidx.compose.ui.window", "application")
addImport("org.jetbrains.compose.storytale.gallery", "Gallery")

function("main") {
function("MainViewController") {
addStatement("""
|application {
| Window(onCloseRequest = ::exitApplication, title = "example") {
| Gallery()
| }
|}""".trimMargin())
|application {
| Window(onCloseRequest = ::exitApplication, title = "example") {
| Gallery()
| }
|}""".trimMargin()
)
}

function("main") {
addStatement("MainViewController()")
}
}.build()

Expand Down
53 changes: 53 additions & 0 deletions modules/gradle-plugin/src/main/kotlin/NativeCopyResourcesTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.jetbrains.compose.plugin.storytale

import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileCollection
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction

internal abstract class NativeCopyResourcesTask : DefaultTask() {
@get:Input
abstract val xcodeTargetPlatform: Property<String>

@get:Input
abstract val xcodeTargetArchs: Property<String>

@get:PathSensitive(PathSensitivity.ABSOLUTE)
@get:InputFiles
abstract val resourceFiles: Property<FileCollection>

@get:OutputDirectory
abstract val outputDir: DirectoryProperty

@TaskAction
fun run() {
val outputDir = outputDir.get().asFile
outputDir.deleteRecursively()
outputDir.mkdirs()
logger.info("Clean ${outputDir.path}")

resourceFiles.get().forEach { dir ->
if (dir.exists() && dir.isDirectory) {
logger.info("Copy '${dir.path}' to '${outputDir.path}'")
dir.walkTopDown().filter { !it.isDirectory && !it.isHidden }.forEach { file ->
val targetFile = outputDir.resolve(file.relativeTo(dir))
if (targetFile.exists()) {
logger.info("Skip [already exists] '${file.path}'")
} else {
logger.info(" -> '${file.path}'")
file.copyTo(targetFile)
}
}
} else {
logger.info("File '${dir.path}' is not a dir or doesn't exist")
}
}
}
}
Loading