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

PortSystem          1.0
PortGroup           github 1.0
PortGroup           python 1.0

github.setup        deepmind tree 0.1.5
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                py-dm-${github.project}
revision 0

license             Apache-2
maintainers         nomaintainer

description         Deepmind tree is a library for working with nested \
                    data structures.
long_description    {*}${description} In a way, tree generalizes the builtin \
                    map function which only supports flat sequences, and \
                    allows to apply a function to each "leaf" preserving \
                    the overall structure.

checksums           rmd160  be34ef9fce02a8812f14b57a24f392649789399b \
                    sha256  c8389c2e58cb02d4721985c6f003ab9b8f5cfe58e678b52501fc586cf9a0b724 \
                    size    34622

python.versions     39

proc python_listify {tcl_list} {
    set python_list {}
    foreach item [lreverse ${tcl_list}] {
        set python_list "'${item}', ${python_list}"
    }
    return ${python_list}
}

if {${name} ne ${subport}} {
    PortGroup       bazel 1.0

    bazel.version   3.7

    # Disable bazel configure and build phase configuration
    bazel.build_cmd
    bazel.configure_cmd

    depends_build-append \
                    port:py${python.version}-setuptools

    depends_run-append \
                    port:py${python.version}-six

    depends_test-append \
                    port:py${python.version}-absl \
                    port:py${python.version}-attrs \
                    port:py${python.version}-numpy \
                    port:py${python.version}-wrapt

    # diff -NaurdwB ./dm-tree-orig/setup.py ./dm-tree-new/setup.py | sed -E -e 's/\.\/dm-tree-(orig|new)*\/(setup\.py)(\.[[:alnum:]]+)*/\.\/setup.py/' > ./patch-setup.py.diff
    patchfiles      patch-setup.py.diff

    post-patch {
        reinplace \
            "s|@BAZEL_CMD@|[python_listify [concat {bazel} [option bazel.build_cmd_opts]]]|g" \
            ${worksrcpath}/setup.py
        reinplace \
            "s|@BAZEL_BUILD_OPTS@|[python_listify ${bazel.build_opts}]|g" \
            ${worksrcpath}/setup.py
    }

    set bazel_lib ${worksrcpath}/build/lib
    pre-destroot {
        xinstall -d ${destroot}${python.pkgd}
        if {![file isdirectory ${bazel_lib}]} {
            foreach dir [glob -directory ${worksrcpath}/build -nocomplain -type d lib.*] {
                set bazel_lib ${dir}
                break
            }
        }
    }

    destroot {
        copy ${bazel_lib}/${github.project} ${destroot}${python.pkgd}
    }

    pre-test {
        if {![file isdirectory ${bazel_lib}]} {
            foreach dir [glob -directory ${worksrcpath}/build -nocomplain -type d lib.*] {
                set bazel_lib ${dir}
                break
            }
            test.env-replace \
                PYTHONPATH=${worksrcpath}/build/lib \
                PYTHONPATH=${bazel_lib}
        }
    }

    test.run        yes
    python.test_framework
    test.cmd        ${python.bin}
    test.target     ${github.project}/tree_test.py
    test.env-append PYTHONPATH=${bazel_lib}

    post-destroot {
        set docdir ${prefix}/share/doc/${subport}
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 -W ${worksrcpath} LICENSE README.md \
            ${destroot}${docdir}
    }

    livecheck.type  none
}