import static org.gradle.kotlin.dsl.KotlinDependencyExtensionsKt.embeddedKotlinVersion

buildscript {
    ext.kotlin_version = embeddedKotlinVersion

    repositories {
        mavenLocal()
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.github.gradle-nexus:publish-plugin:1.1.0"
        classpath "io.johnsonlee.buildprops:buildprops-gradle-plugin:1.1.0"
        classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.10.1"
    }
}

def OSSRH_USERNAME = project.properties['OSSRH_USERNAME'] ?: System.getenv('OSSRH_USERNAME')
def OSSRH_PASSWORD = project.properties['OSSRH_PASSWORD'] ?: System.getenv('OSSRH_PASSWORD')
def OSSRH_STAGING_PROFILE_ID = project.properties['OSSRH_STAGING_PROFILE_ID'] ?: System.getenv('OSSRH_STAGING_PROFILE_ID')

apply plugin: 'org.jetbrains.dokka'
apply plugin: 'io.github.gradle-nexus.publish-plugin'

allprojects { project ->
    apply plugin: 'java'
    apply plugin: 'kotlin'
    apply plugin: 'maven-publish'

    group = 'com.didiglobal.booster'
    version = findProperty('version').with { it != DEFAULT_VERSION ? it : null } ?: '1.0.0-SNAPSHOT'

    repositories {
        mavenLocal()
        google()
        mavenCentral()
    }

    sourceSets {
        main {
            kotlin {
                srcDirs += ['src/main/kotlin', 'src/main/java', 'build/generated/source/buildprops/main/java']
            }
        }
        test {
            kotlin {
                srcDirs += ['src/main/kotlin', 'src/main/java', 'build/generated/source/buildprops/main/java']
            }
        }
    }

    javadoc {
        failOnError = false
    }

    task packageJavadoc(type: Jar) {
        archiveClassifier = 'javadoc'
        from javadoc
    }

    task packageSources(type: Jar) {
        archiveClassifier = 'sources'
        from sourceSets.main.allSource
    }

    artifacts {
        archives packageJavadoc, packageSources
    }

    test {
        testLogging {
            events "passed", "skipped", "failed", "standardOut", "standardError"
        }
    }

    def configurePublication = { MavenPublication publication ->
        groupId = project.group
        artifactId = project.name
        version = project.version

        if ("mavenJava" == publication.name) {
            from components.java
        }

        artifact project.packageSources {
            archiveClassifier.set("sources")
        }

        artifact project.packageJavadoc {
            archiveClassifier.set("javadoc")
        }

        pom {
            name = project.name
            url = 'https://github.com/didi/booster'
            description = project.description ?: project.name

            scm {
                connection = 'scm:git:git://github.com/didi/booster.git'
                developerConnection = 'scm:git:git@github.com:didi/booster.git'
                url = 'https://github.com/didi/booster'
            }

            licenses {
                license {
                    name = 'Apache License'
                    url = 'https://www.apache.org/licenses/LICENSE-2.0'
                }
            }

            withXml { xml ->
                (['git', 'log', '--format=%aN %aE'].execute() | ['sort', '-u'].execute()).with {
                    waitForOrKill(5000)
                    if (0 == exitValue()) {
                        def developers = xml.asNode().appendNode('developers')
                        text.eachLine { line ->
                            def sp = line.lastIndexOf(' ')
                            if (sp > 0) {
                                def id = line.substring(0, sp).trim()
                                def email = line.substring(sp + 1).trim()
                                developers.appendNode('developer').with {
                                    appendNode('id', id)
                                    appendNode('email', email)
                                }
                            }
                        }
                    }
                }
            }
        }


        if (!project.hasProperty("signing.skip")) {
            apply plugin: 'signing'

            def signingTasks = project.tasks.withType(Sign)
            project.tasks.withType(PublishToMavenLocal) {
                dependsOn signingTasks
            }

            project.tasks.withType(PublishToMavenRepository) {
                dependsOn signingTasks
            }

            project.signing {
                sign publication
            }
        }
    }

    project.afterEvaluate {
        publishing {
            publications {
                if (project.plugins.hasPlugin('java-gradle-plugin')) {
                    withType(MavenPublication).configureEach(configurePublication)
                } else {
                    mavenJava(MavenPublication, configurePublication)
                }
            }
        }
    }
}

subprojects { project ->
    rootProject.clean.dependsOn project.clean
}

dependencies {
    implementation project(':booster-gradle-plugin')
}

nexusPublishing {
    packageGroup = "com.didiglobal"

    repositories {
        sonatype {
            username = OSSRH_USERNAME
            password = OSSRH_PASSWORD
            stagingProfileId = OSSRH_STAGING_PROFILE_ID
        }
    }

    clientTimeout = Duration.ofSeconds(300)
    connectTimeout = Duration.ofSeconds(60)

    transitionCheckOptions {
        maxRetries.set(3000)
        delayBetween.set(Duration.ofMillis(3000))
    }
}
