Permalink
Browse files

add in-project macro to make gnarly eval-in-project forms easier to w…

…ork with
  • Loading branch information...
ninjudd committed Nov 20, 2012
1 parent c95fd53 commit 6bd4a77a3471b455d2bf9e111531e09855dbce5d
Showing with 25 additions and 0 deletions.
  1. +25 −0 src/leinjacker/eval.clj
View
@@ -54,3 +54,28 @@
subtask project args
;lein1 has a 4 argument form, which expects a not-found fn.
(utils/try-resolve 'leiningen.core/task-not-found))))
+
+(defmacro in-project
+ "Execute code in the project. You can pass state from lein to the project
+ using the bindings vector. Note that the forms in bindings must be printable
+ and readable. You can set up the namespace the code will execute in by
+ optionally passing a first form like: (ns (:use ...) (:require ...))
+
+ For example:
+ (in-project project [foo [\"bar\" \"baz\" \"bam\"]]
+ (ns (:require [clojure.string :refer [join]])
+ (prn (join \",\" foo)))"
+ {:arglists '([project ns-forms* bindings? body*])}
+ [project bindings & [form :as forms]]
+ (let [bindings (apply hash-map bindings)
+ [ns-forms forms] (if (= 'ns (first form))
+ [(rest form) (rest forms)]
+ [nil forms])
+ ns-forms (if (symbol? (first ns-forms))
+ ns-forms
+ (cons (gensym) ns-forms))
+ f `(fn [[~@(keys bindings)]] ~@forms)]
+ `(eval-in-project ~project
+ (list 'do
+ '(ns ~@ns-forms)
+ (list '~f (list 'quote [~@(vals bindings)]))))))

0 comments on commit 6bd4a77

Please sign in to comment.