I have a MapProperty<String, String>. How ca...
# declarative-gradle
p
I have a MapProperty<String, String>. How can I assign a String key with a Gradle Property as a value:
myMap += mapOf(„foo“ to providers.gradleProperty(„bar“))
v
It's it not like in non-dcl?
myMap.put(...)
Probably not, I should play with dcl myself a bit. :-D
p
Nope, the whole MapProperty is marked as
DeclarativeWithHiddenMembers
, so only the type is available with no members. And only a subset of stdlib functions are available (
mapOf
,
listOf
, `to`: https://github.com/gradle/gradle/blob/94e17d7dc67cff8de74938855042393a5e165cd4/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/common/StandardLibraryComponent.kt), but I should just look at the sources and the error message:
providers
is not available. And turns out,
project.providers
is also marked as
HiddenInDefinition
and there is no intrinsic
providers
like
layout
. As a workaround, I use this implementation:
Copy code
// Before, safe definition
interface JavaDclForkOptions {
    val environment: MapProperty<String, String>
}

// Workaround, unsafe definition
abstract class JavaDclForkOptions {
    @get:HiddenInDefinition
    @get:Inject
    internal abstract val providers: ProviderFactory

    abstract val environment: MapProperty<String, String>

    @Adding
    fun environmentOfProperty(key: String, propertyName: String) {
        environment.put(key, providers.gradleProperty(propertyName))
    }
}
j
No
append
I guess
t
I would say
gradleProperty
should not be part of definition/available in it. And if you want to set map value from Gradle property - you need to create custom software feature
p
I am okay with that decision too, but only iff https://github.com/gradle/gradle/issues/36168 is fixed, because creating a custom software feature for that (IMHO) basic feature requires to much glue code.
t
yeah, requirement to create
Plugin
doesn't make sense and we've discussed it, so most probably it will go away
basically
Binding
+ `ProjectFeatureApplyAction`s are only needed
j
I would like to be able to apply a feature to all project types and/or features without having to do a massive codegen. I mean, if I want to pass a CLI parameter potentially to any
Provider<String>
and I need to create a custom feature for each feature, that would be madness. The same for environment variables.
t
this is more requirement to ecosystem plugins than DG. If they are using some common interfaces for the same functionality in different project types - you will be able to write your software feature only once