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

org.jetbrains.kotlin.gradle.tasks.TasksProvider.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.kotlin.gradle.tasks

import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.UnknownTaskException
import org.gradle.api.tasks.TaskCollection
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.mapKotlinTaskProperties
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData
import org.jetbrains.kotlin.gradle.plugin.runOnceAfterEvaluated
import org.jetbrains.kotlin.gradle.plugin.sources.applyLanguageSettingsToKotlinOptions
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLinkWithWorkers

/**
 * Registers the task with [name] and [type] and initialization script [body]
 */
@JvmName("registerTaskOld")
@Deprecated("please use Project.registerTask", ReplaceWith("project.registerTask(name, type, emptyList(), body)"))
internal fun  registerTask(project: Project, name: String, type: Class, body: (T) -> (Unit)): TaskProvider =
    project.registerTask(name, type, emptyList(), body)

internal inline fun  Project.registerTask(
    name: String,
    args: List = emptyList(),
    noinline body: ((T) -> (Unit))? = null
): TaskProvider =
    [email protected](name, T::class.java, args, body)

internal fun  Project.registerTask(
    name: String,
    type: Class,
    constructorArgs: List = emptyList(),
    body: ((T) -> (Unit))? = null
): TaskProvider {
    val resultProvider = project.tasks.register(name, type, *constructorArgs.toTypedArray())
    if (body != null) {
        resultProvider.configure(body)
    }
    return resultProvider
}

internal fun TaskProvider<*>.dependsOn(other: TaskProvider<*>) = configure { it.dependsOn(other) }

internal inline fun  TaskCollection.withType(): TaskCollection = withType(S::class.java)

/**
 * Locates a task by [name] and [type], without triggering its creation or configuration.
 */
internal inline fun  Project.locateTask(name: String): TaskProvider? =
    try {
        tasks.withType(T::class.java).named(name)
    } catch (e: UnknownTaskException) {
        null
    }

/**
 * Locates a task by [name] and [type], without triggering its creation or configuration or registers new task
 * with [name], type [T] and initialization script [body]
 */
internal inline fun  Project.locateOrRegisterTask(name: String, noinline body: (T) -> (Unit)): TaskProvider {
    return project.locateTask(name) ?: project.registerTask(name, T::class.java, body = body)
}

internal open class KotlinTasksProvider {
    open fun registerKotlinJVMTask(
        project: Project,
        name: String,
        compilation: KotlinCompilationData<*>,
        configureAction: (KotlinCompile) -> (Unit)
    ): TaskProvider {
        val properties = PropertiesProvider(project)
        val taskClass = taskOrWorkersTask(properties)
        val kotlinCompile = project.registerTask(name, taskClass, constructorArgs = listOf(compilation.kotlinOptions))

        val configurator = KotlinCompile.Configurator(compilation, properties)
        @Suppress("UNCHECKED_CAST")
        configurator.runAtConfigurationTime(kotlinCompile as TaskProvider, project)

        kotlinCompile.configure {
            configureAction(it)
            configurator.configure(it)
        }
        configure(kotlinCompile, project, properties, compilation)

        return kotlinCompile
    }

    fun registerKotlinJSTask(
        project: Project,
        name: String,
        compilation: KotlinCompilationData<*>,
        configureAction: (Kotlin2JsCompile) -> Unit
    ): TaskProvider {
        val properties = PropertiesProvider(project)
        val taskClass = taskOrWorkersTask(properties)
        val result = project.registerTask(name, taskClass, constructorArgs = listOf(compilation.kotlinOptions)) {
            configureAction(it)
            Kotlin2JsCompile.Configurator(compilation).configure(it)
        }
        configure(result, project, properties, compilation)
        return result
    }

    fun registerKotlinJsIrTask(
        project: Project,
        name: String,
        compilation: KotlinCompilationData<*>,
        configureAction: (KotlinJsIrLink) -> Unit
    ): TaskProvider {
        val properties = PropertiesProvider(project)
        val taskClass = taskOrWorkersTask(properties)
        val result = project.registerTask(name, taskClass) {
            it.compilation = compilation
            configureAction(it)
            KotlinJsIrLink.Configurator(compilation).configure(it)
        }
        configure(result, project, properties, compilation)
        return result
    }

    fun registerKotlinCommonTask(
        project: Project,
        name: String,
        compilation: KotlinCompilationData<*>,
        configureAction: (KotlinCompileCommon) -> (Unit)
    ): TaskProvider {
        val properties = PropertiesProvider(project)
        val taskClass = taskOrWorkersTask(properties)
        val result = project.registerTask(name, taskClass, constructorArgs = listOf(compilation.kotlinOptions)) {
            configureAction(it)
            KotlinCompileCommon.Configurator(compilation).configure(it)
        }
        configure(result, project, properties, compilation)
        return result
    }

    open fun configure(
        kotlinTaskHolder: TaskProvider>,
        project: Project,
        propertiesProvider: PropertiesProvider,
        compilation: KotlinCompilationData<*>
    ) {
        project.runOnceAfterEvaluated("apply properties and language settings to ${kotlinTaskHolder.name}", kotlinTaskHolder) {
            propertiesProvider.mapKotlinTaskProperties(kotlinTaskHolder.get())

            applyLanguageSettingsToKotlinOptions(
                compilation.languageSettings,
                (kotlinTaskHolder.get() as org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>).kotlinOptions
            )
        }
    }

    private inline fun  taskOrWorkersTask(properties: PropertiesProvider): Class =
        if (properties.parallelTasksInProject != true) Task::class.java else WorkersTask::class.java
}

internal class AndroidTasksProvider : KotlinTasksProvider() {
    override fun configure(
        kotlinTaskHolder: TaskProvider>,
        project: Project,
        propertiesProvider: PropertiesProvider,
        compilation: KotlinCompilationData<*>
    ) {
        super.configure(kotlinTaskHolder, project, propertiesProvider, compilation)
        kotlinTaskHolder.configure {
            it.useModuleDetection.set(true)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy