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

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

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2020 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 java.util.*

/**
 * Base implementation of an actions executor that executes them once.
 */
internal abstract class SingleAction {
    private val performedActions = WeakHashMap>()

    /**
     * Calculates a part of a key that is used to determine whether an action from [run] was already executed
     */
    protected abstract fun selectKey(project: Project): Project

    /**
     * Runs an [action] once per key value which is being calculated as a combination of a [selectKey] value and an [actionId]
     *
     * Warning: if KGP is loaded multiple times by different classloaders, actions with the same [actionId] may be executed more than once
     */
    fun run(project: Project, actionId: String, action: () -> Unit) {
        val performedActions = performedActions.computeIfAbsent(selectKey(project)) { mutableSetOf() }
        if (performedActions.add(actionId)) {
            action()
        }
    }
}

/**
 * Object that allows to run actions once per build
 *
 * Warning: if KGP is loaded multiple times by different classloaders, actions with the same id may be executed more than once
 */
internal object SingleActionPerBuild : SingleAction() {
    override fun selectKey(project: Project): Project = project.rootProject
}

/**
 * Object that allows to run actions once per project
 *
 * Warning: if KGP is loaded multiple times by different classloaders, actions with the same id may be executed more than once
 */
internal object SingleActionPerProject : SingleAction() {
    override fun selectKey(project: Project) = project
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy