buildscript {
  project.ext {
    if(has('privateRepoDir')) {
      privateRepoDir = privateRepoDir instanceof File ? privateRepoDir.path : privateRepoDir.toString()
      if(!new File(privateRepoDir).isAbsolute())
        privateRepoDir = new File(projectDir, privateRepoDir).canonicalPath
    } else
      privateRepoDir = new File(project.buildDir, 'privateRepo').canonicalPath
  }

  repositories {
    maven {
      url "file:$privateRepoDir"
    }
    jcenter()
    maven {
      url 'http://dl.bintray.com/content/noamt/gradle-plugins'
    }
  }

  dependencies {
    classpath 'org.apache.commons:commons-lang3:3.4'
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
    classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:3.1.0'
    classpath 'org._10ne.gradle:rest-gradle-plugin:0.3.2'
    classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
  }
}

import org._10ne.gradle.rest.RestPlugin
import org._10ne.gradle.rest.RestTask
import groovyx.net.http.ContentType

plugins {
  id 'base'
  id 'maven-publish'
}

apply plugin: 'com.jfrog.artifactory'
apply plugin: RestPlugin

ext {
  project_website = "https://github.com/${developerId}/${projectId}"
  project_scm = "scm:git@github.com:${developerId}/${projectId}.git"
  license_url = "https://raw.github.com/${developerId}/${projectId}/master/LICENSE"

  if( !has( 'bintrayUser' ) )
    bintrayUser = ''

  if( !has( 'bintrayKey' ) )
    bintrayKey = ''

  if( !has( 'bintrayKeyPhrase' ) )
    bintrayKeyPhrase = ''

  if( !has( 'bintrayDryRun' ) )
    bintrayDryRun = false

  if( !has('artifactory_contextUrl'))
    artifactory_contextUrl = 'https://oss.jfrog.org/artifactory'
}

if(!bintrayDryRun)
  task bintraySign(type: RestTask) {
    description 'Push to bintray and sign it.'
    httpMethod = 'post'
    uri = "https://api.bintray.com/gpg/${project.developerId}/maven/${project.projectId}/versions/${project.version}"
    username = project.bintrayUser
    password = project.bintrayKey
    requestBody = [ passphrase: project.bintrayKeyPhrase ]
    contentType = ContentType.JSON
  }

task('buildIntegrationTests', type: GradleBuild) {
  dir = file('integrationTests')
  tasks = [ 'build' ]
  project.tasks.build.finalizedBy it
  onlyIf { !project.tasks.build.getState().getFailure() }
}

task('testAll', dependsOn: tasks.build)

task('testAllIntegrationTests', type: GradleBuild) {
  dependsOn buildIntegrationTests
  dir = file('integrationTests')
  tasks = [ 'testAll' ]
  project.tasks.testAll.finalizedBy it
  onlyIf { !project.tasks.testAll.getState().getFailure() }
}

task('cleanIntegrationTests', type: GradleBuild) {
  dir = file('integrationTests')
  tasks = [ 'clean' ]
  project.tasks.clean.dependsOn it
}

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

artifactory {
  contextUrl = artifactory_contextUrl
  publish {
    repository {
      if (project.version.endsWith('-SNAPSHOT'))
        repoKey = 'oss-snapshot-local'
      else
        repoKey = 'oss-release-local'
      username = project.bintrayUser
      password = project.bintrayKey
    }
  }
  resolve {
    repository {
      repoKey = 'repo'
    }
  }
}
