diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/MultiplatformResourcesPlugin.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/MultiplatformResourcesPlugin.kt index 9e898c4c..6fec0b47 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/MultiplatformResourcesPlugin.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/MultiplatformResourcesPlugin.kt @@ -22,6 +22,7 @@ import dev.icerock.gradle.generator.ResourceGeneratorFeature import dev.icerock.gradle.generator.StringsGenerator import dev.icerock.gradle.tasks.GenerateMultiplatformResourcesTask import dev.icerock.gradle.utils.dependsOnObservable +import dev.icerock.gradle.utils.kotlinSourceSetsObservable import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.file.SourceDirectorySet @@ -68,7 +69,10 @@ open class MultiplatformResourcesPlugin : Plugin { mrExtension: MultiplatformResourcesPluginExtension, kmpExtension: KotlinMultiplatformExtension, ) { + kmpExtension.sourceSets.configureEach { kotlinSourceSet: KotlinSourceSet -> + project.logger.warn("i kmpExtension.sourceSets: ${kotlinSourceSet.name}") + val resourcesSourceDirectory: SourceDirectorySet = createMokoResourcesSourceSet( project = project, kotlinSourceSet = kotlinSourceSet @@ -101,25 +105,25 @@ open class MultiplatformResourcesPlugin : Plugin { project.logger.warn("i target ${target.targetName}") target.compilations.configureEach { compilation -> - val sourceSet: KotlinSourceSet = compilation.defaultSourceSet - val genTask: TaskProvider = requireNotNull( - sourceSet.extras[mokoResourcesGenTaskKey()] - ) - genTask.configure { - project.logger.warn("i configure platformName ${target.platformType.name}") + compilation.kotlinSourceSetsObservable.forAll { sourceSet -> + project.logger.warn("i compilation kotlinSourceSets: $sourceSet") - it.platformType.set(target.platformType.name) + project.logger.warn(("i compilationSourceSet: $sourceSet")) - project.logger.warn("i configure it.platformType = ${it.platformType.get()}") + val genTask: TaskProvider = requireNotNull( + sourceSet.extras[mokoResourcesGenTaskKey()] + ) - if (target is KotlinNativeTarget) { - it.konanTarget.set(target.konanTarget.name) + genTask.configure { + project.logger.warn("i configure platformName ${target.platformType.name}") + + it.platformType.set(target.platformType.name) } - } - compilation.compileTaskProvider.configure { - it.dependsOn(genTask) + compilation.compileTaskProvider.configure { + it.dependsOn(genTask) + } } } } @@ -357,8 +361,14 @@ open class MultiplatformResourcesPlugin : Plugin { dependsSourceSet.extras[mokoResourcesGenTaskKey()] ) - genTask.configure { - it.dependsOn(dependsGenTask) + genTask.configure { resourceTask -> + resourceTask.dependsOn(dependsGenTask) //TODO: Убрать после реализации связи через метадату + + resourceTask.inputMetadataFile.set( + dependsGenTask.flatMap { + it.outputMetadataFile + } + ) } } } diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/AssetsGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/AssetsGenerator.kt index f10c17ed..6e7ee244 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/AssetsGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/AssetsGenerator.kt @@ -14,6 +14,7 @@ import dev.icerock.gradle.generator.apple.AppleAssetsGenerator import dev.icerock.gradle.generator.common.CommonAssetsGenerator import dev.icerock.gradle.generator.js.JsAssetsGenerator import dev.icerock.gradle.generator.jvm.JvmAssetsGenerator +import dev.icerock.gradle.metadata.GeneratedObject import org.gradle.api.file.FileTree import java.io.File @@ -86,6 +87,8 @@ abstract class AssetsGenerator( } override fun generate( + metadata: List, + typeSpecIsInterface: Boolean, assetsGenerationDir: File, resourcesGenerationDir: File, objectBuilder: TypeSpec.Builder diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/BaseGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/BaseGenerator.kt index a1389c4f..5a016ed9 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/BaseGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/BaseGenerator.kt @@ -7,15 +7,19 @@ package dev.icerock.gradle.generator import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.PropertySpec +import com.squareup.kotlinpoet.PropertySpec.Builder import com.squareup.kotlinpoet.TypeSpec +import dev.icerock.gradle.metadata.GeneratedObject import java.io.File abstract class BaseGenerator : MRGenerator.Generator { override fun generate( + metadata: List, + typeSpecIsInterface: Boolean, assetsGenerationDir: File, resourcesGenerationDir: File, - objectBuilder: TypeSpec.Builder + objectBuilder: TypeSpec.Builder, ): TypeSpec { // language - key - value val languageMap: Map> = loadLanguageMap() @@ -23,7 +27,12 @@ abstract class BaseGenerator : MRGenerator.Generator { beforeGenerateResources(objectBuilder, languageMap) - val stringsClass = createTypeSpec(languageKeyValues.keys.toList(), objectBuilder) + val stringsClass = createTypeSpec( + metadata = metadata, + typeSpecIsInterface = typeSpecIsInterface, + keys = languageKeyValues.keys.toList(), + objectBuilder = objectBuilder + ) languageMap.forEach { (language, strings) -> generateResources(resourcesGenerationDir, language, strings) @@ -33,19 +42,31 @@ abstract class BaseGenerator : MRGenerator.Generator { } @Suppress("SpreadOperator") - private fun createTypeSpec(keys: List, objectBuilder: TypeSpec.Builder): TypeSpec { + private fun createTypeSpec( + metadata: List = emptyList(), + typeSpecIsInterface: Boolean, + keys: List, + objectBuilder: TypeSpec.Builder, + ): TypeSpec { objectBuilder.addModifiers(*getClassModifiers()) extendObjectBodyAtStart(objectBuilder) keys.forEach { key -> val name = key.replace(".", "_") - val property = - PropertySpec.builder(name, resourceClassName) - property.addModifiers(*getPropertyModifiers()) - getPropertyInitializer( - key - )?.let { property.initializer(it) } + val property: Builder = PropertySpec.builder(name, resourceClassName) + + if (!typeSpecIsInterface) { + property + .addModifiers(*getPropertyModifiers()) + + addOverrideModifier(property, metadata) + + getPropertyInitializer( + key + )?.let { property.initializer(it) } + } + objectBuilder.addProperty(property.build()) } @@ -53,6 +74,17 @@ abstract class BaseGenerator : MRGenerator.Generator { return objectBuilder.build() } + private fun addActualOverrideModifier( + property: PropertySpec.Builder, + metadata: List, + ) { + + + property + .addModifiers(KModifier.OVERRIDE) + + } + protected abstract fun loadLanguageMap(): Map> protected abstract fun getPropertyInitializer(key: String): CodeBlock? @@ -61,15 +93,14 @@ abstract class BaseGenerator : MRGenerator.Generator { protected open fun beforeGenerateResources( objectBuilder: TypeSpec.Builder, - languageMap: Map> + languageMap: Map>, ) = Unit protected open fun generateResources( resourcesGenerationDir: File, language: LanguageType, - strings: Map - ) { - } + strings: Map, + ) = Unit protected companion object { const val BASE_LANGUAGE = "base" diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/ColorsGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/ColorsGenerator.kt index a3b4b7d1..ba026d75 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/ColorsGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/ColorsGenerator.kt @@ -14,6 +14,7 @@ import dev.icerock.gradle.generator.apple.AppleColorsGenerator import dev.icerock.gradle.generator.common.CommonColorsGenerator import dev.icerock.gradle.generator.js.JsColorsGenerator import dev.icerock.gradle.generator.jvm.JvmColorsGenerator +import dev.icerock.gradle.metadata.GeneratedObject import org.gradle.api.file.FileTree import org.w3c.dom.Node import org.w3c.dom.NodeList @@ -36,6 +37,8 @@ abstract class ColorsGenerator( @Suppress("SpreadOperator") override fun generate( + metadata: List, + typeSpecIsInterface: Boolean, assetsGenerationDir: File, resourcesGenerationDir: File, objectBuilder: TypeSpec.Builder, diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/FilesGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/FilesGenerator.kt index 08031f2a..68a3a8e4 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/FilesGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/FilesGenerator.kt @@ -14,6 +14,7 @@ import dev.icerock.gradle.generator.apple.AppleFilesGenerator import dev.icerock.gradle.generator.common.CommonFilesGenerator import dev.icerock.gradle.generator.js.JsFilesGenerator import dev.icerock.gradle.generator.jvm.JvmFilesGenerator +import dev.icerock.gradle.metadata.GeneratedObject import org.gradle.api.file.FileTree import java.io.File @@ -26,6 +27,8 @@ abstract class FilesGenerator( override val mrObjectName: String = "files" override fun generate( + metadata: List, + typeSpecIsInterface: Boolean, assetsGenerationDir: File, resourcesGenerationDir: File, objectBuilder: TypeSpec.Builder diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/FontsGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/FontsGenerator.kt index 62caa8f4..88422287 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/FontsGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/FontsGenerator.kt @@ -14,6 +14,7 @@ import dev.icerock.gradle.generator.apple.AppleFontsGenerator import dev.icerock.gradle.generator.common.CommonFontsGenerator import dev.icerock.gradle.generator.js.JsFontsGenerator import dev.icerock.gradle.generator.jvm.JvmFontsGenerator +import dev.icerock.gradle.metadata.GeneratedObject import dev.icerock.gradle.utils.decapitalize import org.gradle.api.file.FileTree import java.io.File @@ -27,6 +28,8 @@ abstract class FontsGenerator( override val mrObjectName: String = "fonts" override fun generate( + metadata: List, + typeSpecIsInterface: Boolean, assetsGenerationDir: File, resourcesGenerationDir: File, objectBuilder: TypeSpec.Builder diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/ImagesGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/ImagesGenerator.kt index 814aa73e..614f2200 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/ImagesGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/ImagesGenerator.kt @@ -14,6 +14,7 @@ import dev.icerock.gradle.generator.apple.AppleImagesGenerator import dev.icerock.gradle.generator.common.CommonImagesGenerator import dev.icerock.gradle.generator.js.JsImagesGenerator import dev.icerock.gradle.generator.jvm.JvmImagesGenerator +import dev.icerock.gradle.metadata.GeneratedObject import dev.icerock.gradle.utils.withoutScale import org.gradle.api.file.FileTree import org.gradle.api.logging.Logger @@ -30,6 +31,8 @@ abstract class ImagesGenerator( override val mrObjectName: String = "images" override fun generate( + metadata: List, + typeSpecIsInterface: Boolean, assetsGenerationDir: File, resourcesGenerationDir: File, objectBuilder: TypeSpec.Builder diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/LanguageType.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/LanguageType.kt index 9b70945f..30d75eab 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/LanguageType.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/LanguageType.kt @@ -45,7 +45,7 @@ sealed interface LanguageType { override val jvmResourcesSuffix: String = "_${toLocaleString()}" /** - * Throw an error here so that we safeguard ourselves from implcitly calling `Local.toString`. + * Throw an error here so that we safeguard ourselves from implicitly calling `Local.toString`. * You should always use the more explicit methods defined above. */ override fun toString(): String = TODO("Use toLocaleString or toBcpString instead!") diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/MRGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/MRGenerator.kt index 8433991f..d2e7cbf7 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/MRGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/MRGenerator.kt @@ -9,6 +9,7 @@ import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.TypeSpec import dev.icerock.gradle.MRVisibility +import dev.icerock.gradle.metadata.GeneratedObject import dev.icerock.gradle.tasks.GenerateMultiplatformResourcesTask import org.gradle.api.Project import org.gradle.api.file.Directory @@ -80,6 +81,8 @@ abstract class MRGenerator( val inputFiles: Iterable fun generate( + metadata: List = emptyList(), + typeSpecIsInterface: Boolean = false, assetsGenerationDir: File, resourcesGenerationDir: File, objectBuilder: TypeSpec.Builder, diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/StringsGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/StringsGenerator.kt index fcb84fe8..71dbcbbc 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/StringsGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/StringsGenerator.kt @@ -25,7 +25,7 @@ abstract class StringsGenerator( ) : BaseGenerator() { override val inputFiles: Iterable - get() = (lowerStringsFileTree + ownStringsFileTree).matching { + get() = ownStringsFileTree.matching { it.include(STRINGS_MASK) }.files diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/TargetMRGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/TargetMRGenerator.kt index fd4eec3b..54e8a6e0 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/TargetMRGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/TargetMRGenerator.kt @@ -28,14 +28,25 @@ abstract class TargetMRGenerator( override fun generateFileSpec(): FileSpec? { val visibilityModifier: KModifier = settings.visibility.toModifier() + val inputMetadata: MutableList = mutableListOf() //Read list of generated resources on previous level - val inputMetadata: List = readInputMetadata( - buildDir = project.buildDir, - sourceSetName = settings.lowerResourcesFileTree.files.first().targetName - ) -// TODO: Check need of this for empty resources targets -// if (hasNoResourcesForGenerator(inputMetadata)) return null + if (settings.lowerResourcesFileTree.files.isNotEmpty()) { + inputMetadata.addAll( + readInputMetadata( + inputMetadataFile = project.buildDir, + sourceSetName = settings.lowerResourcesFileTree.files.firstOrNull()?.targetName + ?: throw Exception("Lower resources is empty") + ) + ) + } + + inputMetadata.forEach { + logger.warn("i prev: $it") + } + + // TODO: Check need of this for empty resources targets + // if (hasNoResourcesForGenerator(inputMetadata)) return null val fileSpec: FileSpec.Builder = FileSpec.builder( packageName = settings.packageName, @@ -44,12 +55,6 @@ abstract class TargetMRGenerator( val generatedObjects = mutableListOf() - inputMetadata.forEach { - logger.warn("i prev: $it") - } - - logger.warn("i mrClassSpec") - @Suppress("SpreadOperator") val mrClassSpec = TypeSpec.objectBuilder(settings.className) // default: object MR .addModifiers(KModifier.ACTUAL) @@ -69,38 +74,30 @@ abstract class TargetMRGenerator( it.modifier == GeneratedObjectModifier.EXPECT } - //Add actual implementation of expect interfaces from previous level - generateActualInterface( - visibilityModifier = settings.visibility.toModifier(), - generatedObjectsList = generatedObjects, - fileSpec = fileSpec - ) - - generatedObjects.forEach { - logger.warn("i generated objects item: ${it.name}") - } - - //Implementation not realised expect interfaces - expectInterfacesList.forEach { expectInterface -> - val hasInGeneratedActualObjects = generatedObjects.firstOrNull { - it.name == expectInterface.name - } != null - - logger.warn("i has in generatedObjects: $hasInGeneratedActualObjects") + // Add actual implementation of expect interfaces from previous levels + if (inputMetadata.isNotEmpty()) { + generateActualInterface( + visibilityModifier = settings.visibility.toModifier(), + generatedObjectsList = generatedObjects, + fileSpec = fileSpec + ) - if (hasInGeneratedActualObjects) return@forEach + // Generation of actual interfaces not realised on current level + expectInterfacesList.forEach { expectInterface -> + val hasInGeneratedActualObjects = generatedObjects.firstOrNull { + it.name == expectInterface.name + } != null - val resourcesInterface: TypeSpec = - TypeSpec.interfaceBuilder(expectInterface.name) - .addModifiers(visibilityModifier) - .addModifiers(KModifier.ACTUAL) - .build() + if (hasInGeneratedActualObjects) return@forEach - fileSpec.addType(resourcesInterface) - } + val resourcesInterface: TypeSpec = + TypeSpec.interfaceBuilder(expectInterface.name) + .addModifiers(visibilityModifier) + .addModifiers(KModifier.ACTUAL) + .build() - expectInterfacesList.forEach { - logger.warn("i expectInterfacesList item: $it") + fileSpec.addType(resourcesInterface) + } } generators.forEach { generator -> @@ -108,6 +105,8 @@ abstract class TargetMRGenerator( .objectBuilder(generator.mrObjectName) .addModifiers(visibilityModifier) + // Implement to object expect interfaces from previous + // levels of resources expectInterfacesList.forEach { generatedObject: GeneratedObject -> builder.addSuperinterface( ClassName( @@ -119,9 +118,9 @@ abstract class TargetMRGenerator( mrClassSpec.addType( generator.generate( - assetsGenerationDir, - resourcesGenerationDir, - builder + assetsGenerationDir = assetsGenerationDir, + resourcesGenerationDir = resourcesGenerationDir, + objectBuilder = builder, ) ) } @@ -132,8 +131,9 @@ abstract class TargetMRGenerator( fileSpec.addType(mrClass) createOutputMetadata( - buildDir = project.buildDir, - sourceSetName = settings.ownResourcesFileTree.first().targetName, + outputMetadataFile = project.buildDir, + sourceSetName = settings.ownResourcesFileTree.firstOrNull()?.targetName + ?: "unknownTarget", generatedObjects = generatedObjects ) @@ -150,18 +150,20 @@ abstract class TargetMRGenerator( generatedObjectsList: MutableList, fileSpec: FileSpec.Builder, ) { - logger.warn("i res") settings.lowerResourcesFileTree.forEach { - logger.warn("i lowerResourcesFileTree file: $it") + logger.warn("i generateActualInterface lowerResourcesFileTree file: $it") } settings.upperResourcesFileTree.forEach { - logger.warn("i upperResourcesFileTree file: $it") + logger.warn("i generateActualInterface upperResourcesFileTree file: $it") } settings.ownResourcesFileTree.forEach { - logger.warn("i ownResourcesFileTree file: $it") + logger.warn("i generateActualInterface ownResourcesFileTree file: $it") } - val targetName = - settings.ownResourcesFileTree.files.firstOrNull()?.targetName ?: "androidMain" + + if (settings.ownResourcesFileTree.files.isEmpty()) return + + val targetName: String = + settings.ownResourcesFileTree.files.firstOrNull()?.targetName ?: return generators.forEach { generator -> val interfaceName = getInterfaceName( @@ -174,9 +176,11 @@ abstract class TargetMRGenerator( .addModifiers(visibilityModifier) val generatedResources: TypeSpec = generator.generate( - assetsGenerationDir, - resourcesGenerationDir, - resourcesInterfaceBuilder + metadata = generatedObjectsList, + typeSpecIsInterface = true, + assetsGenerationDir = assetsGenerationDir, + resourcesGenerationDir = resourcesGenerationDir, + objectBuilder = resourcesInterfaceBuilder ) generatedObjectsList.add( diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/common/CommonMRGenerator.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/common/CommonMRGenerator.kt index 3ab00d87..65e9ea24 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/common/CommonMRGenerator.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/generator/common/CommonMRGenerator.kt @@ -15,6 +15,7 @@ import dev.icerock.gradle.metadata.GeneratedObject import dev.icerock.gradle.metadata.GeneratedObjectModifier import dev.icerock.gradle.metadata.GeneratedObjectType import dev.icerock.gradle.metadata.GeneratedProperties +import dev.icerock.gradle.metadata.Metadata import dev.icerock.gradle.metadata.Metadata.createOutputMetadata import dev.icerock.gradle.metadata.getInterfaceName import dev.icerock.gradle.tasks.GenerateMultiplatformResourcesTask @@ -58,6 +59,23 @@ class CommonMRGenerator( override fun generateFileSpec(): FileSpec? { if (settings.ownResourcesFileTree.files.isEmpty()) return null + val inputMetadata: MutableList = mutableListOf() + + //Read list of generated resources on previous level + if (settings.lowerResourcesFileTree.files.isNotEmpty()) { + inputMetadata.addAll( + Metadata.readInputMetadata( + inputMetadataFile = project.buildDir, + sourceSetName = settings.lowerResourcesFileTree.files.firstOrNull()?.targetName + ?: throw Exception("Lower resources is empty") + ) + ) + } + + inputMetadata.forEach { + logger.warn("i prev: $it") + } + val visibilityModifier: KModifier = settings.visibility.toModifier() val generatedObjectsList = mutableListOf() @@ -79,7 +97,8 @@ class CommonMRGenerator( // need to generate actual interface with fields generateActualInterfacesFileSpec( visibilityModifier = visibilityModifier, - generatedObjectsList = generatedObjectsList, + generatedObjects = generatedObjectsList, + inputMetadata = inputMetadata, fileSpec = fileSpec ) } @@ -92,7 +111,7 @@ class CommonMRGenerator( } createOutputMetadata( - buildDir = project.buildDir, + outputMetadataFile = project.buildDir, sourceSetName = settings.ownResourcesFileTree.first().targetName, generatedObjects = generatedObjectsList ) @@ -204,7 +223,8 @@ class CommonMRGenerator( private fun generateActualInterfacesFileSpec( visibilityModifier: KModifier, - generatedObjectsList: MutableList, + inputMetadata: MutableList, + generatedObjects: MutableList, fileSpec: Builder, ) { val targetName = settings.ownResourcesFileTree.files.first().targetName @@ -221,12 +241,14 @@ class CommonMRGenerator( .addModifiers(KModifier.ACTUAL) val generatedResources: TypeSpec = generator.generate( - assetsGenerationDir, - resourcesGenerationDir, - resourcesInterfaceBuilder + metadata = generatedObjects, + typeSpecIsInterface = true, + assetsGenerationDir = assetsGenerationDir, + resourcesGenerationDir = resourcesGenerationDir, + objectBuilder = resourcesInterfaceBuilder, ) - generatedObjectsList.add( + generatedObjects.add( GeneratedObject( name = interfaceName, type = GeneratedObjectType.INTERFACE, @@ -237,10 +259,20 @@ class CommonMRGenerator( fileSpec.addType(generatedResources) } + + inputMetadata.forEach { metadata -> + val hasInGeneratedActual = generatedObjects.firstOrNull { actualMetadata -> + metadata.name == actualMetadata.name + } != null + + if (!hasInGeneratedActual) { + generatedObjects.add(metadata) + } + } } } -fun List.toGeneratedVariables() : List { +fun List.toGeneratedVariables(): List { return map { GeneratedProperties( name = it.name, diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/GeneratedObject.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/GeneratedObject.kt index 189fb56e..5a10af9e 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/GeneratedObject.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/GeneratedObject.kt @@ -4,9 +4,9 @@ import kotlinx.serialization.Serializable @Serializable data class GeneratedObject( + val modifier: GeneratedObjectModifier, val type: GeneratedObjectType, val name: String, - val modifier: GeneratedObjectModifier, val properties: List, ) { val objectSpec: String diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/GeneratedProperties.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/GeneratedProperties.kt index fe18bfe3..fb115660 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/GeneratedProperties.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/GeneratedProperties.kt @@ -4,6 +4,6 @@ import kotlinx.serialization.Serializable @Serializable data class GeneratedProperties( - val name: String, val modifier: GeneratedObjectModifier, + val name: String, ) diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/Metadata.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/Metadata.kt index 6795a20c..09304866 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/Metadata.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/metadata/Metadata.kt @@ -9,13 +9,13 @@ object Metadata { private const val DIRECTORY = "generated/moko-resources/metadata" fun createOutputMetadata( - buildDir: File, + outputMetadataFile: File, sourceSetName: String, generatedObjects: List, ) { if (generatedObjects.isEmpty()) return - val metadataDir = File(buildDir, DIRECTORY).apply { + val metadataDir = File(outputMetadataFile, DIRECTORY).apply { mkdirs() } val metadataFile = File(metadataDir, "$sourceSetName-metadata.json") @@ -27,10 +27,10 @@ object Metadata { } fun readInputMetadata( - buildDir: File, + inputMetadataFile: File, sourceSetName: String, ): List { - val metadataDir = File(buildDir, DIRECTORY) + val metadataDir = File(inputMetadataFile, DIRECTORY) val inputMetadata = File(metadataDir, "$sourceSetName-metadata.json") val bufferedReader: BufferedReader = File(inputMetadata.toURI()).bufferedReader() val inputString: String = bufferedReader.use { it.readText() } diff --git a/resources-generator/src/main/kotlin/dev/icerock/gradle/tasks/GenerateMultiplatformResourcesTask.kt b/resources-generator/src/main/kotlin/dev/icerock/gradle/tasks/GenerateMultiplatformResourcesTask.kt index 91aabd89..184c0e20 100644 --- a/resources-generator/src/main/kotlin/dev/icerock/gradle/tasks/GenerateMultiplatformResourcesTask.kt +++ b/resources-generator/src/main/kotlin/dev/icerock/gradle/tasks/GenerateMultiplatformResourcesTask.kt @@ -18,13 +18,18 @@ import dev.icerock.gradle.utils.isStrictLineBreaks import org.gradle.api.DefaultTask import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.CacheableTask import org.gradle.api.tasks.Classpath import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType import org.jetbrains.kotlin.konan.target.KonanTarget @@ -64,6 +69,15 @@ abstract class GenerateMultiplatformResourcesTask : DefaultTask() { @get:Input abstract val resourcesVisibility: Property + @get:OutputFile + @get:Optional + abstract val outputMetadataFile: RegularFileProperty + + @get:PathSensitive(PathSensitivity.ABSOLUTE) + @get:InputFile + @get:Optional + abstract val inputMetadataFile: RegularFileProperty + //TODO Realise // @get:OutputFile // abstract val generationReport: RegularFileProperty