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

github.setup        clojure-lsp clojure-lsp 2023.02.27-13.12.12
github.tarball_from archive
revision            0

categories          devel
license             MIT
maintainers         {@kakuhen} openmaintainer
platforms           darwin
description         Clojure & ClojureScript Language Server (LSP) implementation
long_description    The goal of this project is to bring great editing tools \
    for Clojure/Clojurescript to all editors. It aims to work alongside you to \
    help you navigate, identify and fix errors, perform refactors and much more!

checksums           rmd160  5d952615dba87724c96891677baf55d8b2e13a7d \
                    sha256  7a19fa7357a9818b757565fd59f62937efa3a778597f6f36a7631231520146d5 \
                    size    4339165

depends_lib         port:clojure

variant jdk17 conflicts jdk11 description {Build using JDK 17} {
    depends_build-append port:openjdk17-graalvm
}

variant jdk11 conflicts jdk17 description {Build using the older JDK 11} {
    depends_build-append port:openjdk11-graalvm-native-image
}

default_variants    +jdk17
use_configure       no

# Patching lib/deps.edn so that clojure.tools.build can pick up a local Maven
# repository we will use later.
patchfiles deps-edn-patch.diff

# Leaving this stub since we cannot declare global variables in the scope of pre-fetch
set graalvm_home /Library/Java/JavaVirtualMachines

pre-fetch {
    if {![variant_isset jdk11] && ![variant_isset jdk17]} {
        error "Either +jdk11 or +jdk17 is required"
    }
}

post-patch {
    reinplace "s|%%WORKSRCPATH%%|${worksrcpath}|g" ${worksrcpath}/lib/deps.edn
}

# We need to set up a local Maven repository within the source directory.
set gitlibs ${worksrcpath}/.gitlibs
set repo_args "'\{:mvn/local-repo \"${worksrcpath}/.m2/repository\"\}'"

# NOTE: upstream recommends running babashka scripts to install, but there is
# absolutely no need to do this. Assuming you know some Clojure, you can read
# the source for #'make/native-cli. Running `bb native-cli` simply runs this.
# I replicate the behavior of this function below and install the binary.

build {
    if {[variant_isset jdk17]} {
        set graalvm_home ${graalvm_home}/openjdk17-graalvm
    } elseif {[variant_isset jdk11]} {
        set graalvm_home ${graalvm_home}/openjdk11-graalvm
    }
    # NOTE: This ultimately calls #'build/native-cli. Refer to the source of
    # that function if you are curious on how native-image is invoked.
    system -W ${worksrcpath}/cli \
        "GITLIBS=${gitlibs} GRAALVM_HOME=${graalvm_home}/Contents/Home clojure -Sdeps ${repo_args} -T:build native-cli"
}

destroot {
    xinstall -m 755 ${worksrcpath}/cli/clojure-lsp ${destroot}${prefix}/bin
}
