All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.gradle_plugins.dsl.extensions.kotlin.Any.kt Maven / Gradle / Ivy

package name.remal.gradle_plugins.dsl.extensions

import groovy.lang.Closure
import name.remal.lambda.Function1
import name.remal.lambda.VoidFunction1
import name.remal.orNull
import name.remal.uncheckedCast
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.repositories.ArtifactRepository
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.internal.HasConvention
import org.gradle.api.plugins.Convention
import org.gradle.api.provider.Provider
import java.lang.reflect.Array
import java.util.*
import java.util.concurrent.Callable
import java.util.concurrent.CompletionStage
import java.util.concurrent.Future
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.atomic.AtomicReference
import java.util.function.*
import java.util.function.Function as JFunction
import kotlin.Function1 as KFunction1
import kotlin.reflect.KFunction1 as KReflectFunction1

val Any.convention: Convention
    get() = when (this) {
        is Project -> this.convention
        is Task -> this.convention
        else -> (this as HasConvention).convention
    }


data class ConventionWithSelf(val convention: Convention, val self: T)

val  T.conventionWithSelf: ConventionWithSelf get() = ConventionWithSelf(this.convention, this)


fun  T.configureWith(configurer: Any?): T = apply {
    if (configurer == null) {
        // do nothing
    } else if (configurer is Action<*>) {
        configurer.uncheckedCast>().execute(this)
    } else if (configurer is Closure<*>) {
        configurer.toConfigureAction().execute(this)
    } else if (configurer is Consumer<*>) {
        configurer.uncheckedCast>().toConfigureAction().execute(this)
    } else if (configurer is JFunction<*, *>) {
        configurer.uncheckedCast>().toConfigureAction().execute(this)
    } else if (configurer is KFunction1<*, *>) {
        configurer.uncheckedCast>().toConfigureAction().execute(this)
    } else if (configurer is KReflectFunction1<*, *>) {
        configurer.uncheckedCast>().toConfigureAction().execute(this)
    } else if (configurer is Function1<*, *>) {
        configurer.uncheckedCast>().toConfigureAction().execute(this)
    } else if (configurer is VoidFunction1<*>) {
        configurer.uncheckedCast>().toConfigureAction().execute(this)
    } else {
        throw IllegalArgumentException("Unsupported configurer: ${configurer.javaClass.name}")
    }
}


fun Any?.unwrapProviders(): Any? = when (this) {
    is Optional<*> -> this.orNull.unwrapProviders()
    is Provider<*> -> this.orNull.unwrapProviders()
    is Callable<*> -> this.call().unwrapProviders()
    is Supplier<*> -> this.get().unwrapProviders()
    is BooleanSupplier -> this.asBoolean.unwrapProviders()
    is IntSupplier -> this.asInt.unwrapProviders()
    is LongSupplier -> this.asLong.unwrapProviders()
    is DoubleSupplier -> this.asDouble.unwrapProviders()
    is Future<*> -> this.get().unwrapProviders()
    is CompletionStage<*> -> this.toCompletableFuture().get().unwrapProviders()
    is AtomicBoolean -> this.get().unwrapProviders()
    is AtomicInteger -> this.get().unwrapProviders()
    is AtomicLong -> this.get().unwrapProviders()
    is AtomicReference<*> -> this.get().unwrapProviders()
    is Lazy<*> -> this.value.unwrapProviders()
    is Closure<*> -> this.call().unwrapProviders()
    else -> this
}


fun Any?.toStringSmart(): String {
    if (this == null) return "null"

    if (this.javaClass.isArray) {
        return buildString {
            append("[")
            (0 until Array.getLength(this@toStringSmart)).joinTo(this, ", ", transform = { Array.get(this@toStringSmart, it).toStringSmart() })
            append("]")
        }
    }

    if (this is Iterable<*>) {
        return buildString {
            append("[")
            [email protected](this, ", ", transform = { it.toStringSmart() })
            append("]")
        }
    }

    val toStringDeclaringClass = this.javaClass.getMethod("toString").declaringClass
    if (Any::class.java == toStringDeclaringClass || toStringDeclaringClass.isInterface) {
        when (this) {
            is MavenArtifactRepository -> return "$name($url)"
            is ArtifactRepository -> return name
        }
    }

    return toString()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy