Initial check-in.

This commit is contained in:
Ray Miller 2024-07-14 17:11:24 +01:00
commit 3e1b3e3f37
3 changed files with 57 additions and 0 deletions

10
ordo/task.scm Normal file
View file

@ -0,0 +1,10 @@
(define-module (ordo task)
#:use-module (srfi srfi-9)
#:export (make-task task? task-name task-prerequisite-data set-task-prerequisite-data! task-action set-task-action!))
(define-record-type <task>
(make-task name prerequisite-data action)
task?
(name task-name)
(prerequisite-data task-prerequisite-data set-task-prerequisite-data!)
(action task-action set-task-action!))

17
ordo/task/command.scm Normal file
View file

@ -0,0 +1,17 @@
(define-module (ordo task command)
#:use-module (ice-9 format)
#:use-module (srfi srfi-11)
#:use-module (ordo task)
#:use-module (ordo util run)
#:export (task-command))
(define* (task-command name cmd #:optional (args '())
#:key (fail-ok? #f) (stdin #f) (cwd #f) (env #f))
(make-task name
#f
(lambda ()
(let-values (((exit-code output) (run cmd args #:stdin stdin #:cwd cwd #:env env #:combine-output #t)))
(if (or fail-ok? (zero? exit-code))
(values exit-code output)
(error (format #f "Error running ~a (exit ~d): ~a" cmd exit-code output)))))))

30
ordo/util/run.scm Normal file
View file

@ -0,0 +1,30 @@
(define-module (ordo util run)
#:use-module (ice-9 textual-ports)
#:export (run with-cwd))
(define-syntax with-cwd
(syntax-rules ()
((_ new-dir body ...)
(let ((original-dir (getcwd)))
(dynamic-wind
(lambda () (chdir new-dir))
(lambda () body ...)
(lambda () (chdir original-dir)))))))
(define* (run cmd #:optional (args '()) #:key (combine-output #f) (env #f) (stdin #f) (cwd #f))
(if cwd
(with-cwd cwd (run cmd args #:combine-output combine-output #:env env #:stdin stdin))
(let* ((input-pipe (pipe))
(output-pipe (pipe))
(pid (spawn cmd (cons cmd args)
#:input (car input-pipe)
#:output (cdr output-pipe)
#:error (if combine-output (cdr output-pipe) (current-error-port))
#:environment (or env (environ)))))
(close-port (cdr output-pipe))
(close-port (car input-pipe))
(when stdin (put-string (cdr input-pipe) stdin))
(close-port (cdr input-pipe))
(let ((output (get-string-all (car output-pipe))))
(close-port (car output-pipe))
(values (cdr (waitpid pid)) output)))))