plugins {
    // Note that the Kotlin Gradle Plugin and the Android Gradle Plugin are specified as
    // dependencies in `buildSrc/build.gradle` instead of here as otherwise it was causing
    // obscure errors during builds, for some reason.

    id 'com.diffplug.spotless' version '7.2.1'
    id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
    id 'maven-publish'
    id 'signing'
    id 'dokka-convention'
}

dependencies {
    dokka project(':ouisync-android')
    dokka project(':ouisync-service')
    dokka project(':ouisync-session')
}

subprojects {
    layout.buildDirectory.set(rootProject.layout.buildDirectory.dir(name))
}


spotless {
    kotlin {
        target("**/*.kt")
        targetExclude("**/build/**/*.kt")

        ktfmt("0.58")
        ktlint()
            .editorConfigOverride(
                ["ktlint_function_naming_ignore_when_annotated_with": "Composable"]
            )

    }
}

dokka {
    dokkaPublications.html {
        includes.from(project.layout.projectDirectory.file('README.md'))
    }
}

tasks.named('clean') {
    doLast {
        delete rootProject.layout.buildDirectory.get()
    }
}

// Grab version from the top level Cargo.toml
def cargoManifest = file("$rootDir/../../Cargo.toml").text
def cargoVersionPattern = /(?m)^version\s*=\s*"(.*)"/
def cargoVersion = (cargoManifest =~ cargoVersionPattern)[0][1].trim()

version = "$cargoVersion-$revision"
group   = "ie.equalit.ouinet"

// Use system environment variables for signing and publishing
ext {
    ossrhUsername = System.getenv('OSSRH_USERNAME')
    ossrhPassword = System.getenv('OSSRH_PASSWORD')
    sonatypeStagingProfileId = System.getenv('SONATYPE_STAGING_PROFILE_ID')
    signingKey = System.getenv('SIGNING_KEY')
    signingKeyId = System.getenv('SIGNING_KEY_ID')
    signingPassword = System.getenv('SIGNING_PASSWORD')
}

// Set up Sonatype repository
nexusPublishing {
    repositories {
        sonatype {
            stagingProfileId = sonatypeStagingProfileId
            username = ossrhUsername
            password = ossrhPassword
            nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
            snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
        }
    }
}

