/*
 * Copyright 2013-2019 the original author or authors.
 *
 * 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.
 */

// Works around missing 4.0.1 of grgit-core.
buildscript {
    dependencies {
        classpath 'org.ajoberstar.grgit:grgit-core:4.1.1'
    }
}

plugins {
    id 'net.nemerosa.versioning' version '2.15.1' apply false
    id 'com.github.ben-manes.versions' version '0.50.0' apply false
    id 'com.github.hierynomus.license' version '0.16.1' apply false
    id 'com.github.kt3k.coveralls' version '2.12.2' apply false
    id 'net.ossindex.audit' version '0.4.11' apply false
    id 'org.kordamp.jdeps' version '0.2.0' apply false
    id 'fi.linuxbox.download.worker' version '0.4' apply false
    id 'org.ysb33r.ivypot' version '1.0.0' apply false
    id 'org.ysb33r.os' version '1.0.0' apply false
    id 'org.ysb33r.cloudci' version '4.0.0' apply false
    id 'org.ysb33r.cloudci.appveyor.testreporter' version '4.0.0' apply false
    id 'org.ysb33r.gradletest' version '3.0.0-alpha.4' apply false
    id 'idea'
}

import org.gradle.util.GradleVersion
import java.text.SimpleDateFormat

apply plugin: 'net.nemerosa.versioning'

ext {
    buildTimeAndDate = new Date()
    buildDate = new SimpleDateFormat('yyyy-MM-dd').format(buildTimeAndDate)
    buildTime = new SimpleDateFormat('HH:mm:ss.SSSZ').format(buildTimeAndDate)
    buildRevision = versioning.info.commit
    buildCreatedBy = "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})".toString()
}

allprojects {
    ext {
        offlineRepoRoot = file("${project(':testfixtures-offline-repo').buildDir}/repo")
        offlineRepoBinariesRoot = file("${offlineRepoRoot}/binaries")
        offlineRepoDescriptionFile = file("${offlineRepoRoot}/repositories.gradle")

        withoutIntegrationTests = [
            'kindlegen-gradle'
        ]

        withoutCompatibilityTests = [
            'kindlegen-gradle'
        ]

        withoutCodeCoverage = [
        ]
    }
}

subprojects {

    apply plugin: AsciidoctorGradleModuleVersions
    apply plugin: 'org.ysb33r.os'

    if (project.name != 'testfixtures-offline-repo') {
        apply plugin: 'groovy'
        apply plugin: 'idea'

        if (!project.name.startsWith('testfixture')) {

            apply plugin: AsciidoctorGradlePluginProject
            apply plugin: 'com.github.ben-manes.versions'
            apply plugin: 'net.ossindex.audit'
            apply plugin: 'org.kordamp.jdeps'
            apply plugin: 'org.ysb33r.ivypot.binary.base'

            if (!(project.name in withoutCodeCoverage)) {
                apply plugin: 'jacoco'

                jacoco {
                    if (JavaVersion.current().isJava11Compatible() && GradleVersion.version(toolVersion) < GradleVersion.version('0.8.2')) {
                        toolVersion = '0.8.2'
                    }
                }
            }
        }

        apply from: "${rootDir}/gradle/standard-repositories.gradle"
        apply from: "${rootDir}/gradle/code-quality.gradle"

        if (!project.name.startsWith('testfixture')) {

            if (!(project.name in withoutIntegrationTests)) {
                apply from: "${rootDir}/gradle/integration-tests.gradle"
            }

            apply from: "${rootDir}/gradle/publishing.gradle"
            apply from: "${rootDir}/gradle/gradle-plugin-documentation.gradle"
            apply from: "${rootDir}/gradle/ci.gradle"

            if (project.name.startsWith('asciidoctor-gradle-jvm')) {
                apply from: "${rootDir}/gradle/asciidoctorj-versions.gradle"
                apply from: "${rootDir}/gradle/jruby-versions.gradle"
            }

            dependencyUpdates.resolutionStrategy = {
                componentSelection { rules ->
                    rules.all { selection ->
                        boolean rejected = ['beta', 'rc', 'cr', 'm'].any { qualifier ->
                            selection.candidate.version ==~ /(?i).*[.-]${qualifier}[.\d-]*/
                        }
                        if (rejected) {
                            selection.reject('Release candidate')
                        }
                    }
                }
            }

            configurations.all {
                resolutionStrategy {
                    eachDependency { DependencyResolveDetails details ->
                        if (details.requested.group == 'org.codehaus.groovy') {
                            details.useVersion GroovySystem.version
                            details.because 'The same version as Gradle Groovy is required'
                        }
                    }
                }
            }

            audit {
                failOnError = false
            }

            if (!(project.name in withoutCompatibilityTests)) {
                apply from: "${rootDir}/gradle/compatibility-tests.gradle"
            }

            tasks.withType(Test).configureEach {
                useJUnitPlatform()
            }
        }

        java {
            toolchain {
                languageVersion = JavaLanguageVersion.of(8)
            }
        }

        project.tasks.withType(Test) {
            testLogging {
                afterSuite { desc, result ->
                    if (!desc.parent) {
                        println "Test results ${project.name}: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
                    }
                }
            }
        }
    }
}

apply from: 'gradle/coveralls.gradle'

task syncWrapper(type: Copy) {
    from 'gradle', {
        include 'wrapper/**'
    }
    into 'docs/gradle'
}

wrapper {
    finalizedBy syncWrapper
}

tasks.idea.dependsOn syncWrapper

