/*
 * 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.
 */

plugins {
    id 'accord.java-conventions'
    id 'maven-publish'
}

java {
  withJavadocJar()
  withSourcesJar()
}

repositories {
    mavenCentral()
    // needed for clojure dependencies
    maven { url "https://clojars.org/repo" }
}

dependencies {
    implementation group: "com.google.guava", name: "guava", version: "27.0-jre"
    implementation group: "net.ju-n.compile-command-annotations", name: "compile-command-annotations", version: "1.2.0"

    // Dependencies we depend on that are not part of our API.
    // These act as runtimeOnly dependencies to users
    implementation 'org.slf4j:slf4j-api:1.7.36'
    implementation 'org.agrona:agrona:1.17.1'

    testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.24.2'
    testImplementation 'org.clojure:clojure:1.11.1'
    testImplementation 'elle:elle:0.1.7'
    // for some reason this isn't pulled in properly?  Have to be explicit
    testImplementation('spootnik:unilog:0.7.31') {
        exclude group: 'ch.qos.logback'

        exclude group: 'org.slf4j', module: 'slf4j-api'
        exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
        exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
    }
    testImplementation group: 'org.awaitility', name: 'awaitility', version: '4.2.0'
}

task burn(type: JavaExec) {
    classpath sourceSets.main.runtimeClasspath, sourceSets.test.runtimeClasspath
    mainClass = 'accord.burn.BurnTest'
    jvmArgs '-Dlogback.configurationFile=burn-logback.xml -Xmx4g'
    args = ['-c', '1']
}

// the team is used to 'install' so make sure that still publishes locally
task install(dependsOn: publishToMavenLocal)
task testJar(type: Jar, dependsOn: testClasses) {
  archiveClassifier = 'tests'
  from sourceSets.test.output
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      artifactId = accord_artifactId
      pom {
        name = 'Apache Cassandra Accord'
        description = 'General purpose transactions library using a leaderless consensus protocol to have highly available transactions'
        url = 'https://cwiki.apache.org/confluence/display/CASSANDRA/CEP-15%3A+General+Purpose+Transactions'
        inceptionYear = '2021'
        licenses {
          license {
            name = 'The Apache Software License, Version 2.0'
            url = 'https://www.apache.org/licenses/LICENSE-2.0.txt'
          }
        }
        scm {
          connection = 'scm:https://gitbox.apache.org/repos/asf/cassandra-accord.git'
          developerConnection = 'scm:https://gitbox.apache.org/repos/asf/cassandra-accord.git'
          url = 'https://gitbox.apache.org/repos/asf?p=cassandra-accord.git;a=tree'
        }
      }
      // This is a hack to remove accord from the dependency list as this breaks Apache Cassandra
      // This is a bug with Gradle that was supposed to be fixed in 8.0 but does not seem to work
      // see https://github.com/gradle/gradle/commit/6e596c638dff59dfa06032a7e7a3974275ddb3e1
      pom.withXml {
        asNode().dependencies.'*'.findAll() { it.artifactId.text() == accord_artifactId }.each() { it.parent().remove(it) }
      }
      from components.java
      // don't need to add javadoc or sources as they were added already in the java block
      artifact testJar
    }
  }
}

task burnforkloop {
    doLast {
        (project.hasProperty('burnTimes') ? Integer.parseInt(project.getProperty('burnTimes')) : Integer.MAX_VALUE)
                .times {
                    javaexec {
                        classpath sourceSets.main.runtimeClasspath, sourceSets.test.output
                        mainClass = 'accord.burn.BurnTest'
                        jvmArgs '-Dlogback.configurationFile=burn-logback.xml'
                        args = ['-c', '1']
                    }
                }
    }
}

task burnloop {
    doLast {
        javaexec {
            classpath sourceSets.main.runtimeClasspath, sourceSets.test.runtimeClasspath
            mainClass = 'accord.burn.BurnTest'
            jvmArgs '-Dlogback.configurationFile=burn-logback.xml -Xmx4g'
            args = ['-c', project.hasProperty('burnTimes') ? project.getProperty('burnTimes') : '1000000']
        }
    }
}

tasks {
    javadoc {
        options { addStringOption("Xdoclint:none", "-quiet") }
    }
}

task allTestJar(type: Jar) {
    dependsOn testJar
    archiveBaseName = project.name + '-test-all'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from { configurations.testCompileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    from { configurations.testRuntimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    from { sourceSets.test.output }
    with jar
}

test {
    maxHeapSize = '4g'
}