/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

buildscript {

  apply from: "${rootProject.projectDir}/gradle/scripts/repositories.gradle", to: buildscript

  dependencies {
    classpath (group: 'com.linkedin.pegasus', name: 'gradle-plugins', version: rootProject.pegasusVersion) {
      exclude group: 'org.apache.ant'
    }
  }

}

apply plugin: 'java'
apply plugin: 'pegasus'

if (file('extraDependencies.gradle').exists()) {
  apply from: 'extraDependencies.gradle'
}

dependencies {
  dataModel externalDependency.pegasus.restliCommon

  compile externalDependency.pegasus.data
  compile externalDependency.pegasus.restliClient
  compile externalDependency.pegasus.restliCommon
  compile externalDependency.pegasus.pegasusCommon
}

// Configure sources and javadoc jars for the data template library
def dataTemplateName = project.name + "-data-template"

task dataTemplateSourcesJar(type: Jar, dependsOn: classes) {
  from sourceSets.mainGeneratedDataTemplate.allSource
  classifier = 'sources'
  baseName = dataTemplateName
}

task dataTemplateJavadoc(type: Javadoc, dependsOn: classes) {
  source sourceSets.mainGeneratedDataTemplate.allSource
  classpath = files(sourceSets.mainGeneratedDataTemplate.compileClasspath)
  destinationDir file("${rootProject.buildDir}/${dataTemplateName}/docs/javadoc")
}

task dataTemplateJavadocJar(type: Jar, dependsOn: 'dataTemplateJavadoc') {
  from dataTemplateJavadoc
  baseName = dataTemplateName
  classifier = 'javadoc'
}

artifacts {
  archives mainDataTemplateJar, dataTemplateSourcesJar, dataTemplateJavadocJar
}

def generateRestClient = file("${project.projectDir}/src/main/idl").exists()

if (generateRestClient) {

  // Configure sources and javadoc jars for the rest client  library
  def restClientName = project.name + "-rest-client"

  task restClientSourcesJar(type: Jar, dependsOn: classes) {
    from sourceSets.mainGeneratedRest.allSource
    classifier = 'sources'
    baseName = restClientName
  }

  task restClientJavadoc(type: Javadoc, dependsOn: classes) {
    source sourceSets.mainGeneratedRest.allSource
    classpath = files(sourceSets.mainGeneratedRest.compileClasspath)
    destinationDir file("${rootProject.buildDir}/${restClientName}/docs/javadoc")
  }

  task restClientJavadocJar(type: Jar, dependsOn: 'restClientJavadoc') {
    from restClientJavadoc
    baseName = restClientName
    classifier = 'javadoc'
  }

  artifacts {
    archives mainRestClientJar, restClientSourcesJar, restClientJavadocJar
  }

  publishing.publications {
      restClient(MavenPublication) {
        artifactId restClientName
        artifacts = [mainRestClientJar, restClientSourcesJar, restClientJavadocJar]
        pom pomAttributes
        pom.withXml addRuntimeDependenciesToPom
      }
  }
}

install {
  repositories {
    mavenInstaller {
      addFilter('data-template') { artifact, file -> artifact.name.contains("data-template")
      }
      addFilter('rest-client') { artifact, file -> artifact.name.contains("rest-client")
      }
      addFilter('') { artifact, file -> artifact.name == project.name
      }
    }
  }
}

if (rootProject.ext.publishToMaven || rootProject.ext.publishToNexus) {
  uploadArchives {
    repositories {
      mavenDeployer {
        addFilter('data-template') { artifact, file -> artifact.name.contains("data-template")
        }.project(rootProject.pomAttributes)
        addFilter('rest-client') { artifact, file -> artifact.name.contains("rest-client")
        }.project(rootProject.pomAttributes)
        addFilter('') { artifact, file -> artifact.name == project.name
        }.project(rootProject.pomAttributes)
      }
    }
  }
}

publishing.publications {
    dataTemplate(MavenPublication) {
      artifactId dataTemplateName
      artifacts = [mainDataTemplateJar, dataTemplateSourcesJar, dataTemplateJavadocJar]
      pom pomAttributes
      pom.withXml addRuntimeDependenciesToPom
    }
}

artifactory {
  publish {
    defaults {
      if (generateRestClient) {
        publications('mavenJava', 'restClient', 'dataTemplate')
      } else {
        publications('mavenJava', 'dataTemplate')
      }
    }
  }
}
