import com.android.builder.core.BuilderConstants;

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        consumerProguardFiles 'consumer-proguard-rules.pro'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile(project(':bundler-annotations')) {
        transitive = true;
    }

    testAnnotationProcessor project(':bundler-compiler')
    testCompile "org.robolectric:robolectric:3.1.4"
    testCompile 'junit:junit:4.12'
    testCompile 'org.unitils:unitils-core:3.4.2'
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += configurations.compile
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task jar(type: Jar) {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    from android.sourceSets.main.java.srcDirs
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

task copyLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

artifacts {
    archives javadocJar
    archives sourcesJar
}

// Inspiration:
// https://gist.github.com/kellyfj/9666950
android.libraryVariants.all { variant ->
    def name = variant.buildType.name
    if (BuilderConstants.DEBUG.equals(name)) {
        return; // Skip debug builds.
    }

    // Create a new task ("jarRelease", etc)
    def task = project.tasks.create "jar${name.capitalize()}", Jar
    task.dependsOn variant.javaCompile
    task.from variant.javaCompile.destinationDir

    // Add bundler dependencies
    task.from configurations.compile.collect {
        if (it.getName().contains("bundler") && it.getName().contains("jar")) {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    artifacts.add('archives', task);
}

publishing {
    publications {
        library(MavenPublication) {
            artifact "$buildDir/libs/bundler-api.jar"
            artifact sourcesJar
            artifact javadocJar

            artifactId 'easybundler-api'
            groupId rootProject.ext.groupId
            version rootProject.ext.version

            pom.withXml {
                def root = asNode()
                root.appendNode('description', rootProject.ext.projectDescription)
                root.appendNode('name', rootProject.ext.projectName)
                root.appendNode('url', rootProject.ext.projectUrl)
                root.children().last() + rootProject.ext.pomConfig
            }
        }
    }
}

bintray {
    user = rootProject.ext.bintrayUser
    key = rootProject.ext.bintrayKey

    publications = ['library']

    pkg {
        name = 'easybundler-api'
        repo = rootProject.ext.bintrayRepo
        userOrg = rootProject.ext.bintrayOrg
        licenses = rootProject.ext.licenses
        vcsUrl = "${rootProject.ext.projectUrl}.git"

        version {
            name = rootProject.ext.version
        }
    }
}
