# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               github 1.0
PortGroup               java 1.0

name                    junit5
github.setup            junit-team junit-framework 5.14.4 r
github.tarball_from     archive
github.livecheck.regex  {(5\.[0-9.]+)}
revision                2

categories              java devel
license                 EPL-2
platforms               any
supported_archs         noarch
homepage                https://junit.org
maintainers             {sethm.id.au:dev @McDaMastR} \
                        openmaintainer

description             Programmer-friendly testing framework for Java
long_description        JUnit 5 is a simple framework to write repeatable \
                        tests.  It is an instance of the xUnit architecture \
                        for unit testing frameworks.

depends_build-append    port:apiguardian \
                        port:gradle8
depends_lib-append      port:opentest4j
depends_test-append     port:apiguardian

# Building requires Java == 8 and Java == 21
# Running requires Java >= 8
#
# Until the java PortGroup has a way to specify multiple required JDKs,
# just depend on JDK 21 as required for building
# and let Gradle fetch JDK 8 if not found on the system.
#
# When this functionality is available, add
# --project-prop=org.gradle.java.installations.auto-download=false
# to the build and test arguments to prevent Gradle from fetching JDKs.
#
# Gradle can have trouble fetching JDK 21:
# <https://trac.macports.org/ticket/74533>
java.version            21
java.fallback           openjdk21

set gradle_home         ${workpath}/.home/.gradle

# Build requires a Git repo
fetch.type              git

patchfiles              update-develocity-hostname.diff \
                        use-installed-jars.diff

post-patch {
    reinplace -W ${worksrcpath} \
        "s|@@PREFIX@@|${prefix}|g" \
        documentation/documentation.gradle.kts \
        junit-jupiter-api/junit-jupiter-api.gradle.kts \
        junit-jupiter-engine/junit-jupiter-engine.gradle.kts \
        junit-jupiter-migrationsupport/junit-jupiter-migrationsupport.gradle.kts \
        junit-jupiter-params/junit-jupiter-params.gradle.kts \
        junit-platform-commons/junit-platform-commons.gradle.kts \
        junit-platform-console/junit-platform-console.gradle.kts \
        junit-platform-console-standalone/junit-platform-console-standalone.gradle.kts \
        junit-platform-engine/junit-platform-engine.gradle.kts \
        junit-platform-jfr/junit-platform-jfr.gradle.kts \
        junit-platform-launcher/junit-platform-launcher.gradle.kts \
        junit-platform-reporting/junit-platform-reporting.gradle.kts \
        junit-platform-runner/junit-platform-runner.gradle.kts \
        junit-platform-suite-api/junit-platform-suite-api.gradle.kts \
        junit-platform-suite-commons/junit-platform-suite-commons.gradle.kts \
        junit-platform-suite-engine/junit-platform-suite-engine.gradle.kts \
        junit-platform-testkit/junit-platform-testkit.gradle.kts \
        junit-vintage-engine/junit-vintage-engine.gradle.kts \
        platform-tests/platform-tests.gradle.kts \
        platform-tooling-support-tests/platform-tooling-support-tests.gradle.kts
}

use_configure           no

build.cmd               gradle8
build.target            build
build.args              --no-daemon \
                        --exclude-task=check \
                        --exclude-task=javadocJar \
                        --exclude-task=sourcesJar \
                        --gradle-user-home="${gradle_home}" \
                        --max-workers=${build.jobs}

test.run                yes
test.target             check
test.args               --no-daemon \
                        --exclude-task=spotlessJava \
                        --gradle-user-home="${gradle_home}" \
                        --max-workers=${build.jobs} \
                        --project-prop=publishing.signArtifacts=false \
                        --system-prop=platform.tooling.support.tests.enabled=false \
                        --system-prop=pts.enabled=false

destroot {
    set destdocdir  ${destroot}${prefix}/share/doc/${name}
    set destjavadir ${destroot}${prefix}/share/java/${name}

    set modules {
        junit-jupiter
        junit-jupiter-api
        junit-jupiter-engine
        junit-jupiter-migrationsupport
        junit-jupiter-params
        junit-platform-commons
        junit-platform-console
        junit-platform-console-standalone
        junit-platform-engine
        junit-platform-jfr
        junit-platform-launcher
        junit-platform-reporting
        junit-platform-runner
        junit-platform-suite
        junit-platform-suite-api
        junit-platform-suite-commons
        junit-platform-suite-engine
        junit-platform-testkit
        junit-vintage-engine
    }

    xinstall -d ${destjavadir}
    foreach module ${modules} {
        # Platform jar files are named *-1.x.x.jar instead of *-5.x.x.jar
        xinstall -m 644 \
            [glob -directory ${worksrcpath}/${module}/build/libs *.jar] \
            ${destjavadir}/${module}.jar
    }

    xinstall -d ${destdocdir}
    xinstall -W ${worksrcpath} -m 644 \
        CODE_OF_CONDUCT.md \
        CONTRIBUTING.md \
        LICENSE.md \
        NOTICE.md \
        README.md \
        RELEASING.md \
        SECURITY.md \
        ${destdocdir}

    if {[variant_isset doc]} {
        xinstall -d ${destdocdir}/javadoc
        foreach module ${modules} {
            # Not all modules contain javadoc
            if {[file exists ${worksrcpath}/${module}/build/docs/javadoc]} {
                copy \
                    ${worksrcpath}/${module}/build/docs/javadoc \
                    ${destdocdir}/javadoc/${module}
            }
        }
    }
}

variant doc \
        description {Install javadoc} {
    build.target-append javadoc
}
