|
|
@ -2,79 +2,86 @@ package org.jetbrains.compose.resources |
|
|
|
|
|
|
|
|
|
|
|
import org.gradle.api.Project |
|
|
|
import org.gradle.api.Project |
|
|
|
import org.gradle.api.provider.Provider |
|
|
|
import org.gradle.api.provider.Provider |
|
|
|
import org.jetbrains.compose.ComposeExtension |
|
|
|
|
|
|
|
import org.jetbrains.compose.ComposePlugin |
|
|
|
import org.jetbrains.compose.ComposePlugin |
|
|
|
import org.jetbrains.compose.ExperimentalComposeLibrary |
|
|
|
|
|
|
|
import org.jetbrains.compose.desktop.application.internal.ComposeProperties |
|
|
|
import org.jetbrains.compose.desktop.application.internal.ComposeProperties |
|
|
|
import org.jetbrains.compose.internal.KOTLIN_MPP_PLUGIN_ID |
|
|
|
import org.jetbrains.compose.internal.KOTLIN_MPP_PLUGIN_ID |
|
|
|
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension |
|
|
|
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
|
|
|
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet |
|
|
|
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
|
|
|
|
internal const val COMPOSE_RESOURCES_DIR = "composeResources" |
|
|
|
internal const val COMPOSE_RESOURCES_DIR = "composeResources" |
|
|
|
private const val RES_GEN_DIR = "generated/compose/resourceGenerator" |
|
|
|
private const val RES_GEN_DIR = "generated/compose/resourceGenerator" |
|
|
|
|
|
|
|
|
|
|
|
internal fun Project.configureResourceGenerator() { |
|
|
|
internal fun Project.configureComposeResources() { |
|
|
|
pluginManager.withPlugin(KOTLIN_MPP_PLUGIN_ID) { |
|
|
|
pluginManager.withPlugin(KOTLIN_MPP_PLUGIN_ID) { |
|
|
|
val kotlinExtension = project.extensions.getByType(KotlinProjectExtension::class.java) |
|
|
|
val kotlinExtension = project.extensions.getByType(KotlinMultiplatformExtension::class.java) |
|
|
|
val commonSourceSet = kotlinExtension.sourceSets.findByName(KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME) ?: return@withPlugin |
|
|
|
kotlinExtension.sourceSets.all { sourceSet -> |
|
|
|
val commonResourcesDir = provider { commonSourceSet.resources.sourceDirectories.first() } |
|
|
|
val sourceSetName = sourceSet.name |
|
|
|
|
|
|
|
val composeResourcesPath = project.projectDir.resolve("src/$sourceSetName/$COMPOSE_RESOURCES_DIR") |
|
|
|
val packageName = provider { |
|
|
|
sourceSet.resources.srcDirs(composeResourcesPath) |
|
|
|
buildString { |
|
|
|
if (sourceSetName == KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME) { |
|
|
|
val group = project.group.toString().asUnderscoredIdentifier() |
|
|
|
configureResourceGenerator(composeResourcesPath, sourceSet) |
|
|
|
append(group) |
|
|
|
|
|
|
|
if (group.isNotEmpty()) append(".") |
|
|
|
|
|
|
|
append("generated.resources") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun buildDir(path: String) = layout.dir(layout.buildDirectory.map { File(it.asFile, path) }) |
|
|
|
private fun Project.configureResourceGenerator(commonComposeResourcesDir: File, commonSourceSet: KotlinSourceSet) { |
|
|
|
|
|
|
|
val commonComposeResources = provider { commonComposeResourcesDir } |
|
|
|
|
|
|
|
val packageName = provider { |
|
|
|
|
|
|
|
buildString { |
|
|
|
|
|
|
|
val group = project.group.toString().asUnderscoredIdentifier() |
|
|
|
|
|
|
|
append(group) |
|
|
|
|
|
|
|
if (group.isNotEmpty()) append(".") |
|
|
|
|
|
|
|
append(project.name.lowercase()) |
|
|
|
|
|
|
|
append(".generated.resources") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val resDir = layout.dir(commonResourcesDir.map { it.resolve(COMPOSE_RESOURCES_DIR) }) |
|
|
|
fun buildDir(path: String) = layout.dir(layout.buildDirectory.map { File(it.asFile, path) }) |
|
|
|
|
|
|
|
|
|
|
|
//lazy check a dependency on the Resources library |
|
|
|
val resDir = layout.dir(commonComposeResources) |
|
|
|
val shouldGenerateResourceAccessors: Provider<Boolean> = provider { |
|
|
|
|
|
|
|
if (ComposeProperties.alwaysGenerateResourceAccessors(providers).get()) { |
|
|
|
|
|
|
|
true |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
configurations |
|
|
|
|
|
|
|
.getByName(commonSourceSet.implementationConfigurationName) |
|
|
|
|
|
|
|
.allDependencies.any { dep -> |
|
|
|
|
|
|
|
val depStringNotation = dep.let { "${it.group}:${it.name}:${it.version}" } |
|
|
|
|
|
|
|
depStringNotation == ComposePlugin.CommonComponentsDependencies.resources |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val genTask = tasks.register( |
|
|
|
//lazy check a dependency on the Resources library |
|
|
|
"generateComposeResClass", |
|
|
|
val shouldGenerateResourceAccessors: Provider<Boolean> = provider { |
|
|
|
GenerateResClassTask::class.java |
|
|
|
if (ComposeProperties.alwaysGenerateResourceAccessors(providers).get()) { |
|
|
|
) { |
|
|
|
true |
|
|
|
it.packageName.set(packageName) |
|
|
|
} else { |
|
|
|
it.resDir.set(resDir) |
|
|
|
configurations |
|
|
|
it.codeDir.set(buildDir("$RES_GEN_DIR/kotlin")) |
|
|
|
.getByName(commonSourceSet.implementationConfigurationName) |
|
|
|
it.onlyIf { shouldGenerateResourceAccessors.get() } |
|
|
|
.allDependencies.any { dep -> |
|
|
|
|
|
|
|
val depStringNotation = dep.let { "${it.group}:${it.name}:${it.version}" } |
|
|
|
|
|
|
|
depStringNotation == ComposePlugin.CommonComponentsDependencies.resources |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//register generated source set |
|
|
|
val genTask = tasks.register( |
|
|
|
commonSourceSet.kotlin.srcDir(genTask.map { it.codeDir }) |
|
|
|
"generateComposeResClass", |
|
|
|
|
|
|
|
GenerateResClassTask::class.java |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
it.packageName.set(packageName) |
|
|
|
|
|
|
|
it.resDir.set(resDir) |
|
|
|
|
|
|
|
it.codeDir.set(buildDir("$RES_GEN_DIR/kotlin")) |
|
|
|
|
|
|
|
it.onlyIf { shouldGenerateResourceAccessors.get() } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//setup task execution during IDE import |
|
|
|
//register generated source set |
|
|
|
tasks.configureEach { |
|
|
|
commonSourceSet.kotlin.srcDir(genTask.map { it.codeDir }) |
|
|
|
if (it.name == "prepareKotlinIdeaImport") { |
|
|
|
|
|
|
|
it.dependsOn(genTask) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val androidExtension = project.extensions.findByName("android") |
|
|
|
//setup task execution during IDE import |
|
|
|
if (androidExtension != null) { |
|
|
|
tasks.configureEach { |
|
|
|
configureAndroidResources( |
|
|
|
if (it.name == "prepareKotlinIdeaImport") { |
|
|
|
commonResourcesDir, |
|
|
|
it.dependsOn(genTask) |
|
|
|
buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile }, |
|
|
|
|
|
|
|
shouldGenerateResourceAccessors |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val androidExtension = project.extensions.findByName("android") |
|
|
|
|
|
|
|
if (androidExtension != null) { |
|
|
|
|
|
|
|
configureAndroidResources( |
|
|
|
|
|
|
|
commonComposeResources, |
|
|
|
|
|
|
|
buildDir("$RES_GEN_DIR/androidFonts").map { it.asFile }, |
|
|
|
|
|
|
|
shouldGenerateResourceAccessors |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|