Refactor, implement inventory, add examples
This commit is contained in:
parent
d16df7616f
commit
54b6fd0377
17 changed files with 373 additions and 483 deletions
|
@ -1,59 +0,0 @@
|
|||
(define-module (ordo context)
|
||||
#:use-module (srfi srfi-1)
|
||||
#:use-module (srfi srfi-9)
|
||||
#:use-module (srfi srfi-26)
|
||||
#:use-module (srfi srfi-71)
|
||||
#:use-module (ordo connection)
|
||||
#:export (make-context
|
||||
context?
|
||||
context-connection
|
||||
add-context-triggers!
|
||||
get-context-triggers
|
||||
context-triggered?
|
||||
register-context-var!
|
||||
context-ref
|
||||
bind-context-vars
|
||||
run
|
||||
must))
|
||||
|
||||
(define-record-type <context>
|
||||
(make-context connection vars)
|
||||
context?
|
||||
(connection context-connection)
|
||||
(vars context-vars set-context-vars!)
|
||||
(triggers context-triggers set-context-triggers!))
|
||||
|
||||
(define (context-ref ctx var-name)
|
||||
(let ((kv (assoc var-name (context-vars ctx))))
|
||||
(if kv
|
||||
(cdr kv)
|
||||
(error (format #f "failed to resolve context reference: ~a" var-name)))))
|
||||
|
||||
(define (add-context-triggers! ctx triggers)
|
||||
(when triggers
|
||||
(set-context-triggers! ctx
|
||||
(apply lset-adjoin equal? (or (context-triggers ctx) '()) triggers))))
|
||||
|
||||
(define (context-triggered? ctx trigger)
|
||||
(find (lambda (t) (equal? t trigger)) (context-triggers ctx)))
|
||||
|
||||
(define (register-context-var! ctx var-name val)
|
||||
(set-context-vars! ctx (assoc-set! (context-vars ctx) var-name val)))
|
||||
|
||||
(define-syntax bind-context-vars
|
||||
(syntax-rules ()
|
||||
((bind-context-vars (var-name ...) proc)
|
||||
(lambda (ctx)
|
||||
(let ((var-name (context-ref ctx (quote var-name))) ...)
|
||||
(proc ctx))))))
|
||||
|
||||
(define* (run ctx prog args #:key (env #f) (pwd #f))
|
||||
(connection-run (context-connection ctx) pwd env prog args))
|
||||
|
||||
(define* (must ctx prog args #:key (env #f) (pwd #f) (error-msg #f))
|
||||
(let ((out rc (run ctx prog args #:env env #:pwd pwd)))
|
||||
(if (zero? rc)
|
||||
out
|
||||
(error (if error-msg
|
||||
(format #f "~a: ~a" error-msg out)
|
||||
(format #f "~a error: ~a" prog out))))))
|
Loading…
Add table
Add a link
Reference in a new issue