buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.27.0'
    }
}

allprojects {
    repositories {
        if (project.hasProperty('LOCAL_MAVEN_URL')) {
            maven { url LOCAL_MAVEN_URL }
        }
        google()
        jcenter()
    }
    version = VERSION_NAME
    group = GROUP

    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
    }
}

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

wrapper {
    gradleVersion '5.5.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'    : '28.0.3',
            'compile-sdk'    : 28,
            'target-sdk'     : 28,
            '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'

    // 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',
            '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',
            'android-gif'             : 'pl.droidsonroids.gif:android-gif-drawable:1.2.15',
            'jlatexmath-android'      : 'ru.noties:jlatexmath-android:0.1.0',
            'okhttp'                  : 'com.squareup.okhttp3:okhttp:3.9.0',
            'prism4j'                 : 'io.noties:prism4j:2.0.0',
            'debug'                   : 'io.noties:debug:5.0.0@jar',
            'adapt'                   : 'io.noties:adapt:2.0.0',
            'dagger'                  : "com.google.dagger:dagger:$daggerVersion",
            'picasso'                 : 'com.squareup.picasso:picasso:2.71828',
            'glide'                   : 'com.github.bumptech.glide:glide:4.9.0',
            'coil'                    : 'io.coil-kt:coil:0.8.0'
    ]

    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',
            'ix-java'             : 'com.github.akarnokd:ixjava:1.0.0',
            'commons-io'          : 'commons-io:commons-io:2.6',
            '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
            }
        }
    }
}
