import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main

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

buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'org.aspectj:aspectjtools:1.8.8'
    }
}

repositories {
    google()
    maven { url 'https://dl.bintray.com/frogermcs/maven/' }
}

dependencies {
    implementation 'org.aspectj:aspectjrt:1.8.8'
    implementation 'com.android.support:support-compat:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
}

android {
    compileSdkVersion = 28

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 28
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

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

android.libraryVariants.all { variant ->
    JavaCompile javaCompile = variant.javaCompileProvider.get()
    javaCompile.doLast {
        String[] args = ["-showWeaveInfo",
                         "-1.5",
                         "-inpath", javaCompile.destinationDir.toString(),
                         "-aspectpath", javaCompile.classpath.asPath,
                         "-d", javaCompile.destinationDir.toString(),
                         "-classpath", javaCompile.classpath.asPath]

        MessageHandler handler = new MessageHandler(true);
        new Main().run(args, handler)

        def log = project.logger
        for (IMessage message : handler.getMessages(null, true)) {
            switch (message.getKind()) {
                case IMessage.ABORT:
                case IMessage.ERROR:
                case IMessage.FAIL:
                    log.error message.message, message.thrown
                    break;
                case IMessage.WARNING:
                case IMessage.INFO:
                    log.info message.message, message.thrown
                    break;
                case IMessage.DEBUG:
                    log.debug message.message, message.thrown
                    break;
            }
        }
    }
}

group = GROUP
version = VERSION_NAME

install {
    repositories.mavenInstaller {
        pom {
            project {
                packaging 'aar'
                groupId GROUP
                artifactId "androiddevmetrics-runtime"

                name "AndroidDevMetrics Runtime"
                description POM_DESCRIPTION
                url POM_URL

                licenses {
                    license {
                        name POM_LICENCE_NAME
                        url POM_LICENCE_URL
                        Distribution POM_LICENCE_DIST
                    }
                }

                developers {
                    developer {
                        id POM_DEVELOPER_ID
                        name POM_DEVELOPER_NAME
                        email POM_DEVELOPER_EMAIL
                    }
                }

                scm {
                    url POM_SCM_URL
                    connection POM_SCM_CONNECTION
                    developerConnection POM_SCM_DEV_CONNECTION
                }
            }
        }
    }
}

// Bintray
bintray {
    // Global gradle.properties
    user = project.hasProperty('BINTRAY_USER') ? project.getProperty('BINTRAY_USER') : ""
    key = project.hasProperty('BINTRAY_KEY') ? project.getProperty('BINTRAY_KEY') : ""

    configurations = ['archives']
    pkg {
        repo = 'maven'
        name = 'androiddevmetrics-runtime'
        desc = POM_DESCRIPTION
        websiteUrl = POM_URL
        vcsUrl = GIT_URL
        licenses = ["Apache-2.0"]
        publish = true
        publicDownloadNumbers = true
        version {
            desc = POM_DESCRIPTION
            gpg {
                sign = true
                passphrase = project.hasProperty('BINTRAY_GPG_PASSWORD') ? project.getProperty('BINTRAY_GPG_PASSWORD') : ""
            }
        }
    }
}

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

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

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

artifacts {
    archives javadocJar
    archives sourcesJar
}