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

github.setup        khoi sand 1.4.0 v
github.tarball_from archive
name                sand-runner
revision            0

categories          sysutils devel
license             MIT
maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

description         Self-hosted GitHub Actions runners on ephemeral macOS VMs
long_description    sand runs self-hosted GitHub Actions runners inside \
                    ephemeral Tart virtual machines on Apple Silicon. Each \
                    runner gets a fresh macOS or Linux VM cloned from an OCI \
                    image, is registered with GitHub through a GitHub App, \
                    and is destroyed and recreated after every job.

checksums           rmd160  227f8cc80104ce1b75cb529b465aa730b0cb3856 \
                    sha256  91f7732e8b766261d313c33a0ebe4f580d289079611e08ecbc1ee7b1186c1bf0 \
                    size    46031

# Package.swift requires macOS 15 and swift-tools-version 6.2, first shipped in Xcode 26
platforms           {macosx >= 24}
supported_archs     arm64

minimum_xcodeversions \
                    {24 26.0}

if {${os.platform} eq "darwin" && (${os.major} < 24 || ${os.arch} ne "arm")} {
    known_fail yes
    pre-fetch {
        ui_error "${name} requires macOS 15 or later on Apple Silicon (arm64)."
        return -code error "incompatible macOS version or architecture"
    }
} elseif {![info exists xcodeversion] || ${xcodeversion} eq "none" || [vercmp ${xcodeversion} < 26.0]} {
    # minimum_xcodeversions errors out at fetch; this lets CI skip instead
    known_fail yes
}

conflicts           sand

depends_run         port:sshpass \
                    port:tart

# Clearing CPATH because of header conflicts with the macOS SDK when using the SwiftPM build system
compiler.cpath

use_configure       no
use_xcode           yes
installs_libs       no

# Disable the SwiftPM sandbox since the port build already runs in a sandbox
set swiftpm_args    [list --disable-sandbox \
                        --cache-path ${workpath}/.swiftpm/cache \
                        --config-path ${workpath}/.swiftpm/config \
                        --security-path ${workpath}/.swiftpm/security]

build.cmd           swift
build.target        build
build.args          --configuration release \
                    --product sand \
                    {*}${swiftpm_args}

test.run            yes
test.cmd            swift
test.target         test
test.args           {*}${swiftpm_args}

destroot {
    xinstall -m 0755 ${worksrcpath}/.build/release/sand ${destroot}${prefix}/bin/

    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 -W ${worksrcpath} LICENSE README.md \
        ${destroot}${prefix}/share/doc/${name}

    xinstall -d ${destroot}${prefix}/share/examples/${name}
    xinstall -m 0644 {*}[glob ${worksrcpath}/fixtures/*.yml] \
        ${destroot}${prefix}/share/examples/${name}
}

notes "
    The command is installed as 'sand'. Run 'sand doctor' to check that tart,\
    sshpass and ssh are on your PATH, and see ${prefix}/share/examples/${name}\
    for sample configurations.

    To start sand at login, see the LaunchAgent example in\
    ${prefix}/share/doc/${name}/README.md; its launchctl commands say\
    com.khoi.sand, but the label it defines is io.khoi.sand.

    To avoid DHCP address shortage when running many VMs per day, consider reducing\
    the default lease time from 86400 to 600 seconds:

    sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.InternetSharing.default.plist bootpd -dict DHCPLeaseTimeSecs -int 600
"
