plugins {
    id 'java-base'
    id 'jacoco'
    id 'com.github.jk1.dependency-license-report'
    id 'biz.aQute.bnd.builder' apply false
    id 'maven-publish'
}

ext {
    leafProjects = subprojects.findAll { p -> p.subprojects.empty }
    javaProjects = leafProjects - project( ':runtime' ) - project( ':repack:repack-elasticsearch' ) - project( ':repack::repack-tika' )
    apiProjects = rootProject.subprojects.findAll { project -> project.name.endsWith( '-api' ) }
}

allprojects {
    group = 'com.enonic.xp'

    afterEvaluate {
        def subProject = it
        def hasPublishPlugins = subProject.plugins.hasPlugin( PublishingPlugin ) && subProject.plugins.hasPlugin( MavenPublishPlugin )
        def hasJavaPlugin = subProject.plugins.hasPlugin( JavaPlugin )

        if ( !hasPublishPlugins )
        {
            return
        }

        if ( hasJavaPlugin )
        {
            javadoc {
                options {
                    quiet()
                    encoding( 'UTF-8' )
                    addStringOption('Xdoclint:all,-missing', '-quiet')
                }
            }

            subProject.publishing {
                publications {
                    mavenJava( MavenPublication ) {
                        from components.java
                        suppressAllPomMetadataWarnings()
                        if ( subProject.name.startsWith( 'lib-' ) || subProject.name.endsWith( '-api' ) )
                        {
                            java {
                                withJavadocJar()
                                withSourcesJar()
                            }
                            customizePomXml( subProject, it )
                        }
                    }
                }
            }
        }

        subProject.publishing {
            repositories {
                maven {
                    credentials {
                        username = rootProject.findProperty( 'repoUser' )
                        password = rootProject.findProperty( 'repoPassword' )
                    }
                    name = "public"
                    url = "https://repo.enonic.com/${rootProject.version.endsWith( 'SNAPSHOT' ) ? 'snapshot' : 'public'}"
                }
            }
        }
    }
}

def customizePomXml( subProject, mavenJava )
{
    mavenJava.pom {
        name = subProject.name
        description = subProject.name
        url = "https://github.com/enonic/xp"
        licenses {
            license {
                def isLibrary = subProject.name.startsWith( 'lib-' )
                name = "${isLibrary ? 'The Apache Software License, Version 2.0' : 'GNU General Public License, Version 3.0'}"
                url = "https://github.com/enonic/xp/blob/master/${isLibrary ? 'LICENSE_AL.txt' : 'LICENSE.txt'}"
            }
        }
        developers {
            developer {
                id = 'developers'
                name = 'Enonic developers'
                email = 'developers@enonic.com'
            }
        }
        scm {
            connection = "scm:git:git@github.com:enonic/xp.git"
            developerConnection = "scm:git:git@github.com:enonic/xp.git"
            url = "https://github.com/enonic/xp"
        }
    }
}

configure( javaProjects ) {
    apply from: "$rootDir/gradle/java.gradle"
}

tasks.register('ci') {
    description = 'Builds and generates coverage report.'
    dependsOn = ['build', 'jacocoMergedReport']
}

tasks.register('jacocoMergedReport', JacocoReport) {
    group = 'verification'
    description = 'Generates a merged report all sub-projects.'

    def projects = subprojects.findAll { it.plugins.hasPlugin( JacocoPlugin ) }

    dependsOn projects*.jacocoTestReport

    sourceDirectories.setFrom( projects*.sourceSets*.main*.allSource )
    classDirectories.setFrom( files( projects*.sourceSets*.main*.output ) )
    executionData.setFrom(files(projects*.jacocoTestReport*.executionData))

    reports {
        html.required = true
        xml.required = true
        csv.required = false
    }
}

tasks.register('javadoc', Javadoc) { t ->
    group = 'documentation'
    title = "Enonic XP API ${version}"

    options.with {
        links "https://docs.oracle.com/en/java/javase/21/docs/api/"
        links "https://www.javadoc.io/doc/com.google.guava/guava/${libs.versions.guava.get()}/"
        quiet()
        addStringOption('Xdoclint:all,-missing', '-quiet')
        encoding('UTF-8')
    }

    apiProjects.each  { p ->
        def javaExt = p.providers.provider {
            p.extensions.getByType(JavaPluginExtension)
        }
        def mainSs = javaExt.map { it.sourceSets.getByName('main') }

        t.source(mainSs.map { it.allJava })
        t.classpath.from(mainSs.map { it.compileClasspath })

        t.dependsOn(p.tasks.named('classes'))
    }
}

tasks.register('javadocZip', Zip ) {
    from javadoc
    archiveFileName = "${project.name}-${project.version}-javadoc.zip"
    destinationDirectory = base.distsDirectory
    archiveClassifier = 'javadoc'
}


publishing {
    publications {
        mavenDocs( MavenPublication ) {
            artifact( tasks.named("javadocZip") ) {
                artifactId = 'docs'
            }
        }
    }
}
