From 817de694b0805b965e007ebc3ac8cb2a10462df1be5257f1e03dfe0d89b40e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Bj=C3=B6rnstam?= Date: Tue, 1 Dec 2026 11:22:07 +0100 Subject: [PATCH] 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 --- plan.org | 6 ++++- src/main.bjo | 14 +++++------ src/user.bjo | 25 +++++++++++++++----- tests/extension.bjo | 57 ++++++++++++++++++++++++++++++++++----------- 4 files changed, 75 insertions(+), 27 deletions(-) diff --git a/plan.org b/plan.org index caa8d86..7ce0004 100644 --- a/plan.org +++ b/plan.org @@ -436,8 +436,12 @@ User commands, compiled, loaded into the shell at start-up. Needs - The first interactive start writes the package with its example, when its directory is not there; an empty directory turns this off. ~--new-commands~ stays, for a start that is not interactive. +- The build runs ~$BJO~, else ~bjo~ on ~PATH~. An interactive bjosh runs + the start-up file first and loads the commands after it, so the file can + set either; the start-up file cannot use the user's commands. A bjo that + is not there gives a message that says what to set. - Measured: 132 ms for ~bjosh -c true~ with the example package loaded. -- Tests: ~tests/extension.bjo~, 25 checks. The other tests: syntax 72, +- Tests: ~tests/extension.bjo~, 30 checks. The other tests: syntax 72, expand 26, values 59, editor 24, run 60, interactive 16. ** Phase 5: later diff --git a/src/main.bjo b/src/main.bjo index 6162f5b..ed59f93 100644 --- a/src/main.bjo +++ b/src/main.bjo @@ -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)))) diff --git a/src/user.bjo b/src/user.bjo index f776c15..d85c736 100644 --- a/src/user.bjo +++ b/src/user.bjo @@ -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)))))) diff --git a/tests/extension.bjo b/tests/extension.bjo index a02b606..7e0cdd4 100644 --- a/tests/extension.bjo +++ b/tests/extension.bjo @@ -27,13 +27,14 @@ (def source (path-combine config "bjosh/commands/src/commands.bjo")) -;; Runs bjosh with `args`, and this test's config directory. Returns "out", -;; then "err: " and the error text when there is one, then "code: n" when it -;; is not 0. -(: (-> (Vec string) string)) -(defun (bjosh args) +;; Runs bjosh with `args`, the environment `env` (NAME=value), and this +;; test's config directory. Returns "out", then "err: " and the error text +;; when there is one, then "code: n" when it is not 0. +(: (-> (Vec string) (Vec string) string)) +(defun (bjosh-env env args) (def err-path (path-combine config "stderr")) - (match (run/output (FErrInto err-path (FArgv (vec-append ["env" (str "XDG_CONFIG_HOME=" config) "dotnet" shell] args)))) + (match (run/output (FErrInto err-path (FArgv (vec-append (vec-append ["env" (str "XDG_CONFIG_HOME=" config)] env) + (vec-append ["dotnet" shell] args))))) ((Ok (Tuple code out)) (def err (match (file-read-text err-path) ((Ok t) t) ((Err _) ""))) (string-trim-right @@ -42,6 +43,9 @@ (if (= code 0) "" (str "code: " (int->string code)))))) ((Err e) (str "did not run: " (run-error-message e))))) +(: (-> (Vec string) string)) +(defun (bjosh args) (bjosh-env [] args)) + (: (-> string string)) (defun (c line) (bjosh ["-c" line])) @@ -122,13 +126,13 @@ (else (stringbuilder-add! out (vec-ref chars i)) (go (+ i 1) out))))) ;; Types lines into an interactive bjosh under `script`, with `dir` as -;; XDG_CONFIG_HOME, after `wait` seconds. Returns what it writes, without -;; escape sequences. -(: (-> string string (Vec string) string)) -(defun (interactive dir wait lines) +;; XDG_CONFIG_HOME and `env` (NAME=value ...) before it, after `wait` +;; seconds. Returns what it writes, without escape sequences. +(: (-> string string string (Vec string) string)) +(defun (interactive dir env wait lines) (def keys (str "sleep " wait "; " (string-join (vec-map (fun (l) (str "printf '" l "\\r'; sleep 0.7")) lines) "; "))) - (def command (str "(" keys ") | XDG_STATE_HOME=" dir " XDG_CONFIG_HOME=" dir + (def command (str "(" keys ") | " env " XDG_STATE_HOME=" dir " XDG_CONFIG_HOME=" dir " script -qec 'dotnet src/main.exe' /dev/null")) (match (run/output '(sh "-c" ,command)) ((Ok (Tuple _ text)) (plain text)) @@ -176,7 +180,7 @@ (if (not (file-exists? "/usr/bin/script")) (println "skipped: the prompt (no script command)") - (let ((out (interactive config "2.5" ["true" "false" "true" "exit"]))) + (let ((out (interactive config "" "2.5" ["true" "false" "true" "exit"]))) (expect-true "the prompt of the commands" (string-contains? out "[ext 0]> true")) (expect-true "a prompt that raises writes its message" (string-contains? out "bjosh: the prompt of your commands raised: the prompt failed")) @@ -185,12 +189,39 @@ (if (file-exists? "/usr/bin/script") (let* ((fresh (path-combine config "fresh")) - (out (interactive fresh "12" ["echo a | twice" "exit"]))) + (out (interactive fresh "" "12" ["echo a | twice" "exit"]))) (expect-true "the first interactive start writes the package" (string-contains? out "bjosh: wrote your commands to ")) (expect-true "and builds and loads it" (string-contains? out "twice\na\na\n"))) unit) + ;; The bjo program: $BJO, else bjo on PATH. + (def real-bjo (match (run/output '(sh "-c" "command -v bjo")) + ((Ok (Tuple 0 out)) (string-trim-right out)) + (_ "bjo"))) + (def narrow "PATH=/usr/bin:/bin") + (write-source (commands "with BJO")) + (expect-true "no bjo on PATH: the message says what to set" + (string-contains? (bjosh-env [narrow] ["-c" "true"]) + "bjo was not found. Set PATH, or BJO to the bjo program, in ")) + (expect-true "BJO names the program" + (string-contains? (bjosh-env [narrow (str "BJO=" real-bjo)] ["-c" "help"]) "with BJO")) + (write-source (commands "with a BJO that is not there")) + (expect-true "a BJO that is not there" + (string-contains? (bjosh-env [(str "BJO=" config "/no-bjo")] ["-c" "true"]) + "/no-bjo was not found")) + + (if (file-exists? "/usr/bin/script") + (let ((rc (path-combine config "bjosh/rc"))) + (write-source (commands "set in rc")) + (ignore (file-write-text rc (str "export BJO=" real-bjo "\n"))) + (def out (interactive config narrow "10" ["echo a | twice" "exit"])) + (expect-true "the start-up file runs before the commands are built" + (string-contains? out "twice\na\na\n")) + (expect-true "and they build with its BJO" (not (string-contains? out "was not found"))) + (ignore (file-delete rc))) + unit) + (ignore (directory-delete-tree config)) (println "extension ok") 0)