apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'signing'

def artifactId = 'audiovisualizer'
def ossrhUsername = findProperty('OSSRH_USERNAME')
def ossrhPassword = findProperty('OSSRH_PASSWORD')
def signingKey = findProperty('SIGNING_KEY')
def signingKeyPwd = findProperty('SIGNING_KEY_PWD')

group = "io.github.gautamchibde"
version = findProperty('LIBRARY_VERSION') ? findProperty('LIBRARY_VERSION')  : "1.0.13-snapshot" //Hardcoding snapshot version if none exist in environment variable

android {
    compileSdkVersion 33

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 33

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.appcompat:appcompat:1.6.1'
    testImplementation 'junit:junit:4.13.2'
}

println("Version to use")
println(version)

task javadoc(type: Javadoc) {

    doFirst {
        configurations.implementation
                .filter { it.name.endsWith('.aar') }
                .each { aar ->
                    copy {
                        from zipTree(aar)
                        include "**/classes.jar"
                        into "$buildDir/tmp/aarsToJars/${aar.name.replace('.aar', '')}/"
                    }
                }
    }

    configurations.implementation.setCanBeResolved(true)
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    classpath += configurations.implementation
    classpath += fileTree(dir: "$buildDir/tmp/aarsToJars/")
    destinationDir = file("${project.buildDir}/outputs/javadoc/")
    failOnError false
    exclude '**/BuildConfig.java'
    exclude '**/R.java'
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    archiveClassifier.set('javadoc')
    from javadoc
}

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

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                // You can then customize attributes of the publication as shown below.
                groupId = group
                artifactId = artifactId
                version = version

                artifact androidSourcesJar
                artifact javadocJar

                pom {
                    name = artifactId
                    description = ' Audio visualisation for android MediaPlayer'
                    url = 'https://github.com/GautamChibde/android-audio-visualizer'
                    licenses {
                        license {
                            name = 'Apache License 2.0'
                            url = 'https://github.com/GautamChibde/android-audio-visualizer/blob/master/LICENSE'
                        }
                    }
                    developers {
                        developer {
                            id = 'gautamchibde'
                            name = 'Gautam Chibde'
                            email = 'gautamchibde@gmail.com'
                        }
                    }
                    scm {
                        connection = 'scm:git@github.com:GautamChibde/android-audio-visualizer.git'
                        developerConnection = 'scm:git:ssh://github.com:GautamChibde/android-audio-visualizer.git'
                        url = 'https://github.com/GautamChibde/android-audio-visualizer/tree/master'
                    }
                }
            }
        }
        repositories {
            maven {
                name = "sonatype"

                def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
                def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

                credentials {
                    username ossrhUsername
                    password ossrhPassword
                }
            }
        }
    }
}

signing {
    useInMemoryPgpKeys(signingKey, signingKeyPwd)
    sign publishing.publications
}

