Slackbot
11/13/2022, 8:13 PMChris Lee
11/13/2022, 8:18 PMpublic fun Project.resolveProperty(name : String) : String? {
return (extra[name]?: System.getenv(name)) as String?
}
Stylianos Gakis
11/13/2022, 8:21 PMextra[""] ?:
Doesn't work since extra.get throws an exception, doesn't default to null when there's no such key found.
Also I'd optimally like to use this inside my settings file too, where I don't have access to Project
afaik.
As a side note, where would you host such an extension function so that it's available throughout your entire project? For all modules?Chris Lee
11/13/2022, 8:23 PMbuild-logic
project (successor to buildSrc).Stylianos Gakis
11/13/2022, 8:27 PMVampire
11/13/2022, 9:02 PMproject.findProperty
? That should automatically give you what you want. At least if the env variable does not have to be the exact same name, as there is a naming convention how to set a Gradle property.Stylianos Gakis
11/13/2022, 9:14 PMORG_GRADLE_PROJECT
suffix right?Vampire
11/13/2022, 9:24 PMfindProperty
. And yes about the naming convention.grossws
11/14/2022, 12:09 PMval propertyName: String? by project
or by settings
when applicable. Requires use of a naming convention (gradle property must be valid kotlin identifier then) and supports setting via env var in the same way as above.Stylianos Gakis
11/14/2022, 12:14 PMproviders.gradleProperty("KEY").getOrElse(System.getenv("KEY"))
.
I didn’t know this settings
existed. If I did this by settings
and it only exists in the environment variables (true for our CI) would it work? Or would I still need to fallback to null, and do System.getEnv() anyway?Vampire
11/14/2022, 12:15 PMgrossws
11/14/2022, 12:15 PMVampire
11/14/2022, 12:16 PMgrossws
11/14/2022, 12:19 PMVampire
11/14/2022, 12:21 PMORG_GRADLE_PROJECT_propertyName
works fine, also in settings script, I quickly triedgrossws
11/14/2022, 12:21 PMStylianos Gakis
11/14/2022, 12:23 PMproviders.gradleProperty("KEY").getOrElse(System.getenv("KEY"))
to
providers.gradleProperty("KEY").getOrElse(providers.environmentVariable("KEY").get()).
Interesting!
For the by settings
case, awesome to hear that it works, thanks for checking 🙌
Is there a way I can do this more inline instead of having to assign it to a variable using by
?
Just so that it doesn’t have to do something like this
val x: String by settings
this.foo = x
Vampire
11/14/2022, 12:25 PMgrossws
11/14/2022, 12:25 PMVampire
11/14/2022, 12:26 PMproviders.gradleProperty
in a build script, you will not get the extra properties considered.Stylianos Gakis
11/14/2022, 12:27 PMVampire
11/14/2022, 12:27 PMStylianos Gakis
11/14/2022, 12:29 PM