/**
 * Commands:
 *   - gradle build
 *   - gradle test
 *   - gradle assemble
 *   - gradle uploadArchives
 *
 * To execute the 'uploadArchives' task, the following properties must be specified
 * in an external 'gradle.properties' file:
 *   - sonatypeUsername
 *   - sonatypePassword
 */

apply plugin: 'java'
apply plugin: 'fatjar'
apply plugin: 'maven'
apply plugin: 'signing'

group         = 'com.sendgrid'
version       = '4.0.1'
ext.packaging = 'jar'

allprojects {
  apply plugin: 'java'
  sourceCompatibility = 1.7
  targetCompatibility = 1.7
}

if (!hasProperty("sonatypeUsername")) {
  ext.sonatypeUsername = null
  ext.sonatypePassword = null
}

task wrapper(type: Wrapper) {
  gradleVersion = '1.8'
}

buildscript {
  dependencies {
    classpath 'eu.appsatori:gradle-fatjar-plugin:0.1.2' // adds fatJar task
  }
  repositories {
    mavenCentral()
  }
}

dependencies {
  compile 'com.sendgrid:java-http-client:4.1.0'
  compile 'com.fasterxml.jackson.core:jackson-core:2.5.3'
  compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.3'
  compile 'com.fasterxml.jackson.core:jackson-databind:2.5.3'
  testCompile group: 'junit', name: 'junit', version: '4.12'
}

repositories {
  mavenCentral()
}

allprojects {
  gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
      options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
      }
  }
}

// adds 'with-dependencies' to the fatJar name
fatJar {
  classifier 'jar'
  baseName "sendgrid"
  manifest {
    attributes("Implementation-Title": "SendGrid", "Implementation-Version": version)
  }
}

// copy fatJar to base project directory so they will be in git (and on github for download)
build << {
  copy {
    println "Copying ${fatJar.archiveName} to $projectDir/repo/com/sendgrid"
    from("$buildDir/libs/${fatJar.archiveName}")
    into("$projectDir/repo/com/sendgrid")
  }
  tasks.renameSendGridVersionJarToSendGridJar.execute()
}

task renameSendGridVersionJarToSendGridJar {
  doLast {
    file("$projectDir/repo/com/sendgrid/${fatJar.archiveName}").renameTo(file("$projectDir/repo/com/sendgrid/sendgrid-java-latest.jar"))
    copy {
      from("$buildDir/libs/${fatJar.archiveName}")
      into("$projectDir/repo/com/sendgrid")
    }
    file("$projectDir/repo/com/sendgrid/${fatJar.archiveName}").renameTo(file("$projectDir/repo/com/sendgrid/sendgrid-java-${version}.jar"))
  }
}

task javadocJar(type: Jar, dependsOn: javadoc) {
  classifier = 'javadoc'
  from 'build/docs/javadoc'
}

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

signing {
  required { gradle.taskGraph.hasTask("uploadArchives") }
  sign configurations.archives
}

uploadArchives {
  repositories {
    mavenDeployer {
      beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
      repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
        authentication(userName: sonatypeUsername, password: sonatypePassword)
      }

      pom.project {
        name 'sendgrid-java'
        packaging 'jar'
        description 'SendGrid Java helper library'
        url 'https://github.com/sendgrid/sendgrid-java'

        scm {
          url 'scm:git@github.com:sendgrid/sendgrid-java.git'
          connection 'scm:git@github.com:sendgrid/sendgrid-java.git'
          developerConnection 'scm:git@github.com:sendgrid/sendgrid-java.git'
        }

        licenses {
          license {
            name 'MIT License'
            url 'http://opensource.org/licenses/MIT'
            distribution 'repo'
          }
        }

        developers {
          developer {
            id 'thinkingserious'
            name 'Elmer Thomas'
          }
          developer {
            id 'scottmotte'
            name 'Scott Motte'
          }
          developer {
            id 'elbou8'
            name 'Yamil Asusta'
          }
          developer {
            id 'eddiezane'
            name 'Eddie Zaneski'
          }
        }
      }
    }
  }
}

artifacts {
  archives fatJar
  archives jar
  archives javadocJar
  archives sourcesJar
}