/*
 * MIT License
 *
 * Copyright (c) 2020-present Cloudogu GmbH and Contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

plugins {
  id 'war'
  id 'org.scm-manager.java'
  id 'org.scm-manager.run'
  id 'org.scm-manager.core-plugins'
  id 'io.swagger.core.v3.swagger-gradle-plugin' version '2.1.6'
}

configurations {
  assets
  webapp {
    canBeConsumed = true
    canBeResolved = false
  }
}

dependencies {
  implementation platform(project(':'))

  corePlugin project(path: ':scm-plugins:scm-git-plugin', configuration: 'smp')
  corePlugin project(path: ':scm-plugins:scm-hg-plugin', configuration: 'smp')
  corePlugin project(path: ':scm-plugins:scm-svn-plugin', configuration: 'smp')
  corePlugin project(path: ':scm-plugins:scm-legacy-plugin', configuration: 'smp')
  assets project(path: ':scm-ui', configuration: 'assets')

  implementation project(':scm-core')
  implementation project(':scm-dao-xml')
  testImplementation project(':scm-test')
  annotationProcessor project(':scm-annotation-processor')

  // servlet api
  providedCompile libraries.servletApi
  testImplementation libraries.servletApi

  // security
  implementation libraries.shiroWeb
  implementation libraries.shiroGuice

  // jwt
  implementation libraries.jjwtApi
  implementation libraries.jjwtRuntime
  implementation libraries.jjwtJackson

  // gpg
  implementation libraries.bouncycastlePg
  implementation libraries.bouncycastleProv
  implementation libraries.bouncycastlePkix

  // json
  implementation libraries.jacksonJaxbAnnotations
  implementation libraries.jacksonJaxRsBase
  implementation libraries.jacksonJaxRsJsonProvider
  implementation libraries.jacksonDatatypeJdk8
  implementation libraries.jacksonDatatypeJsr310

  // rest api
  implementation libraries.resteasyJaxbProvider
  implementation libraries.resteasyJackson2Provider
  implementation libraries.resteasyMultiartProvider
  implementation libraries.resteasyGuice
  implementation libraries.resteasyServletInitializer
  implementation libraries.resteasyValidatorProvider

  // openapi
  compileOnly libraries.swaggerJaxRs

  // logging
  implementation libraries.logback

  // cron expression
  implementation libraries.cronUtils

  // templates
  implementation libraries.mustache

  // static resources
  implementation libraries.webResources

  // content type detection
  implementation libraries.spotter
  implementation libraries.tika

  // restart on unix
  implementation libraries.akuma

  // native access
  implementation libraries.jna

  // util
  implementation libraries.commonsCompress

  // events
  implementation libraries.legmanShiro
  implementation libraries.legmanMicrometer

  // metrics
  implementation libraries.micrometerExtra

  // lombok
  compileOnly libraries.lombok
  testCompileOnly libraries.lombok
  annotationProcessor libraries.lombok
  testAnnotationProcessor libraries.lombok

  // dto mapping
  annotationProcessor libraries.mapstructProcessor

  // testing async code
  testImplementation libraries.awaitility

  // shiro unit
  testImplementation libraries.shiroUnit
}

war {
  from 'build/war'
  from project.configurations.assets
  into('WEB-INF/classes/META-INF/scm') {
    from('build/openapi')
  }
  dependsOn 'copy-core-plugins', 'resolve'
}

artifacts {
  webapp(war)
}

// war without assets for development and livereload
task 'dev-war' (type: War) {
  archiveName 'scm-webapp-dev.war'
  from 'build/war'
  into('WEB-INF/classes/META-INF/scm') {
    from('build/openapi')
  }
  dependsOn 'copy-core-plugins', 'resolve'
}

scmServer {
  liveReload = true
  openBrowser = true
  warFile = file('build/libs/scm-webapp-dev.war')
  loggingConfiguration = file('src/main/conf/logback.serve.xml')
}

run {
  dependsOn 'dev-war'
}

resolve {
  outputFileName = 'openapi'
  outputFormat = 'JSONANDYAML'
  prettyPrint = 'TRUE'
  classpath = sourceSets.main.runtimeClasspath
  resourcePackages = ['sonia.scm.api.v2.resources']
  outputDir = file('build/openapi')
  openApiFile = file('build/tmp/openapi.yml')
}

task prepareOpenAPI(type: Copy) {
  from 'src/main/doc/openapi.yml'
  into 'build/tmp'
  expand(version: project.version)
}

tasks.getByName("resolve").configure {
  dependsOn 'prepareOpenAPI'
}

/**
WTF???
<!-- fix javadoc -->

<dependency>
  <groupId>javax.transaction</groupId>
  <artifactId>jta</artifactId>
  <version>1.1</version>
  <scope>provided</scope>
</dependency>

<!--
fix java.lang.NoClassDefFoundError org/w3c/dom/ElementTraversal
-->

<dependency>
  <groupId>xml-apis</groupId>
  <artifactId>xml-apis</artifactId>
  <version>1.4.01</version>
</dependency>
**/
