Skip to content

Commit

Permalink
#535 add input/output metadata property, started work on generate wit…
Browse files Browse the repository at this point in the history
…h metadata
  • Loading branch information
ExNDY committed Nov 28, 2023
1 parent 4fe55fa commit 819ac49
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,7 +69,10 @@ open class MultiplatformResourcesPlugin : Plugin<Project> {
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
Expand Down Expand Up @@ -101,25 +105,25 @@ open class MultiplatformResourcesPlugin : Plugin<Project> {
project.logger.warn("i target ${target.targetName}")

target.compilations.configureEach { compilation ->
val sourceSet: KotlinSourceSet = compilation.defaultSourceSet
val genTask: TaskProvider<GenerateMultiplatformResourcesTask> = 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<GenerateMultiplatformResourcesTask> = 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)
}
}
}
}
Expand Down Expand Up @@ -357,8 +361,14 @@ open class MultiplatformResourcesPlugin : Plugin<Project> {
dependsSourceSet.extras[mokoResourcesGenTaskKey()]
)

genTask.configure {
it.dependsOn(dependsGenTask)
genTask.configure { resourceTask ->
resourceTask.dependsOn(dependsGenTask) //TODO: Убрать после реализации связи через метадату

resourceTask.inputMetadataFile.set(
dependsGenTask.flatMap {
it.outputMetadataFile
}
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -86,6 +87,8 @@ abstract class AssetsGenerator(
}

override fun generate(
metadata: List<GeneratedObject>,
typeSpecIsInterface: Boolean,
assetsGenerationDir: File,
resourcesGenerationDir: File,
objectBuilder: TypeSpec.Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@ 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<T> : MRGenerator.Generator {

override fun generate(
metadata: List<GeneratedObject>,
typeSpecIsInterface: Boolean,
assetsGenerationDir: File,
resourcesGenerationDir: File,
objectBuilder: TypeSpec.Builder
objectBuilder: TypeSpec.Builder,
): TypeSpec {
// language - key - value
val languageMap: Map<LanguageType, Map<KeyType, T>> = loadLanguageMap()
val languageKeyValues = languageMap[LanguageType.Base].orEmpty()

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)
Expand All @@ -33,26 +42,49 @@ abstract class BaseGenerator<T> : MRGenerator.Generator {
}

@Suppress("SpreadOperator")
private fun createTypeSpec(keys: List<KeyType>, objectBuilder: TypeSpec.Builder): TypeSpec {
private fun createTypeSpec(
metadata: List<GeneratedObject> = emptyList(),
typeSpecIsInterface: Boolean,
keys: List<KeyType>,
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())
}

extendObjectBodyAtEnd(objectBuilder)
return objectBuilder.build()
}

private fun addActualOverrideModifier(
property: PropertySpec.Builder,
metadata: List<GeneratedObject>,
) {


property
.addModifiers(KModifier.OVERRIDE)

}

protected abstract fun loadLanguageMap(): Map<LanguageType, Map<KeyType, T>>
protected abstract fun getPropertyInitializer(key: String): CodeBlock?

Expand All @@ -61,15 +93,14 @@ abstract class BaseGenerator<T> : MRGenerator.Generator {

protected open fun beforeGenerateResources(
objectBuilder: TypeSpec.Builder,
languageMap: Map<LanguageType, Map<KeyType, T>>
languageMap: Map<LanguageType, Map<KeyType, T>>,
) = Unit

protected open fun generateResources(
resourcesGenerationDir: File,
language: LanguageType,
strings: Map<KeyType, T>
) {
}
strings: Map<KeyType, T>,
) = Unit

protected companion object {
const val BASE_LANGUAGE = "base"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +37,8 @@ abstract class ColorsGenerator(

@Suppress("SpreadOperator")
override fun generate(
metadata: List<GeneratedObject>,
typeSpecIsInterface: Boolean,
assetsGenerationDir: File,
resourcesGenerationDir: File,
objectBuilder: TypeSpec.Builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -26,6 +27,8 @@ abstract class FilesGenerator(
override val mrObjectName: String = "files"

override fun generate(
metadata: List<GeneratedObject>,
typeSpecIsInterface: Boolean,
assetsGenerationDir: File,
resourcesGenerationDir: File,
objectBuilder: TypeSpec.Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +28,8 @@ abstract class FontsGenerator(
override val mrObjectName: String = "fonts"

override fun generate(
metadata: List<GeneratedObject>,
typeSpecIsInterface: Boolean,
assetsGenerationDir: File,
resourcesGenerationDir: File,
objectBuilder: TypeSpec.Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,8 @@ abstract class ImagesGenerator(
override val mrObjectName: String = "images"

override fun generate(
metadata: List<GeneratedObject>,
typeSpecIsInterface: Boolean,
assetsGenerationDir: File,
resourcesGenerationDir: File,
objectBuilder: TypeSpec.Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,6 +81,8 @@ abstract class MRGenerator(
val inputFiles: Iterable<File>

fun generate(
metadata: List<GeneratedObject> = emptyList(),
typeSpecIsInterface: Boolean = false,
assetsGenerationDir: File,
resourcesGenerationDir: File,
objectBuilder: TypeSpec.Builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class StringsGenerator(
) : BaseGenerator<String>() {

override val inputFiles: Iterable<File>
get() = (lowerStringsFileTree + ownStringsFileTree).matching {
get() = ownStringsFileTree.matching {
it.include(STRINGS_MASK)
}.files

Expand Down
Loading

0 comments on commit 819ac49

Please sign in to comment.