Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add eval of STDIN

  • Loading branch information...
commit 22200d957e415bffb9c8a8850c0b45403abbe993 1 parent 0276928
@kumarshantanu authored
Showing with 43 additions and 21 deletions.
  1. +43 −21 src/leiningen/exec.clj
View
64 src/leiningen/exec.clj
@@ -21,25 +21,44 @@
[]
(println "
Usage:
- lein exec -e|-ep <string-s-expr>
+ lein exec [-p]
+ lein exec -e[p] <string-s-expr>
lein exec [-p] <script-path> [args]
--e evaluates the following string as an s-expression
+When invoked without args it reads S-expressions from STDIN and evaluates them.
+When only option `-p` is specified, it evaluates STDIN in project context.
+
+-e evaluates the following string as an S-expression
-ep evaluates the following string as an S-expression in project (w/classpath)
-p indicates the script should be evaluated in project (with classpath)
Examples:
+ cat foo.clj | lein exec
lein exec -e '(println \"foo\" (+ 20 30)'
lein exec -ep \"(use 'foo.bar) (pprint (map baz (range 200)))\"
lein exec -p script/run-server.clj -p 8088
lein exec ~/common/delete-logs.clj
Optional args after script-path are bound to clojure.core/*command-line-args*
+Executable Clojure script files should have the following on the first line:
+#!/usr/bin/env lein exec
"))
+(defn eval-stdin
+ "Evaluate STDIN. If project is not nil, evaluate in context of project."
+ [project]
+ (if project
+ ;; eval in project
+ (eval/eval-in-project project `(load-reader *in*))
+ ;; else eval without project
+ (load-reader *in*))
+ (flush)
+ 0)
+
+
(defn eval-sexp
- "Evaluate S-expression"
+ "Evaluate S-expression. If project is not nil, evaluate in context of project."
[project sexp-str]
(if project
;; eval in project
@@ -67,28 +86,31 @@ Optional args after script-path are bound to clojure.core/*command-line-args*
0)
-(defn not-in-project
- []
- (binding [*err* *out*]
- (println "ERROR: Not in a project"))
- 1)
+(defmacro in
+ "Execute body of code in context of project"
+ [project & body]
+ `(if ~project (do ~@body)
+ (do (binding [*err* *out*]
+ (println "ERROR: Not in a project"))
+ 1)))
(defn ^:no-project-needed exec
- "Executes a Clojure s-expr/script"
+ "Execute Clojure S-expresions from command-line or scripts"
[project & args]
(let [option (when (= \- (first (first args))) (first args))
opt? (fn [choice & more] (some #(= option %) (cons choice more)))
params (if option (rest args) args)]
- (if (empty? params)
- (show-help)
- (cond (opt? "-e") (eval-sexp nil (first params))
- (opt? "-ep") (if project (eval-sexp project (first params))
- (not-in-project))
- (opt? "-pe") (if project (eval-sexp project (first params))
- (not-in-project))
- (opt? "-p") (if project (eval-script project (first params)
- (pr-str params))
- (not-in-project))
- (not option) (eval-script nil (first params) (pr-str params))
- :otherwise (show-help)))))
+ (cond ;; eval STDIN
+ (empty? params) (cond (nil? option) (eval-stdin nil)
+ (opt? "-p") (in project (eval-stdin project))
+ :otherwise (show-help))
+ ;; eval (first param) from command line
+ (opt? "-e") (eval-sexp nil (first params))
+ (opt? "-ep") (in project (eval-sexp project (first params)))
+ (opt? "-pe") (in project (eval-sexp project (first params)))
+ ;; eval script
+ (opt? "-p") (in project (eval-script project (first params)
+ (pr-str params)))
+ (not option) (eval-script nil (first params) (pr-str params))
+ :otherwise (show-help))))
Please sign in to comment.
Something went wrong with that request. Please try again.