buildscript {
    ext.kotlin_version = '1.4.10'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.2'
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.28.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        if (project.hasProperty('LOCAL_MAVEN_URL')) {
            maven { url LOCAL_MAVEN_URL }
        }
        google()
        jcenter()
//        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    }
    version = VERSION_NAME
    group = GROUP

    // do we actually need javadoc any more?
    tasks.withType(Javadoc) {
        enabled = false
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

wrapper {
    gradleVersion '6.1.1'
    distributionType 'all'
}

if (hasProperty('local')) {
    if (!hasProperty('LOCAL_MAVEN_URL')) {
        throw new RuntimeException('Cannot publish to local maven as no such property exists: LOCAL_MAVEN_URL')
    }
    ext.RELEASE_REPOSITORY_URL = LOCAL_MAVEN_URL
    ext.SNAPSHOT_REPOSITORY_URL = LOCAL_MAVEN_URL
}

ext {

    config = [
            'build-tools'    : '29.0.3',
            'compile-sdk'    : 29,
            'target-sdk'     : 29,
            'min-sdk'        : 16,
            'push-aar-gradle': 'https://raw.githubusercontent.com/noties/gradle-mvn-push/master/gradle-mvn-push-aar.gradle'
    ]

    final def commonMarkVersion = '0.13.0'
    final def daggerVersion = '2.10'
    final def coilVersion = '0.13.0'

    // please note that `pl.droidsonroids.gif:android-gif-drawable:1.2.15` is used due to the minimum
    // api level mismatch that Markwon supports (16) and later versions of AndroidGifDrawable (17).
    // It should not be a problem as this dependency is used as `compileOnly` and users
    // must specify version explicitly (until library's API changes...)

    deps = [
            'x-annotations'           : 'androidx.annotation:annotation:1.1.0',
            'x-recycler-view'         : 'androidx.recyclerview:recyclerview:1.0.0',
            'x-core'                  : 'androidx.core:core:1.0.2',
            'x-appcompat'             : 'androidx.appcompat:appcompat:1.1.0',
            'x-cardview'              : 'androidx.cardview:cardview:1.0.0',
            'x-fragment'              : 'androidx.fragment:fragment:1.0.0',
            'commonmark'              : "com.atlassian.commonmark:commonmark:$commonMarkVersion",
            'commonmark-strikethrough': "com.atlassian.commonmark:commonmark-ext-gfm-strikethrough:$commonMarkVersion",
            'commonmark-table'        : "com.atlassian.commonmark:commonmark-ext-gfm-tables:$commonMarkVersion",
            'android-svg'             : 'com.caverock:androidsvg:1.4',
            // we need 2 gif artifacts at long as we have a difference in version used for API compatibility
            'android-gif'             : 'pl.droidsonroids.gif:android-gif-drawable:1.2.15',
            'android-gif-impl'        : 'pl.droidsonroids.gif:android-gif-drawable:1.2.20',
            'jlatexmath-android'      : 'ru.noties:jlatexmath-android:0.2.0',
            'okhttp'                  : 'com.squareup.okhttp3:okhttp:3.9.0',
            'prism4j'                 : 'io.noties:prism4j:2.0.0',
            'debug'                   : 'io.noties:debug:5.1.0',
            'adapt'                   : 'io.noties:adapt:2.2.0',
            'dagger'                  : "com.google.dagger:dagger:$daggerVersion",
            'picasso'                 : 'com.squareup.picasso:picasso:2.71828',
            'glide'                   : 'com.github.bumptech.glide:glide:4.11.0',
            'coil'                    : "io.coil-kt:coil:$coilVersion",
            'coil-base'               : "io.coil-kt:coil-base:$coilVersion",
            'ix-java'                 : 'com.github.akarnokd:ixjava:1.0.0',
            'gson'                    : 'com.google.code.gson:gson:2.8.6',
            'commons-io'              : 'commons-io:commons-io:2.6'
    ]

    deps['annotationProcessor'] = [
            'prism4j-bundler': 'io.noties:prism4j-bundler:2.0.0',
            'dagger-compiler': "com.google.dagger:dagger-compiler:$daggerVersion"
    ]

    deps['test'] = [
            'junit'               : 'junit:junit:4.12',
            'robolectric'         : 'org.robolectric:robolectric:3.8',
            'mockito'             : 'org.mockito:mockito-core:2.21.0',
            'commonmark-test-util': "com.atlassian.commonmark:commonmark-test-util:$commonMarkVersion",
    ]

    registerArtifact = this.&registerArtifact
}

task checkUpdates {
    apply plugin: 'com.github.ben-manes.versions'
    dependsOn 'dependencyUpdates'
}

def registerArtifact(project) {

    if (hasProperty('release')) {

        // to be used in github actions (to publish a snapshot)
        // but only if we have snapshot in the version name
        if (hasProperty('CI') && VERSION_NAME.contains('SNAPSHOT')) {
            ext.NEXUS_USERNAME = System.getenv('NEXUS_USERNAME')
            ext.NEXUS_PASSWORD = System.getenv('NEXUS_PASSWORD')
        }

        project.apply from: config['push-aar-gradle']
    }

    project.afterEvaluate {

        // disable generation of BuildConfig files
        project.generateDebugBuildConfig.enabled = false
        project.generateReleaseBuildConfig.enabled = false

        // print test status (for CI)
        project.android.testOptions.unitTests.all {
            testLogging {
                events "passed", "skipped", "failed"
                exceptionFormat "short"
                showStandardStreams = true
            }
        }
    }
}