Build the user commands with $BJO, after the start-up file

- The build runs $BJO, else bjo on PATH. A bjo that is not there gives a
  message that says what to set, and where.
- An interactive bjosh runs the start-up file first, and loads the user
  commands after it, so the file can set PATH or BJO.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 11:22:07 +01:00
parent bf961b12f9
commit 817de694b0
4 changed files with 75 additions and 27 deletions

View file

@ -17,7 +17,9 @@
;;
;; - An interactive shell first runs the lines of the start-up file,
;; $XDG_CONFIG_HOME/bjosh/rc or ~/.config/bjosh/rc.
;; - Each bjosh loads the user's commands first, when there are any.
;; - Each bjosh loads the user's commands, when there are any: an
;; interactive one after the start-up file, so that the file can set PATH
;; or BJO for the build; others before the first line.
;; - An interactive bjosh writes the package of user commands, with an
;; example, when its directory is not there. An empty directory there
;; turns this off.
@ -50,10 +52,7 @@
;; The start-up file.
(: (-> string))
(defun (rc-path)
(def config (match (getenv "XDG_CONFIG_HOME")
((Some c) c)
(None (path-combine (match (getenv "HOME") ((Some h) h) (None ".")) ".config"))))
(path-combine config "bjosh/rc"))
(path-combine (config-dir) "rc"))
;; The directory, with ~ for home.
(: (-> string))
@ -170,13 +169,14 @@
(def path (history-path))
(ignore (line-history-load! ed path))
(ignore (try (directory-create (path-combine path "..")) #:catch (System.Exception)))
(set-up-commands)
(def sh (with-extension (make-shell jc (Some ed) ["bjosh"])))
(def sh (make-shell jc (Some ed) ["bjosh"]))
(def rc (rc-path))
(when (file-exists? rc)
(match (file-read-text rc)
((Ok text) (ignore (run-text sh text)))
((Err e) (shell-error (str rc ": " (fs-error-message e))))))
(set-up-commands)
(ignore (with-extension sh))
(let go ()
(report-jobs sh)
(match (line-read ed term (prompt sh) #:notices (Some (fun () (job-changes sh))))

View file

@ -10,6 +10,8 @@
;; - Building: `bjo build` in the package, when a file of it is newer than
;; src/commands.dll, or when asked (`reload`). A build that does nothing
;; takes 0.4 s, so it is not run at each start.
;; - The bjo program: $BJO when it is set, else bjo on PATH. An interactive
;; bjosh loads the commands after the start-up file, which can set either.
;; - Loading: with (std plugin). The package shares bjosh's modules, so its
;; commands are of bjosh's own types.
;; - Errors: a build that fails, or a load that fails, writes its message,
@ -20,18 +22,24 @@
(import (std plugin))
(import (bjosh api))
(export commands-dir load-extension write-example)
(export config-dir commands-dir load-extension write-example)
(import/extern
(base-directory (: System.AppContext.BaseDirectory string #:get #:no-raise)))
;; The directory of the package of user commands.
;; bjosh's directory of configuration: $XDG_CONFIG_HOME/bjosh, or
;; ~/.config/bjosh.
(: (-> string))
(defun (commands-dir)
(defun (config-dir)
(def config (match (getenv "XDG_CONFIG_HOME")
((Some c) c)
(None (path-combine (match (getenv "HOME") ((Some h) h) (None ".")) ".config"))))
(path-combine config "bjosh/commands"))
(path-combine config "bjosh"))
;; The directory of the package of user commands.
(: (-> string))
(defun (commands-dir)
(path-combine (config-dir) "commands"))
(: (-> string long))
(defun (modified-ms path)
@ -48,11 +56,16 @@
(any? (fun (p) (and (string-ends-with? p ".bjo") (> (modified-ms p) built)))
(->vec (map path->string/lossy (directory-walk (path-combine dir "src")))))))
;; Runs `bjo build` in `dir`. Returns the message of a failure.
;; Runs `bjo build` in `dir`, with the bjo of $BJO or PATH. Returns the
;; message of a failure.
(: (-> string (Option string)))
(defun (build dir)
(match (run/output '(sh "-c" "cd \"$0\" && bjo build 2>&1" ,dir))
(def bjo (match (getenv "BJO") ((Some p) p) (None "bjo")))
(match (run/output '(sh "-c" "cd \"$0\" && exec \"$1\" build 2>&1" ,dir ,bjo))
((Ok (Tuple 0 _)) None)
((Ok (Tuple 127 _))
(Some (str "the user commands did not build: " bjo " was not found. Set PATH, or BJO to the bjo"
" program, in " (path-combine (config-dir) "rc") ".")))
((Ok (Tuple _ out)) (Some (str "the user commands did not build:\n" (string-trim-right out))))
((Err e) (Some (str "the user commands did not build: " (run-error-message e))))))