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

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

dependencies {
  implementation "org.jetbrains:annotations:$project.annotationsVersion"
  implementation "org.aspectj:aspectjrt:$project.aspectjVersion"
  implementation project(':frodo2-api')
  implementation project(':frodo2-logger')
}

android {
  buildToolsVersion project.buildToolsVersion
  compileSdkVersion Integer.parseInt(project.compileSdkVersion)

  defaultConfig {
    versionName project.version
    minSdkVersion Integer.parseInt(project.minSdkVersion)
    targetSdkVersion Integer.parseInt(project.targetSdkVersion)
  }

  lintOptions {
    abortOnError false
  }

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

android.libraryVariants.all { variant ->

  //  Compile aspects
  JavaCompile javaCompile = variant.getJavaCompiler()
  javaCompile.doLast {
    String[] args = ["-showWeaveInfo",
                     "-1.5",
                     "-inpath", javaCompile.destinationDir.toString(),
                     "-aspectpath", javaCompile.classpath.asPath,
                     "-d", javaCompile.destinationDir.toString(),
                     "-classpath", javaCompile.classpath.asPath]

    final MessageHandler handler = new MessageHandler(true);
    final def log = project.logger
    new Main().run(args, handler)
    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.INFO:
          log.info message.message, message.thrown
          break;
        case IMessage.DEBUG:
          log.debug message.message, message.thrown
          break;
      }
    }
  }
}

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

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

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