/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.
 */

/*
    This plugin declares a component type corresponding to the "groovy-all" component.
    Groovy all is basically a platform which aggregates _some_ of the Groovy modules,
    but not all. In that sense it's different from the Groovy BOM, which is a platform
    which references all Groovy modules.

    It also aggregates docs and sources but does NOT provide an "uber" jar.
 */
import org.apache.groovy.gradle.GroovyLibraryExtension

plugins {
    id 'org.apache.groovy-common'
    id 'org.apache.groovy-platform'
    id 'org.apache.groovy-documented'
    id 'org.apache.groovy-doc-aggregator'
}

javaPlatform {
    allowDependencies()
}

configurations {
    allSources {
        extendsFrom runtimeElements
    }
    allSourcesRuntimeClasspath {
        extendsFrom runtimeElements
    }
}

def projectsIncludedInGroovyAll = rootProject.allprojects.findAll {
    if (it.pluginManager.hasPlugin('org.apache.groovy-base')) {
        return it.extensions.getByType(GroovyLibraryExtension).includeInGroovyAll.get()
    }
    return false
}

dependencies {
    projectsIncludedInGroovyAll.each {
        api(it)
    }
}

def createDocumentationPublication = { String name, TaskProvider artifact ->
    def outgoing = configurations.create("${name}Elements") {
        it.canBeConsumed = true
        it.canBeResolved = false
        it.attributes {
            it.attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
            it.attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.EXTERNAL))
            it.attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, name))
            it.attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
        }
        it.outgoing {
            it.artifact artifact
        }
    }
    components.javaPlatform {
        addVariantsFromConfiguration(outgoing) {
            mapToOptional()
        }
    }
}

createDocumentationPublication('sources', tasks.named('sourcesAllJar'))
createDocumentationPublication('javadoc', tasks.named('javadocAllJar'))
createDocumentationPublication('groovydoc', tasks.named('groovydocAllJar'))

configurations {
    allJavadoc {
        canBeConsumed = true
        canBeResolved = false
        attributes {
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
            attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, DocsType.JAVADOC))
        }
        outgoing {
            artifacts {
                artifact tasks.named('javadocAll')
            }
        }
    }
    allGroovydoc {
        canBeConsumed = true
        canBeResolved = false
        attributes {
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
            attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, "groovydoc"))
        }
        outgoing {
            artifacts {
                artifact tasks.named('groovydocAll')
            }
        }
    }
    // GROOVY-10543 - The platform configurations are published as libraries
    apiElements {
        attributes {
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
            attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements, LibraryElements.JAR))
            attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.EXTERNAL))
            attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_API))
        }
    }
    runtimeElements {
        attributes {
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
            attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements, LibraryElements.JAR))
            attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.EXTERNAL))
            attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
        }
    }
}
