Is there a way to write an RepositoryHandler exten...
# community-support
h
Is there a way to write an RepositoryHandler extension function in groovy? I have this kotlin extension function, but I want to use it in a groovy gradle buildscript.
Copy code
fun RepositoryHandler.myGithubPackages(
    repoName: String,
    contentFilter: InclusiveRepositoryContentDescriptor.() -> Unit
) = exclusiveContent {
    forRepositories(
        maven {
            name = "${repoName}Packages"
            url = uri("<https://maven.pkg.github.com/myOrg/$repoName>")
        }
    )
    filter(contentFilter)
}

// Example Usage
repositories {
    myGithubPackages("myRepo") {
        includeGroup("artifact.group")
    }
}
v
Extension functions are a Kotlin thing. For a similar Groovy thing you would need to write a category class with that function and manually use it. Better use Gradle facilities that properly work cross-DSL. For example add an extension with that function to the repository handlers where you want it to be available.
h
Thanks @Vampire, any suggestions on what I could use to work cross DSL?
v
I already did. The mobile app just mixed up my message
thank you 1
h
Copy code
allprojects {
    repositories.ext.myPackages = { repoName, contentFilter ->
        repositories.exclusiveContent {
            forRepository(repositories.maven {
                name = "${repoName}-packages"
                url = uri("<https://maven.pkg.github.com/myName/$repoName>")
                credentials {
                    username = guser
                    password = gtoken
                }
            })
            filter(contentFilter)
        }
    }
@Vampire is this close to what you were suggesting?
v
No,
ext
are "extra" properties. Using those is practically always a work-around for not doing something properly. And besides that,
allprojects { ... }
and other means of doing cross-project configuration is highly discouraged as it adds project coupling and works against some more sophisticated features and optimizations.
What I said was, create an extension with that function and add that as extension, then you also get according accessors for Kotlin DSL.
h
Is it possible to provide a doc link or example? Really appreciate your help.
v
Copy code
abstract class MyExtension(val repositories: RepositoryHandler) : ExtensionAware {
    fun myPackages() {
        repositories.maven("<https://foo.example.com>")
    }
}

(repositories as ExtensionAware).extensions.create<MyExtension>("myExtension", repositories)
=>
Copy code
repositories {
    myExtension.myPackages()
    myExtension {
        myPackages()
    }
}
thank you 1