Ich versuche gradle
Plugin in kotlin
zu implementieren, die die Struktur wie diesedelegieren verschachtelt Schließung Invokation zu entsprechenden Klasse
env {
app ("ghc-haskell") {
srcLink { "http://downloads.haskell.org/~ghc/8.0.1/ghc-8.0.1-x86_64-unknown-mingw32.tar.xz" }
downloadPath { project(':').projectDir.absolutePath + "\\applications\\plugin" }
installPath { project(':').projectDir.absolutePath + "\\applications\\plugin" }
}
}
Aber ich habe ein Problem mit inneren Verschlüsse wie srcLink
, downloadPath
, installPath
unterstützen würde. Wie kann man seine Aufrufe an die entsprechende Klasse kotlin
delegieren?
Meine Erweiterung sieht wie folgt aus
/**
* Keeps all environments.
*/
open class ApplicationEnvironmentContainerExtension {
val environs = mutableMapOf<String, ApplicationEnvironmentExtension>()
fun app(id: String, closure : ApplicationEnvironmentContainerExtension.() -> String) : ApplicationEnvironmentExtension {
val extension = ApplicationEnvironmentExtension()
extension.installationId = id
environs.put(id, extension)
return extension
}
/**
* Sub extension.
*/
open class ApplicationEnvironmentExtension {
var installationId = ""
var srcLink = ""
var downloadPath = ""
var installPath = ""
fun srcLink(closure: ApplicationEnvironmentExtension.() -> String) {
srcLink = closure()
}
fun downloadPath(closure: ApplicationEnvironmentExtension.() -> String) {
downloadPath = closure()
}
fun installPath(closure: ApplicationEnvironmentExtension.() -> String) {
installPath = closure()
}
override fun toString(): String {
return "ApplicationEnvironmentExtension(" +
"installationId='$installationId', " +
"srcLink='$srcLink', " +
"downloadPath='$downloadPath', " +
"installPath='$installPath')"
}
}
}
Und wenn ich meine print
Aufgabe aufrufen Liste der Umgebung anzuzeigen es zeigt nur gesetzt installationId
- andere Felder sind leer
open class PrintingTask : DefaultTask() {
@TaskAction
fun print() {
val extension = this.project.extensions.getByName("env") as ApplicationEnvironmentContainerExtension
extension.environs.forEach {
println(it.value)
}
}
}
Wie delegieren closure
Invokation zu ApplicationEnvironmentExtension
?
In diesem Fall kehrt 'konnte nicht Methode srcLink() für Argumente [build_7r01ofigr1pt144ypsds3y84p $ _run_closure2_closure5_closure7 @ 718989fa]' – lapots
finden Was meinst du "returns"? Wird während der Laufzeit ein Kompilierfehler oder eine Ausnahme ausgelöst? – marstran
in Laufzeit. Wenn ich einige Aufgaben von meinem Plugin – lapots