apply plugin: 'java'

description = 'ProGuard minified SDK test suite for ProGuard compatibility tests.'
group = 'com.dropbox.core.test'
archivesBaseName = 'dropbox-proguard-test'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

ext {
    authInfoPropertyName = 'com.dropbox.test.authInfoFile'
}

buildscript {
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath dropboxJavaSdkLibs.guardsquare.proguard.gradle
    }
}

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

dependencies {
    implementation(project(":core"))
    implementation(dropboxJavaSdkLibs.testng)
}

tasks.named("compileJava", JavaCompile).configure {
    options.compilerArgs << '-Xlint:all'
    options.warnings = true
    options.deprecation = true
    options.encoding = 'utf-8'
}

// copied from rootProject's build.gradle. We avoid using subprojects due to Android Studio bug and
// dbapp-android integration.
def getAuthInfoFile() {
    if (!project.hasProperty(authInfoPropertyName)) {
        throw new GradleException('' +
            "These tests require the \"${authInfoPropertyName}\" " +
            "project property be set to point to an authorization JSON file " +
            "(e.g. ./gradlew proguardTest -P${authInfoPropertyName}=auth.json)."
        )
    }

    def authInfoFile = file(project.property(authInfoPropertyName))
    if (!authInfoFile.exists()) {
        throw new GradleException('' +
            "The test auth info file does not exist: \"${authInfoFile.absolutePath}\". " +
            "Please ensure the \"${authInfoPropertyName}\" project property is set to point to " +
            "the correct authorization JSON file."
        )
    }
    return authInfoFile
}


tasks.register('proguard', proguard.gradle.ProGuardTask) {
    def buildDir = project.buildDir
    def archivesBaseName = project.archivesBaseName
    def version = project.version
    ext {
        configDir = file("src/main/resources/proguard/")
        configFile = file("../examples/android/proguard-rules.pro")
        proguardJar = file("$buildDir/$archivesBaseName-$version-proguard.jar")
    }

    inputs.dir configDir
    inputs.file configFile

    injars sourceSets.main.runtimeClasspath, filter: '!META-INF/*'
    if (System.getProperty('java.version').startsWith('1.')) {
        libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
    } else {
        // As of Java 9, the runtime classes are packaged in modular jmod files.
        libraryjars "${System.getProperty('java.home')}/jmods/java.base.jmod", jarfilter: '!**.jar', filter: '!module-info.class'
        libraryjars "${System.getProperty('java.home')}/jmods/java.logging.jmod", jarfilter: '!**.jar', filter: '!module-info.class'
        //libraryjars "${System.getProperty('java.home')}/jmods/java.sql.jmod", jarfilter: '!**.jar', filter: '!module-info.class'
        //libraryjars "${System.getProperty('java.home')}/jmods/....."
    }
    libraryjars sourceSets.main.compileClasspath.filter { file ->
        return !sourceSets.main.runtimeClasspath.contains(file)
    }

    outjars proguardJar

    fileTree(dir: configDir, include: '**/*.pro').each { file ->
        configuration file
    }
    configuration configFile
}

tasks.register('proguardTest', JavaExec) {
    dependsOn(tasks.named("proguard"))
    classpath = files(proguard.proguardJar)
    main = 'com.dropbox.core.test.proguard.Main'
    ignoreExitValue false

    doFirst {
        args getAuthInfoFile().absolutePath
    }
}
