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

Add API to not apply the Compose Compiler plugin #3722

Merged
merged 5 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget

class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin {
private lateinit var composeCompilerArtifactProvider: ComposeCompilerArtifactProvider
private lateinit var applicableForPlatformTypes: Provider<Set<KotlinPlatformType>>


override fun apply(target: Project) {
super.apply(target)
Expand All @@ -27,6 +29,8 @@ class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin {
ComposeCompilerCompatibility.compilerVersionFor(target.getKotlinPluginVersion())
}

applicableForPlatformTypes = composeExt.platformTypes

collectUnsupportedCompilerPluginUsages(target)
}
}
Expand Down Expand Up @@ -62,15 +66,14 @@ class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin {
override fun getPluginArtifactForNative(): SubpluginArtifact =
composeCompilerArtifactProvider.compilerHostedArtifact

override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean =
when (kotlinCompilation.target.platformType) {
KotlinPlatformType.common -> true
KotlinPlatformType.jvm -> true
KotlinPlatformType.js -> isApplicableJsTarget(kotlinCompilation.target)
KotlinPlatformType.androidJvm -> true
KotlinPlatformType.native -> true
KotlinPlatformType.wasm -> false
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean {
val applicableTo = applicableForPlatformTypes.get()

return when (val type = kotlinCompilation.target.platformType) {
KotlinPlatformType.js -> isApplicableJsTarget(kotlinCompilation.target) && applicableTo.contains(type)
igordmn marked this conversation as resolved.
Show resolved Hide resolved
else -> applicableTo.contains(type)
}
}

private fun isApplicableJsTarget(kotlinTarget: KotlinTarget): Boolean {
if (kotlinTarget !is KotlinJsIrTarget) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.jetbrains.compose.internal.utils.nullableProperty
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import javax.inject.Inject

abstract class ComposeExtension @Inject constructor(
Expand Down Expand Up @@ -41,5 +43,26 @@ abstract class ComposeExtension @Inject constructor(
*/
val kotlinCompilerPluginArgs: ListProperty<String> = objects.listProperty(String::class.java)

/**
* A set of kotlin platform types to which the Compose Compiler plugin will be applied.
eymar marked this conversation as resolved.
Show resolved Hide resolved
* By default, it contains all KotlinPlatformType values.
* It can be used to disable the Compose Compiler plugin for one or more targets:
eymar marked this conversation as resolved.
Show resolved Hide resolved
* ```
* platformTypes.set(platformTypes.get() - KotlinPlatformType.native)
* ```
*/
@Suppress("MemberVisibilityCanBePrivate")
val platformTypes: SetProperty<KotlinPlatformType> = objects.setProperty(KotlinPlatformType::class.java).also {
igordmn marked this conversation as resolved.
Show resolved Hide resolved
it.set(KotlinPlatformType.values().toMutableSet())
}

val dependencies = ComposePlugin.Dependencies(project)

/**
* @param platformType - the type of platform(s) which should not have the Compose Compiler plugin applied.
* Removes [platformType] from [platformTypes].
*/
fun excludePlatform(vararg platformType: KotlinPlatformType) {
platformTypes.set(platformTypes.get() - platformType.toSet())
}
}