# -*- 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               cmake 1.1

name                    neovim
categories              editors
platforms               {darwin >= 15}
maintainers             {l2dy @l2dy} \
                        {judaew @judaew} \
                        openmaintainer
license                 Apache-2 Vim GPL-2+

description             Neovim is a aggressively refactored fork of Vim

long_description \
    Neovim is a project that seeks to aggressively refactor Vim. It already adds \
    a new plugin architecture, job control, and a remote API.

homepage                https://neovim.io

depends_build-append    port:pkgconfig

depends_lib             port:gettext \
                        port:libuv \
                        port:unibilium \
                        path:lib/libluajit-5.1.2.dylib:luajit \
                        port:lua51-lpeg \
                        port:luv-luajit \
                        port:libiconv \
                        port:libutf8proc \
                        port:tree-sitter

cmake.build_type        Release

configure.args-append   -DLUA_PRG=${prefix}/bin/luajit

patch.pre_args-replace  -p0 -p1

# Building parsers is normally an extra step, see https://github.com/neovim/neovim/issues/29042
patchfiles              0001-build-and-install-tree-sitter-parsers.patch \
                        0002-include-lua51-headers-to-build-properly.patch

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/CMakeLists.txt

    # workaround for https://github.com/neovim/neovim/pull/30749
    reinplace "s|lpeg.so lpeg\${CMAKE_SHARED_LIBRARY_SUFFIX}|lpeg\${CMAKE_SHARED_LIBRARY_SUFFIX} lpeg.so|g" ${worksrcpath}/cmake/FindLpeg.cmake
}

notes {
    If you would like to re-use your existing Vim configuration with Neovim,
    follow the advice in `:help nvim-from-vim`:

        nvim -c 'tab h nvim-from-vim'

    For a full list of differences with Vim, read `:help vim-differences`.
}

subport neovim-devel {}

