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

name                vega-lite
version             6.4.2
revision            0

categories          graphics devel
license             BSD
maintainers         nomaintainer

description         Vega-Lite: a high-level grammar of interactive graphics

long_description    Vega-Lite is a high-level grammar of interactive graphics. It \
                    provides a concise, declarative JSON syntax to create an expressive \
                    range of visualizations for data analysis and presentation. \
                    Vega-Lite specifications compile to full Vega specifications. \
                    This port installs the Vega-Lite library and the command-line \
                    utilities vl2vg, vl2svg, vl2png, and vl2pdf.

homepage            https://vega.github.io/vega-lite/

checksums           rmd160  5f646631c7b2c108e7034a832383556d84983beb \
                    sha256  158643e963d9a4b81cb971846056fcd629798fae8bc27012b13cddb1f5c03c0f \
                    size    1077598

platform darwin {
    if {${os.major} >= 22} {
        npm.nodejs_version  24
        npm.version         11
    }
}

destroot {
    file mkdir ${workpath}/.home
    # Install vega-lite from the distfile tarball, with vega and canvas
    # fetched from the npm registry to satisfy peer dependencies.
    system -W ${workpath} \
        "HOME=${workpath}/.home \
         ${prefix}/bin/npm install -ddd --global \
         --prefix=${destroot}${prefix} \
         --cache=${workpath}/.npm-cache \
         ${distpath}/${distfiles} vega canvas"

    # Fix shebangs: npm writes #!/usr/bin/env node -- replace with
    # the MacPorts-provided node binary
    foreach f [glob -nocomplain ${destroot}${prefix}/lib/node_modules/vega-lite/bin/vl2*] {
        reinplace "s|#!/usr/bin/env node|#!${prefix}/bin/node|g" ${f}
    }

    # npm bakes the destroot path into internal metadata files; strip it
    foreach f {
        lib/node_modules/.package-lock.json
        lib/node_modules/vega-lite/package.json
    } {
        if {[file exists ${destroot}${prefix}/${f}]} {
            reinplace -q "s|${destroot}||g" ${destroot}${prefix}/${f}
        }
    }

    # The vega port owns the vg2* binaries; remove any duplicates to
    # avoid file conflicts at activation time
    foreach f [glob -nocomplain ${destroot}${prefix}/bin/vg2*] {
        file delete ${f}
    }
}

test.run    yes
test {
    # 1. Smoke test --- vl2vg --help should exit cleanly
    system "${destroot}${prefix}/bin/vl2vg --help"

    # 2. Functional test --- compile a minimal Vega-Lite spec to Vega JSON
    set spec_file [file join ${workpath} test-spec.vl.json]
    set vg_file   [file join ${workpath} test-output.vg.json]
    set fd [open ${spec_file} w]
    puts $fd {
        {
            "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
            "description": "A simple bar chart with embedded data.",
            "data": {
                "values": [
                    {"a": "A", "b": 28},
                    {"a": "B", "b": 55},
                    {"a": "C", "b": 43}
                ]
            },
            "mark": "bar",
            "encoding": {
                "x": {"field": "a", "type": "nominal"},
                "y": {"field": "b", "type": "quantitative"}
            }
        }
    }
    close $fd

    system "${destroot}${prefix}/bin/vl2vg ${spec_file} ${vg_file}"

    if {![file exists ${vg_file}]} {
        return -code error "vl2vg failed to produce output file"
    }

    set vg_content [exec cat ${vg_file}]
    if {![string match "*vega*" $vg_content]} {
        return -code error "vl2vg output does not look like a Vega spec"
    }

    # 3. Render the same spec to SVG via vl2svg
    set svg_file [file join ${workpath} test-output.svg]
    system "${destroot}${prefix}/bin/vl2svg ${spec_file} ${svg_file}"

    if {![file exists ${svg_file}]} {
        return -code error "vl2svg failed to produce output SVG"
    }

    set svg_content [exec cat ${svg_file}]
    if {![string match "*<svg*" $svg_content]} {
        return -code error "vl2svg output does not appear to be valid SVG"
    }
}

notes "The following Vega-Lite command-line utilities have been installed:

    vl2vg   --- compile a Vega-Lite spec to a full Vega spec
    vl2svg  --- compile and render a Vega-Lite spec to SVG
    vl2png  --- compile and render to PNG
    vl2pdf  --- compile and render to PDF

For documentation and examples, visit https://vega.github.io/vega-lite/"
