Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Have rash commands output lists of strings.

More useful when programmatically using the output.

Also, add simple pipe support.
  • Loading branch information...
commit 52cf1915cc4b9bf77d0c0991f0b4f3f061958c7f 1 parent cff524d
@stamourv authored
Showing with 26 additions and 13 deletions.
  1. +20 −7 main.rkt
  2. +6 −6 test.rkt
View
27 main.rkt
@@ -4,17 +4,30 @@
(prefix-in r: racket/base))
(provide (except-out (all-from-out racket) #%app)
- (rename-out [path-#%app #%app]))
+ (rename-out [rash-#%app #%app])
+ pipe)
-(define-syntax (path-#%app stx)
+(define-syntax (rash-#%app stx)
(syntax-parse stx
[(_ op:id args ...)
(if (identifier-binding #'op)
- #'(r:#%app op args ...) ; it's a Racket function
- ;; look in $PATH
- #`(void (system (string-append #,(format "$(which ~a) "
- (syntax->datum #'op))
- (string-join (list args ...) " ")))))]))
+ #'(r:#%app op args ...) ; it's a Racket function, call it
+ ;; try it as a shell command
+ #`(rash-output
+ (system (string-append #,(rash-get-command #'op)
+ (string-join (list args ...) " ")))))]))
+
+(define-for-syntax (rash-get-command op)
+ (format "$(which ~a) " (syntax->datum op))) ; look in $PATH
+
+(define-syntax-rule (rash-output cmd)
+ (let ([output (with-output-to-string (lambda () cmd))])
+ (string-split output "\n")))
+
+(define-syntax-rule (pipe cmd1 cmd2)
+ (let ([output cmd1])
+ (with-input-from-string (string-join output "\n")
+ (lambda () cmd2))))
(module reader syntax/module-reader
#:language 'rash)
View
12 test.rkt
@@ -5,9 +5,9 @@
(define-syntax-rule (->string e)
(with-output-to-string (lambda () e)))
-(check-equal? (->string (displayln (+ 1 2 3))) ; regular function call
- "6\n")
-(check-equal? (->string (echo "a" "b" "c"))
- "a b c\n")
-(check-equal? (->string (ls "test.rkt"))
- "test.rkt\n")
+(check-equal? (+ 1 2 3) ; regular function calls should still work
+ 6)
+(check-equal? (echo "a" "b" "c")
+ '("a b c"))
+(check-equal? (ls "test.rkt" "main.rkt")
+ '("main.rkt" "test.rkt"))
Please sign in to comment.
Something went wrong with that request. Please try again.