plugins {
    id 'java-library'
    id 'maven-publish'
    id 'org.jreleaser' version '1.14.0'
}

repositories {
    mavenCentral()
}

dependencies {
  // none
}

String implementationTitle = 'Antithesis FFI for Java' // !! LOAD BEARING Title: used by com.antithesis.sdk.internal.loadSDKVersion

// Custom Javadoc task configuration
tasks.named('javadoc', Javadoc) {
    source = sourceSets.main.allJava
    exclude 'com.antithesis.sdk.internal.**'

    // Hack to enable Javadoc to compile while excluding internal functions
    var sourceSetDirectories = sourceSets.main.java.srcDirs.join(":")
    options.addStringOption("sourcepath", sourceSetDirectories)

    classpath = sourceSets.main.compileClasspath
    destinationDir = file("${buildDir}/docs/javadoc")
}

java {
    withJavadocJar()
    withSourcesJar()
}

publishing {
    publications {
        ffi(MavenPublication) {
            groupId = 'com.antithesis'
            artifactId = 'ffi'

            from components.java
            pom {
                name = "${implementationTitle}"
                description = 'This internal library provides methods for Java programs to interface with Antithesis platform.'
                url = 'https://antithesis.com/docs/using_antithesis/sdk/java/overview/'

                licenses {
                    license {
                        name = 'MIT License'
                        url = 'https://github.com/antithesishq/antithesis-sdk-java/blob/main/LICENSE'
                    }
                }

                developers {
                    developer {
                        id = 'Antithesis'
                        name = 'Antithesis HQ'
                        email = 'noreply@antithesis.com'
                    }
                }

                scm {
                    connection = 'scm:git:git://github.com/antithesishq/antithesis-sdk-java.git'
                    developerConnection = 'scm:git:ssh://github.com/antithesishq/antithesis-sdk-java.git'
                    url = 'https://github.com/antithesishq/antithesis-sdk-java'
                }
            }
        }
    }
    repositories {
        maven {
            url = layout.buildDirectory.dir('staging-deploy')
        }
    }
}

jreleaser {
    gitRootSearch = true
    signing {
        active = 'ALWAYS'
        armored = true
    }
    deploy {
        maven {
            mavenCentral {
                sonatype {
                    applyMavenCentralRules = true
                    active = 'ALWAYS'
                    url = 'https://central.sonatype.com/api/v1/publisher'
                    stagingRepository('build/staging-deploy')
                }
            }
        }
    }
}
jreleaserFullRelease.dependsOn(publish)

compileJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    options.compilerArgs += ["-Xlint:-options"]
}

task buildFfiBridge(type: Exec) {
    workingDir projectDir
    environment.putIfAbsent("JAVA_HOME", System.properties['java.home'])
    if (file("./ffi_build.sh").isFile()) {
        commandLine 'sh', '-c', './ffi_build.sh'
    }
    else {
        // Enter the swig directory and make the 'build' target
        commandLine 'make', '-C', 'swig', 'build'
    }
}
compileJava.dependsOn buildFfiBridge
compileTestJava.dependsOn buildFfiBridge

task cleanFfiBridge(type: Exec) {
    workingDir projectDir
    if (!file("./ffi_build.sh").isFile()) {
        // Enter the swig directory and make the 'clean' target
        commandLine 'make', '-C', 'swig', 'clean'
    }
}
clean.dependsOn cleanFfiBridge

jar {
    archiveClassifier = ''
    archiveAppendix = 'ffi'
    archiveBaseName = 'antithesis'
    manifest {
        attributes 'Implementation-Title': "${implementationTitle}",
                'Implementation-Version': "${project.version}",
                'Specification-Title': 'Antithesis SDK Protocol',
                'Specification-Version': "${project.findProperty('com.antithesis.sdk.protocol') ?: '0.0.0'}",
                'Created-By': "Gradle ${gradle.gradleVersion}",
                'Build-Jdk': "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
                'Build-OS': "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
    }
    reproducibleFileOrder = true
    preserveFileTimestamps = false
}
