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

org.jetbrains.kotlin.gradle.utils.providerApiUtils.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.gradle.utils

import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import java.io.File
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

internal operator fun  Provider.getValue(thisRef: Any?, property: KProperty<*>) = get()

internal operator fun  Property.setValue(thisRef: Any?, property: KProperty<*>, value: T) {
    set(value)
}

private class OptionalProviderDelegate(private val provider: Provider) : ReadOnlyProperty {
    override fun getValue(thisRef: Any?, property: KProperty<*>): T? =
        if (provider.isPresent)
            provider.get()
        else null
}

internal fun  Project.optionalProvider(initialize: () -> T?): ReadOnlyProperty =
    OptionalProviderDelegate(provider(initialize))

internal fun  Project.newProperty(initialize: (() -> T)? = null): Property =
    @Suppress("UNCHECKED_CAST")
    (project.objects.property(Any::class.java) as Property).apply {
        if (initialize != null)
            set(provider(initialize))
    }

internal inline fun  ObjectFactory.property() = property(T::class.java)

internal inline fun  ObjectFactory.property(initialValue: T) = property().value(initialValue)

internal inline fun  ObjectFactory.property(initialValue: Provider) = property().value(initialValue)

internal inline fun  ObjectFactory.propertyWithConvention(
    conventionValue: Provider
) = property().convention(conventionValue)

internal inline fun  ObjectFactory.propertyWithConvention(
    conventionValue: T
) = property().convention(conventionValue)

internal inline fun  ObjectFactory.providerWithLazyConvention(
    noinline lazyConventionValue: () -> T
) = property(lazyConventionValue).map { it.invoke() }

internal inline fun  ObjectFactory.newInstance() = newInstance(T::class.java)

internal inline fun  ObjectFactory.newInstance(vararg parameters: Any) =
    newInstance(T::class.java, *parameters)

internal inline fun  ObjectFactory.propertyWithNewInstance(
    vararg parameters: Any
) = propertyWithConvention(newInstance(T::class.java, *parameters))

internal inline fun  ObjectFactory.propertyWithNewInstance() =
    propertyWithConvention(newInstance())

internal fun > T.chainedFinalizeValueOnRead(): T =
    apply {
        finalizeValueOnRead()
    }

// Before 5.0 fileProperty is created via ProjectLayout
// https://docs.gradle.org/current/javadoc/org/gradle/api/model/ObjectFactory.html#fileProperty--
internal fun Project.newFileProperty(initialize: (() -> File)? = null): RegularFileProperty {
    val regularFileProperty = project.objects.fileProperty()

    return regularFileProperty.apply {
        if (initialize != null) {
            set(project.layout.file(project.provider(initialize)))
        }
    }
}

internal fun Project.filesProvider(provider: () -> Any) : ConfigurableFileCollection {
    return project.files(project.provider(provider))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy