#!/usr/bin/env bash ### ### a simple complement to bild which only deals with launching repls ### ### > repl [opts] ### ### Starts a repl/shell for one or more targets. (Currently, all targets must ### have the same extension for this to work.) Repls started with this script ### should bind to `localhost:$PORT`. ### ### Options: ### --bash start bash instead of the target language repl help() { sed -rn 's/^### ?//;T;p' "$0" } if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then help exit 1 fi ## set -e CMD= if [[ "$1" == "--bash" ]]; then CMD="bash" shift fi targets=${@:?} json=$(bild --json ${targets[@]}) langdeps=$(jq --raw-output '.[].langdeps | join(" ")' <<< $json) sysdeps=$(jq --raw-output '.[].sysdeps | join(" ")' <<< $json) exts=$(jq --raw-output '.[].namespace.ext' <<< $json | sort | uniq) packageSet=$(jq --raw-output '.[].packageSet' <<< $json) BILD="(import ${BIZ_ROOT:?}/Biz/Bild.nix {})" for lib in ${sysdeps[@]}; do flags+=(--packages "$BILD.private.nixpkgs.${lib}") flags+=(--packages "$BILD.private.nixpkgs.pkg-config") done case $exts in C) flags+=(--packages "$BILD.private.nixpkgs.gcc") command="bash" ;; Hs) if [ -z ${var+PORT} ]; then echo "warn: repl: ghci does not support binding to a port" fi flags+=(--packages "$BILD.bild.haskell.ghcWith (h: with h; [$langdeps])") command=${CMD:-"ghci -i${BIZ_ROOT:?} -ghci-script ${BIZ_ROOT:?}/.ghci ${targets[@]}"} ;; Scm) for lib in ${langdeps[@]}; do flags+=(--packages "$BILD.guile-${lib}") done flags+=(--packages "$BILD.guile") command=${CMD:-"guile -L ${BIZ_ROOT:?} -C ${BIZ_ROOT:?}/_/int --r7rs --listen=${PORT:-37146}"} ;; Lisp) flags+=(--packages "$BILD.bild.$packageSet (p: with p; [asdf swank $langdeps])") command=${CMD:-"sbcl --eval '(require :asdf)' --eval '(require :swank)' --eval '(swank:create-server :port ${PORT:-4005})' --load $targets"} ;; Rs) flags+=(--packages "$BILD.nixpkgs.rustc") command=bash ;; Py) langdeps="$langdeps mypy" flags+=(--packages "$BILD.bild.python.pythonWith (p: with p; [$langdeps])") command=${CMD:-"python -i $targets"} ;; *) echo "unsupported targets: ${targets[@]}" exit 1 ;; esac ## nix-shell "${flags[@]}" --command "$command" ##