if {${subport} eq ${name}} {
    # stable

    github.setup        neovim neovim 0.11.2 v
    github.tarball_from archive
    revision            0
    conflicts           neovim-devel

    # cat cmake.deps/deps.txt \
    #     | perl -n -e '/(TREESITTER_.*)_URL https:\/\/github.com\/([^\/]*)\/([^\/]*)\/archive\/([^\d]*)((?:\d+\.){2}\d+)([^\d\.]*)/ && print join(" ", $2, $3, $5, $4 || "\"\"", $6 || "\"\"", lc($1), "\n")' \
    #     | column -t
    set deps {
        tree-sitter           tree-sitter-c         0.23.4  v  ""  treesitter_c
        tree-sitter-grammars  tree-sitter-lua       0.3.0   v  ""  treesitter_lua
        tree-sitter-grammars  tree-sitter-vim       0.5.0   v  ""  treesitter_vim
        neovim                tree-sitter-vimdoc    3.0.1   v  ""  treesitter_vimdoc
        tree-sitter-grammars  tree-sitter-query     0.5.1   v  ""  treesitter_query
        tree-sitter-grammars  tree-sitter-markdown  0.4.1   v  ""  treesitter_markdown
    }

    checksums           neovim-0.11.2.tar.gz \
                        rmd160  5afe1184a5aba71b9fece43b6e0eeafaf90eff7f \
                        sha256  324759a1bcd1a80b32a7eae1516ee761ec3e566d08284a24c4c7ca59079aabfa \
                        size    12923046 \
                        tree-sitter-c-0.23.4.tar.gz \
                        rmd160  23c74c3b947bcda2e05921c06e65259f46a0c360 \
                        sha256  b66c5043e26d84e5f17a059af71b157bcf202221069ed220aa1696d7d1d28a7a \
                        size    380057 \
                        tree-sitter-lua-0.3.0.tar.gz \
                        rmd160  6d929ed4ee48e71204774ca1bd4cdc06d720193b \
                        sha256  a34cc70abfd8d2d4b0fabf01403ea05f848e1a4bc37d8a4bfea7164657b35d31 \
                        size    62157 \
                        tree-sitter-vim-0.5.0.tar.gz \
                        rmd160  971bc651fc62c5f18bdabc61da39620e8410eb87 \
                        sha256  90019d12d2da0751c027124f27f5335babf069a050457adaed53693b5e9cf10a \
                        size    357652 \
                        tree-sitter-vimdoc-3.0.1.tar.gz \
                        rmd160  cd3dd1aa3543b041e3c00dc0885e56c057125233 \
                        sha256  76b65e5bee9ff78eb21256619b1995aac4d80f252c19e1c710a4839481ded09e \
                        size    58427 \
                        tree-sitter-query-0.5.1.tar.gz \
                        rmd160  352bd633db597d87ff768dc379168105cd5b527f \
                        sha256  fe8c712880a529d454347cd4c58336ac2db22243bae5055bdb5844fb3ea56192 \
                        size    45070 \
                        tree-sitter-markdown-0.4.1.tar.gz \
                        rmd160  770039a4ed01fd69c102cff7b1717cdce3702a04 \
                        sha256  e0fdb2dca1eb3063940122e1475c9c2b069062a638c95939e374c5427eddee9f \
                        size    419261
} else {
    # devel

    github.setup        neovim neovim 0d658660c29e920e74c4dade3819d80dccad0dde
    github.tarball_from archive
    version             20250615-[string range ${github.version} 0 6]
    revision            0
    conflicts           neovim
    github.livecheck.branch \
                        nightly

    # see stable's version of `deps` for how to regenerate
    set deps {
        tree-sitter           tree-sitter-c         0.24.1  v  ""  treesitter_c
        tree-sitter-grammars  tree-sitter-lua       0.4.0   v  ""  treesitter_lua
        tree-sitter-grammars  tree-sitter-vim       0.6.0   v  ""  treesitter_vim
        neovim                tree-sitter-vimdoc    4.0.0   v  ""  treesitter_vimdoc
        tree-sitter-grammars  tree-sitter-query     0.6.1   v  ""  treesitter_query
        tree-sitter-grammars  tree-sitter-markdown  0.5.0   v  ""  treesitter_markdown
    }

    checksums           neovim-0d658660c29e920e74c4dade3819d80dccad0dde.tar.gz \
                        rmd160  9f5b0ca800ba051d87e56aab894c83ada9b9ae85 \
                        sha256  9a424d2b0a4b38c9cc55c376b42a4ba374a0e3bb14733b84c022b6a462dc91cc \
                        size    13121084 \
                        tree-sitter-c-0.24.1.tar.gz \
                        rmd160  12c732f8ccc097fd1ef4f171c19a89d06cfb85cd \
                        sha256  25dd4bb3dec770769a407e0fc803f424ce02c494a56ce95fedc525316dcf9b48 \
                        size    379147 \
                        tree-sitter-lua-0.4.0.tar.gz \
                        rmd160  2d738c83f4a1ec560fdbaae2ac7e156f93b96a22 \
                        sha256  b0977aced4a63bb75f26725787e047b8f5f4a092712c840ea7070765d4049559 \
                        size    63999 \
                        tree-sitter-vim-0.6.0.tar.gz \
                        rmd160  4480c436ba517fb67425f36165d0cf0016d9bfff \
                        sha256  b36080be8f9ec53d6413447a792985b984b6f89a223ff758f1acfd380b469a74 \
                        size    361031 \
                        tree-sitter-vimdoc-4.0.0.tar.gz \
                        rmd160  60712382cbd18f39b06cbca4ca7da78d74b2b18d \
                        sha256  8096794c0f090b2d74b7bff94548ac1be3285b929ec74f839bd9b3ff4f4c6a0b \
                        size    58862 \
                        tree-sitter-query-0.6.1.tar.gz \
                        rmd160  de6d4ae2754fc36308c2557248f7928bea6af045 \
                        sha256  18e158092789e4391668760e9ad1b5b4135c8f483d29163912d29e5619518d40 \
                        size    43412 \
                        tree-sitter-markdown-0.5.0.tar.gz \
                        rmd160  8582b36c12a8a825d2873e9ec25b2c2572a80253 \
                        sha256  14c2c948ccf0e9b606eec39b09286c59dddf28307849f71b7ce2b1d1ef06937e \
                        size    419516
}

# Add each dependency's master_site, tag it and associate it back to the distfile
# e.g.: master_sites-append https://github.com/tree-sitter/tree-sitter-c/archive/v0.23.4:treesitter_c
#       distfiles-append    tree-sitter-c-0.23.4.tar.gz:treesitter_c
foreach {gh_author gh_project gh_version gh_tag_prefix gh_tag_suffix dirname} ${deps} {
    master_sites-append https://github.com/${gh_author}/${gh_project}/archive/[join ${gh_tag_prefix}]${gh_version}[join ${gh_tag_suffix}]:${dirname}
    distfiles-append    ${gh_project}-${gh_version}${extract.suffix}:${dirname}
}

# Only extract Neovim's source and not its dependencies
extract.only        ${distname}${extract.suffix}
post-extract {
    # Create the file structure where the build job expects the dependencies
    # e.g. file mkdir ${workpath}/build/build/downloads/treesitter_c
    #      file copy \
    #          ${distpath}/tree-sitter-c-0.23.4.tar.gz \
    #          ${workpath}/build/build/downloads/treesitter_c/v0.23.4.tar.gz
    foreach {_ gh_project gh_version gh_tag_prefix gh_tag_suffix dirname} ${deps} {
        file mkdir ${workpath}/build/build/downloads/${dirname}
        file copy \
            ${distpath}/${gh_project}-${gh_version}${extract.suffix} \
            ${workpath}/build/build/downloads/${dirname}/[join ${gh_tag_prefix}]${gh_version}[join ${gh_tag_suffix}]${extract.suffix}
    }
}
