A similar question has already been posted in june 2020 without a solution.
In my buildSrc/src/main/kotlin/version-convention.gradle.kts
convention plugin I have the following function:
fun prefixFrom(name: String): String =
name.replace("_", "-") // simplified
I would like to use this function a few lines later:
open class GitVersionExtension @Inject constructor(
project: Project
) {
val prefix: Property<String> = project.objects.property(String::class.java)
.convention(project.provider {
prefixFrom(project.name)
})
}
This fails with a java.lang.ArrayIndexOutOfBoundsException
.
1 Like
Because the function is called in the project context I have to pass it using the extra
functionality:
fun prefixFrom(name: String): String =
name.replace("_", "-")
// Put the function into the extra variables:
val gitVersionPrefixFrom by extra<(String) -> String>(::prefixFrom)
open class GitVersionExtension @Inject constructor(
project: Project
) {
val prefix: Property<String> = project.objects.property(String::class.java)
.convention(project.provider {
// retrieve the function from the extra variables:
val gitVersionPrefixFrom: (String) -> String by project.extra
gitVersionPrefixFrom(project.name)
})
}
I don’t think that it is possible to hide the function from users of the plugin.