init()

apply plugin: 'com.android.library'

dependencies {
    implementation "androidx.appcompat:appcompat:$appcompat_library_version"
    implementation "androidx.legacy:legacy-support-v4:$legacy_support_v4_version"
    implementation "com.google.android.material:material:$material_design_version"
    implementation "com.google.android.play:core:$play_core_version"
    implementation "com.google.android.gms:play-services-base:$play_services_base_version"

    testImplementation "junit:junit:$junit_version"
    testImplementation "org.powermock:powermock-module-junit4:$powermock_version"
    testImplementation "org.powermock:powermock-module-junit4-rule:$powermock_version"
    testImplementation "org.powermock:powermock-api-mockito2:$powermock_version"
    testImplementation "org.powermock:powermock-classloading-xstream:$powermock_version"

    androidTestImplementation "androidx.annotation:annotation:$annotation_version"
    androidTestImplementation "androidx.test:runner:$androidx_test_version"
    androidTestImplementation "androidx.test:rules:$androidx_test_version"
}

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        // BUILD_NUMBER is provided by Jenkins. Default to 1 in dev builds.
        versionCode System.getenv("BUILD_NUMBER") as Integer ?: System.getenv("TRAVIS_BUILD_NUMBER") as Integer ?: 1
        versionName project.version
        consumerProguardFiles 'consumer-proguard-rules.pro'
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        resValue "string", "apptentive_distribution_version", versionName
        vectorDrawables.useSupportLibrary = true
    }

    lintOptions {
        abortOnError false
    }

    sourceSets {
        test {
            java.srcDirs = ['src/test/java', 'src/testCommon/java']
        }
        androidTest {
            java.srcDirs = ['src/androidTest/java', 'src/testCommon/java']
        }
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
}

apply plugin: 'maven'
apply plugin: 'signing'

def isReleaseBuild() {
    return hasProperty("MAVEN_RELEASE_BUILD") && MAVEN_RELEASE_BUILD == "true";
}

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

def getSnapshotRepositoryUrl() {
    return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL : System.getenv("SNAPSHOT_REPOSITORY_URL")
}

def getRepositoryUsername() {
    if (isReleaseBuild()) {
        return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
    } else {
        return hasProperty('SNAPSHOT_REPOSITORY_USERNAME') ? SNAPSHOT_REPOSITORY_USERNAME : System.getenv("SNAPSHOT_REPOSITORY_USERNAME")
    }
}

def getRepositoryPassword() {
    if (isReleaseBuild()) {
        return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
    } else {
        return hasProperty('SNAPSHOT_REPOSITORY_PASSWORD') ? SNAPSHOT_REPOSITORY_PASSWORD : System.getenv("SNAPSHOT_REPOSITORY_PASSWORD")
    }
}

uploadArchives {
    repositories.mavenDeployer {
        beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

        repository(url: getReleaseRepositoryUrl()) {
            authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
        }

        snapshotRepository(url: getSnapshotRepositoryUrl()) {
            authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
        }

        pom.project {
            name 'Apptentive Android SDK'
            artifactId 'apptentive-android'
            packaging 'aar'
            description 'Apptentive Android'
            url 'https://github.com/apptentive/apptentive-android'

            scm {
                url 'https://github.com/apptentive/apptentive-android'
                connection 'scm:git@github.com:apptentive/apptentive-android.git'
                developerConnection 'scm:git@github.com:apptentive/apptentive-android.git'
            }

            licenses {
                license {
                    name 'BSD 3-Clause License'
                    url 'https://raw.githubusercontent.com/apptentive/apptentive-android/master/License.txt'
                    distribution 'repo'
                }
            }

            developers {
                developer {
                    id 'apptentive'
                    name 'Apptentive'
                }
            }
        }
    }

}

signing {
    println(isReleaseBuild() ? "Signing release build." : "Signing snapshot build.");
    required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

task androidJavadocs(type: Javadoc) {
    failOnError = false

    source = android.sourceSets.main.java.srcDirs
    // Don't document everything. Just the public API classes.
    include 'com/apptentive/android/sdk/Apptentive.java'
    include 'com/apptentive/android/sdk/module/rating/impl/AmazonAppstoreRatingProvider.java'
    include 'com/apptentive/android/sdk/module/rating/impl/GooglePlayRatingProvider.java'
    include 'com/apptentive/android/sdk/module/rating/InsufficientRatingArgumentsException.java'
    include 'com/apptentive/android/sdk/module/rating/IRatingProvider.java'
    include 'com/apptentive/android/sdk/module/messagecenter/UnreadMessagesListener.java'
    include 'com/apptentive/android/sdk/module/survey/OnSurveyFinishedListener.java'
    include 'com/apptentive/android/sdk/model/CustomData.java'
    include 'com/apptentive/android/sdk/model/ExtendedData.java'
    include 'com/apptentive/android/sdk/model/CommerceExtendedData.java'
    include 'com/apptentive/android/sdk/model/LocationExtendedData.java'
    include 'com/apptentive/android/sdk/model/TimeExtendedData.java'

    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

androidJavadocs.doLast {
    println "Javadocs written to: " + destinationDir
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

afterEvaluate {
    androidJavadocs.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
}
repositories {
    mavenCentral()
}

def init() {
    // Set project information that will be available to all tasks.
    project.group = 'com.apptentive'
    // The SDK version is defined only in source. Get it.
    project.version = null
    def sdkVersionFile = file('src/main/java/com/apptentive/android/sdk/util/Constants.java')
    sdkVersionFile.eachLine {
        def matcher = (it =~ /(?:.*APPTENTIVE_SDK_VERSION = \")(.*)(?:\".*)/)
        if (matcher.matches()) {
            project.version = matcher[0][1]
            if (!isReleaseBuild()) {
                project.version = project.version + "-SNAPSHOT";
            }
            return
        }
    }
    if (project.version.is('unspecified')) {
        throw new GradleScriptException('Version could not be found.', null)
    }
    println "Building Apptentive SDK version: " + project.version;
}
