# -*- 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
PortGroup           select 1.0

name                py-language-server
github.setup        palantir python-language-server 0.36.2
github.tarball_from archive
revision            2

# stealth update, remove when updating to new version
dist_subdir          ${name}/${version}

categories-append   devel
platforms           darwin
supported_archs     noarch
license             MIT
maintainers         {reneeotten @reneeotten} openmaintainer

description         An implementation of the Language Server Protocol for Python
long_description    ${description}

checksums           rmd160  cf9f5be95659eff1c9a9fabbf136e0653a9258d7 \
                    sha256  fb573f780467c1cae0de47065eecbaaf9b457bd5cbe9f5cf8409224e1cd21cf6 \
                    size    463591

python.versions     27 35 36 37 38 39

if {${subport} ne ${name}} {
    depends_lib-append \
                    port:py${python.version}-autopep8 \
                    port:py${python.version}-codestyle \
                    port:py${python.version}-docstyle \
                    port:py${python.version}-flake8 \
                    port:py${python.version}-flake8-mccabe \
                    port:py${python.version}-jedi \
                    port:py${python.version}-pluggy \
                    port:py${python.version}-pyflakes \
                    port:py${python.version}-pylint \
                    port:py${python.version}-python-jsonrpc-server \
                    port:py${python.version}-rope \
                    port:py${python.version}-setuptools \
                    port:py${python.version}-ujson \
                    port:py${python.version}-yapf

    if {${python.version} eq 27} {
        depends_lib-append  port:py${python.version}-backports-functools_lru_cache \
                            port:py${python.version}-configparser \
                            port:py${python.version}-future
    }

    patchfiles-append   patch-pyls_plugins_flake8_lint.py.diff \
                        patch-test_plugins_flake8_lint.py.diff

    post-patch {
        reinplace "s|@@FLAKE8_BIN@@|flake8-${python.branch}|g" ${worksrcpath}/pyls/plugins/flake8_lint.py
        reinplace "s|@@FLAKE8_BIN@@|flake8-${python.branch}|g" ${worksrcpath}/test/plugins/test_flake8_lint.py
        reinplace "s|@@PYTHON_BIN@@|${python.bin}|g" ${worksrcpath}/pyls/plugins/flake8_lint.py

        # temporary reinplace to remove version cap on jedi, pyflakes, and pycodestyle
        reinplace {s|'jedi>=0\.17\.2,<0\.18\.0'|'jedi>=0\.17\.2,<0.19.0'|g} ${worksrcpath}/setup.py
        reinplace {s|'pyflakes>=2\.2\.0,<2\.3\.0'|'pyflakes>=2.2.0,<2.4.0'|g} ${worksrcpath}/setup.py
        reinplace {s|'pycodestyle>=2\.6\.0,<2\.7\.0'|'pycodestyle>=2.6.0,<2.8.0'|g} ${worksrcpath}/setup.py
    }

    depends_test-append \
                    port:py${python.version}-pytest \
                    port:py${python.version}-mock \
                    port:py${python.version}-flaky

    test.run        yes
    test.cmd        py.test-${python.branch}
    test.args       -o addopts=''
    test.target
    test.env        PYTHONPATH=${worksrcpath}/build/lib

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

    depends_run-append \
                    port:python-language-server_select

    select.group    python-language-server
    select.file     ${filespath}/py${python.version}-pyls

    notes "
To make the Python ${python.branch} version of python language server the one \
that is run when you execute the commands without a version suffix, e.g. \
'pyls', run:

port select --set ${select.group} [file tail ${select.file}]
"

    livecheck.type  none
}
