// 重复添加同样的依赖，以保证Spellbook能够独立编译
buildscript {
    ext {
        dokka_version = '0.9.17'
        kotlin_version = '1.3.10'
    }
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:$dokka_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'org.jetbrains.dokka-android'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            postprocessing {
                removeUnusedCode false
                removeUnusedResources false
                obfuscate false
                optimizeCode false
                proguardFile 'proguard-rules.pro'
            }
        }
    }
    lintOptions {
        abortOnError false
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        test.java.srcDirs += 'src/test/kotlin'
        androidTest.java.srcDirs += "src/androidTest/kotlin"
    }
    android.libraryVariants.all { variant ->
        variant.javaCompiler.dependsOn(generateMirrorClassesList)
        variant.javaCompiler.dependsOn(generateMirrorMethodsList)
        variant.javaCompiler.dependsOn(generateMirrorFieldsList)
    }
}

repositories {
    jcenter()
    google()
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.ext:junit:1.0.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

    //noinspection GradleDependency
    compileOnly 'de.robv.android.xposed:api:53'
    compileOnly 'de.robv.android.xposed:api:53:sources'
    api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaAndroidTask) {
    outputFormat = 'javadoc'
    outputDirectory = "$buildDir/dokkaJavadoc"
    externalDocumentationLink {
        url = new URL("https://kotlinlang.org/api/latest/jvm/stdlib/")
    }
    externalDocumentationLink {
        url = new URL("https://developer.android.com/reference/")
    }
    externalDocumentationLink {
        url = new URL("http://api.xposed.info/reference/")
    }
}

def generateMirrorList(String type, String filename) {
    def SRC_DIR = "src/main/kotlin/"
    def MIRROR_DIR = "src/main/kotlin/com/gh0u1l5/wechatmagician/spellbook/mirror"

    def prefixLength = file(SRC_DIR).absolutePath.length() + 1
    def classes = fileTree(MIRROR_DIR).filter { file ->
        file.name == "${type}.kt"
    }.collect { file ->
        def relativePath = file.absolutePath.drop(prefixLength)
        relativePath.replaceAll('[\\\\/]', '.').substring(0, relativePath.length() - 3)
    }.sort()
    file("$MIRROR_DIR/${filename}.kt").text = """package com.gh0u1l5.wechatmagician.spellbook.mirror

/**
 * Dynamically generated by build.gradle
 */
val $filename = listOf (
        ${classes.join(",\n        ")}
)
"""
}

task generateMirrorClassesList() {
    generateMirrorList("Classes", "MirrorClasses")
}

task generateMirrorMethodsList() {
    generateMirrorList("Methods", "MirrorMethods")
}

task generateMirrorFieldsList() {
    generateMirrorList("Fields", "MirrorFields")
}

apply from: 'maven.gradle'