Skip to content

Commit ec1291f

Browse files
committed
Used parameter injection for CurrentOsAttributeDisambiguationRule per gradle/gradle#32979 (comment).
1 parent 3d973b8 commit ec1291f

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

build-logic/src/main/kotlin/org.sdkotlin.buildlogic.native-resources.gradle.kts

+5-4
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ configure<ResourceConfigurations> {
5656
val currentOsAttributeValue =
5757
osdetector.osAsOperatingSystemFamilyAttributeValue()
5858

59-
CurrentOsAttributeDisambiguationRule.currentOsAttributeValue =
60-
objects.named(currentOsAttributeValue)
61-
6259
dependencies {
6360
attributesSchema {
6461
attribute(OPERATING_SYSTEM_ATTRIBUTE) {
6562
disambiguationRules.add(
6663
CurrentOsAttributeDisambiguationRule::class.java
67-
)
64+
) {
65+
params(
66+
objects.named<OperatingSystemFamily>(currentOsAttributeValue)
67+
)
68+
}
6869
}
6970
}
7071
}

build-logic/src/main/kotlin/org/sdkotlin/buildlogic/attributes/CurrentOsAttributeDisambiguationRule.kt

+12-16
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package org.sdkotlin.buildlogic.attributes
22

33
import org.gradle.api.attributes.AttributeDisambiguationRule
44
import org.gradle.api.attributes.MultipleCandidatesDetails
5+
import org.gradle.api.model.ObjectFactory
56
import org.gradle.nativeplatform.OperatingSystemFamily
6-
import org.sdkotlin.buildlogic.attributes.CurrentOsAttributeDisambiguationRule.Companion.currentOsAttributeValue
7+
import javax.inject.Inject
78

89
/**
910
* A rule to disambiguate between multiple candidates of the
@@ -14,25 +15,20 @@ import org.sdkotlin.buildlogic.attributes.CurrentOsAttributeDisambiguationRule.C
1415
* object property [currentOsAttributeValue] during the project configuration
1516
* phase. This value is then used to determine the closest match among the
1617
* provided candidates during dependency resolution.
18+
*
19+
* @param currentOsAttributeValue the [OperatingSystemFamily] attribute value corresponding
20+
* to the current operating system.
21+
* @param objects the Gradle [ObjectFactory].
1722
*/
18-
open class CurrentOsAttributeDisambiguationRule :
19-
AttributeDisambiguationRule<OperatingSystemFamily> {
20-
21-
companion object {
22-
23-
/**
24-
* Represents the [OperatingSystemFamily] attribute value corresponding
25-
* to the current operating system. This variable must be initialized
26-
* during the Gradle project configuration phase to ensure that
27-
* dependency resolution prioritizes candidates that match the current
28-
* operating system's attribute.
29-
*/
30-
lateinit var currentOsAttributeValue: OperatingSystemFamily
31-
}
23+
open class CurrentOsAttributeDisambiguationRule @Inject constructor(
24+
private val currentOsAttribute: OperatingSystemFamily,
25+
) : AttributeDisambiguationRule<OperatingSystemFamily> {
3226

3327
override fun execute(
3428
details: MultipleCandidatesDetails<OperatingSystemFamily>
3529
) {
36-
details.closestMatch(currentOsAttributeValue)
30+
if (details.candidateValues.contains(currentOsAttribute)) {
31+
details.closestMatch(currentOsAttribute)
32+
}
3733
}
3834
}

0 commit comments

Comments
 (0)