ciscorucinski
10/16/2020, 12:51 PMval pluginGroup: String by project
This will search for .property files for the key pluginGroup
and return it's value as a String
. I am impressed by that, but how the heck does this magic work?
This will only return String
values, so even if we have the following in the .properties file...
platformDownloadSources = true
It will still only return "true"
as a String
.
I am curious of how this mechanism works and why it is limited to only String
and why it is limited to the specific name I have for the variable. Meaning I must name my variable the same as is in the .properties file.
This is something that I know works, but I just can't comprehend it.
For background:
To see this in usage, head over to GitHub: JetBrains/intellij-platform-plugin-template
Here is gradle.properties and here is build.gradle.ktsciscorucinski
10/16/2020, 12:54 PMProperties
are implemented as a Map
: https://kotlinlang.org/docs/reference/delegated-properties.html#storing-properties-in-a-map
Also, that ExtensionContainerExtensions.kt defines getValue()
on ExtensionContainer
from which Project
is.
I am trying to understand all of this (including the above), but this only seems to explain why by project
is valid and doesn't address the other pointseskatos
10/16/2020, 1:04 PMciscorucinski
10/16/2020, 1:27 PMgetValue(...)
. Again, this is still a little fuzzy, but I can follow that to a degree.
But where I cannot follow at all is when it comes to by project
and I assume the respectful by settings
and by gradle
approach.
I was trying to follow the code via the IDE, and the ExtensionContainerExtensions.kt file I was told to look at, but I don't see the connection.ciscorucinski
10/16/2020, 1:30 PMString
/ String?
and why it is tied to the name of the property key.ciscorucinski
10/16/2020, 1:33 PMeskatos
10/16/2020, 1:33 PMeskatos
10/16/2020, 1:36 PMString
properties as can be seen in this test https://github.com/gradle/gradle/blob/master/subprojects/kotlin-dsl/src/test/kotlin/org/gradle/kotlin/dsl/DelegatedGradlePropertiesExtensionsTest.ktciscorucinski
10/16/2020, 1:39 PMciscorucinski
10/16/2020, 1:44 PMString
and Int
are fully supported. While it does allow Any
those all appear to expect failures (unless I am overlooking it). But Boolean
is not supported at all??ciscorucinski
10/16/2020, 1:47 PMval platformDownloadSources: Boolean by project
gradle.properties
platformDownloadSources = true
This is something that is currently impossibleciscorucinski
10/16/2020, 1:53 PMpluginName
is already used by the intellij {}
block for the IntelliJ Gradle Plugin.
Is this a pure Kotlin issue, or can it be at least partially solved with the Gradle codebase?eskatos
10/16/2020, 2:03 PMval someName = project.findProperty("name")
ciscorucinski
10/16/2020, 2:26 PMciscorucinski
10/16/2020, 2:27 PMval name: String by project("pluginName")
// instead of
val name = project.findProperty("pluginName")
The syntax is probably wrong, but the fundamental idea is to allow developers one generic way by project
whether there is a name conflict or not. Rather than by project
for one way and project.findProperty(...)
for the other way.
Basically, I am wondering can Gradle implement something like this (whether they will or won't isn't the question) or is this a limitation of Kotlin? Again, syntax is probably wrongeskatos
10/16/2020, 2:37 PMciscorucinski
10/16/2020, 2:56 PMciscorucinski
10/16/2020, 6:27 PMby project
.
Just something I seem to have stumbled on ... while the mechanism should allow for any return type, as you have shown in the tests (including the missing Boolean), in practice it seems to only return the String
type.
It appears the tests never lose the passed in Kotlin type, where as, real world use cases of grabbing the values from .properties files will never return Int
, Boolean
, or any value type besides String
. Basically, it seems to just assume the values are all String
.
I have been trying out the IntelliJ Platform Plugin Template (to make sure there aren't any other issues). Created a new .properties key-value with a value that should be converted to an Int
.
// gradle.properties
integer = 1
// build.gradle.kts
val integer: Any by project // Using Int will cause an error
println("Integer: ${integer}\tType: ${integer.javaClass.name}")
// Result >> Integer: 1 Type: java.lang.String
I found a lot of the code you wrote that allows for retrieving of values from properties files via property delegation here ...
https://github.com/gradle/gradle/commit/27e7819c2ddd11d38b06b33cb7c22bec8f509294
(Side Note: Where is the code that actually pulls the values from properties files? I assume there is more to it than the above commit. It seems like import org.gradle.kotlin.dsl.support.getPropertyValue
is needed for understanding and is key here, but I can't seem to find it. )eskatos
10/20/2020, 9:32 AMString
eskatos
10/20/2020, 9:32 AMeskatos
10/20/2020, 9:32 AMciscorucinski
10/20/2020, 3:51 PMeskatos
10/20/2020, 9:05 PM