/*
 * fdb-record-layer-core.gradle
 *
 * This source file is part of the FoundationDB open source project
 *
 * Copyright 2015-2018 Apple Inc. and the FoundationDB project authors
 *
 * Licensed 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 {
    alias(libs.plugins.gitversion)
    id 'java-test-fixtures'
}

apply from: rootProject.file('gradle/publishing.gradle')


task createVersionPropertiesFile()  {
    // Write a file of version and build info for the java processes to read
    // from their classpaths.
    def versionsFile = new File(projectDir, "src/gen/main/resources/version.properties")
    def projectVersion = project.version
    def rootProjectName = rootProject.name
    def gitDetails = versionDetails()

    outputs.file versionsFile
    doLast {
        println "Writing ${versionsFile}"
        versionsFile.text = """# Generated by fdb-relational-api build.
# Returned by RelationalDatabaseMetaData#getDatabaseProductName()
name=${rootProjectName}
# Returned by RelationalDatabaseMetaData#getDatabaseProductVersion()
# and by RelationalDatabaseMetaData#getDriverVersion()
version=${projectVersion}
gitHash=${gitDetails.gitHashFull}
branch=${gitDetails.branchName}
# Returned as RelationalDatabaseMetaData#getUrl()
url=${url.replace('\'', '')}
user=${System.getProperty("user.name")}
buildtime=${java.time.Instant.now()}
buildHost=${InetAddress.getLocalHost().getCanonicalHostName()}
"""
    }
    outputs.upToDateWhen { false }
}

dependencies {
    api(project(':fdb-java-annotations')) {
        exclude(group: "com.squareup", module: "javapoet")
    }
    api(libs.protobuf)
    compileOnly(libs.jsr305)
    implementation(libs.guava)

    testImplementation(libs.bundles.test.impl)
    testImplementation(libs.bundles.test.runtime)
    testCompileOnly(libs.jsr305)

    testFixturesImplementation(libs.assertj)
}

sourceSets {
    main {
        java.srcDir ".out/generated/sources/annotationProcessor/java/main/"
        resources {
            // Add these two dirs to the default src/main/java.
            srcDir("${projectDir}/src/gen/main/resources")
        }
    }
}

sourcesJar {
    dependsOn "createVersionPropertiesFile"
}

compileJava {
    dependsOn "createVersionPropertiesFile"
}

processResources {
    dependsOn "createVersionPropertiesFile"
}

clean {
    delete "${projectDir}/src/gen/"
}

publishing {
    publications {
        library(MavenPublication) {
            pom {
                // Normally, the variable would be included within the string using variable expansion in a Groovy
                // string. However, the "description" field must be a java.lang.String, hence the use of string
                // concatenation here.
                description = 'Relational Layer API.'
            }
        }
    }
}
