buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id 'com.github.johnrengelman.shadow' version '6.1.0'
    id 'java'
    id 'application'
    id 'org.cadixdev.licenser' version '0.6.1'
}

apply plugin: 'checkstyle'
apply plugin: 'signing'
apply plugin: 'maven-publish'
apply plugin: 'java-library'

def bablVersion = file('version.txt').text.trim()
group = 'com.aitusoftware'
version = bablVersion

defaultTasks('clean', 'licenseFormat', 'check', 'assemble', 'shadowJar')

repositories {
    mavenCentral()
}

ext {
    fullName = 'Babl WebSocket Server'
    fullDescription = 'Babl: A High-Performance WebSocket Server'
    teamName = 'Aitu Software Limited'
    siteUrl = 'https://babl.ws'
    sourceUrl = 'https://github.com/babl-ws/babl.git'

    javaCompilerExecutable = System.env['JAVA_HOME'] ? System.env['JAVA_HOME'] + '/bin/javac' : 'javac'
    if (!project.hasProperty('sonatypeUrl')) sonatypeUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
    if (!project.hasProperty('sonatypeUsername')) sonatypeUsername = ''
    if (!project.hasProperty('sonatypePassword')) sonatypePassword = ''
}

static def getBuildJavaVersion() {
    def buildJavaVersion = System.getenv('BUILD_JAVA_VERSION') ?: JavaVersion.current().getMajorVersion()

    if (buildJavaVersion.indexOf('.') > 0) {
        buildJavaVersion = buildJavaVersion.substring(0, buildJavaVersion.indexOf('.'))
    }

    if (buildJavaVersion.indexOf('-') > 0) {
        buildJavaVersion = buildJavaVersion.substring(0, buildJavaVersion.indexOf('-'))
    }

    Integer.parseInt(buildJavaVersion)
}
int buildJavaVersion = getBuildJavaVersion()

compileJava {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}


java {
    withJavadocJar()
    withSourcesJar()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = 'babl'
            from components.java
            pom {
                name = fullName
                description = fullDescription
                url = siteUrl
                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id = 'epickrram'
                        name = 'Mark Price'
                        email = 'babl@aitusoftware.com'
                    }
                }
                scm {
                    connection = "scm:git:https://github.com/babl-ws/babl.git"
                    developerConnection = "scm:git:https://github.com/babl-ws/babl.git"
                    url = sourceUrl
                }
            }
        }
    }

    repositories {
        maven {
            name = 'MavenCentral'
            url = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
            credentials {
                username = sonatypeUsername
                password = sonatypePassword
            }
        }
    }
}

signing {
    sign publishing.publications.mavenJava
}


shadowJar {
    mainClassName = 'com.aitusoftware.babl.websocket.BablServer'
}

configurations {
    codegen
    jmh
}

File generatedSrcDir = file("${buildDir}/generated-sources/java")

sourceSets {
    main {
        java.srcDirs += generatedSrcDir
    }
    jmh {
        java.srcDirs += 'src/jmh/java'
        compileClasspath += project.sourceSets.test.compileClasspath
        runtimeClasspath += project.sourceSets.main.compileClasspath
    }
}

dependencies {
    implementation 'org.agrona:agrona:1.14.0'
    implementation 'io.aeron:aeron-driver:1.37.0'

    codegen 'uk.co.real-logic:sbe-all:1.25.1'

    jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.32'

    jmhImplementation 'org.openjdk.jmh:jmh-core:1.32'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
    testImplementation 'org.mockito:mockito-core:3.11.0'
    testImplementation 'com.google.truth:truth:1.1.2'
    testImplementation 'io.vertx:vertx-core:3.9.4'
    testImplementation 'io.vertx:vertx-web:3.9.4'
    testImplementation 'org.hdrhistogram:HdrHistogram:2.1.12'
    testImplementation 'org.awaitility:awaitility:4.1.0'
    testImplementation 'com.caucho:resin:4.0.65'

    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.1'
}

compileJava.dependsOn('generateCodecs')

task generateCodecs(type: JavaExec) {
    mainClass.set('uk.co.real_logic.sbe.SbeTool')
    classpath configurations.codegen.asPath
    jvmArgs = ["-Dsbe.output.dir=${generatedSrcDir.getAbsolutePath()}", '-Dsbe.generate.ir=true', '-Dsbe.java.generate.interfaces=true']
    args "${project.projectDir}/src/main/resources/sbe-schema.xml"
}

test {
    useJUnitPlatform()
    if (buildJavaVersion >= 9) {
        jvmArgs = [
            "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED"
        ]
    }
}

task benchmarkJar(type: Jar) {
    manifest {
        attributes('Main-Class': 'org.openjdk.jmh.Main')
    }
    archiveClassifier = 'benchmarks'
    from sourceSets.jmh.output
    from sourceSets.main.output
    from sourceSets.jmh.runtimeClasspath.collect {
        if (it.exists()) {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

jar {
    manifest {
        attributes(
            "Implementation-Title": "Babl",
            "Implementation-Version": project.version,
            "Implementation-Vendor": "Aitu Software Limited",
            "Automatic-Module-Name": "com.aitusoftware.babl"
        )
    }
}

artifacts {
    archives sourcesJar, javadocJar
}

javadoc {
}

checkstyle {
    toolVersion = '8.20'
    sourceSets = project.sourceSets
}

license {
    header = new File(rootDir, 'config/header.txt')
    newLine = false
}
