System.setProperty("file.encoding", "UTF-8")

buildscript {
    repositories {
        google()
        mavenCentral()
        mavenLocal()
        maven { url "https://plugins.gradle.org/m2/" }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'net.evendanan.autoversion:gradle-plugin:0.2.2'

        classpath 'com.github.triplet.gradle:play-publisher:2.4.2'
        //not compatible with gradle 6+
        //classpath 'com.novoda:bintray-release:0.9.1'
    }
}

apply from: "$rootDir/gradle/versioning_root.gradle"

tasks.register("generateMarkDownOfLanguagePacks") {
    group "AnySoftKeyboard"
    description 'Printing a MarkDown-friendly list of languages'

    doLast {
        println("## Languages in this repo")
        println()

        subprojects.each {
            if (it.path.contains('languages') && it.plugins.hasPlugin('com.android.application')) {
                def image = "<img src='languages/${it.parent.name}/apk/flag/{{IMAGE_FILENAME}}' height='16'>&nbsp;"
                if (it.file("flag/flag.png").exists()) {
                    image = image.replace("{{IMAGE_FILENAME}}", "flag.png")
                } else if (it.file("flag/flag.svg").exists()) {
                    image = image.replace("{{IMAGE_FILENAME}}", "flag.svg?sanitize=1")
                } else {
                    image = ""
                }
                def packReadMeDetails = "1. ${image}${it.parent.name.capitalize()}: [Source](languages/${it.parent.name})"

                if (it.ext.shouldBePublished) {
                    packReadMeDetails += " • [Play Store](https://play.google.com/store/apps/details?id=${it.android.defaultConfig.applicationId})"
                }

                println(packReadMeDetails)
            }
        }
    }
}

tasks.register("clean") {
    delete 'buildSrc/build'
}

tasks.register("copyAnySoftKeyboardBaseLib") {
    group "AnySoftKeyboard"
    description 'Copies several of base aars from this repo into AnySoftKeyboard local repo. Use -PanysoftkeyboardPath=[path/to/AnySoftKeyboard/]'

    def libsToCopy = [':api', ':base', ':languages:english:pack']
    def targets = ['external/api/libs', 'external/packs/libs', 'external/packs/libs']
    libsToCopy.forEach {
        dependsOn rootProject.findProject(it).tasks.named('assembleRelease')
    }

    doLast {
        if (!project.hasProperty('anysoftkeyboardPath')) {
            throw new IllegalArgumentException("Please provide path to AnySoftKeyboard code as an argument: -PanysoftkeyboardPath=[path/to/AnySoftKeyboard/]")
        }

        File askPath = file(project.getProperties().get('anysoftkeyboardPath'))

        if (!askPath.exists() || !askPath.isDirectory()) {
            throw new IllegalArgumentException("The path ${askPath.absolutePath} does not exist or is not a folder. This should point to where you have the local copy of AnySoftKeyboard code.")
        }
        (0..libsToCopy.size()-1).each { outputIndex ->
            def libProject = rootProject.findProject(libsToCopy.get(outputIndex))
            def targetFolder = "${askPath.absolutePath}/${targets[outputIndex]}/"
            println "will copy ${libProject.projectDir.absolutePath} to ${targetFolder}"
            delete {
                delete "${targetFolder}/*"
            }
            copy {
                from("${libProject.buildDir.absolutePath}/outputs/aar/") {
                    include '**/*.aar'
                    rename '.*', "${libProject.path.substring(1).replace(':', '-')}.aar"
                }
                into "${targetFolder}/"
            }
        }
    }
}
