# -*- 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
version             6.2.0
revision            0

npm.rootname        vega-cli

categories          graphics devel
license             BSD
maintainers         nomaintainer

description         Vega visualization grammar: declarative interactive visualizations

long_description    Vega is a visualization grammar, a declarative language for \
                    creating, saving, and sharing interactive visualization designs. \
                    With Vega, you can describe the visual appearance and interactive \
                    behavior of a visualization in a JSON format, and generate \
                    web-based views using Canvas or SVG. This port installs the Vega \
                    command-line utilities (vg2png, vg2svg, vg2pdf) and the Vega \
                    JavaScript library.

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

checksums           rmd160  628ebd2ffc088f9eb97cc67b0acbd0bb01087383 \
                    sha256  b6007efa43e3b8fe8d5ad941a4283affeec3f07770c1dcb4ed50cbe70dbce802 \
                    size    4080

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

destroot {
    file mkdir ${workpath}/.home
    system -W ${workpath} \
        "HOME=${workpath}/.home \
         ${prefix}/bin/npm install -ddd --global \
         --prefix=${destroot}${prefix} \
         --cache=${workpath}/.npm-cache \
         ${distpath}/${distfiles}"

    # 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-cli/bin/vg2*] {
        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-cli/package.json
    } {
        if {[file exists ${destroot}${prefix}/${f}]} {
            reinplace -q "s|${destroot}||g" ${destroot}${prefix}/${f}
        }
    }
}

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

    # 2. Version check — the installed version must match the port
    set vg2svg_out [exec ${destroot}${prefix}/bin/vg2svg --version]
    if {![string match "*${version}*" $vg2svg_out]} {
        return -code error "version mismatch: expected ${version}, got ${vg2svg_out}"
    }

    # 3. Functional test — render a minimal Vega spec to SVG
    set spec_file [file join ${workpath} test-spec.vg.json]
    set svg_file  [file join ${workpath} test-output.svg]
    set fd [open ${spec_file} w]
    puts $fd {
        {
            "$schema": "https://vega.github.io/schema/vega/v5.json",
            "width": 200,
            "height": 200,
            "marks": [
                {
                    "type": "rect",
                    "encode": {
                        "enter": {
                            "x":      {"value": 0},
                            "y":      {"value": 0},
                            "width":  {"value": 100},
                            "height": {"value": 100},
                            "fill":   {"value": "steelblue"}
                        }
                    }
                }
            ]
        }
    }
    close $fd

    system "${destroot}${prefix}/bin/vg2svg ${spec_file} ${svg_file}"

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

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

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

    vg2svg --- render a Vega spec to SVG
    vg2png --- render a Vega spec to PNG
    vg2pdf --- render a Vega spec to PDF

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