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

Feature/add jvm target #159

Merged
merged 17 commits into from
Aug 24, 2021
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
8 changes: 5 additions & 3 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
*/

plugins {
id("org.jetbrains.kotlin.jvm") version("1.4.21")
id("org.jetbrains.kotlin.jvm") version("1.4.31")
}

repositories {
jcenter()
google()

maven { url = uri("https://dl.bintray.com/icerockdev/plugins") }
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}

dependencies {
implementation("dev.icerock:mobile-multiplatform:0.9.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21")
implementation("com.android.tools.build:gradle:4.0.1")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")
implementation("com.android.tools.build:gradle:4.1.2")
implementation("org.jetbrains.compose:compose-gradle-plugin:0.3.2")
}
8 changes: 4 additions & 4 deletions buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

object Deps {
private const val kotlinVersion = "1.4.21"
private const val kotlinVersion = "1.4.31"
private const val androidGradleVersion = "4.0.1"

private const val androidAppCompatVersion = "1.1.0"
Expand All @@ -17,8 +17,8 @@ object Deps {

private const val detektVersion = "1.7.4"

private const val mokoGraphicsVersion = "0.5.0"
private const val mokoParcelizeVersion = "0.5.0"
private const val mokoGraphicsVersion = "0.6.0"
private const val mokoParcelizeVersion = "0.6.0"
const val mokoResourcesVersion = "0.15.0"

object Android {
Expand Down Expand Up @@ -66,7 +66,7 @@ object Deps {
const val testCore =
"androidx.test:core:1.3.0"
const val robolectric =
"org.robolectric:robolectric:4.3"
"org.robolectric:robolectric:4.5.1"
const val testRunner =
"androidx.test:runner:$testRunnerVersion"
const val testRules =
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ android.useAndroidX=true
# https://github.com/gradle/gradle/issues/11412
systemProp.org.gradle.internal.publish.checksums.insecure=true

xcodeproj=./sample/ios-app
xcodeproj=./sample/ios-app

mobile.multiplatform.iosTargetWarning=false
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import dev.icerock.gradle.generator.StringsGenerator
import dev.icerock.gradle.generator.android.AndroidMRGenerator
import dev.icerock.gradle.generator.common.CommonMRGenerator
import dev.icerock.gradle.generator.apple.AppleMRGenerator
import dev.icerock.gradle.generator.jvm.JvmMRGenerator
import dev.icerock.gradle.tasks.GenerateMultiplatformResourcesTask
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -30,6 +31,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
import org.jetbrains.kotlin.konan.target.HostManager
import java.io.File
import javax.xml.parsers.DocumentBuilderFactory
Expand Down Expand Up @@ -100,22 +102,32 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
val targets: List<KotlinTarget> = multiplatformExtension.targets.toList()

val commonGenerationTask = setupCommonGenerator(
commonSourceSet = commonSourceSet,
generatedDir = generatedDir,
mrClassPackage = mrClassPackage,
features = features,
target = target
commonSourceSet,
generatedDir,
mrClassPackage,
features,
target
)
setupAndroidGenerator(
commonSourceSet,
targets,
androidMainSourceSet,
generatedDir,
mrClassPackage,
features,
target
)
setupJvmGenerator(
commonSourceSet,
targets,
generatedDir,
mrClassPackage,
features,
target
)
if (HostManager.hostIsMac) {
setupAppleGenerator(
commonSourceSet,
targets,
generatedDir,
mrClassPackage,
Expand Down Expand Up @@ -150,6 +162,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {

@Suppress("LongParameterList")
private fun setupAndroidGenerator(
commonSourceSet: KotlinSourceSet,
targets: List<KotlinTarget>,
androidMainSourceSet: AndroidSourceSet,
generatedDir: File,
Expand All @@ -160,7 +173,9 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
val kotlinSourceSets: List<KotlinSourceSet> = targets
.filterIsInstance<KotlinAndroidTarget>()
.flatMap { it.compilations }
.filterNot { it.name.endsWith("Test") } // remove tests compilations
.filter { compilation ->
compilation.kotlinSourceSets.any { it.isDependsOn(commonSourceSet) }
}
.map { it.defaultSourceSet }

val androidSourceSet: MRGenerator.SourceSet =
Expand All @@ -173,8 +188,33 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
).apply(target)
}

private fun setupJvmGenerator(
commonSourceSet: KotlinSourceSet,
targets: List<KotlinTarget>,
generatedDir: File,
mrClassPackage: String,
features: List<ResourceGeneratorFeature<out MRGenerator.Generator>>,
target: Project
) {
val kotlinSourceSets: List<KotlinSourceSet> = targets
.filterIsInstance<KotlinJvmTarget>()
.flatMap { it.compilations }
.map { it.defaultSourceSet }
.filter { it.isDependsOn(commonSourceSet) }

kotlinSourceSets.forEach { kotlinSourceSet ->
JvmMRGenerator(
generatedDir,
createSourceSet(kotlinSourceSet),
mrClassPackage,
generators = features.map { it.createJvmGenerator() }
).apply(target)
}
}

@Suppress("LongParameterList")
private fun setupAppleGenerator(
commonSourceSet: KotlinSourceSet,
targets: List<KotlinTarget>,
generatedDir: File,
mrClassPackage: String,
Expand All @@ -191,6 +231,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
}

val defSourceSets = compilations.map { it.defaultSourceSet }
.filter { it.isDependsOn(commonSourceSet) }
compilations.forEach { compilation ->
val kss = compilation.defaultSourceSet
val depend = kss.getDependedFrom(defSourceSets)
Expand Down Expand Up @@ -256,4 +297,12 @@ class MultiplatformResourcesPlugin : Plugin<Project> {

return manifest.attributes.getNamedItem("package").textContent
}

private fun KotlinSourceSet.isDependsOn(sourceSet: KotlinSourceSet): Boolean {
if (dependsOn.contains(sourceSet)) return true
dependsOn.forEach { parent ->
if (parent.isDependsOn(sourceSet)) return true
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ abstract class BaseGenerator<T> : MRGenerator.Generator {
private fun createTypeSpec(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) }
getPropertyInitializer(
key
)?.let { property.initializer(it) }
objectBuilder.addProperty(property.build())
}

extendObjectBody(objectBuilder)
extendObjectBodyAtEnd(objectBuilder)
return objectBuilder.build()
}

override fun extendObjectBody(classBuilder: TypeSpec.Builder) = Unit

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.squareup.kotlinpoet.TypeSpec
import dev.icerock.gradle.generator.android.AndroidColorsGenerator
import dev.icerock.gradle.generator.common.CommonColorsGenerator
import dev.icerock.gradle.generator.apple.AppleColorsGenerator
import dev.icerock.gradle.generator.jvm.JvmColorsGenerator
import org.gradle.api.file.FileTree
import java.io.File
import javax.xml.parsers.DocumentBuilderFactory
Expand All @@ -33,7 +34,7 @@ abstract class ColorsGenerator(
@Suppress("SpreadOperator")
override fun generate(resourcesGenerationDir: File, objectBuilder: TypeSpec.Builder): TypeSpec {
objectBuilder.addModifiers(*getClassModifiers())
extendObjectBody(objectBuilder)
extendObjectBodyAtStart(objectBuilder)

val colors = parseColors()
colors.forEach { colorNode ->
Expand All @@ -50,11 +51,11 @@ abstract class ColorsGenerator(

generateResources(resourcesGenerationDir, colors)

extendObjectBodyAtEnd(objectBuilder)

return objectBuilder.build()
}

override fun extendObjectBody(classBuilder: TypeSpec.Builder) = Unit

protected open fun getClassModifiers(): Array<KModifier> = emptyArray()
protected open fun getPropertyModifiers(): Array<KModifier> = emptyArray()
protected open fun getPropertyInitializer(color: ColorNode): CodeBlock? {
Expand Down Expand Up @@ -129,28 +130,18 @@ abstract class ColorsGenerator(
return colorNodes
}

class Feature(
private val info: SourceInfo
) : ResourceGeneratorFeature<ColorsGenerator> {
class Feature(info: SourceInfo) : ResourceGeneratorFeature<ColorsGenerator> {

private val colorsFileTree =
info.commonResources.matching { it.include("MR/**/colors*.xml") }

override fun createCommonGenerator(): ColorsGenerator {
return CommonColorsGenerator(colorsFileTree)
}
override fun createCommonGenerator() = CommonColorsGenerator(colorsFileTree)

override fun createIosGenerator(): ColorsGenerator {
return AppleColorsGenerator(
colorsFileTree
)
}
override fun createIosGenerator() = AppleColorsGenerator(colorsFileTree)

override fun createAndroidGenerator(): ColorsGenerator {
return AndroidColorsGenerator(
colorsFileTree
)
}
override fun createAndroidGenerator() = AndroidColorsGenerator(colorsFileTree)

override fun createJvmGenerator() = JvmColorsGenerator(colorsFileTree)
}

protected fun replaceColorAlpha(color: String?): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.squareup.kotlinpoet.TypeSpec
import dev.icerock.gradle.generator.android.AndroidFilesGenerator
import dev.icerock.gradle.generator.common.CommonFilesGenerator
import dev.icerock.gradle.generator.apple.AppleFilesGenerator
import dev.icerock.gradle.generator.jvm.JvmFilesGenerator
import org.gradle.api.file.FileTree
import java.io.File

Expand All @@ -38,8 +39,10 @@ abstract class FilesGenerator(
@Suppress("SpreadOperator")
objectBuilder.addModifiers(*getClassModifiers())

extendObjectBodyAtStart(objectBuilder)

keys.forEach { objectBuilder.addProperty(generateFileProperty(it)) }
extendObjectBody(objectBuilder)
extendObjectBodyAtEnd(objectBuilder)
return objectBuilder.build()
}

Expand Down Expand Up @@ -67,8 +70,6 @@ abstract class FilesGenerator(
return key.replace("-", "_")
}

override fun extendObjectBody(classBuilder: TypeSpec.Builder) = Unit

abstract fun getClassModifiers(): Array<KModifier>

abstract fun getPropertyModifiers(): Array<KModifier>
Expand All @@ -81,23 +82,20 @@ abstract class FilesGenerator(
)

class Feature(private val info: SourceInfo) : ResourceGeneratorFeature<FilesGenerator> {

private val fileTree = info.commonResources.matching {
it.include("MR/files/**")
}

override fun createCommonGenerator(): FilesGenerator {
return CommonFilesGenerator(fileTree)
}
override fun createCommonGenerator() = CommonFilesGenerator(fileTree)

override fun createIosGenerator(): FilesGenerator {
return AppleFilesGenerator(fileTree)
}
override fun createIosGenerator() = AppleFilesGenerator(fileTree)

override fun createAndroidGenerator(): FilesGenerator {
return AndroidFilesGenerator(
fileTree,
info.androidRClassPackage
)
}
override fun createAndroidGenerator() = AndroidFilesGenerator(
fileTree,
info.androidRClassPackage
)

override fun createJvmGenerator() = JvmFilesGenerator(fileTree)
}
}
Loading