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.0.20-RC
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.TaskProvider
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.sources.applyLanguageSettingsToKotlinTask

/**
 * Registers the task with [name] and [type] and initialization script [body]
 */
@JvmName("registerTaskOld")
@Deprecated("please use Project.createOrRegisterTask", ReplaceWith("project.createOrRegisterTask(name, 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)
): TaskProvider =
    [email protected](name, T::class.java, args, body)

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


/**
 * 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) ?: registerTask(project, name, T::class.java, body)
}

internal open class KotlinTasksProvider(val targetName: String) {
    open fun registerKotlinJVMTask(
        project: Project,
        name: String,
        compilation: AbstractKotlinCompilation<*>,
        configureAction: (KotlinCompile) -> (Unit)
    ): TaskProvider {
        val properties = PropertiesProvider(project)
        val taskClass = taskOrWorkersTask(properties)
        val result = registerTask(project, name, taskClass) {
            configureAction(it)
        }
        configure(result, project, properties, compilation)
        return result
    }

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

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

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy