Skip to content

Commit

Permalink
feat!: try to improve android related source set trees
Browse files Browse the repository at this point in the history
- also, replace old gradle collections `.all {}` calls to `.configureEach {}`

Signed-off-by: Art Shendrik <[email protected]>
  • Loading branch information
amal committed Jun 29, 2024
1 parent b3261a2 commit e0737eb
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 79 deletions.
9 changes: 5 additions & 4 deletions fluxo-kmp-conf/api/plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ public final class Fkc {
public static final fun buildNumberSuffix (Lorg/gradle/api/Project;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
public static synthetic fun buildNumberSuffix$default (Lorg/gradle/api/Project;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
public static final fun bundle (Lorg/gradle/api/NamedDomainObjectContainer;Ljava/lang/String;)Lkotlin/properties/PropertyDelegateProvider;
public static final fun bundle (Lorg/gradle/api/NamedDomainObjectContainer;Ljava/lang/String;Ljava/lang/Boolean;)Lfluxo/conf/kmp/SourceSetBundle;
public static final fun bundle (Lorg/gradle/api/NamedDomainObjectContainer;Ljava/lang/String;Ljava/lang/Boolean;Z)Lfluxo/conf/kmp/SourceSetBundle;
public static final fun bundle (Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSetContainer;Ljava/lang/String;)Lkotlin/properties/PropertyDelegateProvider;
public static final fun bundle (Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSetContainer;Ljava/lang/String;Ljava/lang/Boolean;)Lfluxo/conf/kmp/SourceSetBundle;
public static final fun bundle (Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSetContainer;Ljava/lang/String;Ljava/lang/Boolean;Z)Lfluxo/conf/kmp/SourceSetBundle;
public static synthetic fun bundle$default (Lorg/gradle/api/NamedDomainObjectContainer;Ljava/lang/String;ILjava/lang/Object;)Lkotlin/properties/PropertyDelegateProvider;
public static synthetic fun bundle$default (Lorg/gradle/api/NamedDomainObjectContainer;Ljava/lang/String;Ljava/lang/Boolean;ILjava/lang/Object;)Lfluxo/conf/kmp/SourceSetBundle;
public static synthetic fun bundle$default (Lorg/gradle/api/NamedDomainObjectContainer;Ljava/lang/String;Ljava/lang/Boolean;ZILjava/lang/Object;)Lfluxo/conf/kmp/SourceSetBundle;
public static synthetic fun bundle$default (Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSetContainer;Ljava/lang/String;ILjava/lang/Object;)Lkotlin/properties/PropertyDelegateProvider;
public static synthetic fun bundle$default (Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSetContainer;Ljava/lang/String;Ljava/lang/Boolean;ILjava/lang/Object;)Lfluxo/conf/kmp/SourceSetBundle;
public static synthetic fun bundle$default (Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSetContainer;Ljava/lang/String;Ljava/lang/Boolean;ZILjava/lang/Object;)Lfluxo/conf/kmp/SourceSetBundle;
public static final fun commonCompileOnly (Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSetContainer;Ljava/lang/Object;Lorg/gradle/api/Project;Z)V
public static synthetic fun commonCompileOnly$default (Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSetContainer;Ljava/lang/Object;Lorg/gradle/api/Project;ZILjava/lang/Object;)V
public static final fun dependencies (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)V
Expand Down Expand Up @@ -603,5 +603,6 @@ public abstract interface class fluxo/conf/kmp/SourceSetBundle {
public fun getMoreTests ()[Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSet;
public abstract fun getTest ()Lorg/jetbrains/kotlin/gradle/plugin/KotlinSourceSet;
public synthetic fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public fun isAndroid ()Z
}

1 change: 0 additions & 1 deletion fluxo-kmp-conf/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@
<ID>ForbiddenPublicDataClass:FluxoPublicationConfig.kt$FluxoPublicationConfig</ID>
<ID>Indentation:DependencyUpdatesPlugin.kt.kt$ </ID>
<ID>Indentation:FluxoConfigurationExtensionAndroidImpl.kt$FluxoConfigurationExtensionAndroidImpl$ </ID>
<ID>Indentation:MultiplatformSourceSetUtils.kt$ </ID>
<ID>LongMethod:SetupPublication.kt$context(FluxoKmpConfContext) private fun Project.setupPublicationRepositoryAndSigning( config: FluxoPublicationConfig, publishing: PublishingExtension, mavenRepo: Boolean = true, )</ID>
<ID>LongMethod:TestReportsMergeTask.kt$TestReportsMergeTask$@TaskAction fun merge()</ID>
<ID>MaxLineLength:ShrinkerReflectiveCaller.kt$ShrinkerReflectiveCaller$logger.w("$shrinker could not be loaded in-memory as $callType (class=$className)!")</ID>
Expand Down
11 changes: 6 additions & 5 deletions fluxo-kmp-conf/pg/keep-api-autogenerated.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# ProGuard/R8 keep rules
# Auto-generated by Fluxo task :generateShrinkerKeepRulesFromApi
# From API reports (with sha256short):
# - api/plugin.api (c2ca387)
# - api/plugin.api (a5f64cd)
# DO NOT EDIT MANUALLY!

-keep,includedescriptorclasses public final class Fkc {
Expand All @@ -11,13 +11,13 @@
public static final java.lang.String buildNumberSuffix(org.gradle.api.Project, java.lang.String, java.lang.String);
public static synthetic java.lang.String buildNumberSuffix$default(org.gradle.api.Project, java.lang.String, java.lang.String, int, java.lang.Object);
public static final kotlin.properties.PropertyDelegateProvider bundle(org.gradle.api.NamedDomainObjectContainer, java.lang.String);
public static final fluxo.conf.kmp.SourceSetBundle bundle(org.gradle.api.NamedDomainObjectContainer, java.lang.String, java.lang.Boolean);
public static final fluxo.conf.kmp.SourceSetBundle bundle(org.gradle.api.NamedDomainObjectContainer, java.lang.String, java.lang.Boolean, boolean);
public static final kotlin.properties.PropertyDelegateProvider bundle(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer, java.lang.String);
public static final fluxo.conf.kmp.SourceSetBundle bundle(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer, java.lang.String, java.lang.Boolean);
public static final fluxo.conf.kmp.SourceSetBundle bundle(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer, java.lang.String, java.lang.Boolean, boolean);
public static synthetic kotlin.properties.PropertyDelegateProvider bundle$default(org.gradle.api.NamedDomainObjectContainer, java.lang.String, int, java.lang.Object);
public static synthetic fluxo.conf.kmp.SourceSetBundle bundle$default(org.gradle.api.NamedDomainObjectContainer, java.lang.String, java.lang.Boolean, int, java.lang.Object);
public static synthetic fluxo.conf.kmp.SourceSetBundle bundle$default(org.gradle.api.NamedDomainObjectContainer, java.lang.String, java.lang.Boolean, boolean, int, java.lang.Object);
public static synthetic kotlin.properties.PropertyDelegateProvider bundle$default(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer, java.lang.String, int, java.lang.Object);
public static synthetic fluxo.conf.kmp.SourceSetBundle bundle$default(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer, java.lang.String, java.lang.Boolean, int, java.lang.Object);
public static synthetic fluxo.conf.kmp.SourceSetBundle bundle$default(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer, java.lang.String, java.lang.Boolean, boolean, int, java.lang.Object);
public static final void commonCompileOnly(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer, java.lang.Object, org.gradle.api.Project, boolean);
public static synthetic void commonCompileOnly$default(org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer, java.lang.Object, org.gradle.api.Project, boolean, int, java.lang.Object);
public static final void dependencies(java.lang.Iterable, kotlin.jvm.functions.Function1);
Expand Down Expand Up @@ -600,5 +600,6 @@
public org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet[] getMoreTests();
public abstract org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet getTest();
public synthetic java.lang.Object invoke(kotlin.jvm.functions.Function1);
public boolean isAndroid();
}

95 changes: 49 additions & 46 deletions fluxo-kmp-conf/src/main/kotlin/MultiplatformSourceSetUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@file:JvmMultifileClass

import fluxo.conf.dsl.container.impl.KmpTargetContainerImpl
import fluxo.conf.dsl.container.impl.KmpTargetContainerImpl.CommonJvm.Companion.ANDROID
import fluxo.conf.impl.compileOnlyAndLog
import fluxo.conf.impl.implementation
import fluxo.conf.impl.implementationAndLog
Expand Down Expand Up @@ -120,84 +119,84 @@ public val KotlinSourceSetContainer.commonMingw: SourceSetBundle

/** [SourceSetBundle]s for all enabled targets */
public val <E> E.allSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = allTargetsSet

/** [SourceSetBundle]s for all enabled targets */
public val <E> E.allTargetsSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = targets.toSourceSetBundles()

/** androidJvm, jvm */
public val <E> E.javaSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = targets.matching {
it.platformType == KotlinPlatformType.androidJvm ||
it.platformType == KotlinPlatformType.jvm
it.platformType == KotlinPlatformType.jvm
}.toSourceSetBundles()

/** androidJvm */
public val <E> E.androidSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = targets.matching { it.platformType == KotlinPlatformType.androidJvm }
.toSourceSetBundles()
.toSourceSetBundles()

/** js */
public val <E> E.jsSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = targets.matching { it.platformType == KotlinPlatformType.js }.toSourceSetBundles()


/** All Kotlin/Native targets */
public val <E> E.nativeSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets()


public val <E> E.linuxSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets(Family.LINUX)

public val <E> E.mingwSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets(Family.MINGW)

public val <E> E.androidNativeSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets(Family.ANDROID)


/** All Apple (Darwin) targets */
public val <E> E.appleSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets(Family.IOS, Family.OSX, Family.WATCHOS, Family.TVOS)

/** All Apple (Darwin) targets */
public val <E> E.darwinSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = appleSet

public val <E> E.iosSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets(Family.IOS)

public val <E> E.watchosSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets(Family.WATCHOS)

public val <E> E.tvosSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets(Family.TVOS)

public val <E> E.macosSet: Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer
get() = nativeSourceSets(Family.OSX)


private fun <E> E.nativeSourceSets(vararg families: Family = Family.values()): Set<SourceSetBundle>
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer =
targets.filter { it is KotlinNativeTarget && it.konanTarget.family in families }
.toSourceSetBundles()
where E : KotlinSourceSetContainer, E : KotlinTargetsContainer =
targets.filter { it is KotlinNativeTarget && it.konanTarget.family in families }
.toSourceSetBundles()

context(KotlinSourceSetContainer)
private fun Iterable<KotlinTarget>.toSourceSetBundles(): Set<SourceSetBundle> {
Expand All @@ -219,50 +218,53 @@ internal fun KotlinSourceSetContainer.bundleFor(target: KotlinTarget) = sourceSe
internal fun NamedDomainObjectContainer<out KotlinSourceSet>.bundleFor(
target: KotlinTarget,
androidLayoutV2: Boolean? = null,
isAndroid: Boolean = androidLayoutV2 != null,
): SourceSetBundle {
val compilations = target.compilations
return when {
compilations.isEmpty() || androidLayoutV2 != null ->
bundle(target.name, androidLayoutV2 = androidLayoutV2)
bundle(target.name, androidLayoutV2 = androidLayoutV2, isAndroid = isAndroid)

else -> SourceSetBundle(
main = compilations.getByName(MAIN_SOURCE_SET_NAME).defaultSourceSet,
test = compilations.getByName(TEST_SOURCE_SET_NAME).defaultSourceSet,
main = compilations.getByName(MAIN_SOURCE_SET_NAME).defaultSourceSet,
test = compilations.getByName(TEST_SOURCE_SET_NAME).defaultSourceSet,
)
}
}

public fun KotlinSourceSetContainer.bundle(
name: String,
androidLayoutV2: Boolean? = null,
): SourceSetBundle = sourceSets.bundle(name, androidLayoutV2)
isAndroid: Boolean = androidLayoutV2 != null,
): SourceSetBundle = sourceSets.bundle(name, androidLayoutV2, isAndroid = isAndroid)

public fun NamedDomainObjectContainer<out KotlinSourceSet>.bundle(
name: String,
androidLayoutV2: Boolean? = null,
isAndroid: Boolean = androidLayoutV2 != null,
): SourceSetBundle {
val mainSourceSet = maybeCreate(name + MAIN_SOURCE_SET_POSTFIX)

// region Support for androidSourceSetLayout v2
// https://kotlinlang.org/docs/whatsnew18.html#kotlinsourceset-naming-schema
/** @see fluxo.conf.dsl.container.impl.target.TargetAndroidContainer.setup */
val isAndroid = name == ANDROID
if (isAndroid) {
val useV1 = androidLayoutV2?.not()
?: names.let { "androidAndroidTest" in it || "androidTest" in it }
?: names.let { "androidAndroidTest" in it || "androidTest" in it }
val instrumentedTest =
maybeCreate(if (!useV1) "androidInstrumentedTest" else "androidAndroidTest")
maybeCreate(if (!useV1) "androidInstrumentedTest" else "androidAndroidTest")
return SourceSetBundle(
main = mainSourceSet,
test = maybeCreate(if (!useV1) "androidUnitTest" else "androidTest"),
moreTests = arrayOf(instrumentedTest),
main = mainSourceSet,
test = maybeCreate(if (!useV1) "androidUnitTest" else "androidTest"),
moreTests = arrayOf(instrumentedTest),
isAndroid = true,
)
}
// endregion

return SourceSetBundle(
main = mainSourceSet,
test = maybeCreate(name + TEST_SOURCE_SET_POSTFIX),
main = mainSourceSet,
test = maybeCreate(name + TEST_SOURCE_SET_POSTFIX),
)
}

Expand All @@ -274,15 +276,15 @@ public fun NamedDomainObjectContainer<out KotlinSourceSet>.bundle(
public fun NamedDomainObjectContainer<out KotlinSourceSet>.bundle(
name: String? = null,
): PropertyDelegateProvider<Any?, ReadOnlyProperty<Any?, SourceSetBundle>> =
PropertyDelegateProvider { _, property ->
val bundle = bundle(name = name ?: property.name)
ReadOnlyProperty { _, _ -> bundle }
}
PropertyDelegateProvider { _, property ->
val bundle = bundle(name = name ?: property.name)
ReadOnlyProperty { _, _ -> bundle }
}

public fun KotlinSourceSetContainer.bundle(
name: String? = null,
): PropertyDelegateProvider<Any?, ReadOnlyProperty<Any?, SourceSetBundle>> =
sourceSets.bundle(name)
sourceSets.bundle(name)

internal const val MAIN_SOURCE_SET_NAME = "main"
internal const val TEST_SOURCE_SET_NAME = "test"
Expand All @@ -295,17 +297,18 @@ internal const val TEST_SOURCE_SET_POSTFIX = "Test"
// region Dependecies declaration

public operator fun SourceSetBundle.plus(other: SourceSetBundle): Set<SourceSetBundle> =
this + setOf(other)
this + setOf(other)

public operator fun SourceSetBundle.plus(other: Set<SourceSetBundle>): Set<SourceSetBundle> =
setOf(this) + other
setOf(this) + other

@Deprecated(KOTLIN_SOURCE_SETS_DEPENDS_ON_DEPRECATION)
public infix fun SourceSetBundle.dependsOn(other: SourceSetBundle) {
main.dependsOn(other.main)
test.dependsOn(other.test)
val otherTest = other.test
test.dependsOn(otherTest)
moreTests?.forEach {
it.dependsOn(other.test)
it.dependsOn(otherTest)
}
}

Expand Down Expand Up @@ -379,8 +382,8 @@ public fun <E> E.commonCompileOnly(
?: throw NullPointerException("Please, provide project")
} catch (e: Throwable) {
throw GradleException(
"Unable to add common compileOnly dependency '$dependencyNotation': $e",
e,
"Unable to add common compileOnly dependency '$dependencyNotation': $e",
e,
)
}

Expand All @@ -406,8 +409,8 @@ public fun <E> E.commonCompileOnly(
if (addConstraint) {
if (dependencyNotation is Dependency && dependencyNotation.version.isNullOrEmpty()) {
p.logger.w(
"Dependency version is empty, " +
"can't add a constraint: $dependencyNotation",
"Dependency version is empty, " +
"can't add a constraint: $dependencyNotation",
)
} else {
dependencies.constraints.implementation(dependencyNotation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ internal abstract class FluxoKmpConfContext
}
}

else -> projectInSyncFlag.all {
else -> projectInSyncFlag.configureEach {
try {
context.action()
} catch (e: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal abstract class ContainerImpl(
private val pluginIds = context.objects.set<String>()

fun applyPluginsWith(pluginManager: PluginManager) {
pluginIds.all {
pluginIds.configureEach {
pluginManager.apply(this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal open class CustomTypeContainer<T>(

fun add(action: T.() -> Unit): Boolean = set.add(action)

fun setupCustom(k: T) = set.all { k.this() }
fun setupCustom(k: T) = set.configureEach { k.this() }


override val sortOrder: Byte = CUSTOM_SORT_ORDER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal abstract class KmpTargetContainerImpl<T : KotlinTarget>(

private val lazyTarget = context.objects.set<T.() -> Unit>()

internal val lazyTargetConf: T.() -> Unit = { lazyTarget.all { this() } }
internal val lazyTargetConf: T.() -> Unit = { lazyTarget.configureEach { this() } }

override fun target(action: T.() -> Unit) {
lazyTarget.add(action)
Expand Down Expand Up @@ -69,7 +69,8 @@ internal abstract class KmpTargetContainerImpl<T : KotlinTarget>(
}

override fun setupParentSourceSet(k: KotlinMultiplatformExtension, child: SourceSetBundle) {
if (!allowManualHierarchy) return
// Android source sets can always require manual hierarchy setup.
if (!allowManualHierarchy && !child.isAndroid) return
val bundle = k.commonJvm
@Suppress("DEPRECATION")
child dependsOn bundle
Expand Down
Loading

0 comments on commit e0737eb

Please sign in to comment.