apply plugin: 'com.android.library'
apply plugin: 'maven'

repositories {
    google()
    jcenter()
}

dependencies {
    implementation 'com.android.support:support-annotations:27.1.1'
}

android {
    compileSdkVersion COMPILE_SDK_VERSION

    defaultConfig {
        minSdkVersion MIN_SDK_VERSION
        targetSdkVersion TARGET_SDK_VERSION
        versionCode LIBRARY_VERSION_CODE
        versionName LIBRARY_VERSION_NAME
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

task sourcePackage(type: Jar) {
    baseName = ARTIFACT_ID
    classifier = "sources"
    version LIBRARY_VERSION_NAME
    from android.sourceSets.main.java.srcDirs
    destinationDir file("$buildDir/artifacts")
}

task generatePom {
    doLast {
        pom {
            project {
                groupId "com.github.d-max"
                artifactId "spots-dialog"
                version LIBRARY_VERSION_NAME
                packaging "aar"
                name "spots progress dialog"
                description "Android AlertDialog with moving spots progress indicator"
                url "https://github.com/d-max/spots-dialog"
                scm {
                    url "https://github.com/d-max/spots-dialog"
                    connection "scm:git@github.com:d-max/spots-dialog.git"
                    developerConnection "scm:git@github.com:d-max/spots-dialog.git"
                }
                licenses {
                    license {
                        name "MIT License"
                        url "http://www.opensource.org/licenses/mit-license.php"
                        distribution "repo"
                    }
                }
                developers {
                    developer {
                        id "d-max"
                        name "Maksym Dybarskyi"
                        email "maxim.dybarskyy@gmail.com"
                    }
                }
            }
        }.writeTo("$buildDir/artifacts/${ARTIFACT_ID}-${LIBRARY_VERSION_NAME}.pom")
    }
}

task createArtifacts {
    dependsOn sourcePackage, generatePom
}

android.libraryVariants.all { variant ->
    if (variant.buildType.name == "release") {
        File outDir = file("$buildDir/artifacts")
        File apkFile = variant.outputs[0].outputFile

        def task = project.tasks.create("copy${variant.name.capitalize()}Aar", Copy)
        task.from apkFile
        task.into outDir
        task.rename apkFile.name, "${ARTIFACT_ID}-${LIBRARY_VERSION_NAME}.aar"

        task.dependsOn variant.assemble
        task.dependsOn sourcePackage
        createArtifacts.dependsOn task
    }
}
