apply from: 'secrets.gradle'

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()

        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath libs.gradle
        classpath libs.googleServices
        classpath libs.firebaseCrashlyticsGradle
        classpath libs.kotlinGradlePlugin
        classpath libs.ktlintGradle
        classpath libs.navigationSafeArgsGradlePlugin
        classpath libs.ossLicensesPlugin
    }
}

allprojects {
    repositories {
        maven {
            url "${rootDir}/.local-m2/"
            metadataSources {
                mavenPom()
                artifact() // Supports artifact only dependencies like those from medicmobile repo
            }
        }

        // Needs to go first to get specialty libraries https://stackoverflow.com/a/48438866/137744
        google()

        mavenLocal() // Only used for javarosa_local dependency
        mavenCentral()

        maven { url 'https://oss.sonatype.org/content/groups/public' }
        maven { url 'https://jitpack.io' }
        maven {
            url 'https://staging.dev.medicmobile.org/_couch/maven-repo'
            metadataSources { artifact() }
        }

        maven {
            url 'https://api.mapbox.com/downloads/v2/releases/maven'
            authentication {
                basic(BasicAuthentication)
            }
            credentials {
                username = "mapbox"
                password = getSecrets().getProperty('MAPBOX_DOWNLOADS_TOKEN', '')
            }
        }
    }

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            freeCompilerArgs = ['-Xjvm-default=all']
            jvmTarget = '17'
        }
    }
}

configurations.all {
    transitive = true
}

task checkCode(type: GradleBuild) {
    tasks = ['pmd', 'ktlintCheck', 'checkstyle', 'lintDebug']
}

// Create local Maven repo from cached Gradle dependencies
task cacheToMavenLocal(type: Sync) {
    from new File(gradle.gradleUserHomeDir, 'caches/modules-2/files-2.1')
    into "${rootDir}/.local-m2"

    duplicatesStrategy = 'include'

    // Convert from Gradle cache to Maven format
    eachFile {
        List<String> parts = it.path.split('/')
        it.path = parts[0].replace('.','/') +
                '/' + parts[1] +
                '/' + parts[2] +
                '/' + parts[4]
    }

    includeEmptyDirs false
}

tasks.register('testLab') {
    dependsOn "collect_app:assembleDebug", "collect_app:assembleDebugAndroidTest"
    doLast {
        exec {
            executable 'gcloud'
            args('beta', 'firebase', 'test', 'android', 'run',
                    '--type', 'instrumentation',
                    '--num-uniform-shards=25',
                    '--app', 'collect_app/build/outputs/apk/debug/ODK-Collect-debug.apk',
                    '--test', 'collect_app/build/outputs/apk/androidTest/debug/ODK-Collect-debug-androidTest.apk',
                    '--device', 'model=MediumPhone.arm,version=34,locale=en,orientation=portrait',
                    '--timeout', '10m',
                    '--directories-to-pull', '/sdcard',
                    '--test-targets', "notPackage org.odk.collect.android.regression",
                    '--test-targets', "notPackage org.odk.collect.android.benchmark"
            )
        }
    }
}

tasks.register('releaseCheck') {
    dependsOn "testLab", "collect_app:assembleOdkCollectRelease"
}
