-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
164 additions
and
14 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
build-logic/katana-convention/src/main/kotlin/dev/alvr/katana/buildlogic/consts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package dev.alvr.katana.buildlogic | ||
|
||
internal const val AndroidDir = "src/androidMain" | ||
internal const val ResourcesDir = "src/commonMain/resources" | ||
|
||
internal const val ANDROID_APPLICATION_PLUGIN = "com.android.application" | ||
internal const val ANDROID_LIBRARY_PLUGIN = "com.android.library" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...nvention/src/main/kotlin/dev/alvr/katana/buildlogic/mp/tasks/GenerateResourcesFileTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package dev.alvr.katana.buildlogic.mp.tasks | ||
|
||
import com.squareup.kotlinpoet.AnnotationSpec | ||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import dev.alvr.katana.buildlogic.ResourcesDir | ||
import dev.alvr.katana.buildlogic.mp.identifier | ||
import java.io.File | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.CacheableTask | ||
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 | ||
import org.gradle.internal.FileUtils | ||
|
||
@CacheableTask | ||
internal abstract class GenerateResourcesFileTask : DefaultTask() { | ||
@get:Input | ||
internal abstract val packageName: Property<String> | ||
|
||
@get:InputFiles | ||
@get:PathSensitive(value = PathSensitivity.RELATIVE) | ||
internal abstract val inputFiles: ConfigurableFileCollection | ||
|
||
@get:OutputDirectory | ||
internal abstract val outputDir: DirectoryProperty | ||
|
||
private val String.resourceIdentifier | ||
get() = split('_').identifier | ||
|
||
@TaskAction | ||
private fun generateResourcesFile() { | ||
inputFiles.files.generateResourcesFile() | ||
} | ||
|
||
private fun Set<File>.generateResourcesFile() { | ||
if (isEmpty()) { | ||
outputDir.get().asFile.deleteRecursively() | ||
return | ||
} | ||
|
||
val stableAnnotation = AnnotationSpec | ||
.builder(ClassName(ComposeRuntimePackage, ComposeStableAnnotation)) | ||
.build() | ||
|
||
val properties = map { file -> | ||
val propertyName = FileUtils.removeExtension(file.name).resourceIdentifier | ||
val fileName = file.absolutePath.replace('\\', '/').substringAfter("$ResourcesDir/") | ||
|
||
PropertySpec.builder(propertyName, ClassName(KatanaResourcePackage, KatanaResourceClass)) | ||
.addAnnotation(stableAnnotation) | ||
.addModifiers(KModifier.INTERNAL) | ||
.initializer("%L(%S)", KatanaResourceClass, fileName) | ||
.build() | ||
} | ||
|
||
val resourcesObject = TypeSpec.objectBuilder(KatanaResourcesLocalClass) | ||
.addModifiers(KModifier.INTERNAL) | ||
.addProperties(properties) | ||
.build() | ||
|
||
FileSpec.builder(ClassName("${packageName.get()}$KatanaResourcesLocalPackage", KatanaResourcesLocalClass)) | ||
.addType(resourcesObject) | ||
.build() | ||
.writeTo(outputDir.get().asFile) | ||
} | ||
|
||
private companion object { | ||
const val ComposeRuntimePackage = "androidx.compose.runtime" | ||
const val ComposeStableAnnotation = "Stable" | ||
|
||
const val KatanaResourcePackage = "dev.alvr.katana.core.ui.resources" | ||
const val KatanaResourceClass = "KatanaResource" | ||
|
||
const val KatanaResourcesLocalPackage = ".resources" | ||
const val KatanaResourcesLocalClass = "KatanaResources" | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
core/ui/src/commonMain/kotlin/dev/alvr/katana/core/ui/resources/KatanaResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.alvr.katana.core.ui.resources | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.runtime.saveable.Saver | ||
import kotlin.jvm.JvmInline | ||
import kotlinx.coroutines.runBlocking | ||
import org.jetbrains.compose.resources.DrawableResource | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
import org.jetbrains.compose.resources.InternalResourceApi | ||
import org.jetbrains.compose.resources.painterResource | ||
import org.jetbrains.compose.resources.readResourceBytes | ||
|
||
@Immutable | ||
@JvmInline | ||
value class KatanaResource(private val key: String) { | ||
|
||
@OptIn(ExperimentalResourceApi::class) | ||
val asPainter @Composable get() = painterResource(DrawableResource(key)) | ||
|
||
@OptIn(ExperimentalResourceApi::class, InternalResourceApi::class) | ||
val asByteArray get() = runBlocking { readResourceBytes(key) } | ||
|
||
val id get() = key.substringAfterLast('/').substringBeforeLast('.') | ||
|
||
companion object { | ||
val saver = Saver<KatanaResource, String>( | ||
save = { res -> res.key }, | ||
restore = { key -> KatanaResource(key) } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.