Handling `toUppercase` / `uppercase` in init.d scripts in different Gradle and kotlin version

Hi,

I have an init script in my .gradle/init.d folder, that used .toUppercase(Locale)

version.toUpperCase(Locale.getDefault())

But the build is failing with Gradle 8.3

FAILURE: Build failed with an exception.

* Where:
Initialization script '/Users/brice.dutheil/.gradle/init.d/versions.init.gradle.kts' line: 30

* What went wrong:
Script compilation error:

  Line 30:                                 .any { version.toUpperCase(Locale.ROOT)
                                                          ^ 'toUpperCase(Locale): String' is deprecated. Use uppercase() instead.

1 error

But I have projects on Gradle 7.x, which are failing if I update this to .uppercase(Locale). I have the reverse issue :

e: /Users/brice.dutheil/.gradle/init.d/versions.init.gradle.kts:31:47: Unresolved reference: uppercase

FAILURE: Build failed with an exception.

* Where:
Initialization script '/Users/brice.dutheil/.gradle/init.d/versions.init.gradle.kts' line: 31

* What went wrong:
Script compilation error:

  Line 31:                                .any { version.uppercase(Locale.getDefault())
                                                         ^ Unresolved reference: uppercase

1 error

I can use reflection there to invoke String’s toUppercase as it’s what kotlin’s uppercase do, but this seems somewhat wrong.

@SinceKotlin("1.5")
@WasExperimental(ExperimentalStdlibApi::class)
@kotlin.internal.InlineOnly
public inline fun String.uppercase(locale: Locale): String = (this as java.lang.String).toUpperCase(locale)

Note even using java String class is disallowed.

(this as java.lang.String).toUpperCase(locale)
         ^ This class shouldn't be used in Kotlin. Use kotlin.String instead.

Currently that’s my workaround.

@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
fun String.uppercase(locale: Locale): String = (this as java.lang.String).toUpperCase(locale)

Due to such problems, I usually use only Groovy DSL in general init scripts in my user home directory. Because if you for example also run builds with Gradle versions that do not yet support Kotlin DSL, the init script then would also be effective and a Kotlin DSL init script would be ignored.

Besides that, it seems you somehow enabled -Werror on init scripts, no idea where that can be done and whether that can be done project specific.
Because if I use

println("Foo".toUpperCase(Locale.ROOT))
println("Foo".uppercase(Locale.getDefault()))

with Gradle 8, both work and the build is successful, just giving a deprecation warning.
On Gradle 7 of course only the first line works as the second is not yet available in API version 1.4.

Clearly I assumed that gradle did enable that. I have no idea how to setup that either.

~/.init.d/gradle.properties

org.gradle.caching=true
#org.gradle.unsafe.watch-fs=true
org.gradle.parallel=true
#Disable because quite verbose in IntelliJ
#org.gradle.warning.mode=all

org.gradle.jvmargs=-XX:+IgnoreUnrecognizedVMOptions -XX:+UseParallelGC

fireplace.path=~/opensource/bric3/fireplace
idea.home.path=~/opensource/JetBrains/intellij-community