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

com.intershop.gradle.icm.tasks.WriteCartridgeDescriptor.kt Maven / Gradle / Ivy

There is a newer version: 6.2.1
Show newest version
/*
 * Copyright 2019 Intershop Communications AG.
 *
 * 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 com.intershop.gradle.icm.tasks

import com.intershop.gradle.icm.ICMBasePlugin.Companion.CONFIGURATION_CARTRIDGE
import com.intershop.gradle.icm.ICMBasePlugin.Companion.CONFIGURATION_CARTRIDGERUNTIME
import com.intershop.gradle.icm.extension.IntershopExtension.Companion.INTERSHOP_GROUP_NAME
import com.intershop.gradle.icm.utils.CartridgeUtil
import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFile
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 org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.internal.util.PropertiesUtils
import java.io.File
import java.nio.charset.Charset
import java.util.*
import javax.inject.Inject

/**
 * WriteCartridgeDescriptor Gradle task 'writeCartridgeDescriptor'
 *
 * This task writes a cartridge descriptor file. This file
 * is used by the server startup and special tests.
 */
open class WriteCartridgeDescriptor
        @Inject constructor( projectLayout: ProjectLayout,
                             objectFactory: ObjectFactory ) : DefaultTask() {

    private val outputFileProperty: RegularFileProperty = objectFactory.fileProperty()
    private val nameProperty: Property = objectFactory.property(String::class.java)
    private val descriptionProperty: Property = objectFactory.property(String::class.java)
    private val displayNameProperty: Property = objectFactory.property(String::class.java)

    companion object {
        const val DEFAULT_NAME = "writeCartridgeDescriptor"
        const val CARTRIDGE_DESCRIPTOR = "descriptor/cartridge.descriptor"
    }

    init {
        group = INTERSHOP_GROUP_NAME
        description = "Writes all necessary information of the cartridge to a file."

        outputFileProperty.convention(projectLayout.buildDirectory.file(CARTRIDGE_DESCRIPTOR))

        nameProperty.convention(project.name)
        displayNameProperty.convention(project.description ?: project.name)
    }

    /**
     * Set provider for descriptor cartridge name property.
     *
     * @param cartridgeName set provider for cartridge name.
     */
    @Suppress( "unused")
    fun provideCartridgeName(cartridgeName: Provider) = nameProperty.set(cartridgeName)

    @get:Input
    var cartridgeName: String
        get() = nameProperty.get()
        set(value) = nameProperty.set(value)

    /**
     * Set provider for descriptor display name property.
     *
     * @param displayName set provider for display name.
     */
    @Suppress( "unused")
    fun provideDisplayName(displayName: Provider) = displayNameProperty.set(displayName)

    @get:Input
    var displayName: String
        get() = displayNameProperty.get()
        set(value) = displayNameProperty.set(value)


    @get:Input
    val cartridgeDependencies: List by lazy {
        val returnDeps = mutableListOf()
        project.configurations.getByName(CONFIGURATION_CARTRIDGE).dependencies.forEach {
            returnDeps.add(it.toString())
        }
        returnDeps
    }

    @get:Input
    val cartridgeRuntimeDependencies: List by lazy {
        val returnDeps = mutableListOf()
        project.configurations.getByName(CONFIGURATION_CARTRIDGERUNTIME).dependencies.forEach {
            returnDeps.add(it.toString())
        }
        returnDeps
    }

    /**
     * Provides an output file for this task.
     *
     * @param outputfile
     */
    fun provideOutputfile(outputfile: Provider) = outputFileProperty.set(outputfile)

    /**
     * Output file for generated cluster id.
     *
     * @property outputFile
     */
    @get:OutputFile
    var outputFile: File
        get() = outputFileProperty.get().asFile
        set(value) = outputFileProperty.set(value)

    /**
     * Task method for the creation of a descriptor file.
     */
    @TaskAction
    fun runFileCreation() {
        if(! outputFile.parentFile.exists()) {
            outputFile.parentFile.mkdirs()
        }

        val props = linkedMapOf()
        val comment = "Intershop descriptor file"

        val cartridges = HashSet()
        val noCartridges = HashSet()
        val cartridgesTransitive = HashSet()

        addCartridges(CONFIGURATION_CARTRIDGE, cartridges)
        addCartridges(CONFIGURATION_CARTRIDGERUNTIME, cartridges)

        addTransitiveCartridges(CONFIGURATION_CARTRIDGERUNTIME, cartridgesTransitive)

        cartridges.forEach {
            if(! cartridgesTransitive.contains(it)) {
                logger.info("It seems, that {} is not a cartridge!", it)
                noCartridges.add(it)
            }
        }

        cartridges.removeAll(noCartridges)

        props["cartridge.dependsOn"] = cartridges.toSortedSet().joinToString( separator = ";" )
        props["cartridge.dependsOn.transitive"] = cartridgesTransitive.toSortedSet().joinToString( separator = ";" )

        props["cartridge.name"] = cartridgeName
        props["cartridge.displayName"] = displayName
        props["cartridge.description"] = displayName
        props["cartridge.version"] = project.version.toString()

        val propsObject = Properties()
        propsObject.putAll(props)
        try {
            PropertiesUtils.store(
                propsObject,
                outputFile,
                comment,
                Charset.forName("ISO_8859_1"),
                "\n"
            )
        } finally {
            project.logger.debug("Write properties finished not correct.")
        }
    }

    private fun addCartridges(confName: String, cartridges: HashSet) {
        project.configurations.getByName(confName).allDependencies.forEach { dependency ->
            if(dependency is ModuleDependency) {
                cartridges.add(dependency.name)
            }
        }
    }

    private fun addTransitiveCartridges(confName: String, cartridges: HashSet) {
        project.configurations.getByName(confName).
        resolvedConfiguration.lenientConfiguration.allModuleDependencies.forEach { dependency ->
            dependency.moduleArtifacts.forEach { artifact ->

                val identifier = artifact.id.componentIdentifier
                if(identifier is ProjectComponentIdentifier) {
                    cartridges.add(identifier.projectName)
                }

                if(identifier is ModuleComponentIdentifier) {
                    if(CartridgeUtil.isCartridge(project, identifier)) {
                        cartridges.add(identifier.module)
                    }
                }
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy