name.remal.gradle_plugins.dsl.extensions.org.gradle.api.plugins.ExtensionContainer.kt Maven / Gradle / Ivy
package name.remal.gradle_plugins.dsl.extensions
import org.gradle.api.plugins.Convention
import org.gradle.api.plugins.ExtensionContainer
@Suppress("UNCHECKED_CAST")
operator fun ExtensionContainer.get(name: String): T {
if (this is Convention) this.plugins[name]?.let { return it as T }
return this.getByName(name) as T
}
operator fun ExtensionContainer.get(type: Class): T {
if (this is Convention) this.findPlugin(type)?.let { return it }
return this.getByType(type)
}
operator fun ExtensionContainer.contains(type: Class<*>): Boolean {
if (this is Convention) if (null != this.findPlugin(type)) return true
return null != this.findByType(type)
}
operator fun ExtensionContainer.contains(name: String): Boolean {
if (this is Convention) if (name in this.plugins) return true
return null != this.findByName(name)
}
fun ExtensionContainer.add(publicType: Class, instance: T) = add(publicType, publicType.extensionName, instance)
fun ExtensionContainer.create(instanceType: Class, vararg args: Any?): T {
return create(instanceType.extensionName, instanceType, *args)
}
fun ExtensionContainer.getOrCreate(publicType: Class, instanceType: Class, extensionName: String, onCreate: (T) -> Unit = {}): T {
if (this is Convention) this.findPlugin(publicType)?.let { return it }
this.findByType(publicType)?.let { return it }
return create(publicType, extensionName, instanceType).apply(onCreate)
}
fun ExtensionContainer.getOrCreate(publicType: Class, instanceType: Class, onCreate: (T) -> Unit = {}) = getOrCreate(publicType, instanceType, publicType.extensionName, onCreate)
fun ExtensionContainer.getOrCreate(publicType: Class, extensionName: String, onCreate: (T) -> Unit = {}) = getOrCreate(publicType, publicType, extensionName, onCreate)
fun ExtensionContainer.getOrCreate(publicType: Class, onCreate: (T) -> Unit = {}) = getOrCreate(publicType, publicType, onCreate)
private val Class<*>.extensionName: String get() = "$$" + name.replace('.', '$')
© 2015 - 2025 Weber Informatics LLC | Privacy Policy