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

ext {
    PUBLISH_GROUP_ID = 'com.eightbitlab'
    PUBLISH_ARTIFACT_ID = 'bottomnavigationbar'
}

def getVersionName() {
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags'
        standardOutput = stdout
    }
    //remove "version-" prefix and hash
    def tagAndHash = stdout.toString().trim().replace("version-", "")
    def indexOfDash = tagAndHash.indexOf("-")
    if (indexOfDash == -1) {
        return tagAndHash
    }
    return tagAndHash.substring(0, indexOfDash)
}

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
    }

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

configurations {
    javadocDeps
}

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    javadocDeps 'com.android.support:support-annotations:28.0.0'
    javadocDeps 'com.android.support:appcompat-v7:28.0.0'
}

task androidJavadocs(type: Javadoc) {
    failOnError = false
    source = android.sourceSets.main.java.srcDirs
    ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
    classpath += files(ext.androidJar)
}

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

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

publishing {
    publications {
        Production(MavenPublication) {
            artifact("$buildDir/outputs/aar/bottomnavigationbar-release.aar")
            artifact(androidSourcesJar)
            artifact(androidJavadocsJar)
            groupId PUBLISH_GROUP_ID
            artifactId PUBLISH_ARTIFACT_ID
            version getVersionName()

            //The publication doesn't know about our dependencies, so we have to manually add them to the pom
            pom.withXml {
                //Iterate over the implementation dependencies (we don't want the test ones), adding a <dependency> node for each
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.implementation.allDependencies.each {
                    if (it.name != 'unspecified') {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                    }
                }
            }
        }
    }
}

bintray {
    user = System.getenv('BINTRAY_USER')
    key = System.getenv('BINTRAY_API_KEY')
    dryRun = false
    override = false
    publish = true
    pkg {
        repo = 'maven'
        name = 'BottomNavigationBar'
        userOrg = 'dimezis'
        vcsUrl = 'https://github.com/Dimezis/BottomNavigationBar'
        licenses = ['Apache-2.0']

        version {
            name = getVersionName()
            vcsTag = getVersionName()
        }
    }
    publications = ['Production']
}