/*
 * This file was generated by the Gradle 'init' task.
 *
 * This project uses @Incubating APIs which are subject to change.
 */

plugins {
    // Apply the java Plugin to add support for Java.
    id 'java'
    
    // Git based versioning
    id 'pl.allegro.tech.build.axion-release'
    
    // Apply eclipse plugin
    id 'eclipse'
    
    // Access to git information
    id 'org.ajoberstar.grgit'
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
    mavenLocal()
}

dependencies {
    constraints {
        // Define dependency versions as constraints
        // implementation 'org.apache.commons:commons-text:1.9'
    }
}

// Configure sensible layout
sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
        resources {
            srcDirs = ['resources']
        }
    }

    test {
        java {
            srcDirs = ['test']
        }
        resources {
            srcDirs = ['test-resources']
        }
    }
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

scmVersion {
    versionIncrementer 'incrementMinor'
    tag {
        def shortened = project.name.startsWith(project.group + ".") ?
            project.name.substring(project.group.length() + 1) : project.name
        prefix = shortened.replace('.', '-') + "-"
    }
}
version = scmVersion.version
ext.isSnapshot = version.endsWith('-SNAPSHOT')

jar {
    manifest {
        inputs.property("gitDescriptor", { grgit.describe(always: true) })
        
        // Set Git revision information in the manifests of built bundles
        attributes([
            "Implementation-Title": project.name,
            "Implementation-Version": "$project.version (${grgit.describe(always: true)})",
            "Implementation-Vendor": grgit.repository.jgit.repository.config.getString("user", null, "name")
                + " (" + grgit.repository.jgit.repository.config.getString("user", null, "email") + ")",
            "Git-Descriptor": grgit.describe(always: true),
            "Git-SHA": grgit.head().id,
        ])
    }
}

eclipse {

    project {
        file {
            // closure executed after .project content is loaded from existing file
            // and before gradle build information is merged
            beforeMerged { project ->
                project.natures.clear()
                project.buildCommands.clear()
            }

            project.natures += 'org.eclipse.buildship.core.gradleprojectnature'
            project.buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'

            if (plugins.hasPlugin('java')) {
                project.buildCommand 'org.eclipse.jdt.core.javabuilder'
                project.natures += 'net.sf.eclipsecs.core.CheckstyleNature'
                project.buildCommand 'net.sf.eclipsecs.core.CheckstyleBuilder'
                project.natures += 'ch.acanda.eclipse.pmd.builder.PMDNature'
                project.buildCommand 'ch.acanda.eclipse.pmd.builder.PMDBuilder'
            }

            if (plugins.hasPlugin('biz.aQute.bnd')) {
                project.natures += 'bndtools.core.bndnature'
                project.buildCommand 'bndtools.core.bndbuilder'
            }
        }
    }
    
    classpath {
        downloadJavadoc = true
        downloadSources = true
    }

    jdt {
        file {
            withProperties { properties ->
                def formatterPrefs = new Properties()
                rootProject.file("gradle/org.eclipse.jdt.core.formatter.prefs")
                    .withInputStream { formatterPrefs.load(it) }
                properties.putAll(formatterPrefs)
            }
        }
    }
}

testing {
    suites {
        // Configure the built-in test suite
        test {
            // Use JUnit Jupiter test framework
            useJUnitJupiter('5.9.1')
        }
    }
}
