apply plugin: 'java-library'
apply plugin: 'java-test-fixtures'
apply plugin: 'com.google.protobuf'
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'idea'

sourceCompatibility = 1.8
archivesBaseName    = "signal-service-java"
version             = lib_signal_service_version_number
group               = lib_signal_service_group_info

java {
    targetCompatibility = 1.8
    withJavadocJar()
    withSourcesJar()
}

compileJava {
    options.release = 8
}

repositories {
    mavenCentral()
    mavenLocal()
}

configurations {
    ideaTestFixturesImplementation { extendsFrom testFixturesImplementation; canBeConsumed false; canBeResolved true }
}

dependencies {
    implementation libs.google.protobuf.javalite
    api libs.google.libphonenumber
    api libs.jackson.core

    implementation libs.signal.client.java
    api libs.square.okhttp3
    api libs.square.okio
    implementation libs.threeten.threetenbp

    api libs.rxjava3.rxjava

    testImplementation testLibs.junit.junit
    testImplementation testLibs.assertj.core
    testImplementation testLibs.conscrypt.openjdk.uber
    testImplementation testLibs.mockito.core

    testFixturesImplementation libs.signal.client.java
    testFixturesImplementation testLibs.junit.junit
}

tasks.whenTaskAdded { task ->
    if (task.name.equals("lint")) {
        task.enabled = false
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.18.0'
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option "lite"
                }
            }
        }
    }
}

idea {
    module {
        generatedSourceDirs += file("${protobuf.generatedFilesBaseDir}/main/java")
        scopes.COMPILE.plus += [configurations.ideaTestFixturesImplementation]
    }
}

def isReleaseBuild() {
    return version.contains("SNAPSHOT") == false
}

def getReleaseRepositoryUrl() {
    return hasProperty('sonatypeRepo') ? sonatypeRepo
            : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
}

def getRepositoryUsername() {
    return hasProperty('whisperSonatypeUsername') ? whisperSonatypeUsername : ""
}

def getRepositoryPassword() {
    return hasProperty('whisperSonatypePassword') ? whisperSonatypePassword : ""
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = 'signal-service-java'
            from components.java

            pom {
                name = 'signal-service-java'
                packaging = 'jar'
                description = 'Signal Service communication library for Java'
                url = 'https://github.com/WhisperSystems/libsignal-service-java'

                scm {
                    url = 'scm:git@github.com:WhisperSystems/libsignal-service-java.git'
                    connection = 'scm:git@github.com:WhisperSystems/libsignal-service-java.git'
                    developerConnection = 'scm:git@github.com:WhisperSystems/libsignal-service-java.git'
                }

                licenses {
                    license {
                        name = 'GPLv3'
                        url = 'https://www.gnu.org/licenses/gpl-3.0.txt'
                        distribution = 'repo'
                    }
                }

                developers {
                    developer {
                        name = 'Moxie Marlinspike'
                    }
                }
            }

        }
    }

    repositories {
        maven {
            url = getReleaseRepositoryUrl()
            credentials {
                username getRepositoryUsername()
                password getRepositoryPassword()
            }
        }
    }
}

signing {
    required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
    sign publishing.publications.mavenJava
}

task installArchives(type: Upload) {
    description "Installs the artifacts to the local Maven repository."
    configuration = configurations['archives']
    repositories {
        mavenLocal()
    }
}