This message was deleted.
# community-support
s
This message was deleted.
1
j
And to make things even more interesting: There could be a file with the version string in a sub-project which should be used over the "general" version file, so the hierarchy would be: Generic version file < Project-specific version file < Environment variable < Gradle property.
l
You would express that in a
Provider
chain, starting at the most important one and then do a
Provider.orElse
chain.
j
Thanks, Louis! I thought along those lines, too. How would that work with
RegularFileProperty
and checking that the files (don't) exist? I think something like this wouldn't work, would it?
Copy code
def myVersion = project.objects.fileProperty().fileValue(project.file("specific"))
  .orElse(project.objects.fileProperty().fileValue(project.file("global")))
  .map(file -> "read version string from file")
  .orElse("default")
l
I did not try the code, but it should work. However, you have to begin with the Gradle property, since it wins when present, then go down in priority order, finishing with the files and possibly a default convention.
👍 1
thank you 1
j
It's all working as advertised, thanks! 🙂
Copy code
ext {
    def env = providers.environmentVariable("MY_VERSION")
    def specificFile = providers.fileContents(layout.projectDirectory.file("specific")).asText
    def globalFile = providers.fileContents(layout.projectDirectory.file("global")).asText

    myVersion = env.orElse(specificFile.orElse(globalFile)).getOrElse("default")
}

tasks.register('printVersion') {
    it.doLast {
        project.println(myVersion)
    }
}
Example:
Copy code
# MY_VERSION=env ./gradlew -q printVersion
env
# ./gradlew -q printVersion
specific
# rm specific && ./gradlew -q printVersion
global
# rm global && ./gradlew -q printVersion
default
🎉 1